]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/actions/FoldingActionGroup.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / actions / FoldingActionGroup.java
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  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
12
13 import java.util.ResourceBundle;
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
26 import org.eclipse.ui.texteditor.ITextEditor;
27 import org.eclipse.ui.texteditor.IUpdate;
28 import org.eclipse.ui.texteditor.ResourceAction;
29 import org.eclipse.ui.texteditor.TextOperationAction;
30
31 import org.eclipse.ui.editors.text.IFoldingCommandIds;
32
33 import org.eclipse.jdt.ui.PreferenceConstants;
34 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
35
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
38
39
40 /**
41  * Groups the JDT folding actions.
42  *
43  * @since 3.0
44  */
45 public class FoldingActionGroup extends ActionGroup {
46         private static abstract class PreferenceAction extends ResourceAction implements IUpdate {
47                 PreferenceAction(ResourceBundle bundle, String prefix, int style) {
48                         super(bundle, prefix, style);
49                 }
50
51                 public void generated_5342017134525805024(final ITextEditor editor) {
52                         setChecked(true);
53                         setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
54                         editor.setAction("FoldingToggle", this);
55                 }
56
57                 public void generated_7729534616666090290(FoldingActionGroup foldingactiongroup) {
58                         update();
59                         setChecked(foldingactiongroup.fViewer.isProjectionMode());
60                 }
61         }
62
63         /**
64          * @since 3.2
65          */
66         private class FoldingAction extends PreferenceAction {
67
68                 FoldingAction(ResourceBundle bundle, String prefix) {
69                         super(bundle, prefix, IAction.AS_PUSH_BUTTON);
70                 }
71
72                 public void update() {
73                         setEnabled(FoldingActionGroup.this.isEnabled() && fViewer.isProjectionMode());
74                 }
75
76         }
77
78         private ProjectionViewer fViewer;
79
80         private final PreferenceAction fToggle;
81         private final TextOperationAction fExpand;
82         private final TextOperationAction fCollapse;
83         private final TextOperationAction fExpandAll;
84         private final IProjectionListener fProjectionListener;
85
86         /* since 3.2 */
87         private final PreferenceAction fRestoreDefaults;
88         private final FoldingAction fCollapseMembers;
89         private final FoldingAction fCollapseComments;
90         private final TextOperationAction fCollapseAll;
91
92
93         /**
94          * Creates a new projection action group for <code>editor</code>. If the
95          * supplied viewer is not an instance of <code>ProjectionViewer</code>, the
96          * action group is disabled.
97          *
98          * @param editor the text editor to operate on
99          * @param viewer the viewer of the editor
100          */
101         public FoldingActionGroup(final ITextEditor editor, ITextViewer viewer) {
102                 if (!(viewer instanceof ProjectionViewer)) {
103                         fToggle= null;
104                         fExpand= null;
105                         fCollapse= null;
106                         fExpandAll= null;
107                         fCollapseAll= null;
108                         fRestoreDefaults= null;
109                         fCollapseMembers= null;
110                         fCollapseComments= null;
111                         fProjectionListener= null;
112                         return;
113                 }
114
115                 fViewer= (ProjectionViewer) viewer;
116
117                 fProjectionListener= new IProjectionListener() {
118
119                         public void projectionEnabled() {
120                                 update();
121                         }
122
123                         public void projectionDisabled() {
124                                 update();
125                         }
126                 };
127
128                 fViewer.addProjectionListener(fProjectionListener);
129
130                 fToggle= new PreferenceAction(FoldingMessages.getResourceBundle(), "Projection.Toggle.", IAction.AS_CHECK_BOX) { //$NON-NLS-1$
131                         @Override
132                         public void run() {
133                                 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
134                                 boolean current= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
135                                 store.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED, !current);
136                         }
137
138                         public void update() {
139                                 ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
140
141                                 boolean isEnabled= (target != null && target.canDoOperation(ProjectionViewer.TOGGLE));
142                                 setEnabled(isEnabled);
143                         }
144                 };
145                 fToggle.generated_5342017134525805024(editor); //$NON-NLS-1$
146
147                 fExpandAll= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); //$NON-NLS-1$
148                 fExpandAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL);
149                 editor.setAction("FoldingExpandAll", fExpandAll); //$NON-NLS-1$
150
151                 fCollapseAll= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.CollapseAll.", editor, ProjectionViewer.COLLAPSE_ALL, true); //$NON-NLS-1$
152                 fCollapseAll.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE_ALL);
153                 editor.setAction("FoldingCollapseAll", fCollapseAll); //$NON-NLS-1$
154
155                 fExpand= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); //$NON-NLS-1$
156                 fExpand.setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND);
157                 editor.setAction("FoldingExpand", fExpand); //$NON-NLS-1$
158
159                 fCollapse= new TextOperationAction(FoldingMessages.getResourceBundle(), "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); //$NON-NLS-1$
160                 fCollapse.setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE);
161                 editor.setAction("FoldingCollapse", fCollapse); //$NON-NLS-1$
162
163                 fRestoreDefaults= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.Restore.") { //$NON-NLS-1$
164                         @Override
165                         public void run() {
166                                 if (editor instanceof JavaEditor) {
167                                         JavaEditor javaEditor= (JavaEditor) editor;
168                                         javaEditor.resetProjection();
169                                 }
170                         }
171                 };
172                 fRestoreDefaults.setActionDefinitionId(IFoldingCommandIds.FOLDING_RESTORE);
173                 editor.setAction("FoldingRestore", fRestoreDefaults); //$NON-NLS-1$
174
175                 fCollapseMembers= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseMembers.") { //$NON-NLS-1$
176                         @Override
177                         public void run() {
178                                 if (editor instanceof JavaEditor) {
179                                         JavaEditor javaEditor= (JavaEditor) editor;
180                                         javaEditor.collapseMembers();
181                                 }
182                         }
183                 };
184                 fCollapseMembers.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_MEMBERS);
185                 editor.setAction("FoldingCollapseMembers", fCollapseMembers); //$NON-NLS-1$
186
187                 fCollapseComments= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseComments.") { //$NON-NLS-1$
188                         @Override
189                         public void run() {
190                                 if (editor instanceof JavaEditor) {
191                                         JavaEditor javaEditor= (JavaEditor) editor;
192                                         javaEditor.collapseComments();
193                                 }
194                         }
195                 };
196                 fCollapseComments.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_COMMENTS);
197                 editor.setAction("FoldingCollapseComments", fCollapseComments); //$NON-NLS-1$
198         }
199
200         /**
201          * Returns <code>true</code> if the group is enabled.
202          * <pre>
203          * Invariant: isEnabled() <=> fViewer and all actions are != null.
204          * </pre>
205          *
206          * @return <code>true</code> if the group is enabled
207          */
208         protected boolean isEnabled() {
209                 return fViewer != null;
210         }
211
212         /*
213          * @see org.eclipse.ui.actions.ActionGroup#dispose()
214          */
215         @Override
216         public void dispose() {
217                 if (isEnabled()) {
218                         fViewer.removeProjectionListener(fProjectionListener);
219                         fViewer= null;
220                 }
221                 super.dispose();
222         }
223
224         /**
225          * Updates the actions.
226          */
227         protected void update() {
228                 if (isEnabled()) {
229                         fToggle.generated_7729534616666090290(this);
230                         fExpand.update();
231                         fExpandAll.update();
232                         fCollapse.update();
233                         fCollapseAll.update();
234                         fRestoreDefaults.update();
235                         fCollapseMembers.update();
236                         fCollapseComments.update();
237                 }
238         }
239
240         /**
241          * Fills the menu with all folding actions.
242          *
243          * @param manager the menu manager for the folding submenu
244          */
245         public void fillMenu(IMenuManager manager) {
246                 if (isEnabled()) {
247                         update();
248                         manager.add(fToggle);
249                         manager.add(fExpandAll);
250                         manager.add(fExpand);
251                         manager.add(fCollapse);
252                         manager.add(fCollapseAll);
253                         manager.add(fRestoreDefaults);
254                         manager.add(fCollapseMembers);
255                         manager.add(fCollapseComments);
256                 }
257         }
258
259         /*
260          * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
261          */
262         @Override
263         public void updateActionBars() {
264                 update();
265         }
266 }