Ticket #17344: 17344.patch

File 17344.patch, 11.1 KB (added by GerdP, 7 years ago)
  • src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

     
    2626import javax.swing.event.TreeSelectionEvent;
    2727import javax.swing.event.TreeSelectionListener;
    2828import javax.swing.tree.DefaultMutableTreeNode;
    29 import javax.swing.tree.TreeNode;
    3029import javax.swing.tree.TreePath;
    3130
    3231import org.openstreetmap.josm.actions.AbstractSelectAction;
     
    4039import org.openstreetmap.josm.data.osm.Node;
    4140import org.openstreetmap.josm.data.osm.OsmPrimitive;
    4241import org.openstreetmap.josm.data.osm.WaySegment;
     42import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     43import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
     44import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     45import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    4346import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    4447import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    4548import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
     
    7578 *
    7679 * @author frsantos
    7780 */
    78 public class ValidatorDialog extends ToggleDialog implements DataSelectionListener, ActiveLayerChangeListener {
     81public class ValidatorDialog extends ToggleDialog
     82        implements DataSelectionListener, ActiveLayerChangeListener, DataSetListenerAdapter.Listener {
    7983
    8084    /** The display tree */
    8185    public ValidatorTreePanel tree;
     
    8387    /** The validate action */
    8488    public static final ValidateAction validateAction = new ValidateAction();
    8589
    86     /** The fix button */
    87     private final SideButton fixButton;
    88     /** The ignore button */
    89     private final SideButton ignoreButton;
    90     /** The reset ignorelist button */
    91     private final SideButton ignorelistManagement;
    92     /** The select button */
    93     private final SideButton selectButton;
    94     /** The lookup button */
    95     private final SideButton lookupButton;
     90    /** The fix action */
     91    private final transient Action fixAction;
     92    /** The ignore action */
     93    private final transient Action ignoreAction;
     94    /** The ignore-list management action */
     95    private final transient Action ignorelistManagementAction;
     96    /** The select action */
     97    private final transient Action selectAction;
     98    /** The lookup action */
     99    private final transient LookupAction lookupAction;
    96100
    97101    private final JPopupMenu popupMenu = new JPopupMenu();
    98102    private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
     103    private final transient DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
    99104
    100105    /** Last selected element */
    101106    private DefaultMutableTreeNode lastSelectedNode;
     
    118123
    119124        List<SideButton> buttons = new LinkedList<>();
    120125
    121         selectButton = new SideButton(new AbstractSelectAction() {
     126        selectAction = new AbstractSelectAction() {
    122127            @Override
    123128            public void actionPerformed(ActionEvent e) {
    124129                setSelectedItems();
    125130            }
    126         });
    127         InputMapUtils.addEnterAction(tree, selectButton.getAction());
     131        };
     132        selectAction.setEnabled(false);
     133        InputMapUtils.addEnterAction(tree, selectAction);
     134        buttons.add(new SideButton(selectAction));
    128135
    129         selectButton.setEnabled(false);
    130         buttons.add(selectButton);
     136        lookupAction = new LookupAction();
     137        buttons.add(new SideButton(lookupAction));
    131138
    132         lookupButton = new SideButton(new LookupAction());
    133         buttons.add(lookupButton);
    134 
    135139        buttons.add(new SideButton(validateAction));
    136140
    137         fixButton = new SideButton(new AbstractAction() {
     141        fixAction = new AbstractAction() {
    138142            {
    139143                putValue(NAME, tr("Fix"));
    140144                putValue(SHORT_DESCRIPTION, tr("Fix the selected issue."));
     
    144148            public void actionPerformed(ActionEvent e) {
    145149                fixErrors();
    146150            }
    147         });
    148         fixButton.setEnabled(false);
    149         buttons.add(fixButton);
     151        };
     152        fixAction.setEnabled(false);
     153        buttons.add(new SideButton(fixAction));
    150154
    151155        if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
    152             ignoreButton = new SideButton(new AbstractAction() {
     156            ignoreAction = new AbstractAction() {
    153157                {
    154158                    putValue(NAME, tr("Ignore"));
    155159                    putValue(SHORT_DESCRIPTION, tr("Ignore the selected issue next time."));
     
    159163                public void actionPerformed(ActionEvent e) {
    160164                    ignoreErrors();
    161165                }
    162             });
    163             ignoreButton.setEnabled(false);
    164             buttons.add(ignoreButton);
     166            };
     167            ignoreAction.setEnabled(false);
     168            buttons.add(new SideButton(ignoreAction));
    165169
    166             ignorelistManagement = new SideButton(new AbstractAction() {
     170            ignorelistManagementAction = new AbstractAction() {
    167171                {
    168172                    putValue(NAME, tr("Manage Ignore"));
    169173                    putValue(SHORT_DESCRIPTION, tr("Manage the ignore list"));
     
    174178                public void actionPerformed(ActionEvent e) {
    175179                    new ValidatorListManagementDialog("Ignore");
    176180                }
    177             });
    178             buttons.add(ignorelistManagement);
     181            };
     182            buttons.add(new SideButton(ignorelistManagementAction));
    179183        } else {
    180             ignoreButton = null;
    181             ignorelistManagement = null;
     184            ignoreAction = null;
     185            ignorelistManagementAction = null;
    182186        }
    183187
    184188        createLayout(tree, true, buttons);
     
    187191    /**
    188192     * The action to lookup the selection in the error tree.
    189193     */
    190     class LookupAction extends AbstractAction implements DataSelectionListener {
     194    class LookupAction extends AbstractAction {
    191195
    192196        LookupAction() {
    193197            putValue(NAME, tr("Lookup"));
    194198            putValue(SHORT_DESCRIPTION, tr("Looks up the selected primitives in the error list."));
    195199            new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
    196             SelectionEventManager.getInstance().addSelectionListener(this);
    197200            updateEnabledState();
    198201        }
    199202
     
    206209            tree.selectRelatedErrors(ds.getSelected());
    207210        }
    208211
    209         protected void updateEnabledState() {
     212        public void updateEnabledState() {
    210213            boolean found = false;
    211214            for (TestError e : tree.getErrors()) {
    212215                for (OsmPrimitive p : e.getPrimitives()) {
     
    218221            }
    219222            setEnabled(found);
    220223        }
    221 
    222         @Override
    223         public void selectionChanged(SelectionChangeEvent event) {
    224             updateEnabledState();
    225         }
    226224    }
    227225
    228226    @Override
    229227    public void showNotify() {
     228        DatasetEventManager.getInstance().addDatasetListener(dataChangedAdapter, FireMode.IN_EDT_CONSOLIDATED);
    230229        SelectionEventManager.getInstance().addSelectionListener(this);
    231230        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    232231        if (ds != null) {
     
    233232            updateSelection(ds.getAllSelected());
    234233        }
    235234        MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(this);
     235
    236236    }
    237237
    238238    @Override
    239239    public void hideNotify() {
     240        DatasetEventManager.getInstance().removeDatasetListener(dataChangedAdapter);
    240241        MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
    241242        SelectionEventManager.getInstance().removeSelectionListener(this);
    242243    }
     
    347348        Collection<OsmPrimitive> sel = new HashSet<>(40);
    348349        for (TreePath path : selectedPaths) {
    349350            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
    350             Enumeration<TreeNode> children = node.breadthFirstEnumeration();
     351            Enumeration<?> children = node.breadthFirstEnumeration();
    351352            while (children.hasMoreElements()) {
    352353                DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
    353354                Object nodeInfo = childNode.getUserObject();
     
    392393                            .forEach(sel::add);
    393394                }
    394395            });
    395             selectButton.setEnabled(true);
    396             if (ignoreButton != null) {
    397                 ignoreButton.setEnabled(node.getDepth() <= 1);
     396            selectAction.setEnabled(true);
     397            if (ignoreAction != null) {
     398                ignoreAction.setEnabled(node.getDepth() <= 1);
    398399            }
    399400        }
    400401
     
    470471                tree.clearSelection();
    471472            }
    472473
    473             fixButton.setEnabled(false);
    474             if (ignoreButton != null) {
    475                 ignoreButton.setEnabled(false);
     474            fixAction.setEnabled(false);
     475            if (ignoreAction != null) {
     476                ignoreAction.setEnabled(false);
    476477            }
    477             selectButton.setEnabled(false);
     478            selectAction.setEnabled(false);
    478479
    479480            boolean isDblClick = isDoubleClick(e);
    480481
     
    481482            Collection<OsmPrimitive> sel = isDblClick ? new HashSet<>(40) : null;
    482483
    483484            boolean hasFixes = setSelection(sel, isDblClick);
    484             fixButton.setEnabled(hasFixes);
     485            fixAction.setEnabled(hasFixes);
    485486
    486487            if (isDblClick) {
    487488                DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
     
    512513    public class SelectionWatch implements TreeSelectionListener {
    513514        @Override
    514515        public void valueChanged(TreeSelectionEvent e) {
    515             fixButton.setEnabled(false);
    516             if (ignoreButton != null) {
    517                 ignoreButton.setEnabled(false);
     516            if (ignoreAction!= null) {
     517                ignoreAction.setEnabled(false);
    518518            }
    519             selectButton.setEnabled(false);
     519            selectAction.setEnabled(false);
    520520
    521521            Collection<OsmPrimitive> sel = new HashSet<>();
    522522            boolean hasFixes = setSelection(sel, true);
    523             fixButton.setEnabled(hasFixes);
     523            fixAction.setEnabled(hasFixes);
    524524            popupMenuHandler.setPrimitives(sel);
    525525            invalidateValidatorLayers();
    526526        }
     
    576576    @Override
    577577    public void selectionChanged(SelectionChangeEvent event) {
    578578        updateSelection(event.getSelection());
     579        lookupAction.updateEnabledState();
    579580    }
    580581
    581582    /**
     
    655656    }
    656657
    657658    @Override
    658     public void destroy() {
    659         if (lookupButton != null && lookupButton.getAction() instanceof DataSelectionListener) {
    660             Action a = lookupButton.getAction();
    661             SelectionEventManager.getInstance().removeSelectionListener((DataSelectionListener) a);
    662         }
    663         super.destroy();
     659    public void processDatasetEvent(AbstractDatasetChangedEvent event) {
     660        validateAction.updateEnabledState();
    664661    }
    665662}