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