Changeset 193 in josm
- Timestamp:
- 2007-01-11T03:22:19+01:00 (18 years ago)
- Files:
-
- 6 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/gui/annotation/AnnotationCellRenderer.java
r180 r193 15 15 final public class AnnotationCellRenderer extends DefaultListCellRenderer { 16 16 @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 17 AnnotationPreset a = value == null ? null : ((ForwardActionListener)value).preset; 17 AnnotationPreset a = null; 18 if (value instanceof ForwardActionListener) 19 a = ((ForwardActionListener)value).preset; 20 else if (value instanceof AnnotationPreset) 21 a = (AnnotationPreset)value; 18 22 String name = a == null ? null : (String)a.getValue(Action.NAME); 19 23 if (name == null) -
src/org/openstreetmap/josm/gui/annotation/AnnotationPreset.java
r190 r193 20 20 21 21 import javax.swing.AbstractAction; 22 import javax.swing.Action; 22 23 import javax.swing.ImageIcon; 23 24 import javax.swing.JCheckBox; … … 34 35 import org.openstreetmap.josm.data.osm.OsmPrimitive; 35 36 import org.openstreetmap.josm.tools.GBC; 36 import org. xml.sax.Attributes;37 import org.openstreetmap.josm.tools.XmlObjectParser; 37 38 import org.xml.sax.SAXException; 38 39 import uk.co.wilson.xml.MinML2;40 39 41 40 … … 53 52 void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds); 54 53 } 55 54 56 55 public static class Text implements Item { 57 private String key; 58 private String label; 56 public String key; 57 public String text; 58 public String default_; 59 public boolean delete_if_empty = false; 60 59 61 private JTextField value = new JTextField(); 60 private boolean deleteIfEmpty; 61 62 public void addToPanel(JPanel p) {63 p.add(new JLabel( label), GBC.std().insets(0,0,10,0));62 63 public void addToPanel(JPanel p) { 64 value.setText(default_ == null ? "" : default_); 65 p.add(new JLabel(text), GBC.std().insets(0,0,10,0)); 64 66 p.add(value, GBC.eol().fill(GBC.HORIZONTAL)); 65 67 } 66 public Text(String key, String label, String value, boolean deleteIfEmpty) {67 this.key = key;68 this.label = label;69 this.value.setText(value == null ? "" : value);70 this.deleteIfEmpty = deleteIfEmpty;71 }72 68 public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 73 69 String v = value.getText(); 74 if (delete IfEmpty && v.length() == 0)70 if (delete_if_empty && v.length() == 0) 75 71 v = null; 76 72 cmds.add(new ChangePropertyCommand(sel, key, v)); … … 79 75 80 76 public static class Check implements Item { 81 private String key; 77 public String key; 78 public String text; 79 public boolean default_ = false; 80 82 81 private JCheckBox check = new JCheckBox(); 83 82 84 83 public void addToPanel(JPanel p) { 84 check.setSelected(default_); 85 check.setText(text); 85 86 p.add(check, GBC.eol().fill(GBC.HORIZONTAL)); 86 87 } 87 public Check(String key, String label, boolean check) {88 this.key = key;89 this.check.setText(label);90 this.check.setSelected(check);91 }92 88 public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 93 89 cmds.add(new ChangePropertyCommand(sel, key, check.isSelected() ? "true" : null)); … … 96 92 97 93 public static class Combo implements Item { 98 private String key; 99 private String label; 94 public String key; 95 public String text; 96 public String values; 97 public String display_values = ""; 98 public String default_; 99 public boolean delete_if_empty = false; 100 public boolean editable = true; 101 100 102 private JComboBox combo; 101 private final String[] values; 102 private boolean deleteIfEmpty; 103 104 public void addToPanel(JPanel p) { 105 p.add(new JLabel(label), GBC.std().insets(0,0,10,0)); 103 104 public void addToPanel(JPanel p) { 105 combo = new JComboBox(display_values.split(",")); 106 combo.setEditable(editable); 107 combo.setSelectedItem(default_); 108 p.add(new JLabel(text), GBC.std().insets(0,0,10,0)); 106 109 p.add(combo, GBC.eol().fill(GBC.HORIZONTAL)); 107 110 } 108 public Combo(String key, String label, String def, String[] values, String[] displayedValues, boolean editable, boolean deleteIfEmpty) { 109 this.key = key; 110 this.label = label; 111 this.values = values; 112 this.deleteIfEmpty = deleteIfEmpty; 113 combo = new JComboBox(displayedValues); 114 combo.setEditable(editable); 115 combo.setSelectedItem(def); 116 } 117 public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 118 String v = combo.getSelectedIndex() == -1 ? null : values[combo.getSelectedIndex()]; 111 public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 112 String v = combo.getSelectedIndex() == -1 ? null : values.split(",")[combo.getSelectedIndex()]; 119 113 String str = combo.isEditable()?combo.getEditor().getItem().toString() : v; 120 if (delete IfEmpty && str != null && str.length() == 0)114 if (delete_if_empty && str != null && str.length() == 0) 121 115 str = null; 122 116 cmds.add(new ChangePropertyCommand(sel, key, str)); … … 125 119 126 120 public static class Label implements Item { 127 p rivateString text;121 public String text; 128 122 129 123 public void addToPanel(JPanel p) { 130 124 p.add(new JLabel(text), GBC.eol()); 131 125 } 132 public Label(String text) {133 this.text = text;134 }135 126 public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {} 136 127 } 137 128 138 129 public static class Key implements Item { 139 p rivateString key;140 p rivateString value;130 public String key; 131 public String value; 141 132 142 133 public void addToPanel(JPanel p) {} 143 public Key(String key, String value) {144 this.key = key;145 this.value = value;146 }147 134 public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) { 148 135 cmds.add(new ChangePropertyCommand(sel, key, value != null && !value.equals("") ? value : null)); … … 150 137 } 151 138 152 private static class Parser extends MinML2 { 153 List<AnnotationPreset> data = new LinkedList<AnnotationPreset>(); 154 List<Item> current; 155 String currentName; 156 Collection<Class<?>> currentTypes; 157 ImageIcon currentIcon; 158 private static int unknownCounter = 1; 159 160 @Override public void startElement(String ns, String lname, String qname, Attributes a) throws SAXException { 161 if (qname.equals("annotations")) 162 return; 163 if (qname.equals("item")) { 164 current = new LinkedList<Item>(); 165 currentName = a.getValue("name"); 166 if (currentName == null) 167 currentName = "Unnamed Preset #"+(unknownCounter++); 168 if (a.getValue("type") != null) { 169 String s = a.getValue("type"); 170 try { 171 for (String type : s.split(",")) { 172 type = Character.toUpperCase(type.charAt(0))+type.substring(1); 173 if (currentTypes == null) 174 currentTypes = new LinkedList<Class<?>>(); 175 currentTypes.add(Class.forName("org.openstreetmap.josm.data.osm."+type)); 176 } 177 } catch (ClassNotFoundException e) { 178 e.printStackTrace(); 179 throw new SAXException(tr("Unknown type at line {0}", getLineNumber())); 180 } 181 } 182 currentIcon = (a.getValue("icon") == null) ? null : new ImageIcon(a.getValue("icon")); 183 } else if (qname.equals("text")) 184 current.add(new Text(a.getValue("key"), a.getValue("text"), a.getValue("default"), parseBoolean(a.getValue("delete_if_empty")))); 185 else if (qname.equals("check")) { 186 boolean checked = parseBoolean(a.getValue("default")); 187 current.add(new Check(a.getValue("key"), a.getValue("text"), checked)); 188 } else if (qname.equals("label")) 189 current.add(new Label(a.getValue("text"))); 190 else if (qname.equals("combo")) { 191 String[] values = a.getValue("values").split(","); 192 String s = a.getValue("readonly"); 193 String dvstr = a.getValue("display_values"); 194 boolean editable = !parseBoolean(s); 195 if (dvstr != null) { 196 if (editable && s != null) 197 throw new SAXException(tr("Cannot have a writable combobox with default values (line {0})", getLineNumber())); 198 editable = false; // for combos with display_value readonly default to false 199 } 200 String[] displayValues = dvstr == null ? values : dvstr.split(","); 201 if (displayValues.length != values.length) 202 throw new SAXException(tr("display_values ({0}) and values ({1}) must be of same number of elements.", 203 displayValues.length+" "+trn("element", "elements", displayValues.length), 204 values.length+" "+trn("element", "elements", values.length))); 205 current.add(new Combo(a.getValue("key"), a.getValue("text"), a.getValue("default"), values, displayValues, editable, parseBoolean(a.getValue("delete_if_empty")))); 206 } else if (qname.equals("key")) 207 current.add(new Key(a.getValue("key"), a.getValue("value"))); 208 else 209 throw new SAXException(tr("Unknown annotation object {0} at line {1} column {2}", qname, getLineNumber(), getColumnNumber())); 210 } 211 212 private boolean parseBoolean(String s) { 213 return s != null && 214 !s.equals("0") && 215 !s.startsWith("off") && 216 !s.startsWith("false") && 217 !s.startsWith("no"); 218 } 219 220 @Override public void endElement(String ns, String lname, String qname) { 221 if (qname.equals("item")) { 222 data.add(new AnnotationPreset(current, currentName, currentIcon, currentTypes)); 223 current = null; 224 currentName = null; 225 currentTypes = null; 226 currentIcon = null; 227 } 228 } 229 } 230 231 private List<Item> data; 232 public Collection<Class<?>> types; 233 234 public AnnotationPreset(List<Item> data, String name, ImageIcon icon, Collection<Class<?>> currentTypes) { 235 super(name, icon == null ? null : new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH))); 236 putValue("toolbar", "annotation_"+name); 237 this.data = data; 238 this.types = currentTypes; 239 Main.toolbar.register(this); 240 } 139 /** 140 * The types as preparsed collection. 141 */ 142 public Collection<Class<?>> types; 143 private List<Item> data = new LinkedList<Item>(); 241 144 242 145 /** … … 247 150 public AnnotationPreset() {} 248 151 249 public static List<AnnotationPreset> readAll(InputStream inStream) throws IOException, SAXException { 152 /** 153 * Called from the XML parser to set the name of the annotation preset 154 */ 155 public void setName(String name) { 156 putValue(Action.NAME, name); 157 putValue("toolbar", "annotation_"+name); 158 } 159 160 /** 161 * Called from the XML parser to set the icon 162 */ 163 public void setIcon(String icon) { 164 putValue(Action.SMALL_ICON, new ImageIcon(new ImageIcon(icon).getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH))); 165 } 166 167 /** 168 * Called from the XML parser to set the types, this preset affects 169 */ 170 public void setType(String types) throws SAXException { 171 try { 172 for (String type : types.split(",")) { 173 type = Character.toUpperCase(type.charAt(0))+type.substring(1); 174 if (this.types == null) 175 this.types = new LinkedList<Class<?>>(); 176 this.types.add(Class.forName("org.openstreetmap.josm.data.osm."+type)); 177 } 178 } catch (ClassNotFoundException e) { 179 e.printStackTrace(); 180 throw new SAXException(tr("Unknown type")); 181 } 182 } 183 184 public static List<AnnotationPreset> readAll(InputStream inStream) throws SAXException { 250 185 BufferedReader in = null; 251 186 try { … … 255 190 in = new BufferedReader(new InputStreamReader(inStream)); 256 191 } 257 Parser p = new Parser(); 258 p.parse(in); 259 return p.data; 192 XmlObjectParser parser = new XmlObjectParser(); 193 parser.mapOnStart("item", AnnotationPreset.class); 194 parser.map("text", Text.class); 195 parser.map("check", Check.class); 196 parser.map("combo", Combo.class); 197 parser.map("label", Label.class); 198 parser.map("key", Key.class); 199 LinkedList<AnnotationPreset> all = new LinkedList<AnnotationPreset>(); 200 parser.start(in); 201 while(parser.hasNext()) { 202 Object o = parser.next(); 203 if (o instanceof AnnotationPreset) { 204 all.add((AnnotationPreset)o); 205 Main.toolbar.register((AnnotationPreset)o); 206 } else 207 all.getLast().data.add((Item)o); 208 } 209 return all; 260 210 } 261 211 … … 287 237 } 288 238 289 239 290 240 public JPanel createPanel() { 291 241 if (data == null)
Note:
See TracChangeset
for help on using the changeset viewer.