1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.swt.custom.BusyIndicator; 18 19 import org.eclipse.jface.action.IMenuManager; 20 import org.eclipse.jface.action.IToolBarManager; 21 import org.eclipse.jface.preference.IPreferenceStore; 22 import org.eclipse.jface.viewers.StructuredViewer; 23 24 import org.eclipse.ui.IActionBars; 25 import org.eclipse.ui.IMemento; 26 import org.eclipse.ui.actions.ActionGroup; 27 28 import org.eclipse.jdt.ui.PreferenceConstants; 29 30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 import org.eclipse.jdt.internal.ui.JavaPluginImages; 33 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 34 import org.eclipse.jdt.internal.ui.viewsupport.MemberFilter; 35 import org.eclipse.jdt.internal.ui.viewsupport.MemberFilterAction; 36 37 52 public class MemberFilterActionGroup extends ActionGroup { 53 54 public static final int FILTER_NONPUBLIC= MemberFilter.FILTER_NONPUBLIC; 55 public static final int FILTER_STATIC= MemberFilter.FILTER_STATIC; 56 public static final int FILTER_FIELDS= MemberFilter.FILTER_FIELDS; 57 58 59 public static final int FILTER_LOCALTYPES= MemberFilter.FILTER_LOCALTYPES; 60 61 public static final int ALL_FILTERS= FILTER_NONPUBLIC | FILTER_FIELDS | FILTER_STATIC | FILTER_LOCALTYPES; 62 63 private static final String TAG_HIDEFIELDS= "hidefields"; private static final String TAG_HIDESTATIC= "hidestatic"; private static final String TAG_HIDENONPUBLIC= "hidenonpublic"; private static final String TAG_HIDELOCALTYPES= "hidelocaltypes"; 68 private MemberFilterAction[] fFilterActions; 69 private MemberFilter fFilter; 70 71 private StructuredViewer fViewer; 72 private String fViewerId; 73 private boolean fInViewMenu; 74 75 76 83 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId) { 84 this(viewer, viewerId, false); 85 } 86 87 98 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId, boolean inViewMenu) { 99 this(viewer, viewerId, inViewMenu, ALL_FILTERS); 100 } 101 102 116 public MemberFilterActionGroup(StructuredViewer viewer, String viewerId, boolean inViewMenu, int availableFilters) { 117 118 fViewer= viewer; 119 fViewerId= viewerId; 120 fInViewMenu= inViewMenu; 121 122 IPreferenceStore store= PreferenceConstants.getPreferenceStore(); 123 fFilter= new MemberFilter(); 124 125 String title, helpContext; 126 ArrayList actions= new ArrayList (4); 127 128 int filterProperty= FILTER_FIELDS; 130 if (isSet(filterProperty, availableFilters)) { 131 boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty)); 132 if (filterEnabled) { 133 fFilter.addFilter(filterProperty); 134 } 135 title= ActionMessages.MemberFilterActionGroup_hide_fields_label; 136 helpContext= IJavaHelpContextIds.FILTER_FIELDS_ACTION; 137 MemberFilterAction hideFields= new MemberFilterAction(this, title, filterProperty, helpContext, filterEnabled); 138 hideFields.setDescription(ActionMessages.MemberFilterActionGroup_hide_fields_description); 139 hideFields.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_fields_tooltip); 140 JavaPluginImages.setLocalImageDescriptors(hideFields, "fields_co.gif"); actions.add(hideFields); 142 } 143 144 filterProperty= FILTER_STATIC; 146 if (isSet(filterProperty, availableFilters)) { 147 boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty)); 148 if (filterEnabled) { 149 fFilter.addFilter(filterProperty); 150 } 151 title= ActionMessages.MemberFilterActionGroup_hide_static_label; 152 helpContext= IJavaHelpContextIds.FILTER_STATIC_ACTION; 153 MemberFilterAction hideStatic= new MemberFilterAction(this, title, FILTER_STATIC, helpContext, filterEnabled); 154 hideStatic.setDescription(ActionMessages.MemberFilterActionGroup_hide_static_description); 155 hideStatic.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_static_tooltip); 156 JavaPluginImages.setLocalImageDescriptors(hideStatic, "static_co.gif"); actions.add(hideStatic); 158 } 159 160 filterProperty= FILTER_NONPUBLIC; 162 if (isSet(filterProperty, availableFilters)) { 163 boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty)); 164 if (filterEnabled) { 165 fFilter.addFilter(filterProperty); 166 } 167 title= ActionMessages.MemberFilterActionGroup_hide_nonpublic_label; 168 helpContext= IJavaHelpContextIds.FILTER_PUBLIC_ACTION; 169 MemberFilterAction hideNonPublic= new MemberFilterAction(this, title, filterProperty, helpContext, filterEnabled); 170 hideNonPublic.setDescription(ActionMessages.MemberFilterActionGroup_hide_nonpublic_description); 171 hideNonPublic.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_nonpublic_tooltip); 172 JavaPluginImages.setLocalImageDescriptors(hideNonPublic, "public_co.gif"); actions.add(hideNonPublic); 174 } 175 176 filterProperty= FILTER_LOCALTYPES; 178 if (isSet(filterProperty, availableFilters)) { 179 boolean filterEnabled= store.getBoolean(getPreferenceKey(filterProperty)); 180 if (filterEnabled) { 181 fFilter.addFilter(filterProperty); 182 } 183 title= ActionMessages.MemberFilterActionGroup_hide_localtypes_label; 184 helpContext= IJavaHelpContextIds.FILTER_LOCALTYPES_ACTION; 185 MemberFilterAction hideLocalTypes= new MemberFilterAction(this, title, filterProperty, helpContext, filterEnabled); 186 hideLocalTypes.setDescription(ActionMessages.MemberFilterActionGroup_hide_localtypes_description); 187 hideLocalTypes.setToolTipText(ActionMessages.MemberFilterActionGroup_hide_localtypes_tooltip); 188 JavaPluginImages.setLocalImageDescriptors(hideLocalTypes, "localtypes_co.gif"); actions.add(hideLocalTypes); 190 } 191 192 fFilterActions= (MemberFilterAction[]) actions.toArray(new MemberFilterAction[actions.size()]); 194 195 fViewer.addFilter(fFilter); 196 } 197 198 private String getPreferenceKey(int filterProperty) { 199 return "MemberFilterActionGroup." + fViewerId + '.' + String.valueOf(filterProperty); } 201 202 212 public void setMemberFilter(int filterProperty, boolean set) { 213 setMemberFilters(new int[] {filterProperty}, new boolean[] {set}, true); 214 } 215 216 private void setMemberFilters(int[] propertyKeys, boolean[] propertyValues, boolean refresh) { 217 if (propertyKeys.length == 0) 218 return; 219 Assert.isTrue(propertyKeys.length == propertyValues.length); 220 221 for (int i= 0; i < propertyKeys.length; i++) { 222 int filterProperty= propertyKeys[i]; 223 boolean set= propertyValues[i]; 224 225 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 226 boolean found= false; 227 for (int j= 0; j < fFilterActions.length; j++) { 228 int currProperty= fFilterActions[j].getFilterProperty(); 229 if (currProperty == filterProperty) { 230 fFilterActions[j].setChecked(set); 231 found= true; 232 store.setValue(getPreferenceKey(filterProperty), set); 233 } 234 } 235 if (found) { 236 if (set) { 237 fFilter.addFilter(filterProperty); 238 } else { 239 fFilter.removeFilter(filterProperty); 240 } 241 } 242 } 243 if (refresh) { 244 fViewer.getControl().setRedraw(false); 245 BusyIndicator.showWhile(fViewer.getControl().getDisplay(), new Runnable () { 246 public void run() { 247 fViewer.refresh(); 248 } 249 }); 250 fViewer.getControl().setRedraw(true); 251 } 252 } 253 254 private boolean isSet(int flag, int set) { 255 return (flag & set) != 0; 256 } 257 258 265 public boolean hasMemberFilter(int filterProperty) { 266 return fFilter.hasFilter(filterProperty); 267 } 268 269 274 public void saveState(IMemento memento) { 275 memento.putString(TAG_HIDEFIELDS, String.valueOf(hasMemberFilter(FILTER_FIELDS))); 276 memento.putString(TAG_HIDESTATIC, String.valueOf(hasMemberFilter(FILTER_STATIC))); 277 memento.putString(TAG_HIDENONPUBLIC, String.valueOf(hasMemberFilter(FILTER_NONPUBLIC))); 278 memento.putString(TAG_HIDELOCALTYPES, String.valueOf(hasMemberFilter(FILTER_LOCALTYPES))); 279 } 280 281 288 public void restoreState(IMemento memento) { 289 setMemberFilters( 290 new int[] {FILTER_FIELDS, FILTER_STATIC, FILTER_NONPUBLIC, FILTER_LOCALTYPES}, 291 new boolean[] { 292 Boolean.valueOf(memento.getString(TAG_HIDEFIELDS)).booleanValue(), 293 Boolean.valueOf(memento.getString(TAG_HIDESTATIC)).booleanValue(), 294 Boolean.valueOf(memento.getString(TAG_HIDENONPUBLIC)).booleanValue(), 295 Boolean.valueOf(memento.getString(TAG_HIDELOCALTYPES)).booleanValue() 296 }, false); 297 } 298 299 302 public void fillActionBars(IActionBars actionBars) { 303 contributeToToolBar(actionBars.getToolBarManager()); 304 } 305 306 311 public void contributeToToolBar(IToolBarManager tbm) { 312 if (fInViewMenu) 313 return; 314 for (int i= 0; i < fFilterActions.length; i++) { 315 tbm.add(fFilterActions[i]); 316 } 317 } 318 319 325 public void contributeToViewMenu(IMenuManager menu) { 326 if (!fInViewMenu) 327 return; 328 final String filters= "filters"; if (menu.find(filters) != null) { 330 for (int i= 0; i < fFilterActions.length; i++) { 331 menu.prependToGroup(filters, fFilterActions[i]); 332 } 333 } else { 334 for (int i= 0; i < fFilterActions.length; i++) { 335 menu.add(fFilterActions[i]); 336 } 337 } 338 } 339 340 343 public void dispose() { 344 super.dispose(); 345 } 346 347 } 348 | Popular Tags |