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.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.util.regex.Matcher;
|
---|
14 | import java.util.regex.Pattern;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.Map.Entry;
|
---|
21 | import java.util.TreeMap;
|
---|
22 | import java.util.Vector;
|
---|
23 |
|
---|
24 | import javax.swing.JButton;
|
---|
25 | import javax.swing.JColorChooser;
|
---|
26 | import javax.swing.JLabel;
|
---|
27 | import javax.swing.JOptionPane;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.JScrollPane;
|
---|
30 | import javax.swing.JTable;
|
---|
31 | import javax.swing.ListSelectionModel;
|
---|
32 | import javax.swing.table.DefaultTableModel;
|
---|
33 | import javax.swing.table.TableCellRenderer;
|
---|
34 | import javax.swing.BorderFactory;
|
---|
35 | import javax.swing.Box;
|
---|
36 |
|
---|
37 | import org.openstreetmap.josm.Main;
|
---|
38 | import org.openstreetmap.josm.data.osm.visitor.MapPaintVisitor;
|
---|
39 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
40 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
41 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
42 | import org.openstreetmap.josm.gui.MapScaler;
|
---|
43 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
44 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
45 | import org.openstreetmap.josm.tools.GBC;
|
---|
46 |
|
---|
47 | public class ColorPreference implements PreferenceSetting {
|
---|
48 |
|
---|
49 | private DefaultTableModel tableModel;
|
---|
50 | private JTable colors;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Set the colors to be shown in the preference table. This method creates a table model if
|
---|
54 | * none exists and overwrites all existing values.
|
---|
55 | * @param colorMap the map holding the colors
|
---|
56 | * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>),
|
---|
57 | * value = html representation of the color.
|
---|
58 | */
|
---|
59 | public void setColorModel(Map<String, String> colorMap) {
|
---|
60 | if(tableModel == null) {
|
---|
61 | tableModel = new DefaultTableModel();
|
---|
62 | tableModel.addColumn(tr("Color"));
|
---|
63 | tableModel.addColumn(tr("Name"));
|
---|
64 | }
|
---|
65 |
|
---|
66 | // clear old model:
|
---|
67 | while(tableModel.getRowCount() > 0) {
|
---|
68 | tableModel.removeRow(0);
|
---|
69 | }
|
---|
70 | // fill model with colors:
|
---|
71 | Map<String, String> colorKeyList = new TreeMap<String, String>();
|
---|
72 | Map<String, String> colorKeyList_mappaint = new TreeMap<String, String>();
|
---|
73 | for(String key : colorMap.keySet()) {
|
---|
74 | if(key.startsWith("mappaint."))
|
---|
75 | colorKeyList_mappaint.put(getName(key), key);
|
---|
76 | else
|
---|
77 | colorKeyList.put(getName(key), key);
|
---|
78 | }
|
---|
79 | for (Entry k : colorKeyList.entrySet()) {
|
---|
80 | Vector<Object> row = new Vector<Object>(2);
|
---|
81 | row.add(k.getValue());
|
---|
82 | row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
|
---|
83 | tableModel.addRow(row);
|
---|
84 | }
|
---|
85 | for (Entry k : colorKeyList_mappaint.entrySet()) {
|
---|
86 | Vector<Object> row = new Vector<Object>(2);
|
---|
87 | row.add(k.getValue());
|
---|
88 | row.add(ColorHelper.html2color(colorMap.get(k.getValue())));
|
---|
89 | tableModel.addRow(row);
|
---|
90 | }
|
---|
91 | if(this.colors != null) {
|
---|
92 | this.colors.repaint();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Returns a map with the colors in the table (key = color name without prefix, value = html color code).
|
---|
98 | * @return a map holding the colors.
|
---|
99 | */
|
---|
100 | public Map<String, String> getColorModel() {
|
---|
101 | String key;
|
---|
102 | String value;
|
---|
103 | Map<String, String> colorMap = new HashMap<String, String>();
|
---|
104 | for(int row = 0; row < tableModel.getRowCount(); ++row) {
|
---|
105 | key = (String)tableModel.getValueAt(row, 0);
|
---|
106 | value = ColorHelper.color2html((Color)tableModel.getValueAt(row, 1));
|
---|
107 | colorMap.put(key, value);
|
---|
108 | }
|
---|
109 | return colorMap;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private String getName(String o)
|
---|
113 | {
|
---|
114 | try
|
---|
115 | {
|
---|
116 | Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
|
---|
117 | m.matches();
|
---|
118 | return tr("Paint style {0}: {1}", tr(m.group(1)), tr(m.group(2)));
|
---|
119 | }
|
---|
120 | catch (Exception e) {}
|
---|
121 | return tr(o);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void addGui(final PreferenceDialog gui) {
|
---|
125 | fixColorPrefixes();
|
---|
126 | setColorModel(Main.pref.getAllColors());
|
---|
127 |
|
---|
128 | colors = new JTable(tableModel) {
|
---|
129 | @Override public boolean isCellEditable(int row, int column) {
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | };
|
---|
133 | colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
134 | final TableCellRenderer oldColorsRenderer = colors.getDefaultRenderer(Object.class);
|
---|
135 | colors.setDefaultRenderer(Object.class, new TableCellRenderer(){
|
---|
136 | public Component getTableCellRendererComponent(JTable t, Object o, boolean selected, boolean focus, int row, int column) {
|
---|
137 | if (column == 1) {
|
---|
138 | JLabel l = new JLabel(ColorHelper.color2html((Color)o));
|
---|
139 | l.setBackground((Color)o);
|
---|
140 | l.setOpaque(true);
|
---|
141 | return l;
|
---|
142 | }
|
---|
143 | return oldColorsRenderer.getTableCellRendererComponent(t,getName(o.toString()),selected,focus,row,column);
|
---|
144 | }
|
---|
145 | });
|
---|
146 | colors.getColumnModel().getColumn(1).setWidth(100);
|
---|
147 |
|
---|
148 | JButton colorEdit = new JButton(tr("Choose"));
|
---|
149 | colorEdit.addActionListener(new ActionListener(){
|
---|
150 | public void actionPerformed(ActionEvent e) {
|
---|
151 | if (colors.getSelectedRowCount() == 0) {
|
---|
152 | JOptionPane.showMessageDialog(gui, tr("Please select a color."));
|
---|
153 | return;
|
---|
154 | }
|
---|
155 | int sel = colors.getSelectedRow();
|
---|
156 | JColorChooser chooser = new JColorChooser((Color)colors.getValueAt(sel, 1));
|
---|
157 | int answer = JOptionPane.showConfirmDialog(gui, chooser, tr("Choose a color for {0}", colors.getValueAt(sel, 0)), JOptionPane.OK_CANCEL_OPTION);
|
---|
158 | if (answer == JOptionPane.OK_OPTION)
|
---|
159 | colors.setValueAt(chooser.getColor(), sel, 1);
|
---|
160 | }
|
---|
161 | });
|
---|
162 | colors.setToolTipText(tr("Colors used by different objects in JOSM."));
|
---|
163 | colors.setPreferredScrollableViewportSize(new Dimension(100,112));
|
---|
164 |
|
---|
165 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
166 | panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
167 | JScrollPane scrollpane = new JScrollPane(colors);
|
---|
168 | scrollpane.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
---|
169 | panel.add(scrollpane, GBC.eol().fill(GBC.BOTH));
|
---|
170 | panel.add(colorEdit, GBC.eol().anchor(GBC.EAST));
|
---|
171 | gui.displaycontent.addTab(tr("Colors"), panel);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Add all missing color entries.
|
---|
176 | */
|
---|
177 | private void fixColorPrefixes() {
|
---|
178 | (new MapPaintVisitor()).getColors();
|
---|
179 | MarkerLayer.getColor(null);
|
---|
180 | MapScaler.getColor();
|
---|
181 | ConflictDialog.getColor();
|
---|
182 | }
|
---|
183 |
|
---|
184 | public boolean ok() {
|
---|
185 | Boolean ret = false;
|
---|
186 | for (int i = 0; i < colors.getRowCount(); ++i) {
|
---|
187 | String key = (String)colors.getValueAt(i, 0);
|
---|
188 | if(Main.pref.putColor(key, (Color)colors.getValueAt(i, 1)))
|
---|
189 | {
|
---|
190 | if(key.startsWith("mappaint."))
|
---|
191 | ret = true;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | org.openstreetmap.josm.gui.layer.OsmDataLayer.createHatchTexture();
|
---|
195 | return ret;
|
---|
196 | }
|
---|
197 | }
|
---|