KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > ViewActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.workingsets;
12
13 import org.eclipse.jface.action.IMenuManager;
14 import org.eclipse.jface.action.MenuManager;
15 import org.eclipse.jface.action.Separator;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.jface.viewers.ViewerFilter;
21
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.IWorkbenchPartSite;
25 import org.eclipse.ui.actions.ActionGroup;
26
27 import org.eclipse.jdt.ui.IContextMenuConstants;
28
29 /**
30  * An action group to provide access to the working sets.
31  */

32 public class ViewActionGroup extends ActionGroup {
33
34     public static final int SHOW_PROJECTS= 1;
35     public static final int SHOW_WORKING_SETS= 2;
36     public static final String JavaDoc MODE_CHANGED= ViewActionGroup.class.getName() + ".mode_changed"; //$NON-NLS-1$
37

38     private static final Integer JavaDoc INT_SHOW_PROJECTS= new Integer JavaDoc(SHOW_PROJECTS);
39     private static final Integer JavaDoc INT_SHOW_WORKING_SETS= new Integer JavaDoc(SHOW_WORKING_SETS);
40     
41     private IPropertyChangeListener fChangeListener;
42     
43     private int fMode;
44     private IMenuManager fMenuManager;
45     private IWorkingSetActionGroup fActiveActionGroup;
46     private final WorkingSetShowActionGroup fShowActionGroup;
47     private final WorkingSetFilterActionGroup fFilterActionGroup;
48     private final ConfigureWorkingSetAssignementAction fWorkingSetAssignementAction;
49     private final OpenPropertiesWorkingSetAction fEditWorkingSetGroupAction; // active on working sets: edit
50
private final IWorkbenchPartSite fSite;
51
52     public ViewActionGroup(int mode, IPropertyChangeListener changeListener, IWorkbenchPartSite site) {
53         fChangeListener= changeListener;
54         fSite= site;
55         if (fChangeListener == null) {
56             fChangeListener = new IPropertyChangeListener() {
57                 public void propertyChange(PropertyChangeEvent event) {}
58             };
59         }
60         fFilterActionGroup= new WorkingSetFilterActionGroup(site, fChangeListener);
61         fShowActionGroup= new WorkingSetShowActionGroup(site);
62         fWorkingSetAssignementAction= new ConfigureWorkingSetAssignementAction(site);
63         fEditWorkingSetGroupAction= new OpenPropertiesWorkingSetAction(site);
64
65         ISelectionProvider selectionProvider= site.getSelectionProvider();
66         selectionProvider.addSelectionChangedListener(fWorkingSetAssignementAction);
67         selectionProvider.addSelectionChangedListener(fEditWorkingSetGroupAction);
68         
69         fMode= mode;
70         if (showWorkingSets())
71             fActiveActionGroup= fShowActionGroup;
72         else
73             fActiveActionGroup= fFilterActionGroup;
74     }
75     
76     public void dispose() {
77         fFilterActionGroup.dispose();
78         fShowActionGroup.dispose();
79         fChangeListener= null;
80         ISelectionProvider selectionProvider= fSite.getSelectionProvider();
81         selectionProvider.removeSelectionChangedListener(fWorkingSetAssignementAction);
82         selectionProvider.removeSelectionChangedListener(fEditWorkingSetGroupAction);
83         super.dispose();
84     }
85     
86     public void setWorkingSetModel(WorkingSetModel model) {
87         fShowActionGroup.setWorkingSetMode(model);
88         fWorkingSetAssignementAction.setWorkingSetModel(model);
89     }
90     
91     public void fillContextMenu(IMenuManager menu) {
92         if (fWorkingSetAssignementAction.isEnabled())
93             menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fWorkingSetAssignementAction);
94         
95         if (fEditWorkingSetGroupAction.isEnabled()) {
96             menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fEditWorkingSetGroupAction);
97         }
98     }
99
100     /**
101      * {@inheritDoc}
102      */

103     public void fillActionBars(IActionBars actionBars) {
104         super.fillActionBars(actionBars);
105         fMenuManager= actionBars.getMenuManager();
106         fillViewMenu(fMenuManager);
107
108         if (fActiveActionGroup == null)
109             fActiveActionGroup= fFilterActionGroup;
110         ((ActionGroup)fActiveActionGroup).fillActionBars(actionBars);
111     }
112     
113     private void fillViewMenu(IMenuManager menu) {
114         IMenuManager showMenu= new MenuManager(WorkingSetMessages.ViewActionGroup_show_label);
115         fillShowMenu(showMenu);
116         menu.add(showMenu);
117         menu.add(new Separator(IWorkingSetActionGroup.ACTION_GROUP));
118     }
119     
120     private void fillShowMenu(IMenuManager menu) {
121         ViewAction projects= new ViewAction(this, SHOW_PROJECTS);
122         projects.setText(WorkingSetMessages.ViewActionGroup_projects_label);
123         menu.add(projects);
124         ViewAction workingSets= new ViewAction(this, SHOW_WORKING_SETS);
125         workingSets.setText(WorkingSetMessages.ViewActionGroup_workingSets_label);
126         menu.add(workingSets);
127         if (fMode == SHOW_PROJECTS) {
128             projects.setChecked(true);
129         } else {
130             workingSets.setChecked(true);
131         }
132     }
133
134     public void fillFilters(StructuredViewer viewer) {
135         ViewerFilter workingSetFilter= fFilterActionGroup.getWorkingSetFilter();
136         if (showProjects()) {
137             viewer.addFilter(workingSetFilter);
138         } else if (showWorkingSets()) {
139             viewer.removeFilter(workingSetFilter);
140         }
141     }
142     
143     public void setMode(int mode) {
144         fMode= mode;
145         fActiveActionGroup.cleanViewMenu(fMenuManager);
146         PropertyChangeEvent event;
147         if (mode == SHOW_PROJECTS) {
148             fActiveActionGroup= fFilterActionGroup;
149             event= new PropertyChangeEvent(this, MODE_CHANGED, INT_SHOW_WORKING_SETS, INT_SHOW_PROJECTS);
150         } else {
151             fActiveActionGroup= fShowActionGroup;
152             event= new PropertyChangeEvent(this, MODE_CHANGED, INT_SHOW_PROJECTS, INT_SHOW_WORKING_SETS);
153         }
154         fActiveActionGroup.fillViewMenu(fMenuManager);
155         fMenuManager.updateAll(true);
156         if(fChangeListener != null)
157             fChangeListener.propertyChange(event);
158     }
159     
160     public WorkingSetFilterActionGroup getFilterGroup() {
161         return fFilterActionGroup;
162     }
163
164     public void restoreState(IMemento memento) {
165         fFilterActionGroup.restoreState(memento);
166     }
167
168     public void saveState(IMemento memento) {
169         fFilterActionGroup.saveState(memento);
170     }
171     
172     private boolean showProjects() {
173         return fMode == SHOW_PROJECTS;
174     }
175     
176     private boolean showWorkingSets() {
177         return fMode == SHOW_WORKING_SETS;
178     }
179 }
180
Popular Tags