KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > SearchHistoryDropDownAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search2.internal.ui;
12
13 import com.ibm.icu.text.MessageFormat;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Event;
18 import org.eclipse.swt.widgets.Menu;
19 import org.eclipse.swt.widgets.MenuItem;
20
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.ActionContributionItem;
23 import org.eclipse.jface.action.IMenuCreator;
24
25 import org.eclipse.search.ui.ISearchQuery;
26 import org.eclipse.search.ui.ISearchResult;
27 import org.eclipse.search.ui.NewSearchUI;
28
29 import org.eclipse.search.internal.ui.SearchPluginImages;
30
31 class SearchHistoryDropDownAction extends Action implements IMenuCreator {
32
33     private class ShowSearchFromHistoryAction extends Action {
34         private ISearchResult fSearch;
35
36         public ShowSearchFromHistoryAction(ISearchResult search) {
37             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
38
fSearch= search;
39             
40             String JavaDoc label= escapeAmp(search.getLabel());
41             if (InternalSearchUI.getInstance().isQueryRunning(search.getQuery()))
42                 label= MessageFormat.format(SearchMessages.SearchDropDownAction_running_message, new String JavaDoc[] { label });
43             // fix for bug 38049
44
if (label.indexOf('@') >= 0)
45                 label+= '@';
46             setText(label);
47             setImageDescriptor(search.getImageDescriptor());
48             setToolTipText(search.getTooltip());
49         }
50         
51         private String JavaDoc escapeAmp(String JavaDoc label) {
52             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
53             for (int i= 0; i < label.length(); i++) {
54                 char ch= label.charAt(i);
55                 buf.append(ch);
56                 if (ch == '&') {
57                     buf.append('&');
58                 }
59             }
60             return buf.toString();
61         }
62         
63         public void runWithEvent(Event event) {
64             InternalSearchUI.getInstance().showSearchResult(fSearchView, fSearch, event.stateMask == SWT.CTRL);
65         }
66         
67         public void run() {
68             InternalSearchUI.getInstance().showSearchResult(fSearchView, fSearch, false);
69         }
70     }
71
72     public static final int RESULTS_IN_DROP_DOWN= 10;
73
74     private Menu fMenu;
75     private SearchView fSearchView;
76     
77     public SearchHistoryDropDownAction(SearchView searchView) {
78         setText(SearchMessages.SearchDropDownAction_label);
79         setToolTipText(SearchMessages.SearchDropDownAction_tooltip);
80         SearchPluginImages.setImageDescriptors(this, SearchPluginImages.T_LCL, SearchPluginImages.IMG_LCL_SEARCH_HISTORY);
81         fSearchView= searchView;
82         setMenuCreator(this);
83     }
84     
85     public void updateEnablement() {
86         boolean hasQueries= InternalSearchUI.getInstance().getSearchManager().hasQueries();
87         setEnabled(hasQueries);
88     }
89
90     public void dispose() {
91         disposeMenu();
92     }
93
94     void disposeMenu() {
95         if (fMenu != null)
96             fMenu.dispose();
97     }
98
99     public Menu getMenu(Menu parent) {
100         return null;
101     }
102
103     public Menu getMenu(Control parent) {
104         ISearchResult currentSearch= fSearchView.getCurrentSearchResult();
105         disposeMenu();
106         
107         fMenu= new Menu(parent);
108                 
109         ISearchQuery[] searches= NewSearchUI.getQueries();
110         if (searches.length > 0) {
111             for (int i= 0; i < searches.length; i++) {
112                 ISearchResult search= searches[i].getSearchResult();
113                 ShowSearchFromHistoryAction action= new ShowSearchFromHistoryAction(search);
114                 action.setChecked(search.equals(currentSearch));
115                 addActionToMenu(fMenu, action);
116             }
117             new MenuItem(fMenu, SWT.SEPARATOR);
118             addActionToMenu(fMenu, new ShowSearchHistoryDialogAction(fSearchView));
119             addActionToMenu(fMenu, new RemoveAllSearchesAction());
120         }
121         return fMenu;
122     }
123
124     protected void addActionToMenu(Menu parent, Action action) {
125         ActionContributionItem item= new ActionContributionItem(action);
126         item.fill(parent, -1);
127     }
128     
129     public void run() {
130         new ShowSearchHistoryDialogAction(fSearchView).run();
131     }
132 }
133
Popular Tags