KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > FoldingActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.actions;
12
13 import java.util.ResourceBundle JavaDoc;
14
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.preference.IPreferenceStore;
18
19 import org.eclipse.jface.text.ITextOperationTarget;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.source.projection.IProjectionListener;
22 import org.eclipse.jface.text.source.projection.ProjectionViewer;
23
24 import org.eclipse.ui.actions.ActionGroup;
25 import org.eclipse.ui.texteditor.ITextEditor;
26 import org.eclipse.ui.texteditor.IUpdate;
27 import org.eclipse.ui.texteditor.ResourceAction;
28 import org.eclipse.ui.texteditor.TextOperationAction;
29
30 import org.eclipse.ui.editors.text.IFoldingCommandIds;
31
32 import org.eclipse.jdt.ui.PreferenceConstants;
33 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
37
38
39 /**
40  * Groups the JDT folding actions.
41  *
42  * @since 3.0
43  */

44 public class FoldingActionGroup extends ActionGroup {
45     private static abstract class PreferenceAction extends ResourceAction implements IUpdate {
46         PreferenceAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, int style) {
47             super(bundle, prefix, style);
48         }
49     }
50     
51     /**
52      * @since 3.2
53      */

54     private class FoldingAction extends PreferenceAction {
55
56         FoldingAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix) {
57             super(bundle, prefix, IAction.AS_PUSH_BUTTON);
58         }
59
60         public void update() {
61             setEnabled(FoldingActionGroup.this.isEnabled() && fViewer.isProjectionMode());
62         }
63         
64     }
65     
66     private ProjectionViewer fViewer;
67     
68     private final PreferenceAction fToggle;
69     private final TextOperationAction fExpand;
70     private final TextOperationAction fCollapse;
71     private final TextOperationAction fExpandAll;
72     private final IProjectionListener fProjectionListener;
73     
74     /* since 3.2 */
75     private final PreferenceAction fRestoreDefaults;
76     private final FoldingAction fCollapseMembers;
77     private final FoldingAction fCollapseComments;
78     private final TextOperationAction fCollapseAll;
79
80
81     /**
82      * Creates a new projection action group for <code>editor</code>. If the
83      * supplied viewer is not an instance of <code>ProjectionViewer</code>, the
84      * action group is disabled.
85      *
86      * @param editor the text editor to operate on
87      * @param viewer the viewer of the editor
88      */

89     public FoldingActionGroup(final ITextEditor editor, ITextViewer viewer) {
90         if (!(viewer instanceof ProjectionViewer)) {
91             fToggle= null;
92             fExpand= null;
93             fCollapse= null;
94             fExpandAll= null;
95             fCollapseAll= null;
96             fRestoreDefaults= null;
97             fCollapseMembers= null;
98             fCollapseComments= null;
99             fProjectionListener= null;
100             return;
101         }
102         
103         fViewer= (ProjectionViewer) viewer;
104         
105         fProjectionListener= new IProjectionListener() {
106
107             public void projectionEnabled() {
108                 update();
109             }
110
111             public void projectionDisabled() {
112                 update();
113             }
114         };
115         
116         fViewer.addProjectionListener(fProjectionListener);
117         
118         fToggle= new PreferenceAction(FoldingMessages.getResourceBundle(), "Projection.Toggle.", IAction.AS_CHECK_BOX) { //$NON-NLS-1$
119
public void run() {
120                 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
121                 boolean current= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
122                 store.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED, !current);
123             }
124
125             public void update() {
126                 ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
127                     
128                 boolean isEnabled= (target != null && target.canDoOperation(ProjectionViewer.TOGGLE));
129                 setEnabled(isEnabled);
130             }
131         };
132         fToggle.setChecked(true);
133         fToggle.setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
134         editor.setAction("FoldingToggle", fToggle); //$NON-NLS-1$
135

136         fExpandAll= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); //$NON-NLS-1$
137
fExpandAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL);
138         editor.setAction("FoldingExpandAll", fExpandAll); //$NON-NLS-1$
139

