Ignore:
Timestamp:
2009-07-19T10:31:27+02:00 (15 years ago)
Author:
Gubaer
Message:

improved enabling/disabling of menu entries and action buttons depending on current state of JOSM (number of open layers, type of active layer, etc.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r1677 r1808  
    2323import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2424import org.openstreetmap.josm.gui.ExtendedDialog;
     25import org.openstreetmap.josm.gui.layer.Layer;
     26import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
    2527import org.openstreetmap.josm.tools.GBC;
    2628import org.openstreetmap.josm.tools.Shortcut;
    2729
    28 public class SearchAction extends JosmAction {
     30public class SearchAction extends JosmAction implements LayerChangeListener{
    2931
    3032    public static final int SEARCH_HISTORY_SIZE = 10;
     
    4042    public SearchAction() {
    4143        super(tr("Search..."), "dialogs/search", tr("Search for objects."),
    42         Shortcut.registerShortcut("system:find", tr("Search..."), KeyEvent.VK_F, Shortcut.GROUP_HOTKEY), true);
     44                Shortcut.registerShortcut("system:find", tr("Search..."), KeyEvent.VK_F, Shortcut.GROUP_HOTKEY), true);
     45        Layer.listeners.add(this);
     46        refreshEnabled();
    4347    }
    4448
    4549    public void actionPerformed(ActionEvent e) {
     50        if (!isEnabled())
     51            return;
    4652        if (Main.map == null) {
    4753            JOptionPane.showMessageDialog(Main.parent, tr("No data loaded."));
     
    4955        }
    5056        SearchSetting s = lastSearch;
    51         if (s == null)
     57        if (s == null) {
    5258            s = new SearchSetting("", false, false, SearchMode.replace);
     59        }
    5360        showSearchDialog(s);
    5461    }
     
    8188        JPanel right = new JPanel();
    8289        JLabel description =
    83         new JLabel("<html><ul>"
    84                 + "<li>"+tr("<b>Baker Street</b> - 'Baker' and 'Street' in any key or name.")+"</li>"
    85                 + "<li>"+tr("<b>\"Baker Street\"</b> - 'Baker Street' in any key or name.")+"</li>"
    86                 + "<li>"+tr("<b>name:Bak</b> - 'Bak' anywhere in the name.")+"</li>"
    87                 + "<li>"+tr("<b>type=route</b> - key 'type' with value exactly 'route'.") + "</li>"
    88                 + "<li>"+tr("<b>type=*</b> - key 'type' with any value. Try also <b>*=value</b>, <b>type=</b>, <b>*=*</b>, <b>*=</b>") + "</li>"
    89                 + "<li>"+tr("<b>-name:Bak</b> - not 'Bak' in the name.")+"</li>"
    90                 + "<li>"+tr("<b>foot:</b> - key=foot set to any value.")+"</li>"
    91                 + "<li>"+tr("<u>Special targets:</u>")+"</li>"
    92                 + "<li>"+tr("<b>type:</b> - type of the object (<b>node</b>, <b>way</b>, <b>relation</b>)")+"</li>"
    93                 + "<li>"+tr("<b>user:</b>... - all objects changed by user")+"</li>"
    94                 + "<li>"+tr("<b>id:</b>... - object with given ID")+"</li>"
    95                 + "<li>"+tr("<b>nodes:</b>... - object with given number of nodes")+"</li>"
    96                 + "<li>"+tr("<b>modified</b> - all changed objects")+"</li>"
    97                 + "<li>"+tr("<b>selected</b> - all selected objects")+"</li>"
    98                 + "<li>"+tr("<b>incomplete</b> - all incomplete objects")+"</li>"
    99                 + "<li>"+tr("<b>untagged</b> - all untagged objects")+"</li>"
    100                 + "<li>"+tr("<b>child <i>expr</i></b> - all children of objects matching the expression")+"</li>"
    101                 + "<li>"+tr("<b>parent <i>expr</i></b> - all parents of objects matching the expression")+"</li>"
    102                 + "<li>"+tr("Use <b>|</b> or <b>OR</b> to combine with logical or")+"</li>"
    103                 + "<li>"+tr("Use <b>\"</b> to quote operators (e.g. if key contains :)")+"</li>"
    104                 + "<li>"+tr("Use <b>(</b> and <b>)</b> to group expressions")+"</li>"
    105                 + "</ul></html>");
     90            new JLabel("<html><ul>"
     91                    + "<li>"+tr("<b>Baker Street</b> - 'Baker' and 'Street' in any key or name.")+"</li>"
     92                    + "<li>"+tr("<b>\"Baker Street\"</b> - 'Baker Street' in any key or name.")+"</li>"
     93                    + "<li>"+tr("<b>name:Bak</b> - 'Bak' anywhere in the name.")+"</li>"
     94                    + "<li>"+tr("<b>type=route</b> - key 'type' with value exactly 'route'.") + "</li>"
     95                    + "<li>"+tr("<b>type=*</b> - key 'type' with any value. Try also <b>*=value</b>, <b>type=</b>, <b>*=*</b>, <b>*=</b>") + "</li>"
     96                    + "<li>"+tr("<b>-name:Bak</b> - not 'Bak' in the name.")+"</li>"
     97                    + "<li>"+tr("<b>foot:</b> - key=foot set to any value.")+"</li>"
     98                    + "<li>"+tr("<u>Special targets:</u>")+"</li>"
     99                    + "<li>"+tr("<b>type:</b> - type of the object (<b>node</b>, <b>way</b>, <b>relation</b>)")+"</li>"
     100                    + "<li>"+tr("<b>user:</b>... - all objects changed by user")+"</li>"
     101                    + "<li>"+tr("<b>id:</b>... - object with given ID")+"</li>"
     102                    + "<li>"+tr("<b>nodes:</b>... - object with given number of nodes")+"</li>"
     103                    + "<li>"+tr("<b>modified</b> - all changed objects")+"</li>"
     104                    + "<li>"+tr("<b>selected</b> - all selected objects")+"</li>"
     105                    + "<li>"+tr("<b>incomplete</b> - all incomplete objects")+"</li>"
     106                    + "<li>"+tr("<b>untagged</b> - all untagged objects")+"</li>"
     107                    + "<li>"+tr("<b>child <i>expr</i></b> - all children of objects matching the expression")+"</li>"
     108                    + "<li>"+tr("<b>parent <i>expr</i></b> - all parents of objects matching the expression")+"</li>"
     109                    + "<li>"+tr("Use <b>|</b> or <b>OR</b> to combine with logical or")+"</li>"
     110                    + "<li>"+tr("Use <b>\"</b> to quote operators (e.g. if key contains :)")+"</li>"
     111                    + "<li>"+tr("Use <b>(</b> and <b>)</b> to group expressions")+"</li>"
     112                    + "</ul></html>");
    106113        description.setFont(description.getFont().deriveFont(Font.PLAIN));
    107114        right.add(description);
     
    112119
    113120        int result = new ExtendedDialog(Main.parent,
    114             tr("Search"),
    115             p,
    116             new String[] {tr("Start Search"), tr("Cancel")},
    117             new String[] {"dialogs/search.png", "cancel.png"}).getValue();
     121                tr("Search"),
     122                p,
     123                new String[] {tr("Start Search"), tr("Cancel")},
     124                new String[] {"dialogs/search.png", "cancel.png"}).getValue();
    118125        if(result != 1) return;
    119126
     
    132139     */
    133140    public static void searchWithHistory(SearchSetting s) {
    134         if(searchHistory.isEmpty() || !s.equals(searchHistory.getFirst()))
     141        if(searchHistory.isEmpty() || !s.equals(searchHistory.getFirst())) {
    135142            searchHistory.addFirst(s);
    136         while (searchHistory.size() > SEARCH_HISTORY_SIZE)
     143        }
     144        while (searchHistory.size() > SEARCH_HISTORY_SIZE) {
    137145            searchHistory.removeLast();
     146        }
    138147        lastSearch = s;
    139148        search(s.text, s.mode, s.caseSensitive, s.regexSearch);
     
    163172                        sel.add(osm);
    164173                        ++foundMatches;
    165                     } else
     174                    } else {
    166175                        sel.remove(osm);
     176                    }
    167177                } else if (mode == SearchMode.add && !osm.selected && matcher.match(osm)) {
    168178                    sel.add(osm);
     
    176186            if (foundMatches == 0) {
    177187                String msg = null;
    178                 if (mode == SearchMode.replace)
     188                if (mode == SearchMode.replace) {
    179189                    msg = tr("No match found for ''{0}''", search);
    180                 else if (mode == SearchMode.add)
     190                } else if (mode == SearchMode.add) {
    181191                    msg = tr("Nothing added to selection by searching for ''{0}''", search);
    182                 else if (mode == SearchMode.remove)
     192                } else if (mode == SearchMode.remove) {
    183193                    msg = tr("Nothing removed from selection by searching for ''{0}''", search);
     194                }
    184195                Main.map.statusLine.setHelpText(msg);
    185196                JOptionPane.showMessageDialog(Main.parent, msg);
    186             } else
     197            } else {
    187198                Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
     199            }
    188200        } catch (SearchCompiler.ParseError e) {
    189201            JOptionPane.showMessageDialog(Main.parent, e.getMessage());
     
    212224        }
    213225
     226        @Override
    214227        public boolean equals(Object other) {
    215228            if(!(other instanceof SearchSetting))
     
    217230            SearchSetting o = (SearchSetting) other;
    218231            return (o.caseSensitive == this.caseSensitive
    219                  && o.regexSearch == this.regexSearch
    220                  && o.mode.equals(this.mode)
    221                  && o.text.equals(this.text));
    222         }
     232                    && o.regexSearch == this.regexSearch
     233                    && o.mode.equals(this.mode)
     234                    && o.text.equals(this.text));
     235        }
     236    }
     237
     238    /**
     239     * Refreshes the enabled state
     240     *
     241     */
     242    protected void refreshEnabled() {
     243        setEnabled(Main.map != null
     244                && Main.map.mapView !=null
     245                && Main.map.mapView.getEditLayer() != null
     246        );
     247    }
     248
     249    /* ---------------------------------------------------------------------------------- */
     250    /* Interface LayerChangeListener                                                      */
     251    /* ---------------------------------------------------------------------------------- */
     252    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
     253        refreshEnabled();
     254    }
     255
     256    public void layerAdded(Layer newLayer) {
     257        refreshEnabled();
     258    }
     259
     260    public void layerRemoved(Layer oldLayer) {
     261        refreshEnabled();
    223262    }
    224263}
Note: See TracChangeset for help on using the changeset viewer.