Changeset 259 in josm for src/org/openstreetmap
- Timestamp:
- 2007-06-19T21:01:00+02:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/gui/preferences/ColorPreference.java
r168 r259 8 8 import java.awt.event.ActionEvent; 9 9 import java.awt.event.ActionListener; 10 import java.util.ArrayList; 11 import java.util.Collections; 12 import java.util.HashMap; 13 import java.util.List; 10 14 import java.util.Map; 11 15 import java.util.TreeMap; 12 16 import java.util.Vector; 13 import java.util.Map.Entry;14 17 15 18 import javax.swing.JButton; … … 20 23 import javax.swing.JTable; 21 24 import javax.swing.ListSelectionModel; 25 import javax.swing.table.DefaultTableModel; 22 26 import javax.swing.table.TableCellRenderer; 23 27 … … 28 32 public class ColorPreference implements PreferenceSetting { 29 33 34 private DefaultTableModel tableModel; 30 35 private JTable colors; 36 public static final String PREF_COLOR_PREFIX = "color."; 37 38 39 /** 40 * Set the colors to be shown in the preference table. This method creates a table model if 41 * none exists and overwrites all existing values. 42 * @param colorMap the map holding the colors 43 * (key = color id (without prefixes, so only <code>background</code>; not <code>color.background</code>), 44 * value = html representation of the color. 45 */ 46 public void setColorModel(Map<String, String> colorMap) { 47 if(tableModel == null) { 48 tableModel = new DefaultTableModel(); 49 tableModel.addColumn(tr("Color")); 50 tableModel.addColumn(tr("Name")); 51 } 52 53 // clear old model: 54 while(tableModel.getRowCount() > 0) { 55 tableModel.removeRow(0); 56 } 57 // fill model with colors: 58 List<String> colorKeyList = new ArrayList<String>(); 59 for(String key : colorMap.keySet()) { 60 colorKeyList.add(key); 61 } 62 Collections.sort(colorKeyList); 63 for (String key : colorKeyList) { 64 Vector<Object> row = new Vector<Object>(2); 65 row.add(key); 66 row.add(ColorHelper.html2color(colorMap.get(key))); 67 tableModel.addRow(row); 68 } 69 if(this.colors != null) { 70 this.colors.repaint(); 71 } 72 } 73 74 /** 75 * Returns a map with the colors in the table (key = color name without prefix, value = html color code). 76 * @return a map holding the colors. 77 */ 78 public Map<String, String> getColorModel() { 79 String key; 80 String value; 81 Map<String, String> colorMap = new HashMap<String, String>(); 82 for(int row = 0; row < tableModel.getRowCount(); ++row) { 83 key = (String)tableModel.getValueAt(row, 0); 84 value = ColorHelper.color2html((Color)tableModel.getValueAt(row, 1)); 85 colorMap.put(key, value); 86 } 87 return colorMap; 88 } 89 90 /** 91 * Updates the table model with the colors in the color map. 92 */ 93 private void updateTableModel() { 94 } 95 31 96 32 97 public void addGui(final PreferenceDialog gui) { 33 Map<String,String> allColors = new TreeMap<String, String>(Main.pref.getAllPrefix("color.")); 34 35 Vector<Vector<Object>> rows = new Vector<Vector<Object>>(); 36 for (Entry<String,String> e : allColors.entrySet()) { 37 Vector<Object> row = new Vector<Object>(2); 38 row.add(e.getKey().substring("color.".length())); 39 row.add(ColorHelper.html2color(e.getValue())); 40 rows.add(row); 98 // initial fill with colors from preferences: 99 Map<String,String> prefColorMap = new TreeMap<String, String>(Main.pref.getAllPrefix(PREF_COLOR_PREFIX)); 100 Map<String,String> colorMap = new TreeMap<String, String>(); 101 for(String key : prefColorMap.keySet()) { 102 colorMap.put(key.substring(PREF_COLOR_PREFIX.length()), prefColorMap.get(key)); 41 103 } 42 Vector<Object> cols = new Vector<Object>(2); 43 cols.add(tr("Color")); 44 cols.add(tr("Name")); 45 colors = new JTable(rows, cols){ 104 setColorModel(colorMap); 105 106 colors = new JTable(tableModel) { 46 107 @Override public boolean isCellEditable(int row, int column) { 47 108 return false; … … 88 149 String name = (String)colors.getValueAt(i, 0); 89 150 Color col = (Color)colors.getValueAt(i, 1); 90 Main.pref.put( "color."+name, ColorHelper.color2html(col));151 Main.pref.put(PREF_COLOR_PREFIX + name, ColorHelper.color2html(col)); 91 152 } 92 153 }
Note:
See TracChangeset
for help on using the changeset viewer.