Changeset 30117 in osm


Ignore:
Timestamp:
2013-12-07T22:09:36+01:00 (11 years ago)
Author:
donvip
Message:

[josm_openinghours] fix #7903 - ArrayIndexOutOfBoundsException in unknown conditions. IllegalArgumentException will be thrown earlier if reproduced.

Location:
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/OpeningTimeUtils.java

    r30114 r30117  
    113113     */
    114114    public static String makeStringFromRects(ArrayList<TimeRect> givenTimeRects) {
    115         // create an array of booleans representing every minute on all the days
    116         // in a week
     115        // create an array of booleans representing every minute on all the days in a week
    117116        boolean[][] minuteArray = new boolean[7][24 * 60 + 2];
    118117        for (int day = 0; day < 7; ++day) {
     
    259258        return ret;
    260259    }
     260   
     261    /**
     262     * Ensures the given day is comprised between 0 and 6.
     263     * @param day The day to check
     264     * @param paramName The parameter name, used in error message
     265     * @throws IllegalArgumentException if the day is invalid
     266     */
     267    public static final void ensureValidDay(int day, String paramName) throws IllegalArgumentException {
     268        if (day < 0 || day > 6) {
     269            throw new IllegalArgumentException(paramName + " is not a valid day (0-6). Given value is " + day);
     270        }
     271    }
     272
     273    /**
     274     * Ensures the given minute is comprised between 0 and 24*60+1.
     275     * @param minute The minute to check
     276     * @param paramName The parameter name, used in error message
     277     * @throws IllegalArgumentException if the minute is invalid
     278     */
     279    public static final void ensureValidMinute(int minute, String paramName) throws IllegalArgumentException {
     280        if (minute < 0 || minute > 24*60+1) {
     281            throw new IllegalArgumentException(paramName + " is not a valid minute (0-1441). Given value is " + minute);
     282        }
     283    }
    261284}
  • applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/OheEditor.java

    r30116 r30117  
    176176    }
    177177
    178     // update all the TimeRects with new Data
     178    /**
     179     * Updates all the TimeRects with new data.
     180     */
    179181    public void initTimeRects() {
    180182        contentPanel.removeAll();
  • applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/TimeRect.java

    r30116 r30117  
    1919import javax.swing.JPopupMenu;
    2020
     21import org.openstreetmap.josm.plugins.ohe.OpeningTimeUtils;
     22
    2123public class TimeRect extends JPanel implements MouseListener, MouseMotionListener {
    2224   
     
    5456
    5557    public TimeRect(OheEditor editor, int dayStart, int dayEnd, int minutesStart, int minutesEnd) {
     58        OpeningTimeUtils.ensureValidDay(dayStart, "dayStart");
     59        OpeningTimeUtils.ensureValidDay(dayEnd, "dayEnd");
     60        OpeningTimeUtils.ensureValidMinute(minutesStart, "minutesStart");
     61        OpeningTimeUtils.ensureValidMinute(minutesEnd, "minutesEnd");
     62       
    5663        this.editor = editor;
    57 
    5864        this.dayStart = dayStart;
    5965        this.dayEnd = dayEnd;
     
    6975    }
    7076
     77    /**
     78     * Returns the starting day, as an index between 0 and 6.
     79     * @return the starting day index
     80     */
    7181    public int getDayStart() {
    7282        return dayStart;
    7383    }
    7484
     85    /**
     86     * Returns the ending day, as an index between 0 and 6.
     87     * @return the ending day index
     88     */
    7589    public int getDayEnd() {
    7690        return dayEnd;
     
    86100
    87101    public void reposition() {
    88         setBounds(editor.getPanelBoundsForTimeinterval(dayStart, dayEnd + 1,
    89                 minuteStart, minuteEnd));
     102        setBounds(editor.getPanelBoundsForTimeinterval(dayStart, dayEnd + 1, minuteStart, minuteEnd));
    90103        editor.contentPanel.repaint();
    91104    }
     
    99112    }
    100113
    101     private void updateTimeInterval(int newDayStart, int newDayEnd,
    102             int newMinuteStart, int newMinuteEnd) {
     114    private void updateTimeInterval(int newDayStart, int newDayEnd, int newMinuteStart, int newMinuteEnd) {
     115        OpeningTimeUtils.ensureValidDay(newDayStart, "newDayStart");
     116        OpeningTimeUtils.ensureValidDay(newDayEnd, "newDayEnd");
     117        OpeningTimeUtils.ensureValidMinute(newMinuteStart, "newMinuteStart");
     118        OpeningTimeUtils.ensureValidMinute(newMinuteEnd, "newMinuteEnd");
     119
    103120        dayStart = newDayStart;
    104121        dayEnd = newDayEnd;
     
    185202    public void showMenu(MouseEvent evt) {
    186203        JPopupMenu menu = new JPopupMenu();
    187         final JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem(
    188                 tr("open end"), isOpenEndInterval());
     204        final JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem(tr("open end"), isOpenEndInterval());
    189205        menu.add(cbMenuItem);
    190206        cbMenuItem.addActionListener(new ActionListener() {
     
    192208            public void actionPerformed(ActionEvent e) {
    193209                if (cbMenuItem.isSelected())
    194                     updateTimeInterval(dayStart, dayEnd, minuteStart,
    195                             24 * 60 + 1);
     210                    updateTimeInterval(dayStart, dayEnd, minuteStart, 24 * 60 + 1);
    196211                else
    197212                    updateTimeInterval(dayStart, dayEnd, minuteStart, 24 * 60);
     
    265280                } else if (newDayStart >= 0 && newDayEnd <= 6) {
    266281                    actualDayDrag += xDiff;
    267                     updateTimeInterval(newDayStart, newDayEnd, minuteStart,
    268                             minuteEnd);
     282                    updateTimeInterval(newDayStart, newDayEnd, minuteStart, minuteEnd);
    269283                }
    270284            }
     
    282296                        && (newMinutesEnd <= 24 * 60 || isOpenEndInterval())) {
    283297                    actualMinuteDrag += yDiff;
    284                     updateTimeInterval(dayStart, dayEnd, newMinutesStart,
    285                             newMinutesEnd);
     298                    updateTimeInterval(dayStart, dayEnd, newMinutesStart, newMinutesEnd);
    286299                }
    287300            }
Note: See TracChangeset for help on using the changeset viewer.