001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.jface.action.IAction;
015: import org.eclipse.jface.action.IMenuManager;
016: import org.eclipse.jface.preference.IPreferenceStore;
017:
018: import org.eclipse.jface.text.ITextOperationTarget;
019: import org.eclipse.jface.text.ITextViewer;
020: import org.eclipse.jface.text.source.projection.IProjectionListener;
021: import org.eclipse.jface.text.source.projection.ProjectionViewer;
022:
023: import org.eclipse.ui.actions.ActionGroup;
024: import org.eclipse.ui.texteditor.ITextEditor;
025: import org.eclipse.ui.texteditor.IUpdate;
026: import org.eclipse.ui.texteditor.ResourceAction;
027: import org.eclipse.ui.texteditor.TextOperationAction;
028:
029: import org.eclipse.ui.editors.text.IFoldingCommandIds;
030:
031: import org.eclipse.jdt.ui.PreferenceConstants;
032: import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
033:
034: import org.eclipse.jdt.internal.ui.JavaPlugin;
035: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
036:
037: /**
038: * Groups the JDT folding actions.
039: *
040: * @since 3.0
041: */
042: public class FoldingActionGroup extends ActionGroup {
043: private static abstract class PreferenceAction extends
044: ResourceAction implements IUpdate {
045: PreferenceAction(ResourceBundle bundle, String prefix, int style) {
046: super (bundle, prefix, style);
047: }
048: }
049:
050: /**
051: * @since 3.2
052: */
053: private class FoldingAction extends PreferenceAction {
054:
055: FoldingAction(ResourceBundle bundle, String prefix) {
056: super (bundle, prefix, IAction.AS_PUSH_BUTTON);
057: }
058:
059: public void update() {
060: setEnabled(FoldingActionGroup.this .isEnabled()
061: && fViewer.isProjectionMode());
062: }
063:
064: }
065:
066: private ProjectionViewer fViewer;
067:
068: private final PreferenceAction fToggle;
069: private final TextOperationAction fExpand;
070: private final TextOperationAction fCollapse;
071: private final TextOperationAction fExpandAll;
072: private final IProjectionListener fProjectionListener;
073:
074: /* since 3.2 */
075: private final PreferenceAction fRestoreDefaults;
076: private final FoldingAction fCollapseMembers;
077: private final FoldingAction fCollapseComments;
078: private final TextOperationAction fCollapseAll;
079:
080: /**
081: * Creates a new projection action group for <code>editor</code>. If the
082: * supplied viewer is not an instance of <code>ProjectionViewer</code>, the
083: * action group is disabled.
084: *
085: * @param editor the text editor to operate on
086: * @param viewer the viewer of the editor
087: */
088: public FoldingActionGroup(final ITextEditor editor,
089: ITextViewer viewer) {
090: if (!(viewer instanceof ProjectionViewer)) {
091: fToggle = null;
092: fExpand = null;
093: fCollapse = null;
094: fExpandAll = null;
095: fCollapseAll = null;
096: fRestoreDefaults = null;
097: fCollapseMembers = null;
098: fCollapseComments = null;
099: 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
119: .getResourceBundle(),
120: "Projection.Toggle.", IAction.AS_CHECK_BOX) { //$NON-NLS-1$
121: public void run() {
122: IPreferenceStore store = JavaPlugin.getDefault()
123: .getPreferenceStore();
124: boolean current = store
125: .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
126: store.setValue(
127: PreferenceConstants.EDITOR_FOLDING_ENABLED,
128: !current);
129: }
130:
131: public void update() {
132: ITextOperationTarget target = (ITextOperationTarget) editor
133: .getAdapter(ITextOperationTarget.class);
134:
135: boolean isEnabled = (target != null && target
136: .canDoOperation(ProjectionViewer.TOGGLE));
137: setEnabled(isEnabled);
138: }
139: };
140: fToggle.setChecked(true);
141: fToggle
142: .setActionDefinitionId(IFoldingCommandIds.FOLDING_TOGGLE);
143: editor.setAction("FoldingToggle", fToggle); //$NON-NLS-1$
144:
145: fExpandAll = new TextOperationAction(
146: FoldingMessages.getResourceBundle(),
147: "Projection.ExpandAll.", editor, ProjectionViewer.EXPAND_ALL, true); //$NON-NLS-1$
148: fExpandAll
149: .setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND_ALL);
150: editor.setAction("FoldingExpandAll", fExpandAll); //$NON-NLS-1$
151:
152: fCollapseAll = new TextOperationAction(
153: FoldingMessages.getResourceBundle(),
154: "Projection.CollapseAll.", editor, ProjectionViewer.COLLAPSE_ALL, true); //$NON-NLS-1$
155: fCollapseAll
156: .setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE_ALL);
157: editor.setAction("FoldingCollapseAll", fCollapseAll); //$NON-NLS-1$
158:
159: fExpand = new TextOperationAction(
160: FoldingMessages.getResourceBundle(),
161: "Projection.Expand.", editor, ProjectionViewer.EXPAND, true); //$NON-NLS-1$
162: fExpand
163: .setActionDefinitionId(IFoldingCommandIds.FOLDING_EXPAND);
164: editor.setAction("FoldingExpand", fExpand); //$NON-NLS-1$
165:
166: fCollapse = new TextOperationAction(
167: FoldingMessages.getResourceBundle(),
168: "Projection.Collapse.", editor, ProjectionViewer.COLLAPSE, true); //$NON-NLS-1$
169: fCollapse
170: .setActionDefinitionId(IFoldingCommandIds.FOLDING_COLLAPSE);
171: editor.setAction("FoldingCollapse", fCollapse); //$NON-NLS-1$
172:
173: fRestoreDefaults = new FoldingAction(FoldingMessages
174: .getResourceBundle(), "Projection.Restore.") { //$NON-NLS-1$
175: public void run() {
176: if (editor instanceof JavaEditor) {
177: JavaEditor javaEditor = (JavaEditor) editor;
178: javaEditor.resetProjection();
179: }
180: }
181: };
182: fRestoreDefaults
183: .setActionDefinitionId(IFoldingCommandIds.FOLDING_RESTORE);
184: editor.setAction("FoldingRestore", fRestoreDefaults); //$NON-NLS-1$
185:
186: fCollapseMembers = new FoldingAction(FoldingMessages
187: .getResourceBundle(), "Projection.CollapseMembers.") { //$NON-NLS-1$
188: public void run() {
189: if (editor instanceof JavaEditor) {
190: JavaEditor javaEditor = (JavaEditor) editor;
191: javaEditor.collapseMembers();
192: }
193: }
194: };
195: fCollapseMembers
196: .setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_MEMBERS);
197: editor.setAction("FoldingCollapseMembers", fCollapseMembers); //$NON-NLS-1$
198:
199: fCollapseComments = new FoldingAction(FoldingMessages
200: .getResourceBundle(), "Projection.CollapseComments.") { //$NON-NLS-1$
201: public void run() {
202: if (editor instanceof JavaEditor) {
203: JavaEditor javaEditor = (JavaEditor) editor;
204: javaEditor.collapseComments();
205: }
206: }
207: };
208: fCollapseComments
209: .setActionDefinitionId(IJavaEditorActionDefinitionIds.FOLDING_COLLAPSE_COMMENTS);
210: editor.setAction("FoldingCollapseComments", fCollapseComments); //$NON-NLS-1$
211: }
212:
213: /**
214: * Returns <code>true</code> if the group is enabled.
215: * <pre>
216: * Invariant: isEnabled() <=> fViewer and all actions are != null.
217: * </pre>
218: *
219: * @return <code>true</code> if the group is enabled
220: */
221: protected boolean isEnabled() {
222: return fViewer != null;
223: }
224:
225: /*
226: * @see org.eclipse.ui.actions.ActionGroup#dispose()
227: */
228: public void dispose() {
229: if (isEnabled()) {
230: fViewer.removeProjectionListener(fProjectionListener);
231: fViewer = null;
232: }
233: super .dispose();
234: }
235:
236: /**
237: * Updates the actions.
238: */
239: protected void update() {
240: if (isEnabled()) {
241: fToggle.update();
242: fToggle.setChecked(fViewer.isProjectionMode());
243: fExpand.update();
244: fExpandAll.update();
245: fCollapse.update();
246: fCollapseAll.update();
247: fRestoreDefaults.update();
248: fCollapseMembers.update();
249: fCollapseComments.update();
250: }
251: }
252:
253: /**
254: * Fills the menu with all folding actions.
255: *
256: * @param manager the menu manager for the folding submenu
257: */
258: public void fillMenu(IMenuManager manager) {
259: if (isEnabled()) {
260: update();
261: manager.add(fToggle);
262: manager.add(fExpandAll);
263: manager.add(fExpand);
264: manager.add(fCollapse);
265: manager.add(fCollapseAll);
266: manager.add(fRestoreDefaults);
267: manager.add(fCollapseMembers);
268: manager.add(fCollapseComments);
269: }
270: }
271:
272: /*
273: * @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
274: */
275: public void updateActionBars() {
276: update();
277: }
278: }
|