001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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: * Action for shifting code to the right or left by one indentation level.
024: * @since 2.0
025: */
026: public class ShiftAction extends TextEditorAction implements
027: IReadOnlyDependent {
028:
029: /** The text operation code */
030: private int fOperationCode = -1;
031: /** The text operation target */
032: private ITextOperationTarget fOperationTarget;
033:
034: /**
035: * Creates and initializes the action for the given text editor and operation
036: * code. The action configures its visual representation from the given resource
037: * bundle. The action works by asking the text editor at the time for its
038: * text operation target adapter (using
039: * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
040: * operation with the given opcode.
041: *
042: * @param bundle the resource bundle
043: * @param prefix a prefix to be prepended to the various resource keys
044: * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
045: * @param editor the text editor
046: * @param operationCode the operation code
047: * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
048: */
049: public ShiftAction(ResourceBundle bundle, String prefix,
050: ITextEditor editor, int operationCode) {
051: super (bundle, prefix, editor);
052: fOperationCode = operationCode;
053: update();
054: }
055:
056: /**
057: * The <code>TextOperationAction</code> implementation of this
058: * <code>IAction</code> method runs the operation with the current
059: * operation code.
060: */
061: public void run() {
062: if (fOperationCode == -1 || fOperationTarget == null)
063: return;
064:
065: ITextEditor editor = getTextEditor();
066: if (editor == null)
067: return;
068:
069: if (!validateEditorInputState())
070: return;
071:
072: Display display = null;
073:
074: IWorkbenchPartSite site = editor.getSite();
075: Shell shell = site.getShell();
076: if (shell != null && !shell.isDisposed())
077: display = shell.getDisplay();
078:
079: BusyIndicator.showWhile(display, new Runnable() {
080: public void run() {
081: fOperationTarget.doOperation(fOperationCode);
082: }
083: });
084: }
085:
086: /*
087: * @see IUpdate#update()
088: */
089: public void update() {
090: super .update();
091: if (!isEnabled())
092: return;
093:
094: if (!canModifyEditor()) {
095: setEnabled(false);
096: return;
097: }
098:
099: ITextEditor editor = getTextEditor();
100: if (fOperationTarget == null && editor != null
101: && fOperationCode != -1)
102: fOperationTarget = (ITextOperationTarget) editor
103: .getAdapter(ITextOperationTarget.class);
104:
105: }
106:
107: /**
108: * Enablement when tab key is pressed - the current selection has to be cover multiple lines.
109: *
110: * @since 3.0
111: */
112: protected void updateForTab() {
113: super .update();
114:
115: if (isEnabled()) {
116: if (!canModifyEditor()) {
117: setEnabled(false);
118: return;
119: }
120:
121: ITextEditor editor = getTextEditor();
122: if (fOperationTarget == null && editor != null
123: && fOperationCode != -1)
124: fOperationTarget = (ITextOperationTarget) editor
125: .getAdapter(ITextOperationTarget.class);
126:
127: boolean isEnabled = (fOperationTarget != null && fOperationTarget
128: .canDoOperation(fOperationCode));
129: setEnabled(isEnabled);
130: }
131:
132: }
133:
134: /*
135: * @see TextEditorAction#setEditor(ITextEditor)
136: */
137: public void setEditor(ITextEditor editor) {
138: super .setEditor(editor);
139: fOperationTarget = null;
140: }
141:
142: /*
143: * @see IReadOnlyDependent#isEnabled(boolean)
144: */
145: public boolean isEnabled(boolean isWritable) {
146:
147: if (!isWritable)
148: return false;
149:
150: /*
151: * Note that this implementation still honors the result returned by canDoOperation.
152: * I.e. if the viewer is set to read-only, this method still returns false.
153: * It covers the case in which the viewer is also writable.
154: *
155: */
156: ITextEditor editor = getTextEditor();
157: if (fOperationTarget == null && editor != null
158: && fOperationCode != -1)
159: fOperationTarget = (ITextOperationTarget) editor
160: .getAdapter(ITextOperationTarget.class);
161:
162: return (fOperationTarget != null && fOperationTarget
163: .canDoOperation(fOperationCode));
164: }
165: }
|