Changeset 2123 in josm


Ignore:
Timestamp:
2009-09-13T22:42:43+02:00 (15 years ago)
Author:
stoecker
Message:

see #3475 - patch by Petr Dlouhý - display filtering - disabled by default some time until more mature

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r2070 r2123  
    2525import org.openstreetmap.josm.tools.GBC;
    2626import org.openstreetmap.josm.tools.Shortcut;
     27import org.openstreetmap.josm.data.osm.Filter;
    2728
    2829public class SearchAction extends JosmAction{
     
    3132
    3233    public static enum SearchMode {
    33         replace, add, remove
     34        replace, add, remove, in_selection
    3435    }
    3536
     
    5758        SearchSetting s = lastSearch;
    5859        if (s == null) {
    59             s = new SearchSetting("", false, false, SearchMode.replace);
    60         }
    61         showSearchDialog(s);
    62     }
    63 
    64     public void showSearchDialog(SearchSetting initialValues) {
    65         JLabel label = new JLabel(tr("Please enter a search string."));
     60            s = new SearchSetting("", SearchMode.replace, false, false);
     61        }
     62        SearchSetting se = showSearchDialog(s);
     63        if(se != null) searchWithHistory(se);
     64    }
     65
     66    public static SearchSetting showSearchDialog(SearchSetting initialValues) {
     67        JLabel label = new JLabel( initialValues instanceof Filter ? tr("Please enter a filter string.") : tr("Please enter a search string."));
    6668        final JTextField input = new JTextField(initialValues.text);
    6769        input.selectAll();
     
    7072        JRadioButton add = new JRadioButton(tr("add to selection"), initialValues.mode == SearchMode.add);
    7173        JRadioButton remove = new JRadioButton(tr("remove from selection"), initialValues.mode == SearchMode.remove);
     74        JRadioButton in_selection = new JRadioButton(tr("find in selection"), initialValues.mode == SearchMode.in_selection);
    7275        ButtonGroup bg = new ButtonGroup();
    7376        bg.add(replace);
    7477        bg.add(add);
    7578        bg.add(remove);
     79        bg.add(in_selection);
    7680
    7781        JCheckBox caseSensitive = new JCheckBox(tr("case sensitive"), initialValues.caseSensitive);
     
    7983
    8084        JPanel left = new JPanel(new GridBagLayout());
     85
     86        JTextField finput = null;
     87        if(initialValues instanceof Filter){
     88           JLabel fLabel = new JLabel(tr("Please enter a filter name."));
     89           finput = new JTextField(((Filter)initialValues).filterName);
     90           left.add(fLabel, GBC.eop());
     91           left.add(finput, GBC.eop().fill(GBC.HORIZONTAL));
     92        }
     93
    8194        left.add(label, GBC.eop());
    8295        left.add(input, GBC.eop().fill(GBC.HORIZONTAL));
    8396        left.add(replace, GBC.eol());
    8497        left.add(add, GBC.eol());
    85         left.add(remove, GBC.eop());
     98        left.add(remove, GBC.eol());
     99        left.add(in_selection, GBC.eop());
    86100        left.add(caseSensitive, GBC.eol());
    87101        left.add(regexSearch, GBC.eol());
     
    120134        ExtendedDialog dialog = new ExtendedDialog(
    121135                Main.parent,
    122                 tr("Search"),
    123                 new String[] {tr("Start Search"), tr("Cancel")}
     136                initialValues instanceof Filter ? tr("Filter") : tr("Search"),
     137                new String[] {
     138                   initialValues instanceof Filter ? tr("Make filter") : tr("Start Search"),
     139                tr("Cancel")}
    124140        );
    125141        dialog.setButtonIcons(new String[] {"dialogs/search.png", "cancel.png"});
     
    128144        int result = dialog.getValue();
    129145
    130         if(result != 1) return;
     146        if(result != 1) return null;
    131147
    132148        // User pressed OK - let's perform the search
    133149        SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace
    134                 : (add.isSelected() ? SearchAction.SearchMode.add : SearchAction.SearchMode.remove);
    135         SearchSetting setting = new SearchSetting(input.getText(), caseSensitive.isSelected(), regexSearch.isSelected(), mode);
    136         searchWithHistory(setting);
     150                : (add.isSelected() ? SearchAction.SearchMode.add
     151                : (remove.isSelected() ? SearchAction.SearchMode.remove : SearchAction.SearchMode.in_selection));
     152        if(initialValues instanceof Filter){
     153           return new Filter(input.getText(), mode, caseSensitive.isSelected(), regexSearch.isSelected(), finput.getText());
     154        } else {
     155           return new SearchSetting(input.getText(), mode, caseSensitive.isSelected(), regexSearch.isSelected());
     156        }
    137157    }
    138158
     
    151171        }
    152172        lastSearch = s;
    153         search(s.text, s.mode, s.caseSensitive, s.regexSearch);
     173        search(s);
    154174    }
    155175
    156176    public static void searchWithoutHistory(SearchSetting s) {
    157177        lastSearch = s;
    158         search(s.text, s.mode, s.caseSensitive, s.regexSearch);
    159     }
    160 
     178        search(s);
     179    }
     180
     181    public interface Function{
     182       public Boolean isSomething(OsmPrimitive o);
     183    }
     184
     185    public static Integer getSelection(SearchSetting s, Collection<OsmPrimitive> sel, Function f) {
     186        Integer foundMatches = 0;
     187        try {
     188            String searchText = s.text;
     189            if(s instanceof Filter){
     190               searchText = ((Filter)s).inverted ? "-" : "";
     191               searchText = searchText + "(" + ((Filter)s).text + ")" + (((Filter)s).applyForChildren ? ("| child (" + ((Filter)s).text + ")"): "");
     192            }
     193            /*System.out.println(searchText);  */
     194            SearchCompiler.Match matcher = SearchCompiler.compile(searchText, s.caseSensitive, s.regexSearch);
     195            foundMatches = 0;
     196            for (OsmPrimitive osm : Main.main.getCurrentDataSet().allNonDeletedCompletePrimitives()) {
     197                if (s.mode == SearchMode.replace) {
     198                    if (matcher.match(osm)) {
     199                        sel.add(osm);
     200                        ++foundMatches;
     201                    } else {
     202                        sel.remove(osm);
     203                    }
     204                } else if (s.mode == SearchMode.add && !f.isSomething(osm) && matcher.match(osm)) {
     205                    sel.add(osm);
     206                    ++foundMatches;
     207                } else if (s.mode == SearchMode.remove && f.isSomething(osm) && matcher.match(osm)) {
     208                    sel.remove(osm);
     209                    ++foundMatches;
     210                } else if (s.mode == SearchMode.in_selection &&  f.isSomething(osm)&& !matcher.match(osm)) {
     211                    sel.remove(osm);
     212                    ++foundMatches;
     213                }
     214            }
     215        } catch (SearchCompiler.ParseError e) {
     216            JOptionPane.showMessageDialog(
     217                    Main.parent,
     218                    e.getMessage(),
     219                    tr("Error"),
     220                    JOptionPane.ERROR_MESSAGE
     221
     222            );
     223        }
     224        return foundMatches;
     225    }
     226 
    161227    public static void search(String search, SearchMode mode, boolean caseSensitive, boolean regexSearch) {
     228       search(new SearchSetting(search, mode, caseSensitive, regexSearch));
     229    }
     230
     231    public static void search(SearchSetting s) {
    162232        // FIXME: This is confusing. The GUI says nothing about loading primitives from an URL. We'd like to *search*
    163233        // for URLs in the current data set.
     
    172242        //            }
    173243        //        }
    174         try {
    175             Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
    176             SearchCompiler.Match matcher = SearchCompiler.compile(search, caseSensitive, regexSearch);
    177             int foundMatches = 0;
    178             for (OsmPrimitive osm : Main.main.getCurrentDataSet().allNonDeletedCompletePrimitives()) {
    179                 if (mode == SearchMode.replace) {
    180                     if (matcher.match(osm)) {
    181                         sel.add(osm);
    182                         ++foundMatches;
    183                     } else {
    184                         sel.remove(osm);
    185                     }
    186                 } else if (mode == SearchMode.add && !osm.isSelected() && matcher.match(osm)) {
    187                     sel.add(osm);
    188                     ++foundMatches;
    189                 } else if (mode == SearchMode.remove && osm.isSelected() && matcher.match(osm)) {
    190                     sel.remove(osm);
    191                     ++foundMatches;
    192                 }
    193             }
    194             Main.main.getCurrentDataSet().setSelected(sel);
    195             if (foundMatches == 0) {
    196                 String msg = null;
    197                 if (mode == SearchMode.replace) {
    198                     msg = tr("No match found for ''{0}''", search);
    199                 } else if (mode == SearchMode.add) {
    200                     msg = tr("Nothing added to selection by searching for ''{0}''", search);
    201                 } else if (mode == SearchMode.remove) {
    202                     msg = tr("Nothing removed from selection by searching for ''{0}''", search);
    203                 }
    204                 Main.map.statusLine.setHelpText(msg);
    205                 JOptionPane.showMessageDialog(
    206                         Main.parent,
    207                         msg,
    208                         tr("Warning"),
    209                         JOptionPane.WARNING_MESSAGE
    210                 );
    211             } else {
    212                 Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
    213             }
    214         } catch (SearchCompiler.ParseError e) {
    215             JOptionPane.showMessageDialog(
    216                     Main.parent,
    217                     e.getMessage(),
    218                     tr("Error"),
    219                     JOptionPane.ERROR_MESSAGE
    220 
    221             );
    222         }
     244         
     245       Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
     246       int foundMatches = getSelection(s, sel, new Function(){
     247          public Boolean isSomething(OsmPrimitive o){
     248             return o.isSelected();
     249          }
     250       });
     251       Main.main.getCurrentDataSet().setSelected(sel);
     252       if (foundMatches == 0) {
     253           String msg = null;
     254           if (s.mode == SearchMode.replace) {
     255               msg = tr("No match found for ''{0}''", s.text);
     256           } else if (s.mode == SearchMode.add) {
     257               msg = tr("Nothing added to selection by searching for ''{0}''", s.text);
     258           } else if (s.mode == SearchMode.remove) {
     259               msg = tr("Nothing removed from selection by searching for ''{0}''", s.text);
     260           } else if (s.mode == SearchMode.in_selection) {
     261               msg = tr("Nothing find in selection by searching for ''{0}''", s.text);
     262           }
     263           Main.map.statusLine.setHelpText(msg);
     264           JOptionPane.showMessageDialog(
     265                   Main.parent,
     266                   msg,
     267                   tr("Warning"),
     268                   JOptionPane.WARNING_MESSAGE
     269           );
     270       } else {
     271           Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
     272       }
    223273    }
    224274
    225275    public static class SearchSetting {
    226         String text;
    227         SearchMode mode;
    228         boolean caseSensitive;
    229         boolean regexSearch;
    230 
    231         public SearchSetting(String text, boolean caseSensitive, boolean regexSearch, SearchMode mode) {
     276        public String text;
     277        public SearchMode mode;
     278        public boolean caseSensitive;
     279        public boolean regexSearch;
     280
     281        public SearchSetting(String text, SearchMode mode, boolean caseSensitive, boolean regexSearch) {
    232282            super();
    233283            this.caseSensitive = caseSensitive;
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r2021 r2123  
    2929import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
    3030import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
     31import org.openstreetmap.josm.gui.dialogs.FilterDialog;
    3132import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
    3233import org.openstreetmap.josm.gui.dialogs.UserListDialog;
     
    104105        addToggleDialog(new HistoryDialog());
    105106        addToggleDialog(new SelectionListDialog());
     107        if(Main.pref.getBoolean("displayfilter", false))
     108            addToggleDialog(new FilterDialog());
    106109        addToggleDialog(new UserListDialog());
    107110        addToggleDialog(conflictDialog = new ConflictDialog());
     
    251254     * Replies the instance of a toggle dialog of type <code>type</code> managed by this
    252255     * map frame
    253      * 
     256     *
    254257     * @param <T>
    255258     * @param type the class of the toggle dialog, i.e. UserListDialog.class
    256259     * @return the instance of a toggle dialog of type <code>type</code> managed by this
    257260     * map frame; null, if no such dialog exists
    258      * 
     261     *
    259262     */
    260263    public <T> T getToggleDialog(Class<T> type) {
Note: See TracChangeset for help on using the changeset viewer.