Changeset 14847 in josm for trunk


Ignore:
Timestamp:
2019-03-06T12:08:32+01:00 (5 years ago)
Author:
GerdP
Message:

fix #17343 and #17344: Enable/Disable buttons in ValidatorDialog

File:
1 edited

Legend:

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

    r14846 r14847  
    2727import javax.swing.event.TreeSelectionListener;
    2828import javax.swing.tree.DefaultMutableTreeNode;
    29 import javax.swing.tree.TreeNode;
    3029import javax.swing.tree.TreePath;
    3130
     
    4241import org.openstreetmap.josm.data.osm.OsmPrimitive;
    4342import org.openstreetmap.josm.data.osm.WaySegment;
     43import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     44import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
     45import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
     46import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    4447import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    4548import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     
    7780 * @author frsantos
    7881 */
    79 public class ValidatorDialog extends ToggleDialog implements DataSelectionListener, ActiveLayerChangeListener {
     82public class ValidatorDialog extends ToggleDialog
     83        implements DataSelectionListener, ActiveLayerChangeListener, DataSetListenerAdapter.Listener {
    8084
    8185    /** The display tree */
     
    8589    public static final ValidateAction validateAction = new ValidateAction();
    8690
    87     /** The fix button */
    88     private final SideButton fixButton;
    89     /** The ignore button */
    90     private final SideButton ignoreButton;
    91     /** The reset ignorelist button */
    92     private final SideButton ignorelistManagement;
    93     /** The select button */
    94     private final SideButton selectButton;
    95     /** The lookup button */
    96     private final SideButton lookupButton;
     91    /** The fix action */
     92    private final transient Action fixAction;
     93    /** The ignore action */
     94    private final transient Action ignoreAction;
     95    /** The ignore-list management action */
     96    private final transient Action ignorelistManagementAction;
     97    /** The select action */
     98    private final transient Action selectAction;
     99    /** The lookup action */
     100    private final transient LookupAction lookupAction;
    97101
    98102    private final JPopupMenu popupMenu = new JPopupMenu();
    99103    private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
     104    private final transient DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
    100105
    101106    /** Last selected element */
     
    120125        List<SideButton> buttons = new LinkedList<>();
    121126
    122         selectButton = new SideButton(new AbstractSelectAction() {
     127        selectAction = new AbstractSelectAction() {
    123128            @Override
    124129            public void actionPerformed(ActionEvent e) {
    125130                setSelectedItems();
    126131            }
    127         });
    128         InputMapUtils.addEnterAction(tree, selectButton.getAction());
    129 
    130         selectButton.setEnabled(false);
    131         buttons.add(selectButton);
    132 
    133         lookupButton = new SideButton(new LookupAction());
    134         buttons.add(lookupButton);
     132        };
     133        selectAction.setEnabled(false);
     134        InputMapUtils.addEnterAction(tree, selectAction);
     135        buttons.add(new SideButton(selectAction));
     136
     137        lookupAction = new LookupAction();
     138        buttons.add(new SideButton(lookupAction));
    135139
    136140        buttons.add(new SideButton(validateAction));
    137141
    138         fixButton = new SideButton(new AbstractAction() {
     142        fixAction = new AbstractAction() {
    139143            {
    140144                putValue(NAME, tr("Fix"));
     
    146150                fixErrors();
    147151            }
    148         });
    149         fixButton.setEnabled(false);
    150         buttons.add(fixButton);
     152        };
     153        fixAction.setEnabled(false);
     154        buttons.add(new SideButton(fixAction));
    151155
    152156        if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
    153             ignoreButton = new SideButton(new AbstractAction() {
     157            ignoreAction = new AbstractAction() {
    154158                {
    155159                    putValue(NAME, tr("Ignore"));
     
    161165                    ignoreErrors();
    162166                }
    163             });
    164             ignoreButton.setEnabled(false);
    165             buttons.add(ignoreButton);
    166 
    167             ignorelistManagement = new SideButton(new AbstractAction() {
    168                 {
    169                     putValue(NAME, tr("Manage Ignore"));
    170                     putValue(SHORT_DESCRIPTION, tr("Manage the ignore list"));
    171                     new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
    172                 }
    173 
    174                 @Override
    175                 public void actionPerformed(ActionEvent e) {
    176                     new ValidatorListManagementDialog("Ignore");
    177                 }
    178             });
    179             buttons.add(ignorelistManagement);
     167            };
     168            ignoreAction.setEnabled(false);
     169            buttons.add(new SideButton(ignoreAction));
     170
     171            ignorelistManagementAction = new IgnorelistManagementAction();
     172            buttons.add(new SideButton(ignorelistManagementAction));
    180173        } else {
    181             ignoreButton = null;
    182             ignorelistManagement = null;
     174            ignoreAction = null;
     175            ignorelistManagementAction = null;
    183176        }
    184177
     
    187180
    188181    /**
     182     * The action to manage the ignore list.
     183     */
     184    static class IgnorelistManagementAction extends AbstractAction {
     185        IgnorelistManagementAction() {
     186            putValue(NAME, tr("Manage Ignore"));
     187            putValue(SHORT_DESCRIPTION, tr("Manage the ignore list"));
     188            new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     189        }
     190
     191        @Override
     192        public void actionPerformed(ActionEvent e) {
     193            new ValidatorListManagementDialog("Ignore");
     194        }
     195    }
     196
     197    /**
    189198     * The action to lookup the selection in the error tree.
    190199     */
    191     class LookupAction extends AbstractAction implements DataSelectionListener {
    192 
     200    class LookupAction extends AbstractAction {
    193201        LookupAction() {
    194202            putValue(NAME, tr("Lookup"));
    195203            putValue(SHORT_DESCRIPTION, tr("Looks up the selected primitives in the error list."));
    196204            new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
    197             SelectionEventManager.getInstance().addSelectionListener(this);
    198205            updateEnabledState();
    199206        }
     
    208215        }
    209216
    210         protected void updateEnabledState() {
     217        public void updateEnabledState() {
    211218            boolean found = false;
    212219            for (TestError e : tree.getErrors()) {
     
    220227            setEnabled(found);
    221228        }
    222 
    223         @Override
    224         public void selectionChanged(SelectionChangeEvent event) {
    225             updateEnabledState();
    226         }
    227229    }
    228230
    229231    @Override
    230232    public void showNotify() {
     233        DatasetEventManager.getInstance().addDatasetListener(dataChangedAdapter, FireMode.IN_EDT_CONSOLIDATED);
    231234        SelectionEventManager.getInstance().addSelectionListener(this);
    232235        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
     
    235238        }
    236239        MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(this);
     240
    237241    }
    238242
    239243    @Override
    240244    public void hideNotify() {
     245        DatasetEventManager.getInstance().removeDatasetListener(dataChangedAdapter);
    241246        MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
    242247        SelectionEventManager.getInstance().removeSelectionListener(this);
     
    349354        for (TreePath path : selectedPaths) {
    350355            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
    351             Enumeration<TreeNode> children = node.breadthFirstEnumeration();
     356            Enumeration<?> children = node.breadthFirstEnumeration();
    352357            while (children.hasMoreElements()) {
    353358                DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
     
    394399                }
    395400            });
    396             selectButton.setEnabled(true);
    397             if (ignoreButton != null) {
    398                 ignoreButton.setEnabled(node.getDepth() <= 1);
     401            selectAction.setEnabled(true);
     402            if (ignoreAction != null) {
     403                ignoreAction.setEnabled(node.getDepth() <= 1);
    399404            }
    400405        }
     
    472477            }
    473478
    474             fixButton.setEnabled(false);
    475             if (ignoreButton != null) {
    476                 ignoreButton.setEnabled(false);
    477             }
    478             selectButton.setEnabled(false);
     479            fixAction.setEnabled(false);
     480            if (ignoreAction != null) {
     481                ignoreAction.setEnabled(false);
     482            }
     483            selectAction.setEnabled(false);
    479484
    480485            boolean isDblClick = isDoubleClick(e);
     
    483488
    484489            boolean hasFixes = setSelection(sel, isDblClick);
    485             fixButton.setEnabled(hasFixes);
     490            fixAction.setEnabled(hasFixes);
    486491
    487492            if (isDblClick) {
     
    514519        @Override
    515520        public void valueChanged(TreeSelectionEvent e) {
    516             fixButton.setEnabled(false);
    517             if (ignoreButton != null) {
    518                 ignoreButton.setEnabled(false);
    519             }
    520             selectButton.setEnabled(false);
     521            if (ignoreAction != null) {
     522                ignoreAction.setEnabled(false);
     523            }
     524            selectAction.setEnabled(false);
    521525
    522526            Collection<OsmPrimitive> sel = new HashSet<>();
    523527            boolean hasFixes = setSelection(sel, true);
    524             fixButton.setEnabled(hasFixes);
     528            fixAction.setEnabled(hasFixes);
    525529            popupMenuHandler.setPrimitives(sel);
    526530            invalidateValidatorLayers();
     
    578582    public void selectionChanged(SelectionChangeEvent event) {
    579583        updateSelection(event.getSelection());
     584        lookupAction.updateEnabledState();
    580585    }
    581586
     
    682687
    683688    @Override
    684     public void destroy() {
    685         if (lookupButton != null && lookupButton.getAction() instanceof DataSelectionListener) {
    686             Action a = lookupButton.getAction();
    687             SelectionEventManager.getInstance().removeSelectionListener((DataSelectionListener) a);
    688         }
    689         super.destroy();
     689    public void processDatasetEvent(AbstractDatasetChangedEvent event) {
     690        validateAction.updateEnabledState();
    690691    }
    691692
Note: See TracChangeset for help on using the changeset viewer.