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;
19 import net.sourceforge.phpeclipse.ui.WebUI;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.IToolBarManager;
23 import org.eclipse.jface.preference.IPreferenceStore;
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;
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.
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.
43 * This class may be instantiated; it is not intended to be subclassed.
48 public class MemberFilterActionGroup extends ActionGroup {
50 public static final int FILTER_NONPUBLIC = MemberFilter.FILTER_NONPUBLIC;
52 public static final int FILTER_STATIC = MemberFilter.FILTER_STATIC;
54 public static final int FILTER_FIELDS = MemberFilter.FILTER_FIELDS;
56 private static final String TAG_HIDEFIELDS = "hidefields"; //$NON-NLS-1$
58 private static final String TAG_HIDESTATIC = "hidestatic"; //$NON-NLS-1$
60 private static final String TAG_HIDENONPUBLIC = "hidenonpublic"; //$NON-NLS-1$
62 private MemberFilterAction[] fFilterActions;
64 private MemberFilter fFilter;
66 private StructuredViewer fViewer;
68 private String fViewerId;
70 private boolean fInViewMenu;
73 * Creates a new <code>MemberFilterActionGroup</code>.
76 * the viewer to be filtered
78 * a unique id of the viewer. Used as a key to to store the last
79 * used filter settings in the preference store
81 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
82 this(viewer, viewerId, false);
86 * Creates a new <code>MemberFilterActionGroup</code>.
89 * the viewer to be filtered
91 * a unique id of the viewer. Used as a key to to store the last
92 * used filter settings in the preference store
94 * if <code>true</code> the actions are added to the view menu.
95 * If <code>false</code> they are added to the toobar.
99 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId,
100 boolean inViewMenu) {
102 fViewerId = viewerId;
103 fInViewMenu = inViewMenu;
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));
115 fFilter = new MemberFilter();
117 fFilter.addFilter(FILTER_FIELDS);
119 fFilter.addFilter(FILTER_STATIC);
121 fFilter.addFilter(FILTER_NONPUBLIC);
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$
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$
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);
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$
160 // order corresponds to order in toolbar
161 fFilterActions = new MemberFilterAction[] { hideFields, hideStatic,
164 fViewer.addFilter(fFilter);
167 private String getPreferenceKey(int filterProperty) {
168 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
172 * Sets the member filters.
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
180 * if <code>true</code> the given filter is installed. If
181 * <code>false</code> the given filter is removed .
183 public void setMemberFilter(int filterProperty, boolean set) {
184 setMemberFilters(new int[] { filterProperty }, new boolean[] { set },
188 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues,
190 if (propertyKeys.length == 0)
192 Assert.isTrue(propertyKeys.length == propertyValues.length);
194 for (int i = 0; i < propertyKeys.length; i++) {
195 int filterProperty = propertyKeys[i];
196 boolean set = propertyValues[i];
198 fFilter.addFilter(filterProperty);
200 fFilter.removeFilter(filterProperty);
202 IPreferenceStore store = WebUI.getDefault()
203 .getPreferenceStore();
205 for (int j = 0; j < fFilterActions.length; j++) {
206 int currProperty = fFilterActions[j].getFilterProperty();
207 if (currProperty == filterProperty) {
208 fFilterActions[j].setChecked(set);
210 store.setValue(getPreferenceKey(currProperty),
211 hasMemberFilter(currProperty));
215 fViewer.getControl().setRedraw(false);
216 BusyIndicator.showWhile(fViewer.getControl().getDisplay(),
222 fViewer.getControl().setRedraw(true);
227 * Returns <code>true</code> if the given filter is installed.
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
235 public boolean hasMemberFilter(int filterProperty) {
236 return fFilter.hasFilter(filterProperty);
240 * Saves the state of the filter actions in a memento.
243 * the memento to which the state is saved
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)));
255 * Restores the state of the filter actions from a memento.
257 * Note: This method does not refresh the viewer.
261 * the memento from which the state is restored
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))
268 Boolean.valueOf(memento.getString(TAG_HIDESTATIC))
270 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC))
271 .booleanValue() }, false);
277 * @see ActionGroup#fillActionBars(IActionBars)
279 public void fillActionBars(IActionBars actionBars) {
280 contributeToToolBar(actionBars.getToolBarManager());
284 * Adds the filter actions to the given tool bar
287 * the tool bar to which the actions are added
289 public void contributeToToolBar(IToolBarManager tbm) {
292 tbm.add(fFilterActions[0]); // fields
293 tbm.add(fFilterActions[1]); // static
294 tbm.add(fFilterActions[2]); // public
298 * Adds the filter actions to the given menu manager.
301 * the menu manager to which the actions are added
304 public void contributeToViewMenu(IMenuManager menu) {
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
313 menu.add(fFilterActions[0]); // fields
314 menu.add(fFilterActions[1]); // static
315 menu.add(fFilterActions[2]); // public
322 * @see ActionGroup#dispose()
324 public void dispose() {