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