- Timestamp:
- 2017-10-08T17:50:58+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/display/ColorPreference.java
r12947 r12950 15 15 import java.util.Map; 16 16 import java.util.TreeMap; 17 import java.util.Vector;18 17 19 18 import javax.swing.BorderFactory; … … 28 27 import javax.swing.ListSelectionModel; 29 28 import javax.swing.event.ListSelectionEvent; 29 import javax.swing.table.AbstractTableModel; 30 30 import javax.swing.table.DefaultTableCellRenderer; 31 import javax.swing.table.DefaultTableModel;32 31 33 32 import org.openstreetmap.josm.Main; … … 66 65 } 67 66 68 private DefaultTableModel tableModel;67 private ColorTableModel tableModel; 69 68 private JTable colors; 70 private final List<String> del = new ArrayList<>();71 69 72 70 private JButton colorEdit; 73 71 private JButton defaultSet; 74 72 private JButton remove; 73 74 private static class ColorEntry { 75 String key; 76 Color color; 77 } 78 79 private static class ColorTableModel extends AbstractTableModel { 80 81 private final List<ColorEntry> data; 82 private final List<ColorEntry> deleted; 83 84 public ColorTableModel() { 85 this.data = new ArrayList<>(); 86 this.deleted = new ArrayList<>(); 87 } 88 89 public void addEntry(ColorEntry entry) { 90 data.add(entry); 91 } 92 93 public void removeEntry(int row) { 94 deleted.add(data.get(row)); 95 data.remove(row); 96 fireTableDataChanged(); 97 } 98 99 public List<ColorEntry> getData() { 100 return data; 101 } 102 103 public List<ColorEntry> getDeleted() { 104 return deleted; 105 } 106 107 public void clear() { 108 data.clear(); 109 deleted.clear(); 110 } 111 112 @Override 113 public int getRowCount() { 114 return data.size(); 115 } 116 117 @Override 118 public int getColumnCount() { 119 return 2; 120 } 121 122 @Override 123 public Object getValueAt(int rowIndex, int columnIndex) { 124 return columnIndex == 0 ? getName(data.get(rowIndex).key) : data.get(rowIndex).color; 125 } 126 127 @Override 128 public String getColumnName(int column) { 129 return column == 0 ? tr("Name") : tr("Color"); 130 } 131 132 @Override 133 public boolean isCellEditable(int rowIndex, int columnIndex) { 134 return false; 135 } 136 137 @Override 138 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 139 if (columnIndex == 1 && aValue instanceof Color) { 140 data.get(rowIndex).color = (Color) aValue; 141 fireTableCellUpdated(rowIndex, columnIndex); 142 } 143 } 144 } 75 145 76 146 /** … … 83 153 public void setColorModel(Map<String, String> colorMap) { 84 154 if (tableModel == null) { 85 tableModel = new DefaultTableModel(); 86 tableModel.addColumn(tr("Name")); 87 tableModel.addColumn(tr("Color")); 88 } 89 90 // clear old model: 91 while (tableModel.getRowCount() > 0) { 92 tableModel.removeRow(0); 93 } 155 tableModel = new ColorTableModel(); 156 } 157 158 tableModel.clear(); 94 159 // fill model with colors: 95 160 Map<String, String> colorKeyList = new TreeMap<>(); … … 116 181 private void addColorRows(Map<String, String> colorMap, Map<String, String> keyMap) { 117 182 for (String value : keyMap.values()) { 118 Vector<Object> row = new Vector<>(2);183 ColorEntry entry = new ColorEntry(); 119 184 String html = colorMap.get(value); 120 185 Color color = ColorHelper.html2color(html); … … 122 187 Logging.warn("Unable to get color from '"+html+"' for color preference '"+value+'\''); 123 188 } 124 row.add(value);125 row.add(color);126 tableModel.add Row(row);189 entry.key = value; 190 entry.color = color; 191 tableModel.addEntry(entry); 127 192 } 128 193 } … … 133 198 */ 134 199 public Map<String, String> getColorModel() { 135 String key;136 String value;137 200 Map<String, String> colorMap = new HashMap<>(); 138 for (int row = 0; row < tableModel.getRowCount(); ++row) { 139 key = (String) tableModel.getValueAt(row, 0); 140 value = ColorHelper.color2html((Color) tableModel.getValueAt(row, 1)); 141 colorMap.put(key, value); 201 for (ColorEntry e : tableModel.getData()) { 202 colorMap.put(e.key, ColorHelper.color2html(e.color)); 142 203 } 143 204 return colorMap; … … 188 249 remove.addActionListener(e -> { 189 250 int sel = colors.getSelectedRow(); 190 del.add((String) colors.getValueAt(sel, 0)); 191 tableModel.removeRow(sel); 251 tableModel.removeEntry(sel); 192 252 }); 193 253 remove.setEnabled(false); … … 196 256 197 257 colors = new JTable(tableModel) { 198 @Override199 public boolean isCellEditable(int row, int column) {200 return false;201 }202 203 258 @Override public void valueChanged(ListSelectionEvent e) { 204 259 super.valueChanged(e); … … 254 309 255 310 Boolean isRemoveColor(int row) { 256 return ((String) colors.getValueAt(row, 0)).startsWith("layer.");311 return tableModel.getData().get(row).key.startsWith("layer."); 257 312 } 258 313 … … 275 330 public boolean ok() { 276 331 boolean ret = false; 277 for (String d : del) { 278 Main.pref.putColor(d, null); 279 } 280 for (int i = 0; i < colors.getRowCount(); ++i) { 281 String key = (String) colors.getValueAt(i, 0); 282 if (Main.pref.putColor(key, (Color) colors.getValueAt(i, 1)) && key.startsWith("mappaint.")) { 332 for (ColorEntry d : tableModel.getDeleted()) { 333 Main.pref.putColor(d.key, null); 334 } 335 for (ColorEntry e : tableModel.getData()) { 336 if (Main.pref.putColor(e.key, e.color) && e.key.startsWith("mappaint.")) { 283 337 ret = true; 284 338 }
Note:
See TracChangeset
for help on using the changeset viewer.