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