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;
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;
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.
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.
41 * This class may be instantiated; it is not intended to be subclassed.
46 public class MemberFilterActionGroup extends ActionGroup {
48 public static final int FILTER_NONPUBLIC = MemberFilter.FILTER_NONPUBLIC;
50 public static final int FILTER_STATIC = MemberFilter.FILTER_STATIC;
52 public static final int FILTER_FIELDS = MemberFilter.FILTER_FIELDS;
54 private static final String TAG_HIDEFIELDS = "hidefields"; //$NON-NLS-1$
56 private static final String TAG_HIDESTATIC = "hidestatic"; //$NON-NLS-1$
58 private static final String TAG_HIDENONPUBLIC = "hidenonpublic"; //$NON-NLS-1$
60 private MemberFilterAction[] fFilterActions;
62 private MemberFilter fFilter;
64 private StructuredViewer fViewer;
66 private String fViewerId;
68 private boolean fInViewMenu;
71 * Creates a new <code>MemberFilterActionGroup</code>.
74 * the viewer to be filtered
76 * a unique id of the viewer. Used as a key to to store the last
77 * used filter settings in the preference store
79 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
80 this(viewer, viewerId, false);
84 * Creates a new <code>MemberFilterActionGroup</code>.
87 * the viewer to be filtered
89 * a unique id of the viewer. Used as a key to to store the last
90 * used filter settings in the preference store
92 * if <code>true</code> the actions are added to the view menu.
93 * If <code>false</code> they are added to the toobar.
97 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId,
100 fViewerId = viewerId;
101 fInViewMenu = inViewMenu;
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));
113 fFilter = new MemberFilter();
115 fFilter.addFilter(FILTER_FIELDS);
117 fFilter.addFilter(FILTER_STATIC);
119 fFilter.addFilter(FILTER_NONPUBLIC);
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$
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$
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);
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$
158 // order corresponds to order in toolbar
159 fFilterActions = new MemberFilterAction[] { hideFields, hideStatic,
162 fViewer.addFilter(fFilter);
165 private String getPreferenceKey(int filterProperty) {
166 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
170 * Sets the member filters.
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
178 * if <code>true</code> the given filter is installed. If
179 * <code>false</code> the given filter is removed .
181 public void setMemberFilter(int filterProperty, boolean set) {
182 setMemberFilters(new int[] { filterProperty }, new boolean[] { set },
186 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues,
188 if (propertyKeys.length == 0)
190 Assert.isTrue(propertyKeys.length == propertyValues.length);
192 for (int i = 0; i < propertyKeys.length; i++) {
193 int filterProperty = propertyKeys[i];
194 boolean set = propertyValues[i];
196 fFilter.addFilter(filterProperty);
198 fFilter.removeFilter(filterProperty);
200 IPreferenceStore store = WebUI.getDefault()
201 .getPreferenceStore();
203 for (int j = 0; j < fFilterActions.length; j++) {
204 int currProperty = fFilterActions[j].getFilterProperty();
205 if (currProperty == filterProperty) {
206 fFilterActions[j].setChecked(set);
208 store.setValue(getPreferenceKey(currProperty),
209 hasMemberFilter(currProperty));
213 fViewer.getControl().setRedraw(false);
214 BusyIndicator.showWhile(fViewer.getControl().getDisplay(),
220 fViewer.getControl().setRedraw(true);
225 * Returns <code>true</code> if the given filter is installed.
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
233 public boolean hasMemberFilter(int filterProperty) {
234 return fFilter.hasFilter(filterProperty);
238 * Saves the state of the filter actions in a memento.
241 * the memento to which the state is saved
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)));
253 * Restores the state of the filter actions from a memento.
255 * Note: This method does not refresh the viewer.
259 * the memento from which the state is restored
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))
266 Boolean.valueOf(memento.getString(TAG_HIDESTATIC))
268 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC))
269 .booleanValue() }, false);
275 * @see ActionGroup#fillActionBars(IActionBars)
277 public void fillActionBars(IActionBars actionBars) {
278 contributeToToolBar(actionBars.getToolBarManager());
282 * Adds the filter actions to the given tool bar
285 * the tool bar to which the actions are added
287 public void contributeToToolBar(IToolBarManager tbm) {
290 tbm.add(fFilterActions[0]); // fields
291 tbm.add(fFilterActions[1]); // static
292 tbm.add(fFilterActions[2]); // public
296 * Adds the filter actions to the given menu manager.
299 * the menu manager to which the actions are added
302 public void contributeToViewMenu(IMenuManager menu) {
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
311 menu.add(fFilterActions[0]); // fields
312 menu.add(fFilterActions[1]); // static
313 menu.add(fFilterActions[2]); // public
320 * @see ActionGroup#dispose()
322 public void dispose() {