Changeset 36057 in osm


Ignore:
Timestamp:
2023-02-23T22:30:59+01:00 (18 months ago)
Author:
taylor.smock
Message:

Fix #21659: IAE: Listener FastDrawingMode was not registered before or already removed

This occurs because FastDraw tries to only be available when there is an unlocked
data layer.

Also clean up the code a bit (SonarLint, checkstyle).

Location:
applications/editors/josm/plugins/FastDraw
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FDSettings.java

    r34510 r36057  
    99import org.openstreetmap.josm.spi.preferences.Config;
    1010
     11/**
     12 * A holder for plugin settings
     13 */
    1114public class FDSettings {
    1215    public NamedColorProperty COLOR_FIXED = new NamedColorProperty("fastdraw.color.delete", Color.red);
  • applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawConfigDialog.java

    r35860 r36057  
    2727import org.openstreetmap.josm.tools.TextTagParser;
    2828
     29/**
     30 * A configuration dialog
     31 */
    2932public class FastDrawConfigDialog extends ExtendedDialog {
    3033
    31     private final JLabel label1 = new JLabel(tr("Epsilon multiplier"));
    32     private final JLabel label2 = new JLabel(tr("Starting Epsilon"));
    33     private final JLabel label3 = new JLabel(tr("Max points count per 1 km"));
    34     private final JLabel label4 = new JLabel(/* I18n: Combobox to select what a press to return key does */ tr("Enter key mode"));
    35     private final JLabel label5 = new JLabel(tr("Auto add tags"));
     34    private static final long serialVersionUID = -4894608522010226092L;
    3635    private final JFormattedTextField text1 = new JFormattedTextField(NumberFormat.getInstance());
    3736    private final JFormattedTextField text2 = new JFormattedTextField(NumberFormat.getInstance());
     
    4746    private final FDSettings settings;
    4847
     48    /**
     49     * Create anew configuration dialog
     50     * @param settings The settings to use
     51     */
    4952    public FastDrawConfigDialog(FDSettings settings) {
    50         super(MainApplication.getMainFrame(), tr("FastDraw configuration"), new String[] {tr("Ok"), tr("Cancel")});
     53        super(MainApplication.getMainFrame(), tr("FastDraw configuration"), tr("Ok"), tr("Cancel"));
    5154        this.settings = settings;
    5255
     
    5558        all.setLayout(layout);
    5659        JButton pasteButton = new JButton(new AbstractAction(tr("Paste"), ImageProvider.get("apply")) {
     60            private static final long serialVersionUID = -8597276971260620654L;
     61
    5762            @Override
    5863            public void actionPerformed(ActionEvent e) {
    5964                String s = ClipboardUtils.getClipboardStringContent();
    60                 if (s != null) {
    61                     if (TextTagParser.getValidatedTagsFromText(s, TextTagPaster::warning) != null) {
    62                         addTags.setText(s);
    63                     }
     65                if (s != null && TextTagParser.getValidatedTagsFromText(s, TextTagPaster::warning) != null) {
     66                    addTags.setText(s);
    6467                }
    6568            }
     
    6871
    6972        addTags.getModel().prefs().load("fastdraw.tags-history");
    70         while (addTags.getModel().find("") != null)
     73        while (addTags.getModel().find("") != null) {
    7174            addTags.getModel().removeElement("");
     75        }
    7276
     77        JLabel label1 = new JLabel(tr("Epsilon multiplier"));
    7378        all.add(label1, GBC.std().insets(10, 0, 0, 0));
    7479        all.add(text1, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
     80        JLabel label2 = new JLabel(tr("Starting Epsilon"));
    7581        all.add(label2, GBC.std().insets(10, 0, 0, 0));
    7682        all.add(text2, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
     83        JLabel label3 = new JLabel(tr("Max points count per 1 km"));
    7784        all.add(label3, GBC.std().insets(10, 0, 0, 0));
    7885        all.add(text3, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
     86        JLabel label4 = new JLabel(/* I18n: Combobox to select what a press to return key does */ tr("Enter key mode"));
    7987        all.add(label4, GBC.std().insets(10, 0, 0, 0));
    8088        all.add(combo1, GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
    8189
     90        JLabel label5 = new JLabel(tr("Auto add tags"));
    8291        all.add(label5, GBC.std().insets(10, 0, 0, 0));
    8392        all.add(pasteButton, GBC.eop().insets(0, 0, 0, 5));
     
    105114
    106115        setContent(all, false);
    107         setButtonIcons(new String[] {"ok", "cancel"});
    108         setToolTipTexts(new String[] {
    109                 tr("Save settings"),
    110                 tr("Cancel")
    111         });
     116        setButtonIcons("ok", "cancel");
     117        setToolTipTexts(tr("Save settings"),
     118                tr("Cancel"));
    112119        setDefaultButton(1);
    113         //configureContextsensitiveHelp("/Action/DownloadObject", true /* show help button */);
    114120    }
    115121
  • applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawingMode.java

    r35978 r36057  
    5050 */
    5151class FastDrawingMode extends MapMode implements MapViewPaintable, KeyPressReleaseListener, ModifierExListener {
     52    private static final long serialVersionUID = -3395918050593965929L;
    5253    // CHECKSTYLE.OFF: LineLength
    53     private static final String SIMPLIFYMODE_MESSAGE =
     54    private static final String SIMPLIFY_MODE_MESSAGE =
    5455            tr("Q=Options, Enter=save, Ctrl-Enter=save with tags, Up/Down=tune");
    55     private static final String DRAWINGMODE_MESSAGE =
     56    private static final String DRAWING_MODE_MESSAGE =
    5657            tr("Click or Click&drag to continue, Ctrl-Click to add fixed node, Shift-Click to delete, Enter to simplify or save, Ctrl-Shift-Click to start new line");
    5758    // CHECKSTYLE.ON: LineLength
     
    6869    private final Cursor cursorShift;
    6970    private final Cursor cursorReady;
    70     //private final Cursor cursorNode;
    7171    private final Cursor cursorDrawing;
    7272    private boolean nearSomeNode;
     
    8080    private Way oldWay;
    8181
    82     FastDrawingMode(MapFrame mapFrame) {
    83         super(tr("FastDrawing"), "turbopen.png", tr("Fast drawing mode"),
     82    FastDrawingMode() {
     83        super(tr("FastDrawing"), "turbopen", tr("Fast drawing mode"),
    8484                Shortcut.registerShortcut("mapmode:fastdraw", tr("Mode: {0}", tr("Fast drawing mode")), KeyEvent.VK_F, Shortcut.SHIFT),
    8585                Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
     
    8989        cursorShift = ImageProvider.getCursor("crosshair", "new");
    9090        cursorReady = ImageProvider.getCursor("crosshair", "ready");
    91         //cursorNode = ImageProvider.getCursor("crosshair", "joinnode");
    9291        cursorDrawing = ImageProvider.getCursor("crosshair", "mode");
    93         //loadPrefs();
    9492    }
    9593
     
    9896    @Override
    9997    public void enterMode() {
     98        this.updateEnabledState();
    10099        if (!isEnabled())
    101100            return;
     
    150149    }
    151150
    152     private final ArrayList<Point> fixedPoints = new ArrayList<>(3000); // temporary storate for paint
     151    private final ArrayList<Point> fixedPoints = new ArrayList<>(3000); // temporary storage for paint
    153152
    154153    //////////    Event listener methods
     
    201200                    g.fillRect(p2.x - rp, p2.y - rp, dp, dp);
    202201                }
    203                 if (!drawing) {
    204                     if (!line.wasSimplified() && nearestPointIndex == i+1) {
    205                         if (shift) {
    206                             // highlight node to delete
    207                             g.setStroke(settings.deleteStroke);
    208                             g.setColor(settings.COLOR_DELETE.get());
    209                             g.drawLine(p2.x - 5, p2.y - 5, p2.x + 5, p2.y + 5);
    210                             g.drawLine(p2.x - 5, p2.y + 5, p2.x + 5, p2.y - 5);
    211                             g.setStroke(settings.normalStroke);
    212                         } else if (ctrl) {
    213                             // highlight node to toggle fixation
    214                             g.setStroke(settings.deleteStroke);
    215                             g.setColor(line.isFixed(pp2) ? settings.COLOR_NORMAL.get() : settings.COLOR_FIXED.get());
    216                             g.fillOval(p2.x - bigDotSize/2-2, p2.y - bigDotSize/2-2, bigDotSize+4, bigDotSize+4);
    217                             g.setStroke(settings.normalStroke);
    218                         }
     202                if (!drawing && !line.wasSimplified() && nearestPointIndex == i+1) {
     203                    if (shift) {
     204                        // highlight node to delete
     205                        g.setStroke(settings.deleteStroke);
     206                        g.setColor(settings.COLOR_DELETE.get());
     207                        g.drawLine(p2.x - 5, p2.y - 5, p2.x + 5, p2.y + 5);
     208                        g.drawLine(p2.x - 5, p2.y + 5, p2.x + 5, p2.y - 5);
     209                        g.setStroke(settings.normalStroke);
     210                    } else if (ctrl) {
     211                        // highlight node to toggle fixation
     212                        g.setStroke(settings.deleteStroke);
     213                        g.setColor(line.isFixed(pp2) ? settings.COLOR_NORMAL.get() : settings.COLOR_FIXED.get());
     214                        g.fillOval(p2.x - bigDotSize/2-2, p2.y - bigDotSize/2-2, bigDotSize+4, bigDotSize+4);
     215                        g.setStroke(settings.normalStroke);
    219216                    }
    220217                }
     
    286283
    287284    private void startDrawing(Point point, boolean fixFlag) {
    288         //if (line.isClosed()) { setStatusLine(tr(SIMPLIFYMODE_MESSAGE));return;  }
    289285        drawing = true;
    290286        if (line.wasSimplified()) {
     
    293289            saveAsWay(false);
    294290            newDrawing();
    295             //line.clearSimplifiedVersion();
    296291        }
    297292
     
    301296            if (nd1 != null) {
    302297                // found node, make it fixed point of the line
    303                 //System.out.println("node "+nd1);
    304298                p = nd1.getCoor();
    305299                line.fixPoint(p);
     
    325319        drawing = false;
    326320        highlightedFragmentStart = null;
    327         if (!line.isClosed()) setStatusLine(DRAWINGMODE_MESSAGE);
     321        if (!line.isClosed()) setStatusLine(DRAWING_MODE_MESSAGE);
    328322        updateCursor();
    329323        repaint();
     
    380374            return;
    381375        }
    382         if (line.isClosed()) setStatusLine(SIMPLIFYMODE_MESSAGE);
     376        if (line.isClosed()) setStatusLine(SIMPLIFY_MODE_MESSAGE);
    383377
    384378        // do not draw points close to existing points - we do not want self-intersections
     
    391385        // free mouse-drawing
    392386        if (nearSomeNode) {
    393             if (settings.snapNodes && lastP != null && Math.hypot(e.getX() - lastP.x, e.getY() - lastP.y) > 1e-2) {
     387            if (nd1 != null && settings.snapNodes && lastP != null
     388                    && Math.hypot((double) e.getX() - lastP.x, (double) e.getY() - lastP.y) > 1e-2) {
    394389                line.addFixed(nd1.getCoor()); // snap to node coords
    395390                repaint();
     
    397392            }
    398393        } else {
    399             if (lastP != null && Math.hypot(e.getX() - lastP.x, e.getY() - lastP.y) > settings.minPixelsBetweenPoints) {
     394            if (lastP != null && Math.hypot((double) e.getX() - lastP.x, (double) e.getY() - lastP.y) > settings.minPixelsBetweenPoints) {
    400395                line.addLast(getLatLon(e)); // add new point
    401396                repaint();
     
    426421            // first Enter = simplify, second = save the way
    427422            if (!line.wasSimplified()) {
    428                 //line.simplify(eps);
    429423                switch(settings.simplifyMode) {
    430424                case 0: //case 1:
     
    456450        case KeyEvent.VK_UP:
    457451            if (ctrl || shift || alt) return;
    458             // less details
     452            // fewer details
    459453            e.consume();
    460454            if (line.wasSimplified()) changeEpsilon(1/settings.epsilonMult);
     
    483477                    tr("{0} m - length of the line\n{1} nodes\n{2} points per km (maximum)\n{3} points per km (average)",
    484478                            line.getLength(), line.getPoints().size(), line.getNodesPerKm(settings.pkmBlockSize),
    485                             line.getNodesPerKm(1000000)),
     479                            line.getNodesPerKm(1_000_000)),
    486480                    tr("Line information"), JOptionPane.INFORMATION_MESSAGE);
    487481            break;
    488482        case KeyEvent.VK_Q:
    489             // less details
     483            // fewer details
    490484            e.consume();
    491485            new FastDrawConfigDialog(settings).showDialog();
     
    508502    @Override
    509503    public void doKeyReleased(KeyEvent keyEvent) {
    510         //System.out.println("released "+keyEvent);
    511504        if (keyEvent.getKeyCode() == KeyEvent.VK_SPACE) stopDrawing();
    512505        updateCursor();
     
    550543        } else {
    551544            w = new Way(oldWay);
    552             w.setNodes(new ArrayList<Node>()); // nodes will be created frosm scratch
     545            w.setNodes(new ArrayList<>()); // nodes will be created from scratch
    553546        }
    554547
     
    586579        if (!settings.autoTags.isEmpty()) {
    587580            Map<String, String> tags = TextTagParser.readTagsFromText(settings.autoTags);
    588             for (String k: tags.keySet()) {
    589                 w.put(k, tags.get(k));
     581            for (Map.Entry<String, String> entry : tags.entrySet()) {
     582                w.put(entry.getKey(), entry.getValue());
    590583            }
    591584        }
     
    620613
    621614    void changeEpsilon(double k) {
    622         //System.out.println(tr("Eps={0}", eps));
    623615        eps *= k;
    624616        line.simplify(eps);
     
    636628        repaint();
    637629    }
    638 
    639     /*private Node findClosestNode(LatLon p, double d) {
    640         Node nn=null;
    641         double dist,minD=1e10,x,y;
    642         Point pscreen=getPoint(p);   x=pscreen.x; y=pscreen.y;
    643         BBox b=new BBox(new LatLon(p.lat()-deltaLatLon,p.lon()-deltaLatLon),
    644                 new LatLon(p.lat()+deltaLatLon,p.lon()+deltaLatLon));
    645         List<Node> nodes = getCurrentDataSet().searchNodes(b);
    646         for (Node n: nodes) {
    647             dist = Math.sqrt(getPoint(n.getCoor()).distanceSq(x,y));
    648             if (dist<d && dist<minD) {
    649                 nn=n;
    650                 minD=dist;
    651             };
    652         }
    653         return nn;
    654     }*/
    655630
    656631    private void loadFromWay(Way w) {
     
    681656        setStatusLine(tr("Eps={0}, {1} points, {2} p/km",
    682657                eps, line.getSimplePointsCount(), line.getNodesPerKm(settings.pkmBlockSize))+" "
    683                 +SIMPLIFYMODE_MESSAGE);
     658                + SIMPLIFY_MODE_MESSAGE);
    684659    }
    685660
     
    687662        MapView mapView = MainApplication.getMap().mapView;
    688663        if (shift) mapView.setCursor(cursorShift); else
    689             if (line.isClosed() || (nearestPointIndex == 0)) mapView.setCursor(cursorReady); else
    690                 if (ctrl) mapView.setCursor(cursorCtrl); else
    691                     if (nearSomeNode && settings.snapNodes) mapView.setCursor(cursorCtrl); else
    692                         if (drawing) mapView.setCursor(cursorDrawing); else
    693                             mapView.setCursor(cursorDraw);
    694     }
    695 
    696     private void repaint() {
     664            if (line.isClosed() || (nearestPointIndex == 0)) mapView.setCursor(cursorReady);
     665            else if (ctrl) mapView.setCursor(cursorCtrl);
     666            else if (nearSomeNode && settings.snapNodes) mapView.setCursor(cursorCtrl);
     667            else if (drawing) mapView.setCursor(cursorDrawing);
     668            else mapView.setCursor(cursorDraw);
     669    }
     670
     671    private static void repaint() {
    697672        MainApplication.getMap().mapView.repaint();
    698673    }
     
    703678        if (selectedWays != null // if there is a selection
    704679                && selectedWays.size() == 1 // and one way is selected
    705                 && line.getPoints().size() == 0) /* and ther is no already drawn line */ {
     680                && line.getPoints().isEmpty()) /* and there is no already drawn line */ {
    706681            // we can start drawing new way starting from old one
    707682            Way w = selectedWays.iterator().next();
  • applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawingPlugin.java

    r33583 r36057  
    88import org.openstreetmap.josm.plugins.PluginInformation;
    99
     10/**
     11 * The plugin entry point
     12 */
    1013public class FastDrawingPlugin extends Plugin {
    1114
     
    1720    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    1821        if (oldFrame == null && newFrame != null) {
    19             MainApplication.getMap().addMapMode(new IconToggleButton(new FastDrawingMode(MainApplication.getMap())));
     22            MainApplication.getMap().addMapMode(new IconToggleButton(new FastDrawingMode()));
    2023        }
    2124    }
  • applications/editors/josm/plugins/FastDraw/test/unit/org/openstreetmap/josm/plugins/fastdraw/DrawnPolyLineTest.java

    r36056 r36057  
     1// License: GPL. For details, see LICENSE file.
    12package org.openstreetmap.josm.plugins.fastdraw;
    23
Note: See TracChangeset for help on using the changeset viewer.