- Timestamp:
- 2008-03-28T00:23:05+01:00 (17 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r398 r591 16 16 import java.net.URL; 17 17 import java.util.Collection; 18 import java.util.HashSet; 19 import java.util.LinkedHashMap; 18 20 import java.util.LinkedList; 19 21 import java.util.List; 22 import java.util.Set; 20 23 import java.util.StringTokenizer; 21 24 … … 23 26 import javax.swing.Action; 24 27 import javax.swing.ImageIcon; 25 import javax.swing.JCheckBox;26 28 import javax.swing.JComboBox; 29 import javax.swing.JComponent; 27 30 import javax.swing.JLabel; 28 31 import javax.swing.JOptionPane; … … 35 38 import org.openstreetmap.josm.command.SequenceCommand; 36 39 import org.openstreetmap.josm.data.osm.OsmPrimitive; 40 import org.openstreetmap.josm.gui.QuadStateCheckBox; 37 41 import org.openstreetmap.josm.tools.GBC; 38 42 import org.openstreetmap.josm.tools.ImageProvider; … … 52 56 public static abstract class Item { 53 57 public boolean focus = false; 54 abstract void addToPanel(JPanel p );58 abstract void addToPanel(JPanel p, Collection<OsmPrimitive> sel); 55 59 abstract void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds); 56 60 boolean requestFocusInWindow() {return false;} 57 61 } 58 62 63 public static class Usage { 64 Set<String> values; 65 } 66 67 public static final String DIFFERENT = tr("<different>"); 68 69 static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) { 70 Usage returnValue = new Usage(); 71 returnValue.values = new HashSet<String>(); 72 for (OsmPrimitive s : sel) { 73 returnValue.values.add(s.get(key)); 74 } 75 return returnValue; 76 } 77 78 static Usage determineBooleanUsage(Collection<OsmPrimitive> sel, String key) { 79 80 Usage returnValue = new Usage(); 81 returnValue.values = new HashSet<String>(); 82 for (OsmPrimitive s : sel) { 83 String v = s.get(key); 84 if ("true".equalsIgnoreCase(v)) v = "true"; 85 else if ("yes".equalsIgnoreCase(v)) v = "true"; 86 else if ("1".equals(v)) v = "true"; 87 else if ("false".equalsIgnoreCase(v)) v = "false"; 88 else if ("no".equalsIgnoreCase(v)) v = "false"; 89 else if ("0".equals(v)) v = "false"; 90 returnValue.values.add(v); 91 } 92 return returnValue; 93 } 94 59 95 public static class Text extends Item { 60 96 public String key; 61 97 public String text; 62 98 public String default_; 99 public String originalValue; 63 100 public boolean delete_if_empty = false; 64 101 65 private JTextField value = new JTextField(); 66 67 @Override public void addToPanel(JPanel p) { 68 value.setText(default_ == null ? "" : default_); 102 private JComponent value; 103 104 @Override public void addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 105 106 // find out if our key is already used in the selection. 107 Usage usage = determineTextUsage(sel, key); 108 109 if (usage.values.size() == 1) { 110 // all objects use the same value 111 value = new JTextField(); 112 for (String s : usage.values) ((JTextField) value).setText(s); 113 originalValue = ((JTextField)value).getText(); 114 } else { 115 // the objects have different values 116 value = new JComboBox(usage.values.toArray()); 117 ((JComboBox)value).setEditable(true); 118 ((JComboBox)value).getEditor().setItem(DIFFERENT); 119 originalValue = DIFFERENT; 120 } 69 121 p.add(new JLabel(text), GBC.std().insets(0,0,10,0)); 70 122 p.add(value, GBC.eol().fill(GBC.HORIZONTAL)); 71 123 } 124 72 125 @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 73 String v = value.getText(); 126 127 // return if unchanged 128 String v = (value instanceof JComboBox) ? 129 ((JComboBox)value).getEditor().getItem().toString() : 130 ((JTextField)value).getText(); 131 132 if (v.equals(originalValue) || (originalValue == null && v.length() == 0)) return; 133 74 134 if (delete_if_empty && v.length() == 0) 75 135 v = null; … … 80 140 81 141 public static class Check extends Item { 142 82 143 public String key; 83 144 public String text; 84 public boolean default_ = false; 85 86 private JCheckBox check = new JCheckBox(); 87 88 @Override public void addToPanel(JPanel p) { 89 check.setSelected(default_); 90 check.setText(text); 145 public boolean default_ = false; // not used! 146 147 private QuadStateCheckBox check; 148 private QuadStateCheckBox.State initialState; 149 150 @Override public void addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 151 152 // find out if our key is already used in the selection. 153 Usage usage = determineBooleanUsage(sel, key); 154 155 String oneValue = null; 156 for (String s : usage.values) oneValue = s; 157 if (usage.values.size() < 2 && (oneValue == null || "true".equals(oneValue) || "false".equals(oneValue))) { 158 // all selected objects share the same value which is either true or false or unset, 159 // we can display a standard check box. 160 initialState = "true".equals(oneValue) ? 161 QuadStateCheckBox.State.SELECTED : 162 "false".equals(oneValue) ? 163 QuadStateCheckBox.State.NOT_SELECTED : 164 QuadStateCheckBox.State.UNSET; 165 check = new QuadStateCheckBox(text, initialState, 166 new QuadStateCheckBox.State[] { 167 QuadStateCheckBox.State.NOT_SELECTED, 168 QuadStateCheckBox.State.SELECTED, 169 QuadStateCheckBox.State.UNSET }); 170 } else { 171 // the objects have different values, or one or more objects have something 172 // else than true/false. we display a quad-state check box 173 // in "partial" state. 174 initialState = QuadStateCheckBox.State.PARTIAL; 175 check = new QuadStateCheckBox(text, QuadStateCheckBox.State.PARTIAL, 176 new QuadStateCheckBox.State[] { 177 QuadStateCheckBox.State.NOT_SELECTED, 178 QuadStateCheckBox.State.PARTIAL, 179 QuadStateCheckBox.State.SELECTED, 180 QuadStateCheckBox.State.UNSET }); 181 } 91 182 p.add(check, GBC.eol().fill(GBC.HORIZONTAL)); 92 183 } 184 93 185 @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 94 cmds.add(new ChangePropertyCommand(sel, key, check.isSelected() ? "true" : null)); 186 // if the user hasn't changed anything, don't create a command. 187 if (check.getState() == initialState) return; 188 189 // otherwise change things according to the selected value. 190 cmds.add(new ChangePropertyCommand(sel, key, 191 check.getState() == QuadStateCheckBox.State.SELECTED ? "true" : 192 check.getState() == QuadStateCheckBox.State.NOT_SELECTED ? "false" : 193 null)); 95 194 } 96 195 @Override boolean requestFocusInWindow() {return check.requestFocusInWindow();} … … 98 197 99 198 public static class Combo extends Item { 199 100 200 public String key; 101 201 public String text; … … 107 207 108 208 private JComboBox combo; 109 110 @Override public void addToPanel(JPanel p) { 111 combo = new JComboBox((display_values != null ? display_values : values).split(",")); 209 private LinkedHashMap<String,String> lhm; 210 private Usage usage; 211 private String originalValue; 212 213 @Override public void addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 214 215 // find out if our key is already used in the selection. 216 usage = determineTextUsage(sel, key); 217 218 String[] value_array = values.split(","); 219 String[] display_array = (display_values == null) ? value_array : display_values.split(","); 220 221 lhm = new LinkedHashMap<String,String>(); 222 if (usage.values.size() > 1) { 223 lhm.put(DIFFERENT, DIFFERENT); 224 } 225 for (int i=0; i<value_array.length; i++) { 226 lhm.put(value_array[i], display_array[i]); 227 } 228 for (String s : usage.values) { 229 if (!lhm.containsKey(s)) lhm.put(s, s); 230 } 231 if ((default_ != null) && (!lhm.containsKey(default_))) lhm.put(default_, default_); 232 233 combo = new JComboBox(lhm.values().toArray()); 112 234 combo.setEditable(editable); 113 combo.setSelectedItem(default_); 235 if (usage.values.size() == 1) { 236 for (String s : usage.values) { combo.setSelectedItem(lhm.get(s)); originalValue=s; } 237 } else { 238 combo.setSelectedItem(DIFFERENT); originalValue=DIFFERENT; 239 } 114 240 p.add(new JLabel(text), GBC.std().insets(0,0,10,0)); 115 241 p.add(combo, GBC.eol().fill(GBC.HORIZONTAL)); 116 242 } 117 243 @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 118 String v = combo.getSelectedIndex() == -1 ? null : values.split(",")[combo.getSelectedIndex()]; 119 String str = combo.isEditable()?combo.getEditor().getItem().toString() : v; 244 Object display = combo.getSelectedItem(); 245 String value = null; 246 if (display != null) 247 for (String key : lhm.keySet()) { 248 String k = lhm.get(key); 249 if (k != null && k.equals(display)) value=key; 250 } 251 String str = combo.isEditable() ? combo.getEditor().getItem().toString() : value; 252 253 // no change if same as before 254 if (str.equals(originalValue) || (originalValue == null && str.length() == 0)) return; 255 120 256 if (delete_if_empty && str != null && str.length() == 0) 121 257 str = null; … … 128 264 public String text; 129 265 130 @Override public void addToPanel(JPanel p ) {266 @Override public void addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 131 267 p.add(new JLabel(text), GBC.eol()); 132 268 } … … 138 274 public String value; 139 275 140 @Override public void addToPanel(JPanel p ) {}276 @Override public void addToPanel(JPanel p, Collection<OsmPrimitive> sel) { } 141 277 @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 142 278 cmds.add(new ChangePropertyCommand(sel, key, value != null && !value.equals("") ? value : null)); … … 181 317 182 318 /** 183 * Called from the XML parser to set the types ,this preset affects319 * Called from the XML parser to set the types this preset affects 184 320 */ 185 321 public void setType(String types) throws SAXException { … … 262 398 } 263 399 264 public JPanel createPanel( ) {400 public JPanel createPanel(Collection<OsmPrimitive> selected) { 265 401 if (data == null) 266 402 return null; 267 403 JPanel p = new JPanel(new GridBagLayout()); 404 268 405 for (Item i : data) 269 i.addToPanel(p );406 i.addToPanel(p, selected); 270 407 return p; 271 408 } … … 273 410 public void actionPerformed(ActionEvent e) { 274 411 Collection<OsmPrimitive> sel = Main.ds.getSelected(); 275 JPanel p = createPanel( );412 JPanel p = createPanel(sel); 276 413 if (p == null) 277 414 return;
Note:
See TracChangeset
for help on using the changeset viewer.