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 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.TreeMap;
|
---|
16 | import java.util.Vector;
|
---|
17 | import java.util.Map.Entry;
|
---|
18 | import java.util.regex.Matcher;
|
---|
19 | import java.util.regex.Pattern;
|
---|
20 |
|
---|
21 | import javax.swing.BorderFactory;
|
---|
22 | import javax.swing.Box;
|
---|
23 | import javax.swing.JButton;
|
---|
24 | import javax.swing.JColorChooser;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JScrollPane;
|
---|
29 | import javax.swing.JTable;
|
---|
30 | import javax.swing.ListSelectionModel;
|
---|
31 | import javax.swing.event.ListSelectionEvent;
|
---|
32 | import javax.swing.table.DefaultTableModel;
|
---|
33 | import javax.swing.table.TableCellRenderer;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.Main;
|
---|
36 | import org.openstreetmap.josm.data.osm.visitor.MapPaintVisitor;
|
---|
37 | import org.openstreetmap.josm.gui.MapScaler;
|
---|
38 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
39 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
40 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
41 | import org.openstreetmap.josm.tools.GBC;
|
---|
42 |
|
---|
43 | public class ColorPreference implements PreferenceSetting {
|
---|
44 |
|
---|
45 | public static class Factory implements PreferenceSettingFactory {
|
---|
46 | public PreferenceSetting createPreferenceSetting() {
|
---|
47 | return new ColorPreference();
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private DefaultTableModel tableModel;
|
---|
52 | private JTable colors;
|
---|
53 | private ArrayList<String> del = new ArrayList<String>();
|
---|
54 |
|
---|
55 | JButton colorEdit;
|
---|
56 | JButton defaultSet;
|
---|
57 | JButton remove;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Set the colors to be shown in the preference table. This method creates a table model if
|
---|
61 | * none exists and overwrites all existing values.
|
---|
62 | * @param colorMap the map holding the colors
|
---|
63 | * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>),
|
---|
64 | * value = html representation of the color.
|
---|
65 | */
|
---|
66 | public void setColorModel(Map<String, String> colorMap) {
|
---|
67 | if(tableModel == null) {
|
---|
68 | tableModel = new DefaultTableModel();
|
---|
69 | tableModel.addColumn(tr("Name"));
|
---|
70 | tableModel.addColumn(tr("Color"));
|
---|
71 | }
|
---|
72 |
|
---|
73 | // clear old model:
|
---|
74 | while(tableModel.getRowCount() > 0) {
|
---|
75 | tableModel.removeRow(0);
|
---|
76 | }
|
---|
77 | // fill model with colors:
|
---|
78 | Map<String, String> colorKeyList = new TreeMap<String, String>();
|
---|
79 | Map<String, String> colorKeyList_mappaint = new TreeMap<String, String>();
|
---|
80 | Map<String, String> colorKeyList_layer = new TreeMap<String, String>();
|
---|
81 | for(String key : colorMap.keySet()) {
|
---|
82 | if(key.startsWith("layer ")) {
|
---|
83 | colorKeyList_layer.put(getName(key), key);
|
---|
84 | } else if(key.startsWith("mappaint.")) {
|
---|
85 | colorKeyList_mappaint.put(getName(key), key);
|
---|
86 | } else {
|
---|
87 | colorKeyList.put(getName(key), key);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | for (Entry<String, String> k : colorKeyList.entrySet()) {
|
---|
91 | Vector<Object> row = new Vector<Object>(2);
|
---|
92 | row.add(k.getValue());
|
---|
93 | row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
|
---|
94 | tableModel.addRow(row);
|
---|
95 | }
|
---|
96 | for (Entry<String, String> k : colorKeyList_mappaint.entrySet()) {
|
---|
97 | Vector<Object> row = new Vector<Object>(2);
|
---|
98 | row.add(k.getValue());
|
---|
99 | row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
|
---|
100 | tableModel.addRow(row);
|
---|
101 | }
|
---|
102 | for (Entry<String, String> k : colorKeyList_layer.entrySet()) {
|
---|
103 | Vector<Object> row = new Vector<Object>(2);
|
---|
104 | row.add(k.getValue());
|
---|
105 | row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
|
---|
106 | tableModel.addRow(row);
|
---|
107 | }
|
---|
108 | if(this.colors != null) {
|
---|
109 | this.colors.repaint();
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Returns a map with the colors in the table (key = color name without prefix, value = html color code).
|
---|
115 | * @return a map holding the colors.
|
---|
116 | */
|
---|
117 | public Map<String, String> getColorModel() {
|
---|
118 | String key;
|
---|
119 | String value;
|
---|
120 | Map<String, String> colorMap = new HashMap<String, String>();
|
---|
121 | for(int row = 0; row < tableModel.getRowCount(); ++row) {
|
---|
122 | key = (String)tableModel.getValueAt(row, 0);
|
---|
123 | value = ColorHelper.color2html((Color)tableModel.getValueAt(row, 1));
|
---|
124 | colorMap.put(key, value);
|
---|
125 | }
|
---|
126 | return colorMap;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private String getName(String o)
|
---|
130 | {
|
---|
131 | try
|
---|
132 | {
|
---|
133 | Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
|
---|
134 | m.matches();
|
---|
135 | return tr("Paint style {0}: {1}", tr(m.group(1)), tr(m.group(2)));
|
---|
136 | }
|
---|
137 | catch (Exception e) {}
|
---|
138 | try
|
---|
139 | {
|
---|
140 | Matcher m = Pattern.compile("layer (.+)").matcher(o);
|
---|
141 | m.matches();
|
---|
142 | return tr("Layer: {0}", tr(m.group(1)));
|
---|
143 | }
|
---|
144 | catch (Exception e) {}
|
---|
145 | return tr(o);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void addGui(final PreferenceDialog gui) {
|
---|
149 | fixColorPrefixes();
|
---|
150 | setColorModel(Main.pref.getAllColors());
|
---|
151 |
|
---|
152 | colorEdit = new JButton(tr("Choose"));
|
---|
153 | colorEdit.addActionListener(new ActionListener(){
|
---|
154 | public void actionPerformed(ActionEvent e) {
|
---|
155 | int sel = colors.getSelectedRow();
|
---|
156 | JColorChooser chooser = new JColorChooser((Color)colors.getValueAt(sel, 1));
|
---|
157 | int answer = JOptionPane.showConfirmDialog(
|
---|
158 | gui, chooser,
|
---|
159 | tr("Choose a color for {0}", getName((String)colors.getValueAt(sel, 0))),
|
---|
160 | JOptionPane.OK_CANCEL_OPTION,
|
---|
161 | JOptionPane.PLAIN_MESSAGE);
|
---|
162 | if (answer == JOptionPane.OK_OPTION) {
|
---|
163 | colors.setValueAt(chooser.getColor(), sel, 1);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | });
|
---|
167 | defaultSet = new JButton(tr("Set to default"));
|
---|
168 | defaultSet.addActionListener(new ActionListener(){
|
---|
169 | public void actionPerformed(ActionEvent e) {
|
---|
170 | int sel = colors.getSelectedRow();
|
---|
171 | String name = (String)colors.getValueAt(sel, 0);
|
---|
172 | Color c = Main.pref.getDefaultColor(name);
|
---|
173 | if (c != null) {
|
---|
174 | colors.setValueAt(c, sel, 1);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | });
|
---|
178 | JButton defaultAll = new JButton(tr("Set all to default"));
|
---|
179 | defaultAll.addActionListener(new ActionListener(){
|
---|
180 | public void actionPerformed(ActionEvent e) {
|
---|
181 | for(int i = 0; i < colors.getRowCount(); ++i)
|
---|
182 | {
|
---|
183 | String name = (String)colors.getValueAt(i, 0);
|
---|
184 | Color c = Main.pref.getDefaultColor(name);
|
---|
185 | if (c != null) {
|
---|
186 | colors.setValueAt(c, i, 1);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | });
|
---|
191 | remove = new JButton(tr("Remove"));
|
---|
192 | remove.addActionListener(new ActionListener(){
|
---|
193 | public void actionPerformed(ActionEvent e) {
|
---|
194 | int sel = colors.getSelectedRow();
|
---|
195 | del.add((String)colors.getValueAt(sel, 0));
|
---|
196 | tableModel.removeRow(sel);
|
---|
197 | }
|
---|
198 | });
|
---|
199 | remove.setEnabled(false);
|
---|
200 | colorEdit.setEnabled(false);
|
---|
201 | defaultSet.setEnabled(false);
|
---|
202 |
|
---|
203 | colors = new JTable(tableModel) {
|
---|
204 | @Override public boolean isCellEditable(int row, int column) {
|
---|
205 | return false;
|
---|
206 | }
|
---|
207 | @Override public void valueChanged(ListSelectionEvent e) {
|
---|
208 | super.valueChanged(e);
|
---|
209 | int sel = getSelectedRow();
|
---|
210 | remove.setEnabled(sel >= 0 && isRemoveColor(sel));
|
---|
211 | colorEdit.setEnabled(sel >= 0);
|
---|
212 | defaultSet.setEnabled(sel >= 0);
|
---|
213 | }
|
---|
214 | };
|
---|
215 | colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
216 | final TableCellRenderer oldColorsRenderer = colors.getDefaultRenderer(Object.class);
|
---|
217 | colors.setDefaultRenderer(Object.class, new TableCellRenderer(){
|
---|
218 | public Component getTableCellRendererComponent(JTable t, Object o, boolean selected, boolean focus, int row, int column) {
|
---|
219 | if (column == 1) {
|
---|
220 | JLabel l = new JLabel(ColorHelper.color2html((Color)o));
|
---|
221 | l.setBackground((Color)o);
|
---|
222 | l.setOpaque(true);
|
---|
223 | return l;
|
---|
224 | }
|
---|
225 | return oldColorsRenderer.getTableCellRendererComponent(t,getName(o.toString()),selected,focus,row,column);
|
---|
226 | }
|
---|
227 | });
|
---|
228 | colors.getColumnModel().getColumn(1).setWidth(100);
|
---|
229 | colors.setToolTipText(tr("Colors used by different objects in JOSM."));
|
---|
230 | colors.setPreferredScrollableViewportSize(new Dimension(100,112));
|
---|
231 |
|
---|
232 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
233 | panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
234 | JScrollPane scrollpane = new JScrollPane(colors);
|
---|
235 | scrollpane.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
---|
236 | panel.add(scrollpane, GBC.eol().fill(GBC.BOTH));
|
---|
237 | JPanel buttonPanel = new JPanel(new GridBagLayout());
|
---|
238 | panel.add(buttonPanel, GBC.eol().insets(5,0,5,5).fill(GBC.HORIZONTAL));
|
---|
239 | buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
240 | buttonPanel.add(colorEdit, GBC.std().insets(0,5,0,0));
|
---|
241 | buttonPanel.add(defaultSet, GBC.std().insets(5,5,5,0));
|
---|
242 | buttonPanel.add(defaultAll, GBC.std().insets(0,5,0,0));
|
---|
243 | buttonPanel.add(remove, GBC.std().insets(0,5,0,0));
|
---|
244 | gui.displaycontent.addTab(tr("Colors"), panel);
|
---|
245 | }
|
---|
246 |
|
---|
247 | Boolean isRemoveColor(int row)
|
---|
248 | {
|
---|
249 | return ((String)colors.getValueAt(row, 0)).startsWith("layer ");
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Add all missing color entries.
|
---|
254 | */
|
---|
255 | private void fixColorPrefixes() {
|
---|
256 | (new MapPaintVisitor()).getColors();
|
---|
257 | MarkerLayer.getColor(null);
|
---|
258 | MapScaler.getColor();
|
---|
259 | ConflictDialog.getColor();
|
---|
260 | }
|
---|
261 |
|
---|
262 | public boolean ok() {
|
---|
263 | Boolean ret = false;
|
---|
264 | for(String d : del) {
|
---|
265 | Main.pref.put("color."+d, null);
|
---|
266 | }
|
---|
267 | for (int i = 0; i < colors.getRowCount(); ++i) {
|
---|
268 | String key = (String)colors.getValueAt(i, 0);
|
---|
269 | if(Main.pref.putColor(key, (Color)colors.getValueAt(i, 1)))
|
---|
270 | {
|
---|
271 | if(key.startsWith("mappaint.")) {
|
---|
272 | ret = true;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 | org.openstreetmap.josm.gui.layer.OsmDataLayer.createHatchTexture();
|
---|
277 | return ret;
|
---|
278 | }
|
---|
279 | }
|
---|