1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.TreeMap;
|
---|
18 | import java.util.Vector;
|
---|
19 |
|
---|
20 | import javax.swing.JButton;
|
---|
21 | import javax.swing.JColorChooser;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JOptionPane;
|
---|
24 | import javax.swing.JScrollPane;
|
---|
25 | import javax.swing.JTable;
|
---|
26 | import javax.swing.ListSelectionModel;
|
---|
27 | import javax.swing.table.DefaultTableModel;
|
---|
28 | import javax.swing.table.TableCellRenderer;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.Main;
|
---|
31 | import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
|
---|
32 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 |
|
---|
35 | public class ColorPreference implements PreferenceSetting {
|
---|
36 |
|
---|
37 | private DefaultTableModel tableModel;
|
---|
38 | private JTable colors;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Set the colors to be shown in the preference table. This method creates a table model if
|
---|
42 | * none exists and overwrites all existing values.
|
---|
43 | * @param colorMap the map holding the colors
|
---|
44 | * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>),
|
---|
45 | * value = html representation of the color.
|
---|
46 | */
|
---|
47 | public void setColorModel(Map<String, String> colorMap) {
|
---|
48 | if(tableModel == null) {
|
---|
49 | tableModel = new DefaultTableModel();
|
---|
50 | tableModel.addColumn(tr("Color"));
|
---|
51 | tableModel.addColumn(tr("Name"));
|
---|
52 | }
|
---|
53 |
|
---|
54 | // clear old model:
|
---|
55 | while(tableModel.getRowCount() > 0) {
|
---|
56 | tableModel.removeRow(0);
|
---|
57 | }
|
---|
58 | // fill model with colors:
|
---|
59 | List<String> colorKeyList = new ArrayList<String>();
|
---|
60 | for(String key : colorMap.keySet()) {
|
---|
61 | colorKeyList.add(key);
|
---|
62 | }
|
---|
63 | Collections.sort(colorKeyList);
|
---|
64 | for (String key : colorKeyList) {
|
---|
65 | Vector<Object> row = new Vector<Object>(2);
|
---|
66 | row.add(key);
|
---|
67 | row.add(ColorHelper.html2color(colorMap.get(key)));
|
---|
68 | tableModel.addRow(row);
|
---|
69 | }
|
---|
70 | if(this.colors != null) {
|
---|
71 | this.colors.repaint();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Returns a map with the colors in the table (key = color name without prefix, value = html color code).
|
---|
77 | * @return a map holding the colors.
|
---|
78 | */
|
---|
79 | public Map<String, String> getColorModel() {
|
---|
80 | String key;
|
---|
81 | String value;
|
---|
82 | Map<String, String> colorMap = new HashMap<String, String>();
|
---|
83 | for(int row = 0; row < tableModel.getRowCount(); ++row) {
|
---|
84 | key = (String)tableModel.getValueAt(row, 0);
|
---|
85 | value = ColorHelper.color2html((Color)tableModel.getValueAt(row, 1));
|
---|
86 | colorMap.put(key, value);
|
---|
87 | }
|
---|
88 | return colorMap;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void addGui(final PreferenceDialog gui) {
|
---|
92 | // initial fill with colors from preferences:
|
---|
93 | Map<String,String> prefColorMap = new TreeMap<String, String>(Main.pref.getAllPrefix("color."));
|
---|
94 | fixColorPrefixes(prefColorMap);
|
---|
95 | Map<String,String> colorMap = new TreeMap<String, String>();
|
---|
96 | for(String key : prefColorMap.keySet()) {
|
---|
97 | colorMap.put(key.substring("color.".length()), prefColorMap.get(key));
|
---|
98 | }
|
---|
99 | setColorModel(colorMap);
|
---|
100 |
|
---|
101 | colors = new JTable(tableModel) {
|
---|
102 | @Override public boolean isCellEditable(int row, int column) {
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | };
|
---|
106 | colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
107 | final TableCellRenderer oldColorsRenderer = colors.getDefaultRenderer(Object.class);
|
---|
108 | colors.setDefaultRenderer(Object.class, new TableCellRenderer(){
|
---|
109 | public Component getTableCellRendererComponent(JTable t, Object o, boolean selected, boolean focus, int row, int column) {
|
---|
110 | if (column == 1) {
|
---|
111 | JLabel l = new JLabel(ColorHelper.color2html((Color)o));
|
---|
112 | l.setBackground((Color)o);
|
---|
113 | l.setOpaque(true);
|
---|
114 | return l;
|
---|
115 | }
|
---|
116 | return oldColorsRenderer.getTableCellRendererComponent(t,tr(o.toString()),selected,focus,row,column);
|
---|
117 | }
|
---|
118 | });
|
---|
119 | colors.getColumnModel().getColumn(1).setWidth(100);
|
---|
120 |
|
---|
121 | JButton colorEdit = new JButton(tr("Choose"));
|
---|
122 | colorEdit.addActionListener(new ActionListener(){
|
---|
123 | public void actionPerformed(ActionEvent e) {
|
---|
124 | if (colors.getSelectedRowCount() == 0) {
|
---|
125 | JOptionPane.showMessageDialog(gui, tr("Please select a color."));
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | int sel = colors.getSelectedRow();
|
---|
129 | JColorChooser chooser = new JColorChooser((Color)colors.getValueAt(sel, 1));
|
---|
130 | int answer = JOptionPane.showConfirmDialog(gui, chooser, tr("Choose a color for {0}", colors.getValueAt(sel, 0)), JOptionPane.OK_CANCEL_OPTION);
|
---|
131 | if (answer == JOptionPane.OK_OPTION)
|
---|
132 | colors.setValueAt(chooser.getColor(), sel, 1);
|
---|
133 | }
|
---|
134 | });
|
---|
135 | colors.setToolTipText(tr("Colors used by different objects in JOSM."));
|
---|
136 | colors.setPreferredScrollableViewportSize(new Dimension(100,112));
|
---|
137 | gui.display.add(new JLabel(tr("Colors")), GBC.eol());
|
---|
138 | gui.display.add(new JScrollPane(colors), GBC.eol().fill(GBC.BOTH));
|
---|
139 | gui.display.add(colorEdit, GBC.eol().anchor(GBC.EAST));
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Add all missing color entries.
|
---|
144 | */
|
---|
145 | private void fixColorPrefixes(Map<String, String> prefColorMap) {
|
---|
146 | String[] cp = {
|
---|
147 | marktr("background"), ColorHelper.color2html(Color.black),
|
---|
148 | marktr("node"), ColorHelper.color2html(Color.red),
|
---|
149 | marktr("way"), ColorHelper.color2html(SimplePaintVisitor.darkblue),
|
---|
150 | marktr("incomplete way"), ColorHelper.color2html(SimplePaintVisitor.darkerblue),
|
---|
151 | marktr("relation"), ColorHelper.color2html(SimplePaintVisitor.teal),
|
---|
152 | marktr("selected"), ColorHelper.color2html(Color.white),
|
---|
153 | marktr("gps point"), ColorHelper.color2html(Color.gray),
|
---|
154 | marktr("conflict"), ColorHelper.color2html(Color.gray),
|
---|
155 | marktr("scale"), ColorHelper.color2html(Color.white),
|
---|
156 | marktr("inactive"), ColorHelper.color2html(Color.darkGray),
|
---|
157 | };
|
---|
158 | for (int i = 0; i < cp.length/2; ++i)
|
---|
159 | if (!Main.pref.hasKey("color."+cp[i*2]))
|
---|
160 | Main.pref.put("color."+cp[i*2], cp[i*2+1]);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public void ok() {
|
---|
164 | for (int i = 0; i < colors.getRowCount(); ++i) {
|
---|
165 | String name = (String)colors.getValueAt(i, 0);
|
---|
166 | Color col = (Color)colors.getValueAt(i, 1);
|
---|
167 | Main.pref.put("color." + name, ColorHelper.color2html(col));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|