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.ui.texteditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.swt.custom.BusyIndicator;
015: import org.eclipse.swt.widgets.Display;
016: import org.eclipse.swt.widgets.Shell;
017:
018: import org.eclipse.jface.text.ITextOperationTarget;
019:
020: import org.eclipse.ui.IWorkbenchPartSite;
021:
022: /**
023: * An action which gets a text operation target from its text editor.
024: * <p>
025: * The action is initially associated with a text editor via the constructor,
026: * but can subsequently be changed using <code>setEditor</code>.</p>
027: * <p>
028: * If this class is used as is, it works by asking the text editor for its
029: * text operation target adapter (using <code>getAdapter(ITextOperationTarget.class)</code>.
030: * The action runs this operation with the pre-configured opcode.</p>
031: */
032: public final class TextOperationAction extends TextEditorAction {
033:
034: /** The text operation code */
035: private int fOperationCode = -1;
036: /** The text operation target */
037: private ITextOperationTarget fOperationTarget;
038: /**
039: * Indicates whether this action can be executed on read only editors
040: * @since 2.0
041: */
042: private boolean fRunsOnReadOnly = false;
043:
044: /**
045: * Flag to prevent running twice trough {@link #update()}
046: * when creating this action.
047: * @since 3.2
048: */
049: private boolean fAllowUpdate = false;
050:
051: /**
052: * Creates and initializes the action for the given text editor and operation
053: * code. The action configures its visual representation from the given resource
054: * bundle. The action works by asking the text editor at the time for its
055: * text operation target adapter (using
056: * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
057: * operation with the given opcode.
058: *
059: * @param bundle the resource bundle
060: * @param prefix a prefix to be prepended to the various resource keys
061: * (described in <code>ResourceAction</code> constructor), or
062: * <code>null</code> if none
063: * @param editor the text editor
064: * @param operationCode the operation code
065: * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
066: */
067: public TextOperationAction(ResourceBundle bundle, String prefix,
068: ITextEditor editor, int operationCode) {
069: super (bundle, prefix, editor);
070: fOperationCode = operationCode;
071: fAllowUpdate = true;
072: update();
073: }
074:
075: /**
076: * Creates and initializes the action for the given text editor and operation
077: * code. The action configures its visual representation from the given resource
078: * bundle. The action works by asking the text editor at the time for its
079: * text operation target adapter (using
080: * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
081: * operation with the given opcode.
082: *
083: * @param bundle the resource bundle
084: * @param prefix a prefix to be prepended to the various resource keys
085: * (described in <code>ResourceAction</code> constructor), or
086: * <code>null</code> if none
087: * @param editor the text editor
088: * @param operationCode the operation code
089: * @param runsOnReadOnly <code>true</code> if action can be executed on read-only files
090: *
091: * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
092: * @since 2.0
093: */
094: public TextOperationAction(ResourceBundle bundle, String prefix,
095: ITextEditor editor, int operationCode,
096: boolean runsOnReadOnly) {
097: super (bundle, prefix, editor);
098: fOperationCode = operationCode;
099: fRunsOnReadOnly = runsOnReadOnly;
100: fAllowUpdate = true;
101: update();
102: }
103:
104: /**
105: * The <code>TextOperationAction</code> implementation of this
106: * <code>IAction</code> method runs the operation with the current
107: * operation code.
108: */
109: public void run() {
110: if (fOperationCode == -1 || fOperationTarget == null)
111: return;
112:
113: ITextEditor editor = getTextEditor();
114: if (editor == null)
115: return;
116:
117: if (!fRunsOnReadOnly && !validateEditorInputState())
118: return;
119:
120: Display display = null;
121:
122: IWorkbenchPartSite site = editor.getSite();
123: Shell shell = site.getShell();
124: if (shell != null && !shell.isDisposed())
125: display = shell.getDisplay();
126:
127: BusyIndicator.showWhile(display, new Runnable() {
128: public void run() {
129: fOperationTarget.doOperation(fOperationCode);
130: }
131: });
132: }
133:
134: /**
135: * The <code>TextOperationAction</code> implementation of this
136: * <code>IUpdate</code> method discovers the operation through the current
137: * editor's <code>ITextOperationTarget</code> adapter, and sets the
138: * enabled state accordingly.
139: */
140: public void update() {
141: if (!fAllowUpdate)
142: return;
143:
144: super .update();
145:
146: if (!fRunsOnReadOnly && !canModifyEditor()) {
147: setEnabled(false);
148: return;
149: }
150:
151: ITextEditor editor = getTextEditor();
152: if (fOperationTarget == null && editor != null
153: && fOperationCode != -1)
154: fOperationTarget = (ITextOperationTarget) editor
155: .getAdapter(ITextOperationTarget.class);
156:
157: boolean isEnabled = (fOperationTarget != null && fOperationTarget
158: .canDoOperation(fOperationCode));
159: setEnabled(isEnabled);
160: }
161:
162: /*
163: * @see TextEditorAction#setEditor(ITextEditor)
164: */
165: public void setEditor(ITextEditor editor) {
166: super.setEditor(editor);
167: fOperationTarget = null;
168: }
169: }
|