KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > callhierarchy > SearchScopeActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  * Michael Fraenkel (fraenkel@us.ibm.com) - patch
12  * (report 60714: Call Hierarchy: display search scope in view title)
13  *******************************************************************************/

14 package org.eclipse.jdt.internal.ui.callhierarchy;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.jdt.core.search.IJavaSearchScope;
23
24 import org.eclipse.jdt.internal.corext.util.Messages;
25
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.IMenuListener;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.MenuManager;
30 import org.eclipse.jface.action.Separator;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32
33 import org.eclipse.ui.IActionBars;
34 import org.eclipse.ui.IMemento;
35 import org.eclipse.ui.IWorkingSet;
36 import org.eclipse.ui.IWorkingSetManager;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.actions.ActionGroup;
39
40 import org.eclipse.jdt.ui.IContextMenuConstants;
41
42 class SearchScopeActionGroup extends ActionGroup {
43     private static final String JavaDoc TAG_SEARCH_SCOPE_TYPE= "search_scope_type"; //$NON-NLS-1$
44
private static final String JavaDoc TAG_SELECTED_WORKING_SET= "working_set"; //$NON-NLS-1$
45
private static final String JavaDoc TAG_WORKING_SET_COUNT = "working_set_count"; //$NON-NLS-1$
46

47     private static final String JavaDoc DIALOGSTORE_SCOPE_TYPE= "SearchScopeActionGroup.search_scope_type"; //$NON-NLS-1$
48
private static final String JavaDoc DIALOGSTORE_SELECTED_WORKING_SET= "SearchScopeActionGroup.working_set"; //$NON-NLS-1$
49

50     static final int SEARCH_SCOPE_TYPE_WORKSPACE= 1;
51     static final int SEARCH_SCOPE_TYPE_PROJECT= 2;
52     static final int SEARCH_SCOPE_TYPE_HIERARCHY= 3;
53     static final int SEARCH_SCOPE_TYPE_WORKING_SET= 4;
54     
55     private SearchScopeAction fSelectedAction = null;
56     private String JavaDoc[] fSelectedWorkingSetNames = null;
57     private CallHierarchyViewPart fView;
58     private IDialogSettings fDialogSettings;
59     private SearchScopeHierarchyAction fSearchScopeHierarchyAction;
60     private SearchScopeProjectAction fSearchScopeProjectAction;
61     private SearchScopeWorkspaceAction fSearchScopeWorkspaceAction;
62     private SelectWorkingSetAction fSelectWorkingSetAction;
63     
64     public SearchScopeActionGroup(CallHierarchyViewPart view, IDialogSettings dialogSettings) {
65         this.fView= view;
66         this.fDialogSettings= dialogSettings;
67         createActions();
68     }
69     
70     /**
71      * @return IJavaSearchScope
72      */

73     public IJavaSearchScope getSearchScope() {
74         if (fSelectedAction != null) {
75             return fSelectedAction.getSearchScope();
76         }
77         
78         return null;
79     }
80     
81     public void fillActionBars(IActionBars actionBars) {
82         super.fillActionBars(actionBars);
83         fillContextMenu(actionBars.getMenuManager());
84     }
85     
86     protected void setActiveWorkingSets(IWorkingSet[] sets) {
87         if (sets != null) {
88             fSelectedWorkingSetNames = getWorkingSetNames(sets);
89             fSelectedAction = new SearchScopeWorkingSetAction(this, sets, getScopeDescription(sets));
90         } else {
91             fSelectedWorkingSetNames = null;
92             fSelectedAction = null;
93         }
94     }
95     
96     private String JavaDoc[] getWorkingSetNames(IWorkingSet[] sets) {
97         String JavaDoc[] result= new String JavaDoc[sets.length];
98         for (int i = 0; i < sets.length; i++) {
99             result[i]= sets[i].getName();
100         }
101         return result;
102     }
103     
104     protected IWorkingSet[] getActiveWorkingSets() {
105         if (fSelectedWorkingSetNames != null) {
106             return getWorkingSets(fSelectedWorkingSetNames);
107         }
108         
109         return null;
110     }
111     
112     private IWorkingSet[] getWorkingSets(String JavaDoc[] workingSetNames) {
113         if (workingSetNames == null) {
114             return null;
115         }
116         Set JavaDoc workingSets= new HashSet JavaDoc(2);
117         for (int j= 0; j < workingSetNames.length; j++) {
118             IWorkingSet workingSet= getWorkingSetManager().getWorkingSet(workingSetNames[j]);
119             if (workingSet != null) {
120                 workingSets.add(workingSet);
121             }
122         }
123         
124         return (IWorkingSet[])workingSets.toArray(new IWorkingSet[workingSets.size()]);
125     }
126     
127     /**
128      * Sets the new search scope type.
129      *
130      * @param newSelection New action which should be the checked one
131      * @param ignoreUnchecked Ignores actions which are unchecked (necessary since both the old and the new action fires).
132      */

133     protected void setSelected(SearchScopeAction newSelection, boolean ignoreUnchecked) {
134         if (!ignoreUnchecked || newSelection.isChecked()) {
135             if (newSelection instanceof SearchScopeWorkingSetAction) {
136                 fSelectedWorkingSetNames = getWorkingSetNames(((SearchScopeWorkingSetAction) newSelection).getWorkingSets());
137             } else {
138                 fSelectedWorkingSetNames = null;
139             }
140             
141             if (newSelection != null) {
142                 fSelectedAction= newSelection;
143             } else {
144                 fSelectedAction= fSearchScopeWorkspaceAction;
145             }
146             
147             fDialogSettings.put(DIALOGSTORE_SCOPE_TYPE, getSearchScopeType());
148             fDialogSettings.put(DIALOGSTORE_SELECTED_WORKING_SET, fSelectedWorkingSetNames);
149         }
150     }
151     
152     protected CallHierarchyViewPart getView() {
153         return fView;
154     }
155     
156     protected IWorkingSetManager getWorkingSetManager() {
157         IWorkingSetManager workingSetManager = PlatformUI.getWorkbench()
158         .getWorkingSetManager();
159         
160         return workingSetManager;
161     }
162     
163     protected void fillSearchActions(IMenuManager javaSearchMM) {
164         Action[] actions = getActions();
165         
166         for (int i = 0; i < actions.length; i++) {
167             Action action = actions[i];
168             
169             if (action.isEnabled()) {
170                 javaSearchMM.add(action);
171             }
172         }
173         
174         javaSearchMM.setVisible(!javaSearchMM.isEmpty());
175     }
176     
177     public void fillContextMenu(IMenuManager menu) {
178         menu.add(new Separator(IContextMenuConstants.GROUP_SEARCH));
179         
180         MenuManager javaSearchMM = new MenuManager(CallHierarchyMessages.SearchScopeActionGroup_searchScope,
181                 IContextMenuConstants.GROUP_SEARCH);
182         javaSearchMM.setRemoveAllWhenShown(true);
183         
184         javaSearchMM.addMenuListener(new IMenuListener() {
185             /* (non-Javadoc)
186              * @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
187              */

188             public void menuAboutToShow(IMenuManager manager) {
189                 fillSearchActions(manager);
190             }
191         });
192         
193         fillSearchActions(javaSearchMM);
194         menu.appendToGroup(IContextMenuConstants.GROUP_SEARCH, javaSearchMM);
195     }
196     
197     private Action[] getActions() {
198         List JavaDoc actions = new ArrayList JavaDoc(SearchUtil.LRU_WORKINGSET_LIST_SIZE + 4);
199         addAction(actions, fSearchScopeWorkspaceAction);
200         addAction(actions, fSearchScopeProjectAction);
201         addAction(actions, fSearchScopeHierarchyAction);
202         addAction(actions, fSelectWorkingSetAction);
203         
204         Iterator JavaDoc iter= SearchUtil.getLRUWorkingSets().sortedIterator();
205         while (iter.hasNext()) {
206             IWorkingSet[] workingSets= (IWorkingSet[])iter.next();
207             String JavaDoc description = SearchUtil.toString(workingSets);
208             SearchScopeWorkingSetAction workingSetAction = new SearchScopeWorkingSetAction(this, workingSets, description);
209             
210             if (isSelectedWorkingSet(workingSets)) {
211                 workingSetAction.setChecked(true);
212             }
213             
214             actions.add(workingSetAction);
215         }
216         
217         Action[] result = (Action[]) actions.toArray(new Action[actions.size()]);
218         
219         ensureExactlyOneCheckedAction(result);
220         return result;
221     }
222     
223     private void ensureExactlyOneCheckedAction(Action[] result) {
224         int checked = getCheckedActionCount(result);
225         if (checked != 1) {
226             if (checked > 1) {
227                 for (int i = 0; i < result.length; i++) {
228                     Action action = result[i];
229                     action.setChecked(false);
230                 }
231             }
232             fSearchScopeWorkspaceAction.setChecked(true);
233         }
234     }
235     
236     private int getCheckedActionCount(Action[] result) {
237         // Ensure that exactly one action is selected
238
int checked= 0;
239         for (int i = 0; i < result.length; i++) {
240             Action action = result[i];
241             if (action.isChecked()) {
242                 checked++;
243             }
244         }
245         return checked;
246     }
247     
248     private void addAction(List JavaDoc actions, Action action) {
249         if (action == fSelectedAction) {
250             action.setChecked(true);
251         } else {
252             action.setChecked(false);
253         }
254         
255         actions.add(action);
256     }
257     
258     private void createActions() {
259         fSearchScopeWorkspaceAction = new SearchScopeWorkspaceAction(this);
260         fSelectWorkingSetAction = new SelectWorkingSetAction(this);
261         fSearchScopeHierarchyAction = new SearchScopeHierarchyAction(this);
262         fSearchScopeProjectAction = new SearchScopeProjectAction(this);
263         
264         int searchScopeType;
265         try {
266             searchScopeType= fDialogSettings.getInt(DIALOGSTORE_SCOPE_TYPE);
267         } catch (NumberFormatException JavaDoc e) {
268             searchScopeType= SEARCH_SCOPE_TYPE_WORKSPACE;
269         }
270         String JavaDoc[] workingSetNames= fDialogSettings.getArray(DIALOGSTORE_SELECTED_WORKING_SET);
271         setSelected(getSearchScopeAction(searchScopeType, workingSetNames), false);
272     }
273     
274     public void saveState(IMemento memento) {
275         int type= getSearchScopeType();
276         memento.putInteger(TAG_SEARCH_SCOPE_TYPE, type);
277         if (type == SEARCH_SCOPE_TYPE_WORKING_SET) {
278             memento.putInteger(TAG_WORKING_SET_COUNT, fSelectedWorkingSetNames.length);
279             for (int i = 0; i < fSelectedWorkingSetNames.length; i++) {
280                 String JavaDoc workingSetName = fSelectedWorkingSetNames[i];
281                 memento.putString(TAG_SELECTED_WORKING_SET+i, workingSetName);
282             }
283         }
284     }
285     
286     public void restoreState(IMemento memento) {
287         String JavaDoc[] workingSetNames= null;
288         Integer JavaDoc scopeType= memento.getInteger(TAG_SEARCH_SCOPE_TYPE);
289         if (scopeType != null) {
290             if (scopeType.intValue() == SEARCH_SCOPE_TYPE_WORKING_SET) {
291                 Integer JavaDoc workingSetCount= memento.getInteger(TAG_WORKING_SET_COUNT);
292                 if (workingSetCount != null) {
293                     workingSetNames = new String JavaDoc[workingSetCount.intValue()];
294                     for (int i = 0; i < workingSetCount.intValue(); i++) {
295                         workingSetNames[i]= memento.getString(TAG_SELECTED_WORKING_SET+i);
296                     }
297                 }
298             }
299             setSelected(getSearchScopeAction(scopeType.intValue(), workingSetNames), false);
300         }
301     }
302     
303     private SearchScopeAction getSearchScopeAction(int searchScopeType, String JavaDoc[] workingSetNames) {
304         switch (searchScopeType) {
305             case SEARCH_SCOPE_TYPE_WORKSPACE:
306                 return fSearchScopeWorkspaceAction;
307             case SEARCH_SCOPE_TYPE_PROJECT:
308                 return fSearchScopeProjectAction;
309             case SEARCH_SCOPE_TYPE_HIERARCHY:
310                 return fSearchScopeHierarchyAction;
311             case SEARCH_SCOPE_TYPE_WORKING_SET:
312                 IWorkingSet[] workingSets= getWorkingSets(workingSetNames);
313                 if (workingSets != null && workingSets.length > 0) {
314                     return new SearchScopeWorkingSetAction(this, workingSets, getScopeDescription(workingSets));
315                 }
316                 return null;
317         }
318         return null;
319     }
320     
321     private int getSearchScopeType() {
322         if (fSelectedAction != null) {
323             return fSelectedAction.getSearchScopeType();
324         }
325         return 0;
326     }
327     
328     private String JavaDoc getScopeDescription(IWorkingSet[] workingSets) {
329         return Messages.format(CallHierarchyMessages.WorkingSetScope, new String JavaDoc[] {SearchUtil.toString(workingSets)});
330     }
331     
332     /**
333      * Determines whether the specified working sets correspond to the currently selected working sets.
334      * @param workingSets
335      * @return Returns true if the specified working sets correspond to the currently selected working sets
336      */

337     private boolean isSelectedWorkingSet(IWorkingSet[] workingSets) {
338         if (fSelectedWorkingSetNames != null && fSelectedWorkingSetNames.length == workingSets.length) {
339             Set JavaDoc workingSetNames= new HashSet JavaDoc(workingSets.length);
340             for (int i = 0; i < workingSets.length; i++) {
341                 workingSetNames.add(workingSets[i].getName());
342             }
343             for (int i = 0; i < fSelectedWorkingSetNames.length; i++) {
344                 if (!workingSetNames.contains(fSelectedWorkingSetNames[i])) {
345                     return false;
346                 }
347             }
348             return true;
349         }
350         return false;
351     }
352     
353     public String JavaDoc getFullDescription() {
354         if (fSelectedAction != null)
355             return fSelectedAction.getFullDescription();
356         return null;
357     }
358 }
359
360
Popular Tags