KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > MemberFilterActionGroup


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.jdt.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.swt.custom.BusyIndicator;
18
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.IToolBarManager;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.viewers.StructuredViewer;
23
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.actions.ActionGroup;
27
28 import org.eclipse.jdt.ui.PreferenceConstants;
29
30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.JavaPluginImages;
33 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
34 import org.eclipse.jdt.internal.ui.viewsupport.MemberFilter;
35 import org.eclipse.jdt.internal.ui.viewsupport.MemberFilterAction;
36
37 /**
38  * Action Group that contributes filter buttons for a view parts showing
39  * methods and fields. Contributed filters are: hide fields, hide static
40  * members hide non-public members and hide local types.
41  * <p>
42  * The action group installs a filter on a structured viewer. The filter is connected
43  * to the actions installed in the view part's toolbar menu and is updated when the
44  * state of the buttons changes.
45  *
46  * <p>
47  * This class may be instantiated; it is not intended to be subclassed.
48  * </p>
49  *
50  * @since 2.0
51  */

52 public class MemberFilterActionGroup extends ActionGroup {
53
54     public static final int FILTER_NONPUBLIC= MemberFilter.FILTER_NONPUBLIC;
55     public static final int FILTER_STATIC= MemberFilter.FILTER_STATIC;
56     public static final int FILTER_FIELDS= MemberFilter.FILTER_FIELDS;
57     
58     /** @since 3.0 */
59     public static final int FILTER_LOCALTYPES= MemberFilter.FILTER_LOCALTYPES;
60     /** @since 3.0 */
61     public static final int ALL_FILTERS= FILTER_NONPUBLIC | FILTER_FIELDS | FILTER_STATIC | FILTER_LOCALTYPES;
62     
63     private static final String JavaDoc TAG_HIDEFIELDS= "hidefields"; //$NON-NLS-1$
64
private static final String JavaDoc TAG_HIDESTATIC= "hidestatic"; //$NON-NLS-1$
65
private static final String JavaDoc TAG_HIDENONPUBLIC= "hidenonpublic"; //$NON-NLS-1$
66
private static final String JavaDoc TAG_HIDELOCALTYPES= "hidelocaltypes"; //$NON-NLS-1$
67

68     private MemberFilterAction[] fFilterActions;
69     private MemberFilter fFilter;
70     
71     private StructuredViewer fViewer;
72     private String JavaDoc fViewerId;
73     private boolean fInViewMenu;
74     
75     
76     /**
77      * Creates a new <code>MemberFilterActionGroup</code>.
78      *
79      * @param viewer the viewer to be filtered
80      * @param viewerId a unique id of the viewer. Used as a key to to store
81      * the last used filter settings in the preference store
82      */

83     public MemberFilterActionGroup(StructuredViewer viewer, String JavaDoc viewerId) {
84         this(viewer, viewerId, false);
85     }
86
87     /**
88      * Creates a new <code>MemberFilterActionGroup</code>.
89      *
90      * @param viewer the viewer to be filtered
91      * @param viewerId a unique id of the viewer. Used as a key to to store
92      * the last used filter settings in the preference store
93      * @param inViewMenu if <code>true</code> the actions are added to the view
94      * menu. If <code>false</code> they are added to the toolbar.
95      *
96      * @since 2.1
97      */

98     public MemberFilterActionGroup(StructuredViewer viewer, String JavaDoc viewerId, boolean inViewMenu) {
99         this(viewer, viewerId, inViewMenu, ALL_FILTERS);
100     }
101     
102     /**
103      * Creates a new <code>MemberFilterActionGroup</code>.
104      *
105      * @param viewer the viewer to be filtered
106      * @param viewerId a unique id of the viewer. Used as a key to to store
107      * the last used filter settings in the preference store
108      * @param inViewMenu if <code>true</code> the actions are added to the view
109      * menu. If <code>false</code> they are added to the toolbar.
110      * @param availableFilters Specifies which filter action should be contained. <code>FILTER_NONPUBLIC</code>,
111      * <code>FILTER_STATIC</code>, <code>FILTER_FIELDS</code> and <code>FILTER_LOCALTYPES</code>
112      * or a combination of these constants are possible values. Use <code>ALL_FILTERS</code> to select all available filters.
113      *
114      * @since 3.0
115      */

116     public MemberFilterActionGroup(StructuredViewer viewer, String JavaDoc viewerId, boolean inViewMenu, int availableFilters) {
117                 
118         fViewer= viewer;
119         fViewerId= viewerId;
120         fInViewMenu= inViewMenu;
121         
122         IPreferenceStore store= PreferenceConstants.getPreferenceStore();
123         fFilter= new MemberFilter();
124         
125         String JavaDoc title, helpContext;
126         ArrayList JavaDoc actions= new ArrayList JavaDoc(4);
127         
128         // fields
129
int filterProperty= FILTER_FIELDS;
130         if (isSet(filterProperty, availableFilters)) {
131             boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty));
132             if (filterEnabled) {
133                 fFilter.addFilter(filterProperty);
134             }
135             title= ActionMessages.MemberFilterActionGroup_hide_fields_label;
136             helpContext= IJavaHelpContextIds.FILTER_FIELDS_ACTION;
137             MemberFilterAction hideFields= new MemberFilterAction(this, title, filterProperty, helpContext, filterEnabled);
138             hideFields.setDescription(ActionMessages.MemberFilterActionGroup_hide_fields_description);
139             hideFields.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_fields_tooltip);
140             JavaPluginImages.setLocalImageDescriptors(hideFields, "fields_co.gif"); //$NON-NLS-1$
141
actions.add(hideFields);
142         }
143             
144         // static
145
filterProperty= FILTER_STATIC;
146         if (isSet(filterProperty, availableFilters)) {
147             boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty));
148             if (filterEnabled) {
149                 fFilter.addFilter(filterProperty);
150             }
151             title= ActionMessages.MemberFilterActionGroup_hide_static_label;
152             helpContext= IJavaHelpContextIds.FILTER_STATIC_ACTION;
153             MemberFilterAction hideStatic= new MemberFilterAction(this, title, FILTER_STATIC, helpContext, filterEnabled);
154             hideStatic.setDescription(ActionMessages.MemberFilterActionGroup_hide_static_description);
155             hideStatic.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_static_tooltip);
156             JavaPluginImages.setLocalImageDescriptors(hideStatic, "static_co.gif"); //$NON-NLS-1$
157
actions.add(hideStatic);
158         }
159         
160         // non-public
161
filterProperty= FILTER_NONPUBLIC;
162         if (isSet(filterProperty, availableFilters)) {
163             boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty));
164             if (filterEnabled) {
165                 fFilter.addFilter(filterProperty);
166             }
167             title= ActionMessages.MemberFilterActionGroup_hide_nonpublic_label;
168             helpContext= IJavaHelpContextIds.FILTER_PUBLIC_ACTION;
169             MemberFilterAction hideNonPublic= new MemberFilterAction(this, title, filterProperty, helpContext, filterEnabled);
170             hideNonPublic.setDescription(ActionMessages.MemberFilterActionGroup_hide_nonpublic_description);
171             hideNonPublic.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_nonpublic_tooltip);
172             JavaPluginImages.setLocalImageDescriptors(hideNonPublic, "public_co.gif"); //$NON-NLS-1$
173
actions.add(hideNonPublic);
174         }
175         
176         // local types
177
filterProperty= FILTER_LOCALTYPES;
178         if (isSet(filterProperty, availableFilters)) {
179             boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty));
180             if (filterEnabled) {
181                 fFilter.addFilter(filterProperty);
182             }
183             title= ActionMessages.MemberFilterActionGroup_hide_localtypes_label;
184             helpContext= IJavaHelpContextIds.FILTER_LOCALTYPES_ACTION;
185             MemberFilterAction hideLocalTypes= new MemberFilterAction(this, title, filterProperty, helpContext, filterEnabled);
186             hideLocalTypes.setDescription(ActionMessages.MemberFilterActionGroup_hide_localtypes_description);
187             hideLocalTypes.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_localtypes_tooltip);
188             JavaPluginImages.setLocalImageDescriptors(hideLocalTypes, "localtypes_co.gif"); //$NON-NLS-1$
189
actions.add(hideLocalTypes);
190         }
191         
192         // order corresponds to order in toolbar
193
fFilterActions= (MemberFilterAction[]) actions.toArray(new MemberFilterAction[actions.size()]);
194         
195         fViewer.addFilter(fFilter);
196     }
197     
198     private String JavaDoc getPreferenceKey(int filterProperty) {
199         return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
200
}
201     
202     /**
203      * Sets the member filters.
204      *
205      * @param filterProperty the filter to be manipulated. Valid values are <code>FILTER_FIELDS</code>,
206      * <code>FILTER_PUBLIC</code> <code>FILTER_PRIVATE</code> and <code>FILTER_LOCALTYPES_ACTION</code>
207      * as defined by this action group
208      * @param set if <code>true</code> the given filter is installed. If <code>false</code> the
209      * given filter is removed
210      * .
211      */

