Changeset 5639 in josm for trunk/src/org


Ignore:
Timestamp:
2012-12-28T02:16:10+01:00 (11 years ago)
Author:
framm
Message:

Add a new preset option "auto_increment" that allows the user to select one of a number of given auto-increment values. This works like "use_last_as_default", only that the last value is incremented or decremented as selected by the user. Switch this on for house numbers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r5624 r5639  
    1212import java.awt.Insets;
    1313import java.awt.event.ActionEvent;
     14import java.awt.event.ActionListener;
    1415import java.io.BufferedReader;
    1516import java.io.File;
     
    1920import java.io.Reader;
    2021import java.io.UnsupportedEncodingException;
     22import java.text.NumberFormat;
     23import java.text.ParseException;
    2124import java.util.ArrayList;
    2225import java.util.Arrays;
     
    3437import javax.swing.AbstractAction;
    3538import javax.swing.Action;
     39import javax.swing.ButtonGroup;
    3640import javax.swing.ImageIcon;
     41import javax.swing.JButton;
    3742import javax.swing.JComponent;
    3843import javax.swing.JLabel;
     
    4247import javax.swing.JScrollPane;
    4348import javax.swing.JTextField;
     49import javax.swing.JToggleButton;
    4450import javax.swing.ListCellRenderer;
    4551import javax.swing.ListModel;
     
    393399        public String originalValue;
    394400        public String use_last_as_default = "false";
     401        public String auto_increment;
    395402        public String length;
    396403
     
    411418                    if (!"false".equals(use_last_as_default) && lastValue.containsKey(key)) {
    412419                        textField.setText(lastValue.get(key));
     420                    } else if (auto_increment_selected != 0  && auto_increment != null) {
     421                        try {
     422                            textField.setText(Integer.toString(Integer.parseInt(lastValue.get(key)) + auto_increment_selected));
     423                        } catch (NumberFormatException ex) {
     424                            // Ignore - cannot auto-increment if last was non-numeric
     425                        }
    413426                    } else {
    414427                        textField.setText(default_);
     
    443456                }
    444457            }
     458
     459            // if there's an auto_increment setting, then wrap the text field
     460            // into a panel, appending a number of buttons.
     461            // auto_increment has a format like -2,-1,1,2
     462            // the text box being the first component in the panel is relied
     463            // on in a rather ugly fashion further down.
     464            if (auto_increment != null) {
     465                ButtonGroup bg = new ButtonGroup();
     466                JPanel pnl = new JPanel(new GridBagLayout());
     467                pnl.add(value, GBC.std().fill(GBC.HORIZONTAL));
     468
     469                // first, one button for each auto_increment value
     470                for (final String ai : auto_increment.split(",")) {
     471                    JToggleButton aibutton = new JToggleButton(ai);
     472                    aibutton.setToolTipText(tr("Select auto-increment of {0} for this field", ai));
     473                    aibutton.setMargin(new java.awt.Insets(0,0,0,0));
     474                    bg.add(aibutton);
     475                    try {
     476                        // TODO there must be a better way to parse a number like "+3" than this.
     477                        final int buttonvalue = ((Number)NumberFormat.getIntegerInstance().parse(ai.replace("+", ""))).intValue();
     478                        if (auto_increment_selected == buttonvalue) aibutton.setSelected(true);
     479                        aibutton.addActionListener(new ActionListener() {
     480                            public void actionPerformed(ActionEvent e) {
     481                                auto_increment_selected = buttonvalue;
     482                            }
     483                        });
     484                        pnl.add(aibutton, GBC.std());
     485                    } catch (ParseException x) {
     486                        System.err.println("Cannot parse auto-increment value of '" + ai + "' into an integer");
     487                    }
     488                }
     489
     490                // an invisible toggle button for "release" of the button group
     491                final JToggleButton clearbutton = new JToggleButton("X");
     492                clearbutton.setVisible(false);
     493                bg.add(clearbutton);
     494                // and its visible counterpart. - this mechanism allows us to
     495                // have *no* button selected after the X is clicked, instead
     496                // of the X remaining selected
     497                JButton releasebutton = new JButton("X");
     498                releasebutton.setToolTipText(tr("Cancel auto-increment for this field"));
     499                releasebutton.setMargin(new java.awt.Insets(0,0,0,0));
     500                releasebutton.addActionListener(new ActionListener() {
     501                    public void actionPerformed(ActionEvent e) {
     502                        auto_increment_selected = 0;
     503                        clearbutton.setSelected(true);
     504                    }
     505                });
     506                pnl.add(releasebutton, GBC.std().eol());
     507                value = pnl;
     508            }
    445509            p.add(new JLabel(locale_text+":"), GBC.std().insets(0,0,10,0));
    446510            p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
     
    452516
    453517            // return if unchanged
    454             String v = (value instanceof JosmComboBox)
    455                     ? ((JosmComboBox) value).getEditor().getItem().toString()
    456                             : ((JTextField) value).getText();
    457                     v = v.trim();
    458 
    459                     if (!"false".equals(use_last_as_default)) {
    460                         lastValue.put(key, v);
    461                     }
    462                     if (v.equals(originalValue) || (originalValue == null && v.length() == 0))
    463                         return;
    464 
    465                     changedTags.add(new Tag(key, v));
     518            String v = null;
     519            if (value instanceof JosmComboBox) {
     520                v = ((JosmComboBox) value).getEditor().getItem().toString();
     521            } else if (value instanceof JTextField) {
     522                v = ((JTextField) value).getText();
     523            } else if (value instanceof JPanel) {
     524                // this is what was alluded to with "ugly fashion" above.
     525                v = ((JTextField) (((JPanel)value).getComponent(0))).getText();
     526            } else {
     527                System.err.println("No 'last value' support for component " + value);
     528                return;
     529            }
     530               
     531            v = v.trim();
     532
     533            if (!"false".equals(use_last_as_default) || auto_increment != null) {
     534                lastValue.put(key, v);
     535            }
     536            if (v.equals(originalValue) || (originalValue == null && v.length() == 0))
     537                return;
     538
     539            changedTags.add(new Tag(key, v));
    466540        }
    467541
     
    12571331    public Match nameTemplateFilter;
    12581332    private static final HashMap<String,String> lastValue = new HashMap<String,String>();
     1333    private static int auto_increment_selected = 0;
    12591334
    12601335    /**
Note: See TracChangeset for help on using the changeset viewer.