Ignore:
Timestamp:
2009-12-13T23:55:13+01:00 (15 years ago)
Author:
casualwalker
Message:

Code-Cleanup after implemenatation of the tickets #2989 #3343.

Location:
applications/editors/josm/plugins/terracer/src/terracer
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/terracer/src/terracer/HouseNumberInputDialog.java

    r19084 r19085  
    11/**
    2  *
     2 * Terracer: A JOSM Plugin for terraced houses.
     3 *
     4 * Copyright 2009 CloudMade Ltd.
     5 *
     6 * Released under the GPLv2, see LICENSE file for details.
    37 */
    48package terracer;
     
    3034 * Created with the Eclipse Visual Editor.
    3135 *
    32  *  This dialog is concerned with the layout.
     36 *  This dialog is concerned with the layout, all logic goes into the
     37 *  HouseNumberinputHandler class.
    3338 *
    3439 * @author casualwalker
     
    256261        }
    257262
     263        /**
     264         * Registers the handler as a listener to all relevant events.
     265         *
     266         * @param handler the handler
     267         */
    258268        public void addHandler(HouseNumberInputHandler handler) {
    259269                this.hi.addActionListener(handler);
  • applications/editors/josm/plugins/terracer/src/terracer/HouseNumberInputHandler.java

    r19084 r19085  
    11/**
    2  *
     2 * Terracer: A JOSM Plugin for terraced houses.
     3 *
     4 * Copyright 2009 CloudMade Ltd.
     5 *
     6 * Released under the GPLv2, see LICENSE file for details.
    37 */
    48package terracer;
     
    2731 * From a refactoring viewpoint, this class is indeed more interested in the fields
    2832 * of the HouseNumberInputDialog. This is desired design, as the HouseNumberInputDialog
    29  * is already cluttered with auto-generated layout code. 
     33 * is already cluttered with auto-generated layout code.
    3034 *
    3135 * @author casualwalker
     
    3438                ActionListener, FocusListener {
    3539
    36         TerracerAction terracerAction;
    37         Way way;
    38         HouseNumberInputDialog dialog;
    39 
    40         public HouseNumberInputHandler(TerracerAction terracerAction, Way way,
    41                         String title) {
     40        private TerracerAction terracerAction;
     41        private Way way;
     42        private HouseNumberInputDialog dialog;
     43
     44        /**
     45         * Instantiates a new house number input handler.
     46         *
     47         * @param terracerAction the terracer action
     48         * @param way the way
     49         * @param title the title
     50         */
     51        public HouseNumberInputHandler(final TerracerAction terracerAction,
     52                        final Way way, final String title) {
    4253                this.terracerAction = terracerAction;
    4354                this.way = way;
     
    5061        }
    5162
     63        /**
     64         * Validate the current input fields.
     65         * When the validation fails, a red message is
     66         * displayed and the OK button is disabled.
     67         *
     68         * Should be triggered each time the input changes.
     69         */
    5270        private void validateInput() {
    5371                boolean isOk = true;
     
    5876                isOk = isOk && checkSegments(message);
    5977                isOk = isOk
    60                                 && checkNumberStringField(dialog.lo, "Lowest number", message);
     78                                && checkNumberStringField(dialog.lo, tr("Lowest number"),
     79                                                message);
    6180                isOk = isOk
    62                                 && checkNumberStringField(dialog.hi, "Highest number", message);
     81                                && checkNumberStringField(dialog.hi, tr("Highest number"),
     82                                                message);
    6383                isOk = isOk
    64                                 && checkNumberStringField(dialog.segments, "Segments", message);
     84                                && checkNumberStringField(dialog.segments, tr("Segments"),
     85                                                message);
    6586
    6687                if (isOk) {
    6788                        dialog.okButton.setEnabled(true);
    6889                        dialog.messageLabel.setForeground(Color.black);
    69                         dialog.messageLabel.setText(HouseNumberInputDialog.DEFAULT_MESSAGE);
     90                        dialog.messageLabel
     91                                        .setText(tr(HouseNumberInputDialog.DEFAULT_MESSAGE));
    7092
    7193                } else {
     
    7698        }
    7799
    78         private boolean checkNumberOrder(StringBuffer message) {
     100        /**
     101         * Checks, if the lowest house number is indeed lower than the
     102         * highest house number.
     103         * This check applies only, if the house number fields are used at all.
     104         *
     105         * @param message the message
     106         *
     107         * @return true, if successful
     108         */
     109        private boolean checkNumberOrder(final StringBuffer message) {
    79110                if (numberFrom() != null && numberTo() != null) {
    80111                        if (numberFrom().intValue() > numberTo().intValue()) {
    81112                                appendMessageNewLine(message);
    82113                                message
    83                                                 .append("Lowest housenumber cannot be higher than highest housenumber");
     114                                                .append(tr("Lowest housenumber cannot be higher than highest housenumber"));
    84115                                return false;
    85116                        }
     
    88119        }
    89120
    90         private boolean checkSegmentsFromHousenumber(StringBuffer message) {
     121        /**
     122         * Obtain the number segments from the house number fields and check,
     123         * if they are valid.
     124         *
     125         * Also disables the segments field, if the house numbers contain
     126         * valid information.
     127         *
     128         * @param message the message
     129         *
     130         * @return true, if successful
     131         */
     132        private boolean checkSegmentsFromHousenumber(final StringBuffer message) {
    91133                dialog.segments.setEditable(true);
    92134
    93135                if (numberFrom() != null && numberTo() != null) {
     136
    94137                        int segments = numberTo().intValue() - numberFrom().intValue();
    95138
    96139                        if (segments % stepSize() != 0) {
    97140                                appendMessageNewLine(message);
    98                                 message.append("Housenumbers do not match odd/even setting");
     141                                message
     142                                                .append(tr("Housenumbers do not match odd/even setting"));
    99143                                return false;
    100144                        }
    101145
    102146                        int steps = segments / stepSize();
     147                        steps++; // difference 0 means 1 building, see
     148                        // TerracerActon.terraceBuilding
    103149                        dialog.segments.setText(String.valueOf(steps));
    104150                        dialog.segments.setEditable(false);
     
    108154        }
    109155
    110         private boolean checkSegments(StringBuffer message) {
     156        /**
     157         * Check the number of segments.
     158         * It must be a number and greater than 1.
     159         *
     160         * @param message the message
     161         *
     162         * @return true, if successful
     163         */
     164        private boolean checkSegments(final StringBuffer message) {
    111165                if (segments() == null || segments().intValue() < 1) {
    112166                        appendMessageNewLine(message);
    113                         message.append("Segment must be a number greater 1");
     167                        message.append(tr("Segment must be a number greater 1"));
    114168                        return false;
    115169
     
    127181         * @return true, if successful
    128182         */
    129         private boolean checkNumberStringField(JTextField field, String label,
    130                         StringBuffer message) {
    131                 String content = field.getText();
     183        private boolean checkNumberStringField(final JTextField field,
     184                        final String label, final StringBuffer message) {
     185                final String content = field.getText();
    132186                if (content != null && !content.isEmpty()) {
    133187                        try {
     
    135189                                if (i < 0) {
    136190                                        appendMessageNewLine(message);
    137                                         message.append(label + " must be greater than 0");
     191                                        message.append(label + tr(" must be greater than 0"));
    138192                                        return false;
    139193                                }
    140194                        } catch (NumberFormatException e) {
    141195                                appendMessageNewLine(message);
    142                                 message.append(label + " is not a number");
     196                                message.append(label + tr(" is not a number"));
    143197                                return false;
    144198                        }
     
    149203
    150204        /**
    151          * @param message
    152          */
    153         private void appendMessageNewLine(StringBuffer message) {
     205         * Append a new line to the message, if the message is not empty.
     206         *
     207         * @param message the message
     208         */
     209        private void appendMessageNewLine(final StringBuffer message) {
    154210                if (message.length() > 0) {
    155211                        message.append("\n");
     
    157213        }
    158214
     215        /* (non-Javadoc)
     216         * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
     217         */
    159218        @Override
    160219        public void stateChanged(ChangeEvent e) {
     
    163222        }
    164223
     224        /* (non-Javadoc)
     225         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
     226         */
    165227        @Override
    166228        public void itemStateChanged(ItemEvent e) {
     
    168230        }
    169231
    170         @Override
    171         public void actionPerformed(ActionEvent e) {
    172 
     232        /* (non-Javadoc)
     233         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     234         */
     235        @Override
     236        public void actionPerformed(final ActionEvent e) {
     237
     238                // OK or Cancel button-actions
    173239                if (e.getSource() instanceof JButton) {
    174240                        JButton button = (JButton) e.getSource();
     
    182248                        }
    183249                } else {
     250                        // anything else is a change in the input
    184251                        validateInput();
    185252                }
     
    187254        }
    188255
     256        /**
     257         * Calculate the step size between two house numbers,
     258         * based on the interpolation setting.
     259         *
     260         * @return the stepSize (1 for all, 2 for odd /even)
     261         */
    189262        public int stepSize() {
    190                 return (dialog.interpolation.getSelectedItem() == tr("All")) ? 1 : 2;
    191         }
    192 
     263                return (dialog.interpolation.getSelectedItem().equals(tr("All"))) ? 1
     264                                : 2;
     265        }
     266
     267        /**
     268         * Gets the number of segments, if set.
     269         *
     270         * @return the number of segments or null, if not set / invalid.
     271         */
    193272        public Integer segments() {
    194273                try {
     
    199278        }
    200279
     280        /**
     281         * Gets the lowest house number.
     282         *
     283         * @return the number of lowest house number or null, if not set / invalid.
     284         */
    201285        public Integer numberFrom() {
    202286                try {
     
    207291        }
    208292
     293        /**
     294         * Gets the highest house number.
     295         *
     296         * @return the number of highest house number or null, if not set / invalid.
     297         */
    209298        public Integer numberTo() {
    210299                try {
     
    215304        }
    216305
     306        /**
     307         * Gets the street name.
     308         *
     309         * @return the  street name or null, if not set / invalid.
     310         */
    217311        public String streetName() {
    218312                // Object selected = street.getSelectedItem();
     
    230324        }
    231325
     326        /* (non-Javadoc)
     327         * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
     328         */
    232329        @Override
    233330        public void focusGained(FocusEvent e) {
     
    235332        }
    236333
     334        /* (non-Javadoc)
     335         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
     336         */
    237337        @Override
    238338        public void focusLost(FocusEvent e) {
     
    240340        }
    241341
    242         /**
    243          * Indicates, if house numbers should be used (instead of segments).
    244          *
    245          * @return true, if if both number values are set to a number.
    246          */
    247         private boolean useHouseNumbers() {
    248                 return numberFrom() != null && numberTo() != null;
    249 
    250         }
    251342}
  • applications/editors/josm/plugins/terracer/src/terracer/TerracerAction.java

    r19084 r19085  
    1717import java.util.Collections;
    1818import java.util.LinkedList;
    19 import java.util.TreeSet;
    2019
    2120import javax.swing.JOptionPane;
     
    8079                                                title = tr("Nothing selected!");
    8180
    82                                         // first ask the user how many buildings to terrace into
    83                                         HouseNumberInputHandler handler = new HouseNumberInputHandler(
    84                                                         this, way, title);
    85 
    86                                         // IntermediateInputDialog inputDialog = new
    87                                         // IntermediateInputDialog(
    88                                         // this, way);
    89                                         // inputDialog.pack();
    90                                         // inputDialog.setVisible(true);
    91 
    92                                         // HouseNumberDialog dialog = new HouseNumberDialog(this);
    93                                         // final JOptionPane optionPane = new JOptionPane(dialog,
    94                                         // JOptionPane.PLAIN_MESSAGE,
    95                                         // JOptionPane.OK_CANCEL_OPTION);
    96                                         //
    97                                         // String title = trn("Change {0} object",
    98                                         // "Change {0} objects", sel.size(), sel.size());
    99                                         // if (sel.size() == 0)
    100                                         // title = tr("Nothing selected!");
    101                                         //
    102                                         // optionPane.createDialog(Main.parent, title)
    103                                         // .setVisible(true);
    104                                         // Object answerObj = optionPane.getValue();
    105                                         // if (answerObj != null
    106                                         // && answerObj != JOptionPane.UNINITIALIZED_VALUE
    107                                         // && (answerObj instanceof Integer && (Integer) answerObj
    108                                         // == JOptionPane.OK_OPTION)) {
    109                                         //
    110                                         // // call out to the method which does the actual
    111                                         // // terracing.
    112                                         // terraceBuilding(way, dialog.numberFrom(), dialog
    113                                         // .numberTo(), dialog.stepSize(), dialog
    114                                         // .streetName());
    115                                         //
    116                                         // }
     81                                        // show input dialog.
     82                                        new HouseNumberInputHandler(this, way, title);
     83
    11784                                } else {
    11885                                        badSelect = true;
     
    149116                        nb = segments.intValue();
    150117                } else {
    151                         // TODO: we're in trouble.
    152                         // do exception handling.
    153                         nb = 0;
     118                        // if we get here, there is is a bug in the input validation.
     119                        throw new TerracerRuntimeException(
     120                                        "Could not determine segments from parameters, this is a bug. "
     121                                                        + "Parameters were: segments " + segments
     122                                                        + " from " + from + " to " + to + " step " + step);
    154123                }
    155124
     
    419388
    420389        /**
    421          * Generates a list of all visible names of highways in order to do
    422          * autocompletion on the road name.
    423          *
    424          * TODO: REMOVE this method here!
    425          */
    426         TreeSet<String> createAutoCompletionInfo() {
    427                 final TreeSet<String> names = new TreeSet<String>();
    428                 for (OsmPrimitive osm : Main.main.getCurrentDataSet()
    429                                 .allNonDeletedPrimitives()) {
    430                         if (osm.getKeys() != null && osm.keySet().contains("highway")
    431                                         && osm.keySet().contains("name")) {
    432                                 names.add(osm.get("name"));
    433                         }
    434                 }
    435                 return names;
    436         }
    437 
    438         /**
    439390         * Creates a new node at the interpolated position between the argument
    440391         * nodes. Interpolates linearly in Lat/Lon coordinates.
Note: See TracChangeset for help on using the changeset viewer.