]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/MemberFilterActionGroup.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / MemberFilterActionGroup.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.ui.actions;
12
13import org.eclipse.swt.custom.BusyIndicator;
14
15import org.eclipse.core.runtime.Assert;
16
17import org.eclipse.jface.action.IMenuManager;
18import org.eclipse.jface.action.IToolBarManager;
19import org.eclipse.jface.preference.IPreferenceStore;
20import org.eclipse.jface.viewers.StructuredViewer;
21
22import org.eclipse.ui.IActionBars;
23import org.eclipse.ui.IMemento;
24import org.eclipse.ui.actions.ActionGroup;
25
26import org.eclipse.jdt.ui.PreferenceConstants;
27
28import org.eclipse.jdt.internal.ui.JavaPlugin;
29import org.eclipse.jdt.internal.ui.viewsupport.MemberFilter;
30import org.eclipse.jdt.internal.ui.viewsupport.MemberFilterAction;
31
32/**
33 * Action Group that contributes filter buttons for a view parts showing
34 * methods and fields. Contributed filters are: hide fields, hide static
35 * members hide non-public members and hide local types.
36 * <p>
37 * The action group installs a filter on a structured viewer. The filter is connected
38 * to the actions installed in the view part's toolbar menu and is updated when the
39 * state of the buttons changes.
40 *
41 * <p>
42 * This class may be instantiated; it is not intended to be subclassed.
43 * </p>
44 *
45 * @since 2.0
46 *
47 * @noextend This class is not intended to be subclassed by clients.
48 */
49public class MemberFilterActionGroup extends ActionGroup {
50
51 public static final int FILTER_NONPUBLIC= MemberFilter.FILTER_NONPUBLIC;
52 public static final int FILTER_STATIC= MemberFilter.FILTER_STATIC;
53 public static final int FILTER_FIELDS= MemberFilter.FILTER_FIELDS;
54
55 /** @since 3.0 */
56 public static final int FILTER_LOCALTYPES= MemberFilter.FILTER_LOCALTYPES;
57 /** @since 3.0 */
58 public static final int ALL_FILTERS= FILTER_NONPUBLIC | FILTER_FIELDS | FILTER_STATIC | FILTER_LOCALTYPES;
59
60 private static final String TAG_HIDEFIELDS= "hidefields"; //$NON-NLS-1$
61 private static final String TAG_HIDESTATIC= "hidestatic"; //$NON-NLS-1$
62 private static final String TAG_HIDENONPUBLIC= "hidenonpublic"; //$NON-NLS-1$
63 private static final String TAG_HIDELOCALTYPES= "hidelocaltypes"; //$NON-NLS-1$
64
65 public MemberFilterAction[] fFilterActions;
66 public MemberFilter fFilter;
67
68 public StructuredViewer fViewer;
69 private String fViewerId;
70 private boolean fInViewMenu;
71
72
73 /**
74 * Creates a new <code>MemberFilterActionGroup</code>.
75 *
76 * @param viewer the viewer to be filtered
77 * @param viewerId a unique id of the viewer. Used as a key to to store
78 * the last used filter settings in the preference store
79 */
80 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
81 this(viewer, viewerId, false);
82 }
83
84 /**
85 * Creates a new <code>MemberFilterActionGroup</code>.
86 *
87 * @param viewer the viewer to be filtered
88 * @param viewerId a unique id of the viewer. Used as a key to to store
89 * the last used filter settings in the preference store
90 * @param inViewMenu if <code>true</code> the actions are added to the view
91 * menu. If <code>false</code> they are added to the toolbar.
92 *
93 * @since 2.1
94 */
95 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId, boolean inViewMenu) {
96 this(viewer, viewerId, inViewMenu, ALL_FILTERS);
97 }
98
99 /**
100 * Creates a new <code>MemberFilterActionGroup</code>.
101 *
102 * @param viewer the viewer to be filtered
103 * @param viewerId a unique id of the viewer. Used as a key to to store
104 * the last used filter settings in the preference store
105 * @param inViewMenu if <code>true</code> the actions are added to the view
106 * menu. If <code>false</code> they are added to the toolbar.
107 * @param availableFilters Specifies which filter action should be contained. <code>FILTER_NONPUBLIC</code>,
108 * <code>FILTER_STATIC</code>, <code>FILTER_FIELDS</code> and <code>FILTER_LOCALTYPES</code>
109 * or a combination of these constants are possible values. Use <code>ALL_FILTERS</code> to select all available filters.
110 *
111 * @since 3.0
112 */
113 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId, boolean inViewMenu, int availableFilters) {
114
115 fViewer= viewer;
116 fViewerId= viewerId;
117 fInViewMenu= inViewMenu;
118
119 IPreferenceStore store= PreferenceConstants.getPreferenceStore();
120 fFilter= new MemberFilter();
121
122 String title, helpContext;
123 fFilter.generated_3300098908311116714(this, availableFilters, store);
124 }
125
126 public String getPreferenceKey(int filterProperty) {
127 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
128 }
129
130 /**
131 * Sets the member filters.
132 *
133 * @param filterProperty the filter to be manipulated. Valid values are <code>FILTER_FIELDS</code>,
134 * <code>FILTER_PUBLIC</code> <code>FILTER_PRIVATE</code> and <code>FILTER_LOCALTYPES_ACTION</code>
135 * as defined by this action group
136 * @param set if <code>true</code> the given filter is installed. If <code>false</code> the
137 * given filter is removed
138 * .
139 */
140 public void setMemberFilter(int filterProperty, boolean set) {
141 setMemberFilters(new int[] {filterProperty}, new boolean[] {set}, true);
142 }
143
144 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues, boolean refresh) {
145 if (propertyKeys.length == 0)
146 return;
147 Assert.isTrue(propertyKeys.length == propertyValues.length);
148
149 for (int i= 0; i < propertyKeys.length; i++) {
150 int filterProperty= propertyKeys[i];
151 boolean set= propertyValues[i];
152
153 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
154 boolean found= false;
155 fFilter.generated_5612156553343420502(this, filterProperty, set, store, found);
156 }
157 if (refresh) {
158 fViewer.getControl().setRedraw(false);
159 BusyIndicator.showWhile(fViewer.getControl().getDisplay(), new Runnable() {
160 public void run() {
161 fViewer.refresh();
162 }
163 });
164 fViewer.getControl().setRedraw(true);
165 }
166 }
167
168 public boolean isSet(int flag, int set) {
169 return (flag & set) != 0;
170 }
171
172 /**
173 * Returns <code>true</code> if the given filter is installed.
174 *
175 * @param filterProperty the filter to be tested. Valid values are <code>FILTER_FIELDS</code>,
176 * <code>FILTER_PUBLIC</code>, <code>FILTER_PRIVATE</code> and <code>FILTER_LOCALTYPES</code> as defined by this action
177 * group
178 * @return returns <code>true</code> if the given filter is installed
179 */
180 public boolean hasMemberFilter(int filterProperty) {
181 return fFilter.hasFilter(filterProperty);
182 }
183
184 /**
185 * Saves the state of the filter actions in a memento.
186 *
187 * @param memento the memento to which the state is saved
188 */
189 public void saveState(IMemento memento) {
190 memento.putString(TAG_HIDEFIELDS, String.valueOf(hasMemberFilter(FILTER_FIELDS)));
191 memento.putString(TAG_HIDESTATIC, String.valueOf(hasMemberFilter(FILTER_STATIC)));
192 memento.putString(TAG_HIDENONPUBLIC, String.valueOf(hasMemberFilter(FILTER_NONPUBLIC)));
193 memento.putString(TAG_HIDELOCALTYPES, String.valueOf(hasMemberFilter(FILTER_LOCALTYPES)));
194 }
195
196 /**
197 * Restores the state of the filter actions from a memento.
198 * <p>
199 * Note: This method does not refresh the viewer.
200 * </p>
201 * @param memento the memento from which the state is restored
202 */
203 public void restoreState(IMemento memento) {
204 setMemberFilters(
205 new int[] {FILTER_FIELDS, FILTER_STATIC, FILTER_NONPUBLIC, FILTER_LOCALTYPES},
206 new boolean[] {
207 Boolean.valueOf(memento.getString(TAG_HIDEFIELDS)).booleanValue(),
208 Boolean.valueOf(memento.getString(TAG_HIDESTATIC)).booleanValue(),
209 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC)).booleanValue(),
210 Boolean.valueOf(memento.getString(TAG_HIDELOCALTYPES)).booleanValue()
211 }, false);
212 }
213
214 /* (non-Javadoc)
215 * @see ActionGroup#fillActionBars(IActionBars)
216 */
217 @Override
218 public void fillActionBars(IActionBars actionBars) {
219 contributeToToolBar(actionBars.getToolBarManager());
220 }
221
222 /**
223 * Adds the filter actions to the given tool bar
224 *
225 * @param tbm the tool bar to which the actions are added
226 */
227 public void contributeToToolBar(IToolBarManager tbm) {
228 if (fInViewMenu)
229 return;
230 for (int i= 0; i < fFilterActions.length; i++) {
231 tbm.add(fFilterActions[i]);
232 }
233 }
234
235 /**
236 * Adds the filter actions to the given menu manager.
237 *
238 * @param menu the menu manager to which the actions are added
239 * @since 2.1
240 */
241 public void contributeToViewMenu(IMenuManager menu) {
242 if (!fInViewMenu)
243 return;
244 final String filters= "filters"; //$NON-NLS-1$
245 if (menu.find(filters) != null) {
246 for (int i= 0; i < fFilterActions.length; i++) {
247 menu.prependToGroup(filters, fFilterActions[i]);
248 }
249 } else {
250 for (int i= 0; i < fFilterActions.length; i++) {
251 menu.add(fFilterActions[i]);
252 }
253 }
254 }
255
256 /* (non-Javadoc)
257 * @see ActionGroup#dispose()
258 */
259 @Override
260 public void dispose() {
261 super.dispose();
262 }
263
264}