new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / actions / MemberFilterActionGroup.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
12
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
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;
29
30 /**
31  * Action Group that contributes filter buttons for a view parts showing 
32  * methods and fields. Contributed filters are: hide fields, hide static
33  * members and hide non-public members.
34  * <p>
35  * The action group installs a filter on a structured viewer. The filter is connected 
36  * to the actions installed in the view part's toolbar menu and is updated when the 
37  * state of the buttons changes.
38  *  
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  * 
43  * @since 2.0
44  */
45 public class MemberFilterActionGroup extends ActionGroup {
46
47         public static final int FILTER_NONPUBLIC= MemberFilter.FILTER_NONPUBLIC;
48         public static final int FILTER_STATIC= MemberFilter.FILTER_STATIC;
49         public static final int FILTER_FIELDS= MemberFilter.FILTER_FIELDS;
50         
51         private static final String TAG_HIDEFIELDS= "hidefields"; //$NON-NLS-1$
52         private static final String TAG_HIDESTATIC= "hidestatic"; //$NON-NLS-1$
53         private static final String TAG_HIDENONPUBLIC= "hidenonpublic"; //$NON-NLS-1$
54         
55         private MemberFilterAction[] fFilterActions;
56         private MemberFilter fFilter;
57         
58         private StructuredViewer fViewer;
59         private String fViewerId;
60         private boolean fInViewMenu;
61         
62         
63         /**
64          * Creates a new <code>MemberFilterActionGroup</code>.
65          * 
66          * @param viewer the viewer to be filtered
67          * @param viewerId a unique id of the viewer. Used as a key to to store 
68          * the last used filter settings in the preference store
69          */
70         public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) {
71                 this(viewer, viewerId, false);
72         }
73         
74         /**
75          * Creates a new <code>MemberFilterActionGroup</code>.
76          * 
77          * @param viewer the viewer to be filtered
78          * @param viewerId a unique id of the viewer. Used as a key to to store 
79          * the last used filter settings in the preference store
80          * @param inViewMenu if <code>true</code> the actions are added to the view
81          * menu. If <code>false</code> they are added to the toobar.
82          * 
83          * @since 2.1
84          */
85         public MemberFilterActionGroup(StructuredViewer viewer, String viewerId, boolean inViewMenu) {
86                 fViewer= viewer;
87                 fViewerId= viewerId;
88                 fInViewMenu= inViewMenu;
89                 
90                 // get initial values
91                 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
92                 boolean doHideFields= store.getBoolean(getPreferenceKey(FILTER_FIELDS));
93                 boolean doHideStatic= store.getBoolean(getPreferenceKey(FILTER_STATIC));
94                 boolean doHidePublic= store.getBoolean(getPreferenceKey(FILTER_NONPUBLIC));
95
96                 fFilter= new MemberFilter();
97                 if (doHideFields)
98                         fFilter.addFilter(FILTER_FIELDS);
99                 if (doHideStatic)
100                         fFilter.addFilter(FILTER_STATIC);                       
101                 if (doHidePublic)
102                         fFilter.addFilter(FILTER_NONPUBLIC);            
103         
104                 // fields
105                 String title= ActionMessages.getString("MemberFilterActionGroup.hide_fields.label"); //$NON-NLS-1$
106                 String helpContext= IJavaHelpContextIds.FILTER_FIELDS_ACTION;
107                 MemberFilterAction hideFields= new MemberFilterAction(this, title, FILTER_FIELDS, helpContext, doHideFields);
108                 hideFields.setDescription(ActionMessages.getString("MemberFilterActionGroup.hide_fields.description")); //$NON-NLS-1$
109                 hideFields.setToolTipText(ActionMessages.getString("MemberFilterActionGroup.hide_fields.tooltip")); //$NON-NLS-1$
110                 PHPUiImages.setLocalImageDescriptors(hideFields, "fields_co.gif"); //$NON-NLS-1$
111                 
112                 // static
113                 title= ActionMessages.getString("MemberFilterActionGroup.hide_static.label"); //$NON-NLS-1$
114                 helpContext= IJavaHelpContextIds.FILTER_STATIC_ACTION;
115                 MemberFilterAction hideStatic= new MemberFilterAction(this, title, FILTER_STATIC, helpContext, doHideStatic);
116                 hideStatic.setDescription(ActionMessages.getString("MemberFilterActionGroup.hide_static.description")); //$NON-NLS-1$
117                 hideStatic.setToolTipText(ActionMessages.getString("MemberFilterActionGroup.hide_static.tooltip")); //$NON-NLS-1$
118                 PHPUiImages.setLocalImageDescriptors(hideStatic, "static_co.gif"); //$NON-NLS-1$
119                 
120                 // non-public
121                 title= ActionMessages.getString("MemberFilterActionGroup.hide_nonpublic.label"); //$NON-NLS-1$
122                 helpContext= IJavaHelpContextIds.FILTER_PUBLIC_ACTION;
123                 MemberFilterAction hideNonPublic= new MemberFilterAction(this, title, FILTER_NONPUBLIC, helpContext, doHidePublic);
124                 hideNonPublic.setDescription(ActionMessages.getString("MemberFilterActionGroup.hide_nonpublic.description")); //$NON-NLS-1$
125                 hideNonPublic.setToolTipText(ActionMessages.getString("MemberFilterActionGroup.hide_nonpublic.tooltip")); //$NON-NLS-1$
126                 PHPUiImages.setLocalImageDescriptors(hideNonPublic, "public_co.gif"); //$NON-NLS-1$
127         
128                 // order corresponds to order in toolbar
129                 fFilterActions= new MemberFilterAction[] { hideFields, hideStatic, hideNonPublic };
130                 
131                 fViewer.addFilter(fFilter);
132         }
133         
134         private String getPreferenceKey(int filterProperty) {
135                 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); //$NON-NLS-1$
136         }
137         
138         /**
139          * Sets the member filters.
140          * 
141          * @param filterProperty the filter to be manipulated. Valid values are <code>FILTER_FIELDS</code>, 
142          * <code>FILTER_PUBLIC</code>, and <code>FILTER_PRIVATE</code> as defined by this action 
143          * group
144          * @param set if <code>true</code> the given filter is installed. If <code>false</code> the
145          * given filter is removed
146          * .
147          */     
148         public void setMemberFilter(int filterProperty, boolean set) {
149                 setMemberFilters(new int[] {filterProperty}, new boolean[] {set}, true);
150         }
151
152         private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues, boolean refresh) {
153                 if (propertyKeys.length == 0)
154                         return;
155                 Assert.isTrue(propertyKeys.length == propertyValues.length);
156                 
157                 for (int i= 0; i < propertyKeys.length; i++) {
158                         int filterProperty= propertyKeys[i];
159                         boolean set= propertyValues[i];
160                         if (set) {
161                                 fFilter.addFilter(filterProperty);
162                         } else {
163                                 fFilter.removeFilter(filterProperty);
164                         }
165                         IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
166                         
167                         for (int j= 0; j < fFilterActions.length; j++) {
168                                 int currProperty= fFilterActions[j].getFilterProperty();
169                                 if (currProperty == filterProperty) {
170                                         fFilterActions[j].setChecked(set);
171                                 }
172                                 store.setValue(getPreferenceKey(currProperty), hasMemberFilter(currProperty));
173                         }
174                 }
175                 if (refresh) {
176                         fViewer.getControl().setRedraw(false);
177                         BusyIndicator.showWhile(fViewer.getControl().getDisplay(), new Runnable() {
178                                 public void run() {
179                                         fViewer.refresh();
180                                 }
181                         });
182                         fViewer.getControl().setRedraw(true);
183                 }
184         }
185
186         /**
187          * Returns <code>true</code> if the given filter is installed.
188          * 
189          * @param filterProperty the filter to be tested. Valid values are <code>FILTER_FIELDS</code>, 
190          * <code>FILTER_PUBLIC</code>, and <code>FILTER_PRIVATE</code> as defined by this action 
191          * group
192          */     
193         public boolean hasMemberFilter(int filterProperty) {
194                 return fFilter.hasFilter(filterProperty);
195         }
196         
197         /**
198          * Saves the state of the filter actions in a memento.
199          * 
200          * @param memento the memento to which the state is saved
201          */
202         public void saveState(IMemento memento) {
203                 memento.putString(TAG_HIDEFIELDS, String.valueOf(hasMemberFilter(FILTER_FIELDS)));
204                 memento.putString(TAG_HIDESTATIC, String.valueOf(hasMemberFilter(FILTER_STATIC)));
205                 memento.putString(TAG_HIDENONPUBLIC, String.valueOf(hasMemberFilter(FILTER_NONPUBLIC)));
206         }
207         
208         /**
209          * Restores the state of the filter actions from a memento.
210          * <p>
211          * Note: This method does not refresh the viewer.
212          * </p>
213          * @param memento the memento from which the state is restored
214          */     
215         public void restoreState(IMemento memento) {
216                 setMemberFilters(
217                         new int[] {FILTER_FIELDS, FILTER_STATIC, FILTER_NONPUBLIC},
218                         new boolean[] {
219                                 Boolean.valueOf(memento.getString(TAG_HIDEFIELDS)).booleanValue(),
220                                 Boolean.valueOf(memento.getString(TAG_HIDESTATIC)).booleanValue(),
221                                 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC)).booleanValue()
222                         }, false);
223         }
224         
225         /* (non-Javadoc)
226          * @see ActionGroup#fillActionBars(IActionBars)
227          */
228         public void fillActionBars(IActionBars actionBars) {
229                 contributeToToolBar(actionBars.getToolBarManager());
230         };
231         
232         /**
233          * Adds the filter actions to the given tool bar
234          * 
235          * @param tbm the tool bar to which the actions are added
236          */
237         public void contributeToToolBar(IToolBarManager tbm) {
238                 if (fInViewMenu)
239                         return;
240                 tbm.add(fFilterActions[0]); // fields
241                 tbm.add(fFilterActions[1]); // static
242                 tbm.add(fFilterActions[2]); // public
243         }
244         
245         /**
246          * Adds the filter actions to the given menu manager.
247          * 
248          * @param menu the menu manager to which the actions are added
249          * @since 2.1
250          */
251         public void contributeToViewMenu(IMenuManager menu) {
252                 if (!fInViewMenu)
253                         return;
254                 final String filters= "filters"; //$NON-NLS-1$
255                 if (menu.find(filters) != null) {
256                         menu.prependToGroup(filters, fFilterActions[0]); // fields
257                         menu.prependToGroup(filters, fFilterActions[1]); // static
258                         menu.prependToGroup(filters, fFilterActions[2]); // public
259                 } else {
260                         menu.add(fFilterActions[0]); // fields
261                         menu.add(fFilterActions[1]); // static
262                         menu.add(fFilterActions[2]); // public
263                 }
264         }
265         
266         /* (non-Javadoc)
267          * @see ActionGroup#dispose()
268          */
269         public void dispose() {
270                 super.dispose();
271         }
272
273 }