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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import java.util.ResourceBundle;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.preference.IPreferenceStore;
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;
24 import org.eclipse.ui.actions.ActionGroup;
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;
31 import org.eclipse.ui.editors.text.IFoldingCommandIds;
33 import org.eclipse.jdt.ui.PreferenceConstants;
34 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
41 * Groups the JDT folding actions.
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);
51 public void generated_5342017134525805024(final ITextEditor editor) {
53 setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
54 editor.setAction("FoldingToggle", this);
57 public void generated_7729534616666090290(FoldingActionGroup foldingactiongroup) {
59 setChecked(foldingactiongroup.fViewer.isProjectionMode());
66 private class FoldingAction extends PreferenceAction {
68 FoldingAction(ResourceBundle bundle, String prefix) {
69 super(bundle, prefix, IAction.AS_PUSH_BUTTON);
72 public void update() {
73 setEnabled(FoldingActionGroup.this.isEnabled() && fViewer.isProjectionMode());
78 private ProjectionViewer fViewer;
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;
87 private final PreferenceAction fRestoreDefaults;
88 private final FoldingAction fCollapseMembers;
89 private final FoldingAction fCollapseComments;
90 private final TextOperationAction fCollapseAll;
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.
98 * @param editor the text editor to operate on
99 * @param viewer the viewer of the editor
101 public FoldingActionGroup(final ITextEditor editor, ITextViewer viewer) {
102 if (!(viewer instanceof ProjectionViewer)) {
108 fRestoreDefaults= null;
109 fCollapseMembers= null;
110 fCollapseComments= null;
111 fProjectionListener= null;
115 fViewer= (ProjectionViewer) viewer;
117 fProjectionListener= new IProjectionListener() {
119 public void projectionEnabled() {
123 public void projectionDisabled() {
128 fViewer.addProjectionListener(fProjectionListener);
130 fToggle= new PreferenceAction(FoldingMessages.getResourceBundle(), "Projection.Toggle.", IAction.AS_CHECK_BOX) { //$NON-NLS-1$
133 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
134 boolean current= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
135 store.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED, !current);
138 public void update() {
139 ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
141 boolean isEnabled= (target != null && target.canDoOperation(ProjectionViewer.TOGGLE));
142 setEnabled(isEnabled);
145 fToggle.generated_5342017134525805024(editor); //$NON-NLS-1$
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$
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$
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$
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$
163 fRestoreDefaults= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.Restore.") { //$NON-NLS-1$
166 if (editor instanceof JavaEditor) {
167 JavaEditor javaEditor= (JavaEditor) editor;
168 javaEditor.resetProjection();
172 fRestoreDefaults.setActionDefinitionId(IFoldingCommandIds.FOLDING_RESTORE);
173 editor.setAction("FoldingRestore", fRestoreDefaults); //$NON-NLS-1$
175 fCollapseMembers= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseMembers.") { //$NON-NLS-1$
178 if (editor instanceof JavaEditor) {
179 JavaEditor javaEditor= (JavaEditor) editor;
180 javaEditor.collapseMembers();
184 fCollapseMembers.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_MEMBERS);
185 editor.setAction("FoldingCollapseMembers", fCollapseMembers); //$NON-NLS-1$
187 fCollapseComments= new FoldingAction(FoldingMessages.getResourceBundle(), "Projection.CollapseComments.") { //$NON-NLS-1$
190 if (editor instanceof JavaEditor) {
191 JavaEditor javaEditor= (JavaEditor) editor;
192 javaEditor.collapseComments();
196 fCollapseComments.setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_COMMENTS);
197 editor.setAction("FoldingCollapseComments", fCollapseComments); //$NON-NLS-1$
201 * Returns <code>true</code> if the group is enabled.
203 * Invariant: isEnabled() <=> fViewer and all actions are != null.
206 * @return <code>true</code> if the group is enabled
208 protected boolean isEnabled() {
209 return fViewer != null;
213 * @see org.eclipse.ui.actions.ActionGroup#dispose()
216 public void dispose() {
218 fViewer.removeProjectionListener(fProjectionListener);
225 * Updates the actions.
227 protected void update() {
229 fToggle.generated_7729534616666090290(this);
233 fCollapseAll.update();
234 fRestoreDefaults.update();
235 fCollapseMembers.update();
236 fCollapseComments.update();
241 * Fills the menu with all folding actions.
243 * @param manager the menu manager for the folding submenu
245 public void fillMenu(IMenuManager manager) {
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);
260 * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
263 public void updateActionBars() {