Changeset 193 in josm


Ignore:
Timestamp:
2007-01-11T03:22:19+01:00 (18 years ago)
Author:
imi
Message:
  • added new XmlObjectParser that should replace the direct use of MinML2 in the future
  • changed AnnotationPreset to use the new parser
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/gui/annotation/AnnotationCellRenderer.java

    r180 r193  
    1515final public class AnnotationCellRenderer extends DefaultListCellRenderer {
    1616        @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;
    1822                String name = a == null ? null : (String)a.getValue(Action.NAME);
    1923                if (name == null)
  • src/org/openstreetmap/josm/gui/annotation/AnnotationPreset.java

    r190 r193  
    2020
    2121import javax.swing.AbstractAction;
     22import javax.swing.Action;
    2223import javax.swing.ImageIcon;
    2324import javax.swing.JCheckBox;
     
    3435import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3536import org.openstreetmap.josm.tools.GBC;
    36 import org.xml.sax.Attributes;
     37import org.openstreetmap.josm.tools.XmlObjectParser;
    3738import org.xml.sax.SAXException;
    38 
    39 import uk.co.wilson.xml.MinML2;
    4039
    4140
     
    5352                void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds);
    5453        }
    55 
     54       
    5655        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
    5961                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));
    6466                        p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
    6567                }
    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                 }
    7268                public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
    7369                        String v = value.getText();
    74                         if (deleteIfEmpty && v.length() == 0)
     70                        if (delete_if_empty && v.length() == 0)
    7571                                v = null;
    7672                        cmds.add(new ChangePropertyCommand(sel, key, v));
     
    7975
    8076        public static class Check implements Item {
    81                 private String key;
     77                public String key;
     78                public String text;
     79                public boolean default_ = false;
     80               
    8281                private JCheckBox check = new JCheckBox();
    8382
    8483                public void addToPanel(JPanel p) {
     84                        check.setSelected(default_);
     85                        check.setText(text);
    8586                        p.add(check, GBC.eol().fill(GBC.HORIZONTAL));
    8687                }
    87                 public Check(String key, String label, boolean check) {
    88                         this.key = key;
    89                         this.check.setText(label);
    90                         this.check.setSelected(check);
    91                 }
    9288                public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
    9389                        cmds.add(new ChangePropertyCommand(sel, key, check.isSelected() ? "true" : null));
     
    9692
    9793        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
    100102                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));
    106109                        p.add(combo, GBC.eol().fill(GBC.HORIZONTAL));
    107110                }
    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()];
    119113                        String str = combo.isEditable()?combo.getEditor().getItem().toString() : v;
    120                         if (deleteIfEmpty && str != null && str.length() == 0)
     114                        if (delete_if_empty && str != null && str.length() == 0)
    121115                                str = null;
    122116                        cmds.add(new ChangePropertyCommand(sel, key, str));
     
    125119
    126120        public static class Label implements Item {
    127                 private String text;
     121                public String text;
    128122
    129123                public void addToPanel(JPanel p) {
    130124                        p.add(new JLabel(text), GBC.eol());
    131125                }
    132                 public Label(String text) {
    133                         this.text = text;
    134                 }
    135126                public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {}
    136127        }
    137128
    138129        public static class Key implements Item {
    139                 private String key;
    140                 private String value;
     130                public String key;
     131                public String value;
    141132
    142133                public void addToPanel(JPanel p) {}
    143                 public Key(String key, String value) {
    144                         this.key = key;
    145                         this.value = value;
    146                 }
    147134                public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
    148135                        cmds.add(new ChangePropertyCommand(sel, key, value != null && !value.equals("") ? value : null));
     
    150137        }
    151138
    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>();
    241144
    242145        /**
     
    247150        public AnnotationPreset() {}
    248151
    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 {
    250185                BufferedReader in = null;
    251186                try {
     
    255190                        in = new BufferedReader(new InputStreamReader(inStream));
    256191                }
    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;
    260210        }
    261211
     
    287237        }
    288238
    289        
     239
    290240        public JPanel createPanel() {
    291241                if (data == null)
Note: See TracChangeset for help on using the changeset viewer.