140         fCollapseAll= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.CollapseAll.", editor, ProjectionViewer.COLLAPSE_ALL, true); //$NON-NLS-1$
141
fCollapseAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE_ALL);
142         editor.setAction("FoldingCollapseAll", fCollapseAll); //$NON-NLS-1$
143

144         fExpand= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); //$NON-NLS-1$
145
fExpand.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND);
146         editor.setAction("FoldingExpand", fExpand); //$NON-NLS-1$
147

148         fCollapse= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); //$NON-NLS-1$
149
fCollapse.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE);
150         editor.setAction("FoldingCollapse", fCollapse); //$NON-NLS-1$
151

152         fRestoreDefaults= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.Restore.") { //$NON-NLS-1$
153
public void run() {
154                 if (editor instanceof JavaEditor) {
155                     JavaEditor javaEditor= (JavaEditor) editor;
156                     javaEditor.resetProjection();
157                 }
158             }
159         };
160         fRestoreDefaults.setActionDefinitionId(IFoldingCommandIds.FOLDING_RESTORE);
161         editor.setAction("FoldingRestore", fRestoreDefaults); //$NON-NLS-1$
162

163         fCollapseMembers= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseMembers.") { //$NON-NLS-1$
164
public void run() {
165                 if (editor instanceof JavaEditor) {
166                     JavaEditor javaEditor= (JavaEditor) editor;
167                     javaEditor.collapseMembers();
168                 }
169             }
170         };
171         fCollapseMembers.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_MEMBERS);
172         editor.setAction("FoldingCollapseMembers", fCollapseMembers); //$NON-NLS-1$
173

174         fCollapseComments= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseComments.") { //$NON-NLS-1$
175
public void run() {
176                 if (editor instanceof JavaEditor) {
177                     JavaEditor javaEditor= (JavaEditor) editor;
178                     javaEditor.collapseComments();
179                 }
180             }
181         };
182         fCollapseComments.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_COMMENTS);
183         editor.setAction("FoldingCollapseComments", fCollapseComments); //$NON-NLS-1$
184
}
185     
186     /**
187      * Returns <code>true</code> if the group is enabled.
188      * <pre>
189      * Invariant: isEnabled() <=> fViewer and all actions are != null.
190      * </pre>
191      *
192      * @return <code>true</code> if the group is enabled
193      */

194     protected boolean isEnabled() {
195         return fViewer != null;
196     }
197     
198     /*
199      * @see org.eclipse.ui.actions.ActionGroup#dispose()
200      */

201     public void dispose() {
202         if (isEnabled()) {
203             fViewer.removeProjectionListener(fProjectionListener);
204             fViewer= null;
205         }
206         super.dispose();
207     }
208     
209     /**
210      * Updates the actions.
211      */

212     protected void update() {
213         if (isEnabled()) {
214             fToggle.update();
215             fToggle.setChecked(fViewer.isProjectionMode());
216             fExpand.update();
217             fExpandAll.update();
218             fCollapse.update();
219             fCollapseAll.update();
220             fRestoreDefaults.update();
221             fCollapseMembers.update();
222             fCollapseComments.update();
223         }
224     }
225     
226     /**
227      * Fills the menu with all folding actions.
228      *
229      * @param manager the menu manager for the folding submenu
230      */

231     public void fillMenu(IMenuManager manager) {
232         if (isEnabled()) {
233             update();
234             manager.add(fToggle);
235             manager.add(fExpandAll);
236             manager.add(fExpand);
237             manager.add(fCollapse);
238             manager.add(fCollapseAll);
239             manager.add(fRestoreDefaults);
240             manager.add(fCollapseMembers);
241             manager.add(fCollapseComments);
242         }
243     }
244     
245     /*
246      * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
247      */

248     public void updateActionBars() {
249         update();
250     }
251 }
252
Popular Tags