KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ShiftAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12
13 package org.eclipse.ui.texteditor;
14
15
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.swt.custom.BusyIndicator;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.text.ITextOperationTarget;
23
24 import org.eclipse.ui.IWorkbenchPartSite;
25
26
27
28 /**
29  * Action for shifting code to the right or left by one indentation level.
30  * @since 2.0
31  */

32 public class ShiftAction extends TextEditorAction implements IReadOnlyDependent {
33
34     /** The text operation code */
35     private int fOperationCode= -1;
36     /** The text operation target */
37     private ITextOperationTarget fOperationTarget;
38
39     /**
40      * Creates and initializes the action for the given text editor and operation
41      * code. The action configures its visual representation from the given resource
42      * bundle. The action works by asking the text editor at the time for its
43      * text operation target adapter (using
44      * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
45      * operation with the given opcode.
46      *
47      * @param bundle the resource bundle
48      * @param prefix a prefix to be prepended to the various resource keys
49      * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
50      * @param editor the text editor
51      * @param operationCode the operation code
52      * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
53      */

54     public ShiftAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, int operationCode) {
55         super(bundle, prefix, editor);
56         fOperationCode= operationCode;
57         update();
58     }
59
60     /**
61      * The <code>TextOperationAction</code> implementation of this
62      * <code>IAction</code> method runs the operation with the current
63      * operation code.
64      */

65     public void run() {
66         if (fOperationCode == -1 || fOperationTarget == null)
67             return;
68
69         ITextEditor editor= getTextEditor();
70         if (editor == null)
71             return;
72
73         if (!validateEditorInputState())
74             return;
75
76         Display display= null;
77
78         IWorkbenchPartSite site= editor.getSite();
79         Shell shell= site.getShell();
80         if (shell != null && !shell.isDisposed())
81             display= shell.getDisplay();
82
83         BusyIndicator.showWhile(display, new Runnable JavaDoc() {
84             public void run() {
85                 fOperationTarget.doOperation(fOperationCode);
86             }
87         });
88     }
89
90     /*
91      * @see IUpdate#update()
92      */

93     public void update() {
94         super.update();
95         if (!isEnabled())
96             return;
97
98         if (!canModifyEditor()) {
99             setEnabled(false);
100             return;
101         }
102
103         ITextEditor editor= getTextEditor();
104         if (fOperationTarget == null && editor != null && fOperationCode != -1)
105             fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
106
107     }
108
109     /**
110      * Enablement when tab key is pressed - the current selection has to be cover multiple lines.
111      *
112      * @since 3.0
113      */

114     protected void updateForTab() {
115         super.update();
116
117         if (isEnabled()) {
118             if (!canModifyEditor()) {
119                 setEnabled(false);
120                 return;
121             }
122
123             ITextEditor editor= getTextEditor();
124             if (fOperationTarget == null && editor != null && fOperationCode != -1)
125                 fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
126
127             boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
128             setEnabled(isEnabled);
129         }
130
131     }
132
133     /*
134      * @see TextEditorAction#setEditor(ITextEditor)
135      */

136     public void setEditor(ITextEditor editor) {
137         super.setEditor(editor);
138         fOperationTarget= null;
139     }
140
141     /*
142      * @see IReadOnlyDependent#isEnabled(boolean)
143      */

144     public boolean isEnabled(boolean isWritable) {
145
146         if (!isWritable)
147             return false;
148
149         /*
150          * Note that this implementation still honors the result returned by canDoOperation.
151          * I.e. if the viewer is set to read-only, this method still returns false.
152          * It covers the case in which the viewer is also writable.
153          *
154          */

155         ITextEditor editor= getTextEditor();
156         if (fOperationTarget == null && editor!= null && fOperationCode != -1)
157             fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
158
159         return (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
160     }
161 }
162
Popular Tags