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;
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;
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.
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.
42 * This class may be instantiated; it is not intended to be subclassed.
47 public class MemberFilterActionGroup extends ActionGroup {
49 public static final int FILTER_NONPUBLIC = MemberFilter.FILTER_NONPUBLIC;
51 public static final int FILTER_STATIC = MemberFilter.FILTER_STATIC;
53 public static final int FILTER_FIELDS = MemberFilter.FILTER_FIELDS;
55 private static final String TAG_HIDEFIELDS = "hidefields"; //$NON-NLS-1$
57 private static final String TAG_HIDESTATIC = "hidestatic"; //$NON-NLS-1$
59 private static final String TAG_HIDENONPUBLIC = "hidenonpublic"; //$NON-NLS-1$
61 private MemberFilterAction[] fFilterActions;
63 private MemberFilter fFilter;
65 private StructuredViewer fViewer;
67 private String fViewerId;
69 private boolean fInViewMenu;
72 * Creates a new <code>MemberFilterActionGroup</code>.
75 * the viewer to be filtered
77 * a unique id of the viewer. Used as a key to to store the last
78 * used filter settings in the preference store
80 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
81 this(viewer, viewerId, false);
85 * Creates a new <code>MemberFilterActionGroup</code>.
88 * the viewer to be filtered
90 * a unique id of the viewer. Used as a key to to store the last
91 * used filter settings in the preference store
93 * if <code>true</code> the actions are added to the view menu.
94 * If <code>false</code> they are added to the toobar.
98 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId,
101 fViewerId = viewerId;
102 fInViewMenu = inViewMenu;
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));
114 fFilter = new MemberFilter();
116 fFilter.addFilter(FILTER_FIELDS);
118 fFilter.addFilter(FILTER_STATIC);
120 fFilter.addFilter(FILTER_NONPUBLIC);
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$
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$
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);
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$
159 // order corresponds to order in toolbar
160 fFilterActions = new MemberFilterAction[] { hideFields, hideStatic,
163 fViewer.addFilter(fFilter);
166 private String getPreferenceKey(int filterProperty) {
167 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
171 * Sets the member filters.
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
179 * if <code>true</code> the given filter is installed. If
180 * <code>false</code> the given filter is removed .
182 public void setMemberFilter(int filterProperty, boolean set) {
183 setMemberFilters(new int[] { filterProperty }, new boolean[] { set },
187 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues,
189 if (propertyKeys.length == 0)
191 Assert.isTrue(propertyKeys.length == propertyValues.length);
193 for (int i = 0; i < propertyKeys.length; i++) {
194 int filterProperty = propertyKeys[i];
195 boolean set = propertyValues[i];
197 fFilter.addFilter(filterProperty);
199 fFilter.removeFilter(filterProperty);
201 IPreferenceStore store = PHPeclipsePlugin.getDefault()
202 .getPreferenceStore();
204 for (int j = 0; j < fFilterActions.length; j++) {
205 int currProperty = fFilterActions[j].getFilterProperty();
206 if (currProperty == filterProperty) {
207 fFilterActions[j].setChecked(set);
209 store.setValue(getPreferenceKey(currProperty),
210 hasMemberFilter(currProperty));
214 fViewer.getControl().setRedraw(false);
215 BusyIndicator.showWhile(fViewer.getControl().getDisplay(),
221 fViewer.getControl().setRedraw(true);
226 * Returns <code>true</code> if the given filter is installed.
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
234 public boolean hasMemberFilter(int filterProperty) {
235 return fFilter.hasFilter(filterProperty);
239 * Saves the state of the filter actions in a memento.
242 * the memento to which the state is saved
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)));
254 * Restores the state of the filter actions from a memento.
256 * Note: This method does not refresh the viewer.
260 * the memento from which the state is restored
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))
267 Boolean.valueOf(memento.getString(TAG_HIDESTATIC))
269 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC))
270 .booleanValue() }, false);
276 * @see ActionGroup#fillActionBars(IActionBars)
278 public void fillActionBars(IActionBars actionBars) {
279 contributeToToolBar(actionBars.getToolBarManager());
283 * Adds the filter actions to the given tool bar
286 * the tool bar to which the actions are added
288 public void contributeToToolBar(IToolBarManager tbm) {
291 tbm.add(fFilterActions[0]); // fields
292 tbm.add(fFilterActions[1]); // static
293 tbm.add(fFilterActions[2]); // public
297 * Adds the filter actions to the given menu manager.
300 * the menu manager to which the actions are added
303 // public void contributeToViewMenu(IMenuManager menu) {
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
312 // menu.add(fFilterActions[0]); // fields
313 // menu.add(fFilterActions[1]); // static
314 // menu.add(fFilterActions[2]); // public
321 * @see ActionGroup#dispose()
323 public void dispose() {