Ticket #17342: 17342.patch

File 17342.patch, 3.5 KB (added by GerdP, 7 years ago)

enable/disable Lookup button

  • src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

     
    1818import java.util.concurrent.atomic.AtomicBoolean;
    1919
    2020import javax.swing.AbstractAction;
     21import javax.swing.Action;
    2122import javax.swing.JComponent;
    2223import javax.swing.JOptionPane;
    2324import javax.swing.JPopupMenu;
     
    125126        selectButton.setEnabled(false);
    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
    147132        buttons.add(new SideButton(validateAction));
     
    180165        createLayout(tree, true, buttons);
    181166    }
    182167
     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        }
     207    }
     208
    183209    @Override
    184210    public void showNotify() {
    185211        SelectionEventManager.getInstance().addSelectionListener(this);
     
    606632    private static void invalidateValidatorLayers() {
    607633        MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
    608634    }
     635
     636    @Override
     637    public void destroy() {
     638        if (lookupButton != null && lookupButton.getAction() instanceof DataSelectionListener) {
     639            Action a = lookupButton.getAction();
     640            SelectionEventManager.getInstance().removeSelectionListener((DataSelectionListener) a);
     641        }
     642        super.destroy();
     643    }
    609644}