873c76ea081022843068cd19dbecacef52bd088c
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / actions / MemberFilterActionGroup.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
12
13 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
14 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
15 import net.sourceforge.phpdt.internal.ui.actions.ActionMessages;
16 import net.sourceforge.phpdt.internal.ui.viewsupport.MemberFilter;
17 import net.sourceforge.phpdt.internal.ui.viewsupport.MemberFilterAction;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.IToolBarManager;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.util.Assert;
24 import org.eclipse.jface.viewers.StructuredViewer;
25 import org.eclipse.swt.custom.BusyIndicator;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.actions.ActionGroup;
29
30 /**
31  * Action Group that contributes filter buttons for a view parts showing methods
32  * and fields. Contributed filters are: hide fields, hide static members and
33  * hide non-public members.
34  * <p>
35  * The action group installs a filter on a structured viewer. The filter is
36  * connected to the actions installed in the view part's toolbar menu and is
37  * updated when the state of the buttons changes.
38  * 
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  * 
43  * @since 2.0
44  */
45 public class MemberFilterActionGroup extends ActionGroup {
46
47         public static final int FILTER_NONPUBLIC = MemberFilter.FILTER_NONPUBLIC;
48
49         public static final int FILTER_STATIC = MemberFilter.FILTER_STATIC;
50
51         public static final int FILTER_FIELDS = MemberFilter.FILTER_FIELDS;
52
53         private static final String TAG_HIDEFIELDS = "hidefields"; //$NON-NLS-1$
54
55         private static final String TAG_HIDESTATIC = "hidestatic"; //$NON-NLS-1$
56
57         private static final String TAG_HIDENONPUBLIC = "hidenonpublic"; //$NON-NLS-1$
58
59         private MemberFilterAction[] fFilterActions;
60
61         private MemberFilter fFilter;
62
63         private StructuredViewer fViewer;
64
65         private String fViewerId;
66
67         private boolean fInViewMenu;
68
69         /**
70          * Creates a new <code>MemberFilterActionGroup</code>.
71          * 
72          * @param viewer
73          *            the viewer to be filtered
74          * @param viewerId
75          *            a unique id of the viewer. Used as a key to to store the last
76          *            used filter settings in the preference store
77          */
78         public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
79                 this(viewer, viewerId, false);
80         }
81
82         /**
83          * Creates a new <code>MemberFilterActionGroup</code>.
84          * 
85          * @param viewer
86          *            the viewer to be filtered
87          * @param viewerId
88          *            a unique id of the viewer. Used as a key to to store the last
89          *            used filter settings in the preference store
90          * @param inViewMenu
91          *            if <code>true</code> the actions are added to the view menu.
92          *            If <code>false</code> they are added to the toobar.
93          * 
94          * @since 2.1
95          */
96         public MemberFilterActionGroup(StructuredViewer viewer, String viewerId,
97                         boolean inViewMenu) {
98                 fViewer = viewer;
99                 fViewerId = viewerId;
100                 fInViewMenu = inViewMenu;
101
102                 // get initial values
103                 IPreferenceStore store = PHPeclipsePlugin.getDefault()
104                                 .getPreferenceStore();
105                 boolean doHideFields = store
106                                 .getBoolean(getPreferenceKey(FILTER_FIELDS));
107                 boolean doHideStatic = store
108                                 .getBoolean(getPreferenceKey(FILTER_STATIC));
109                 boolean doHidePublic = store
110                                 .getBoolean(getPreferenceKey(FILTER_NONPUBLIC));
111
112                 fFilter = new MemberFilter();
113                 if (doHideFields)
114                         fFilter.addFilter(FILTER_FIELDS);
115                 if (doHideStatic)
116                         fFilter.addFilter(FILTER_STATIC);
117                 if (doHidePublic)
118                         fFilter.addFilter(FILTER_NONPUBLIC);
119
120                 // fields
121                 String title = ActionMessages
122                                 .getString("MemberFilterActionGroup.hide_fields.label"); //$NON-NLS-1$
123                 String helpContext = IJavaHelpContextIds.FILTER_FIELDS_ACTION;
124                 MemberFilterAction hideFields = new MemberFilterAction(this, title,
125                                 FILTER_FIELDS, helpContext, doHideFields);
126                 hideFields.setDescription(ActionMessages
127                                 .getString("MemberFilterActionGroup.hide_fields.description")); //$NON-NLS-1$
128                 hideFields.setToolTipText(ActionMessages
129                                 .getString("MemberFilterActionGroup.hide_fields.tooltip")); //$NON-NLS-1$
130                 PHPUiImages.setLocalImageDescriptors(hideFields, "fields_co.gif"); //$NON-NLS-1$
131
132                 // static
133                 title = ActionMessages
134                                 .getString("MemberFilterActionGroup.hide_static.label"); //$NON-NLS-1$
135                 helpContext = IJavaHelpContextIds.FILTER_STATIC_ACTION;
136                 MemberFilterAction hideStatic = new MemberFilterAction(this, title,
137                                 FILTER_STATIC, helpContext, doHideStatic);
138                 hideStatic.setDescription(ActionMessages
139                                 .getString("MemberFilterActionGroup.hide_static.description")); //$NON-NLS-1$
140                 hideStatic.setToolTipText(ActionMessages
141                                 .getString("MemberFilterActionGroup.hide_static.tooltip")); //$NON-NLS-1$
142                 PHPUiImages.setLocalImageDescriptors(hideStatic, "static_co.gif"); //$NON-NLS-1$
143
144                 // non-public
145                 title = ActionMessages
146                                 .getString("MemberFilterActionGroup.hide_nonpublic.label"); //$NON-NLS-1$
147                 helpContext = IJavaHelpContextIds.FILTER_PUBLIC_ACTION;
148                 MemberFilterAction hideNonPublic = new MemberFilterAction(this, title,
149                                 FILTER_NONPUBLIC, helpContext, doHidePublic);
150                 hideNonPublic
151                                 .setDescription(ActionMessages
152                                                 .getString("MemberFilterActionGroup.hide_nonpublic.description")); //$NON-NLS-1$
153                 hideNonPublic.setToolTipText(ActionMessages
154                                 .getString("MemberFilterActionGroup.hide_nonpublic.tooltip")); //$NON-NLS-1$
155                 PHPUiImages.setLocalImageDescriptors(hideNonPublic, "public_co.gif"); //$NON-NLS-1$
156
157                 // order corresponds to order in toolbar
158                 fFilterActions = new MemberFilterAction[] { hideFields, hideStatic,
159                                 hideNonPublic };
160
161                 fViewer.addFilter(fFilter);
162         }
163
164         private String getPreferenceKey(int filterProperty) {
165                 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
166         }
167
168         /**
169          * Sets the member filters.
170          * 
171          * @param filterProperty
172          *            the filter to be manipulated. Valid values are
173          *            <code>FILTER_FIELDS</code>, <code>FILTER_PUBLIC</code>,
174          *            and <code>FILTER_PRIVATE</code> as defined by this action
175          *            group
176          * @param set
177          *            if <code>true</code> the given filter is installed. If
178          *            <code>false</code> the given filter is removed .
179          */
180         public void setMemberFilter(int filterProperty, boolean set) {
181                 setMemberFilters(new int[] { filterProperty }, new boolean[] { set },
182                                 true);
183         }
184
185         private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues,
186                         boolean refresh) {
187                 if (propertyKeys.length == 0)
188                         return;
189                 Assert.isTrue(propertyKeys.length == propertyValues.length);
190
191                 for (int i = 0; i < propertyKeys.length; i++) {
192                         int filterProperty = propertyKeys[i];
193                         boolean set = propertyValues[i];
194                         if (set) {
195                                 fFilter.addFilter(filterProperty);
196                         } else {
197                                 fFilter.removeFilter(filterProperty);
198                         }
199                         IPreferenceStore store = PHPeclipsePlugin.getDefault()
200                                         .getPreferenceStore();
201
202                         for (int j = 0; j < fFilterActions.length; j++) {
203                                 int currProperty = fFilterActions[j].getFilterProperty();
204                                 if (currProperty == filterProperty) {
205                                         fFilterActions[j].setChecked(set);
206                                 }
207                                 store.setValue(getPreferenceKey(currProperty),
208                                                 hasMemberFilter(currProperty));
209                         }
210                 }
211                 if (refresh) {
212                         fViewer.getControl().setRedraw(false);
213                         BusyIndicator.showWhile(fViewer.getControl().getDisplay(),
214                                         new Runnable() {
215                                                 public void run() {
216                                                         fViewer.refresh();
217                                                 }
218                                         });
219                         fViewer.getControl().setRedraw(true);
220                 }
221         }
222
223         /**
224          * Returns <code>true</code> if the given filter is installed.
225          * 
226          * @param filterProperty
227          *            the filter to be tested. Valid values are
228          *            <code>FILTER_FIELDS</code>, <code>FILTER_PUBLIC</code>,
229          *            and <code>FILTER_PRIVATE</code> as defined by this action
230          *            group
231          */
232         public boolean hasMemberFilter(int filterProperty) {
233                 return fFilter.hasFilter(filterProperty);
234         }
235
236         /**
237          * Saves the state of the filter actions in a memento.
238          * 
239          * @param memento
240          *            the memento to which the state is saved
241          */
242         public void saveState(IMemento memento) {
243                 memento.putString(TAG_HIDEFIELDS, String
244                                 .valueOf(hasMemberFilter(FILTER_FIELDS)));
245                 memento.putString(TAG_HIDESTATIC, String
246                                 .valueOf(hasMemberFilter(FILTER_STATIC)));
247                 memento.putString(TAG_HIDENONPUBLIC, String
248                                 .valueOf(hasMemberFilter(FILTER_NONPUBLIC)));
249         }
250
251         /**
252          * Restores the state of the filter actions from a memento.
253          * <p>
254          * Note: This method does not refresh the viewer.
255          * </p>
256          * 
257          * @param memento
258          *            the memento from which the state is restored
259          */
260         public void restoreState(IMemento memento) {
261                 setMemberFilters(new int[] { FILTER_FIELDS, FILTER_STATIC,
262                                 FILTER_NONPUBLIC }, new boolean[] {
263                                 Boolean.valueOf(memento.getString(TAG_HIDEFIELDS))
264                                                 .booleanValue(),
265                                 Boolean.valueOf(memento.getString(TAG_HIDESTATIC))
266                                                 .booleanValue(),
267                                 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC))
268                                                 .booleanValue() }, false);
269         }
270
271         /*
272          * (non-Javadoc)
273          * 
274          * @see ActionGroup#fillActionBars(IActionBars)
275          */
276         public void fillActionBars(IActionBars actionBars) {
277                 contributeToToolBar(actionBars.getToolBarManager());
278         };
279
280         /**
281          * Adds the filter actions to the given tool bar
282          * 
283          * @param tbm
284          *            the tool bar to which the actions are added
285          */
286         public void contributeToToolBar(IToolBarManager tbm) {
287                 if (fInViewMenu)
288                         return;
289                 tbm.add(fFilterActions[0]); // fields
290                 tbm.add(fFilterActions[1]); // static
291                 tbm.add(fFilterActions[2]); // public
292         }
293
294         /**
295          * Adds the filter actions to the given menu manager.
296          * 
297          * @param menu
298          *            the menu manager to which the actions are added
299          * @since 2.1
300          */
301         public void contributeToViewMenu(IMenuManager menu) {
302                 if (!fInViewMenu)
303                         return;
304                 final String filters = "filters"; //$NON-NLS-1$
305                 if (menu.find(filters) != null) {
306                         menu.prependToGroup(filters, fFilterActions[0]); // fields
307                         menu.prependToGroup(filters, fFilterActions[1]); // static
308                         menu.prependToGroup(filters, fFilterActions[2]); // public
309                 } else {
310                         menu.add(fFilterActions[0]); // fields
311                         menu.add(fFilterActions[1]); // static
312                         menu.add(fFilterActions[2]); // public
313                 }
314         }
315
316         /*
317          * (non-Javadoc)
318          * 
319          * @see ActionGroup#dispose()
320          */
321         public void dispose() {
322                 super.dispose();
323         }
324
325 }