source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/ColorPreference.java@ 1241

Last change on this file since 1241 was 1241, checked in by stoecker, 15 years ago

moved style name into style file

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