Changeset 14826 in josm


Ignore:
Timestamp:
2019-03-03T10:22:54+01:00 (6 years ago)
Author:
GerdP
Message:

fix #17342 ValidatorDialog: Lookup button needs improvement

Location:
trunk/src/org/openstreetmap/josm/gui/dialogs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

    r14825 r14826  
    1919
    2020import javax.swing.AbstractAction;
     21import javax.swing.Action;
    2122import javax.swing.JComponent;
    2223import javax.swing.JOptionPane;
     
    126127        buttons.add(selectButton);
    127128
    128         lookupButton = new SideButton(new AbstractAction() {
    129             {
    130                 putValue(NAME, tr("Lookup"));
    131                 putValue(SHORT_DESCRIPTION, tr("Looks up the selected primitives in the error list."));
    132                 new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
    133             }
    134 
    135             @Override
    136             public void actionPerformed(ActionEvent e) {
    137                 final DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    138                 if (ds == null) {
    139                     return;
    140                 }
    141                 tree.selectRelatedErrors(ds.getSelected());
    142             }
    143         });
    144 
     129        lookupButton = new SideButton(new LookupAction());
    145130        buttons.add(lookupButton);
    146131
     
    179164        }
    180165        createLayout(tree, true, buttons);
     166    }
     167
     168    /**
     169     * The action to lookup the selection in the error tree.
     170     */
     171     class LookupAction extends AbstractAction implements DataSelectionListener {
     172
     173        LookupAction() {
     174            putValue(NAME, tr("Lookup"));
     175            putValue(SHORT_DESCRIPTION, tr("Looks up the selected primitives in the error list."));
     176            new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
     177            SelectionEventManager.getInstance().addSelectionListener(this);
     178            updateEnabledState();
     179        }
     180
     181        @Override
     182        public void actionPerformed(ActionEvent e) {
     183            final DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
     184            if (ds == null) {
     185                return;
     186            }
     187            tree.selectRelatedErrors(ds.getSelected());
     188        }
     189
     190        protected void updateEnabledState() {
     191            boolean found = false;
     192            for (TestError e : tree.getErrors()) {
     193                for (OsmPrimitive p : e.getPrimitives()) {
     194                    if (p.isSelected()) {
     195                        found = true;
     196                        break;
     197                    }
     198                }
     199            }
     200            setEnabled(found);
     201        }
     202
     203        @Override
     204        public void selectionChanged(SelectionChangeEvent event) {
     205            updateEnabledState();
     206        }
    181207    }
    182208
     
    610636        MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
    611637    }
     638
     639    @Override
     640    public void destroy() {
     641        if (lookupButton != null && lookupButton.getAction() instanceof DataSelectionListener) {
     642            Action a = lookupButton.getAction();
     643            SelectionEventManager.getInstance().removeSelectionListener((DataSelectionListener) a);
     644        }
     645        super.destroy();
     646    }
    612647}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/validator/ValidatorTreePanel.java

    r14771 r14826  
    349349     */
    350350    public void selectRelatedErrors(final Collection<OsmPrimitive> primitives) {
    351         final Collection<TreePath> paths = new ArrayList<>();
     351        final List<TreePath> paths = new ArrayList<>();
    352352        walkAndSelectRelatedErrors(new TreePath(getRoot()), new HashSet<>(primitives)::contains, paths);
    353353        getSelectionModel().clearSelection();
     
    355355            expandPath(path);
    356356            getSelectionModel().addSelectionPath(path);
     357        }
     358        // make sure that first path is visible
     359        if (!paths.isEmpty()) {
     360            scrollPathToVisible(paths.get(0));
    357361        }
    358362    }
Note: See TracChangeset for help on using the changeset viewer.