212     public void setMemberFilter(int filterProperty, boolean set) {
213         setMemberFilters(new int[] {filterProperty}, new boolean[] {set}, true);
214     }
215
216     private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues, boolean refresh) {
217         if (propertyKeys.length == 0)
218             return;
219         Assert.isTrue(propertyKeys.length == propertyValues.length);
220         
221         for (int i= 0; i < propertyKeys.length; i++) {
222             int filterProperty= propertyKeys[i];
223             boolean set= propertyValues[i];
224
225             IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
226             boolean found= false;
227             for (int j= 0; j < fFilterActions.length; j++) {
228                 int currProperty= fFilterActions[j].getFilterProperty();
229                 if (currProperty == filterProperty) {
230                     fFilterActions[j].setChecked(set);
231                     found= true;
232                     store.setValue(getPreferenceKey(filterProperty), set);
233                 }
234             }
235             if (found) {
236                 if (set) {
237                     fFilter.addFilter(filterProperty);
238                 } else {
239                     fFilter.removeFilter(filterProperty);
240                 }
241             }
242         }
243         if (refresh) {
244             fViewer.getControl().setRedraw(false);
245             BusyIndicator.showWhile(fViewer.getControl().getDisplay(), new Runnable JavaDoc() {
246                 public void run() {
247                     fViewer.refresh();
248                 }
249             });
250             fViewer.getControl().setRedraw(true);
251         }
252     }
253     
254     private boolean isSet(int flag, int set) {
255         return (flag & set) != 0;
256     }
257
258     /**
259      * Returns <code>true</code> if the given filter is installed.
260      *
261      * @param filterProperty the filter to be tested. Valid values are <code>FILTER_FIELDS</code>,
262      * <code>FILTER_PUBLIC</code>, <code>FILTER_PRIVATE</code> and <code>FILTER_LOCALTYPES</code> as defined by this action
263      * group
264      */

