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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
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;
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;
31 * Action Group that contributes filter buttons for a view parts showing
32 * methods and fields. Contributed filters are: hide fields, hide static
33 * members and hide non-public members.
35 * The action group installs a filter on a structured viewer. The filter is connected
36 * to the actions installed in the view part's toolbar menu and is updated when the
37 * state of the buttons changes.
40 * This class may be instantiated; it is not intended to be subclassed.
45 public class MemberFilterActionGroup extends ActionGroup {
47 public static final int FILTER_NONPUBLIC= MemberFilter.FILTER_NONPUBLIC;
48 public static final int FILTER_STATIC= MemberFilter.FILTER_STATIC;
49 public static final int FILTER_FIELDS= MemberFilter.FILTER_FIELDS;
51 private static final String TAG_HIDEFIELDS= "hidefields"; //$NON-NLS-1$
52 private static final String TAG_HIDESTATIC= "hidestatic"; //$NON-NLS-1$
53 private static final String TAG_HIDENONPUBLIC= "hidenonpublic"; //$NON-NLS-1$
55 private MemberFilterAction[] fFilterActions;
56 private MemberFilter fFilter;
58 private StructuredViewer fViewer;
59 private String fViewerId;
60 private boolean fInViewMenu;
64 * Creates a new <code>MemberFilterActionGroup</code>.
66 * @param viewer the viewer to be filtered
67 * @param viewerId a unique id of the viewer. Used as a key to to store
68 * the last used filter settings in the preference store
70 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
71 this(viewer, viewerId, false);
75 * Creates a new <code>MemberFilterActionGroup</code>.
77 * @param viewer the viewer to be filtered
78 * @param viewerId a unique id of the viewer. Used as a key to to store
79 * the last used filter settings in the preference store
80 * @param inViewMenu if <code>true</code> the actions are added to the view
81 * menu. If <code>false</code> they are added to the toobar.
85 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId, boolean inViewMenu) {
88 fInViewMenu= inViewMenu;
91 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
92 boolean doHideFields= store.getBoolean(getPreferenceKey(FILTER_FIELDS));
93 boolean doHideStatic= store.getBoolean(getPreferenceKey(FILTER_STATIC));
94 boolean doHidePublic= store.getBoolean(getPreferenceKey(FILTER_NONPUBLIC));
96 fFilter= new MemberFilter();
98 fFilter.addFilter(FILTER_FIELDS);
100 fFilter.addFilter(FILTER_STATIC);
102 fFilter.addFilter(FILTER_NONPUBLIC);
105 String title= ActionMessages.getString("MemberFilterActionGroup.hide_fields.label"); //$NON-NLS-1$
106 String helpContext= IJavaHelpContextIds.FILTER_FIELDS_ACTION;
107 MemberFilterAction hideFields= new MemberFilterAction(this, title, FILTER_FIELDS, helpContext, doHideFields);
108 hideFields.setDescription(ActionMessages.getString("MemberFilterActionGroup.hide_fields.description")); //$NON-NLS-1$
109 hideFields.setToolTipText(ActionMessages.getString("MemberFilterActionGroup.hide_fields.tooltip")); //$NON-NLS-1$
110 PHPUiImages.setLocalImageDescriptors(hideFields, "fields_co.gif"); //$NON-NLS-1$
113 title= ActionMessages.getString("MemberFilterActionGroup.hide_static.label"); //$NON-NLS-1$
114 helpContext= IJavaHelpContextIds.FILTER_STATIC_ACTION;
115 MemberFilterAction hideStatic= new MemberFilterAction(this, title, FILTER_STATIC, helpContext, doHideStatic);
116 hideStatic.setDescription(ActionMessages.getString("MemberFilterActionGroup.hide_static.description")); //$NON-NLS-1$
117 hideStatic.setToolTipText(ActionMessages.getString("MemberFilterActionGroup.hide_static.tooltip")); //$NON-NLS-1$
118 PHPUiImages.setLocalImageDescriptors(hideStatic, "static_co.gif"); //$NON-NLS-1$
121 title= ActionMessages.getString("MemberFilterActionGroup.hide_nonpublic.label"); //$NON-NLS-1$
122 helpContext= IJavaHelpContextIds.FILTER_PUBLIC_ACTION;
123 MemberFilterAction hideNonPublic= new MemberFilterAction(this, title, FILTER_NONPUBLIC, helpContext, doHidePublic);
124 hideNonPublic.setDescription(ActionMessages.getString("MemberFilterActionGroup.hide_nonpublic.description")); //$NON-NLS-1$
125 hideNonPublic.setToolTipText(ActionMessages.getString("MemberFilterActionGroup.hide_nonpublic.tooltip")); //$NON-NLS-1$
126 PHPUiImages.setLocalImageDescriptors(hideNonPublic, "public_co.gif"); //$NON-NLS-1$
128 // order corresponds to order in toolbar
129 fFilterActions= new MemberFilterAction[] { hideFields, hideStatic, hideNonPublic };
131 fViewer.addFilter(fFilter);
134 private String getPreferenceKey(int filterProperty) {
135 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
139 * Sets the member filters.
141 * @param filterProperty the filter to be manipulated. Valid values are <code>FILTER_FIELDS</code>,
142 * <code>FILTER_PUBLIC</code>, and <code>FILTER_PRIVATE</code> as defined by this action
144 * @param set if <code>true</code> the given filter is installed. If <code>false</code> the
145 * given filter is removed
148 public void setMemberFilter(int filterProperty, boolean set) {
149 setMemberFilters(new int[] {filterProperty}, new boolean[] {set}, true);
152 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues, boolean refresh) {
153 if (propertyKeys.length == 0)
155 Assert.isTrue(propertyKeys.length == propertyValues.length);
157 for (int i= 0; i < propertyKeys.length; i++) {
158 int filterProperty= propertyKeys[i];
159 boolean set= propertyValues[i];
161 fFilter.addFilter(filterProperty);
163 fFilter.removeFilter(filterProperty);
165 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
167 for (int j= 0; j < fFilterActions.length; j++) {
168 int currProperty= fFilterActions[j].getFilterProperty();
169 if (currProperty == filterProperty) {
170 fFilterActions[j].setChecked(set);
172 store.setValue(getPreferenceKey(currProperty), hasMemberFilter(currProperty));
176 fViewer.getControl().setRedraw(false);
177 BusyIndicator.showWhile(fViewer.getControl().getDisplay(), new Runnable() {
182 fViewer.getControl().setRedraw(true);
187 * Returns <code>true</code> if the given filter is installed.
189 * @param filterProperty the filter to be tested. Valid values are <code>FILTER_FIELDS</code>,
190 * <code>FILTER_PUBLIC</code>, and <code>FILTER_PRIVATE</code> as defined by this action
193 public boolean hasMemberFilter(int filterProperty) {
194 return fFilter.hasFilter(filterProperty);
198 * Saves the state of the filter actions in a memento.
200 * @param memento the memento to which the state is saved
202 public void saveState(IMemento memento) {
203 memento.putString(TAG_HIDEFIELDS, String.valueOf(hasMemberFilter(FILTER_FIELDS)));
204 memento.putString(TAG_HIDESTATIC, String.valueOf(hasMemberFilter(FILTER_STATIC)));
205 memento.putString(TAG_HIDENONPUBLIC, String.valueOf(hasMemberFilter(FILTER_NONPUBLIC)));
209 * Restores the state of the filter actions from a memento.
211 * Note: This method does not refresh the viewer.
213 * @param memento the memento from which the state is restored
215 public void restoreState(IMemento memento) {
217 new int[] {FILTER_FIELDS, FILTER_STATIC, FILTER_NONPUBLIC},
219 Boolean.valueOf(memento.getString(TAG_HIDEFIELDS)).booleanValue(),
220 Boolean.valueOf(memento.getString(TAG_HIDESTATIC)).booleanValue(),
221 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC)).booleanValue()
226 * @see ActionGroup#fillActionBars(IActionBars)
228 public void fillActionBars(IActionBars actionBars) {
229 contributeToToolBar(actionBars.getToolBarManager());
233 * Adds the filter actions to the given tool bar
235 * @param tbm the tool bar to which the actions are added
237 public void contributeToToolBar(IToolBarManager tbm) {
240 tbm.add(fFilterActions[0]); // fields
241 tbm.add(fFilterActions[1]); // static
242 tbm.add(fFilterActions[2]); // public
246 * Adds the filter actions to the given menu manager.
248 * @param menu the menu manager to which the actions are added
251 public void contributeToViewMenu(IMenuManager menu) {
254 final String filters= "filters"; //$NON-NLS-1$
255 if (menu.find(filters) != null) {
256 menu.prependToGroup(filters, fFilterActions[0]); // fields
257 menu.prependToGroup(filters, fFilterActions[1]); // static
258 menu.prependToGroup(filters, fFilterActions[2]); // public
260 menu.add(fFilterActions[0]); // fields
261 menu.add(fFilterActions[1]); // static
262 menu.add(fFilterActions[2]); // public
267 * @see ActionGroup#dispose()
269 public void dispose() {