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 methods
32 * and fields. Contributed filters are: hide fields, hide static members and
33 * hide non-public members.
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.
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;
49 public static final int FILTER_STATIC = MemberFilter.FILTER_STATIC;
51 public static final int FILTER_FIELDS = MemberFilter.FILTER_FIELDS;
53 private static final String TAG_HIDEFIELDS = "hidefields"; //$NON-NLS-1$
55 private static final String TAG_HIDESTATIC = "hidestatic"; //$NON-NLS-1$
57 private static final String TAG_HIDENONPUBLIC = "hidenonpublic"; //$NON-NLS-1$
59 private MemberFilterAction[] fFilterActions;
61 private MemberFilter fFilter;
63 private StructuredViewer fViewer;
65 private String fViewerId;
67 private boolean fInViewMenu;
70 * Creates a new <code>MemberFilterActionGroup</code>.
73 * the viewer to be filtered
75 * a unique id of the viewer. Used as a key to to store the last
76 * used filter settings in the preference store
78 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
79 this(viewer, viewerId, false);
83 * Creates a new <code>MemberFilterActionGroup</code>.
86 * the viewer to be filtered
88 * a unique id of the viewer. Used as a key to to store the last
89 * used filter settings in the preference store
91 * if <code>true</code> the actions are added to the view menu.
92 * If <code>false</code> they are added to the toobar.
96 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId,
100 fInViewMenu = inViewMenu;
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));
112 fFilter = new MemberFilter();
114 fFilter.addFilter(FILTER_FIELDS);
116 fFilter.addFilter(FILTER_STATIC);
118 fFilter.addFilter(FILTER_NONPUBLIC);
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$
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$
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);
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$
157 // order corresponds to order in toolbar
158 fFilterActions = new MemberFilterAction[] { hideFields, hideStatic,
161 fViewer.addFilter(fFilter);
164 private String getPreferenceKey(int filterProperty) {
165 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
169 * Sets the member filters.
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
177 * if <code>true</code> the given filter is installed. If
178 * <code>false</code> the given filter is removed .
180 public void setMemberFilter(int filterProperty, boolean set) {
181 setMemberFilters(new int[] { filterProperty }, new boolean[] { set },
185 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues,
187 if (propertyKeys.length == 0)
189 Assert.isTrue(propertyKeys.length == propertyValues.length);
191 for (int i = 0; i < propertyKeys.length; i++) {
192 int filterProperty = propertyKeys[i];
193 boolean set = propertyValues[i];
195 fFilter.addFilter(filterProperty);
197 fFilter.removeFilter(filterProperty);
199 IPreferenceStore store = PHPeclipsePlugin.getDefault()
200 .getPreferenceStore();
202 for (int j = 0; j < fFilterActions.length; j++) {
203 int currProperty = fFilterActions[j].getFilterProperty();
204 if (currProperty == filterProperty) {
205 fFilterActions[j].setChecked(set);
207 store.setValue(getPreferenceKey(currProperty),
208 hasMemberFilter(currProperty));
212 fViewer.getControl().setRedraw(false);
213 BusyIndicator.showWhile(fViewer.getControl().getDisplay(),
219 fViewer.getControl().setRedraw(true);
224 * Returns <code>true</code> if the given filter is installed.
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
232 public boolean hasMemberFilter(int filterProperty) {
233 return fFilter.hasFilter(filterProperty);
237 * Saves the state of the filter actions in a memento.
240 * the memento to which the state is saved
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)));
252 * Restores the state of the filter actions from a memento.
254 * Note: This method does not refresh the viewer.
258 * the memento from which the state is restored
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))
265 Boolean.valueOf(memento.getString(TAG_HIDESTATIC))
267 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC))
268 .booleanValue() }, false);
274 * @see ActionGroup#fillActionBars(IActionBars)
276 public void fillActionBars(IActionBars actionBars) {
277 contributeToToolBar(actionBars.getToolBarManager());
281 * Adds the filter actions to the given tool bar
284 * the tool bar to which the actions are added
286 public void contributeToToolBar(IToolBarManager tbm) {
289 tbm.add(fFilterActions[0]); // fields
290 tbm.add(fFilterActions[1]); // static
291 tbm.add(fFilterActions[2]); // public
295 * Adds the filter actions to the given menu manager.
298 * the menu manager to which the actions are added
301 public void contributeToViewMenu(IMenuManager menu) {
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
310 menu.add(fFilterActions[0]); // fields
311 menu.add(fFilterActions[1]); // static
312 menu.add(fFilterActions[2]); // public
319 * @see ActionGroup#dispose()
321 public void dispose() {