265     public boolean hasMemberFilter(int filterProperty) {
266         return fFilter.hasFilter(filterProperty);
267     }
268     
269     /**
270      * Saves the state of the filter actions in a memento.
271      *
272      * @param memento the memento to which the state is saved
273      */

274     public void saveState(IMemento memento) {
275         memento.putString(TAG_HIDEFIELDS, String.valueOf(hasMemberFilter(FILTER_FIELDS)));
276         memento.putString(TAG_HIDESTATIC, String.valueOf(hasMemberFilter(FILTER_STATIC)));
277         memento.putString(TAG_HIDENONPUBLIC, String.valueOf(hasMemberFilter(FILTER_NONPUBLIC)));
278         memento.putString(TAG_HIDELOCALTYPES, String.valueOf(hasMemberFilter(FILTER_LOCALTYPES)));
279     }
280     
281     /**
282      * Restores the state of the filter actions from a memento.
283      * <p>
284      * Note: This method does not refresh the viewer.
285      * </p>
286      * @param memento the memento from which the state is restored
287      */

288     public void restoreState(IMemento memento) {
289         setMemberFilters(
290             new int[] {FILTER_FIELDS, FILTER_STATIC, FILTER_NONPUBLIC, FILTER_LOCALTYPES},
291             new boolean[] {
292                 Boolean.valueOf(memento.getString(TAG_HIDEFIELDS)).booleanValue(),
293                 Boolean.valueOf(memento.getString(TAG_HIDESTATIC)).booleanValue(),
294                 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC)).booleanValue(),
295                 Boolean.valueOf(memento.getString(TAG_HIDELOCALTYPES)).booleanValue()
296             }, false);
297     }
298     
299     /* (non-Javadoc)
300      * @see ActionGroup#fillActionBars(IActionBars)
301      */

302     public void fillActionBars(IActionBars actionBars) {
303         contributeToToolBar(actionBars.getToolBarManager());
304     }
305     
306     /**
307      * Adds the filter actions to the given tool bar
308      *
309      * @param tbm the tool bar to which the actions are added
310      */

311     public void contributeToToolBar(IToolBarManager tbm) {
312         if (fInViewMenu)
313             return;
314         for (int i= 0; i < fFilterActions.length; i++) {
315             tbm.add(fFilterActions[i]);
316         }
317     }
318     
319     /**
320      * Adds the filter actions to the given menu manager.
321      *
322      * @param menu the menu manager to which the actions are added
323      * @since 2.1
324      */

325     public void contributeToViewMenu(IMenuManager menu) {
326         if (!fInViewMenu)
327             return;
328         final String JavaDoc filters= "filters"; //$NON-NLS-1$
329
if (menu.find(filters) != null) {
330             for (int i= 0; i < fFilterActions.length; i++) {
331                 menu.prependToGroup(filters, fFilterActions[i]);
332             }
333         } else {
334             for (int i= 0; i < fFilterActions.length; i++) {
335                 menu.add(fFilterActions[i]);
336             }
337         }
338     }
339     
340     /* (non-Javadoc)
341      * @see ActionGroup#dispose()
342      */

343     public void dispose() {
344         super.dispose();
345     }
346
347 }
348
Popular Tags