KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * An action which gets a text operation target from its text editor.
29  * <p>
30  * The action is initially associated with a text editor via the constructor,
31  * but can subsequently be changed using <code>setEditor</code>.</p>
32  * <p>
33  * If this class is used as is, it works by asking the text editor for its
34  * text operation target adapter (using <code>getAdapter(ITextOperationTarget.class)</code>.
35  * The action runs this operation with the pre-configured opcode.</p>
36  */

37 public final class TextOperationAction extends TextEditorAction {
38
39     /** The text operation code */
40     private int fOperationCode= -1;
41     /** The text operation target */
42     private ITextOperationTarget fOperationTarget;
43     /**
44      * Indicates whether this action can be executed on read only editors
45      * @since 2.0
46      */

47     private boolean fRunsOnReadOnly= false;
48
49     /**
50      * Flag to prevent running twice trough {@link #update()}
51      * when creating this action.
52      * @since 3.2
53      */

54     private boolean fAllowUpdate= false;
55
56     /**
57      * Creates and initializes the action for the given text editor and operation
58      * code. The action configures its visual representation from the given resource
59      * bundle. The action works by asking the text editor at the time for its
60      * text operation target adapter (using
61      * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
62      * operation with the given opcode.
63      *
64      * @param bundle the resource bundle
65      * @param prefix a prefix to be prepended to the various resource keys
66      * (described in <code>ResourceAction</code> constructor), or
67      * <code>null</code> if none
68      * @param editor the text editor
69      * @param operationCode the operation code
70      * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
71      */

72     public TextOperationAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, int operationCode) {
73         super(bundle, prefix, editor);
74         fOperationCode= operationCode;
75         fAllowUpdate= true;
76         update();
77     }
78
79     /**
80      * Creates and initializes the action for the given text editor and operation
81      * code. The action configures its visual representation from the given resource
82      * bundle. The action works by asking the text editor at the time for its
83      * text operation target adapter (using
84      * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
85      * operation with the given opcode.
86      *
87      * @param bundle the resource bundle
88      * @param prefix a prefix to be prepended to the various resource keys
89      * (described in <code>ResourceAction</code> constructor), or
90      * <code>null</code> if none
91      * @param editor the text editor
92      * @param operationCode the operation code
93      * @param runsOnReadOnly <code>true</code> if action can be executed on read-only files
94      *
95      * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
96      * @since 2.0
97      */

98     public TextOperationAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, int operationCode, boolean runsOnReadOnly) {
99         super(bundle, prefix, editor);
100         fOperationCode= operationCode;
101         fRunsOnReadOnly= runsOnReadOnly;
102         fAllowUpdate= true;
103         update();
104     }
105
106     /**
107      * The <code>TextOperationAction</code> implementation of this
108      * <code>IAction</code> method runs the operation with the current
109      * operation code.
110      */

111     public void run() {
112         if (fOperationCode == -1 || fOperationTarget == null)
113             return;
114
115         ITextEditor editor= getTextEditor();
116         if (editor == null)
117             return;
118
119         if (!fRunsOnReadOnly && !validateEditorInputState())
120             return;
121
122         Display display= null;
123
124         IWorkbenchPartSite site= editor.getSite();
125         Shell shell= site.getShell();
126         if (shell != null && !shell.isDisposed())
127             display= shell.getDisplay();
128
129         BusyIndicator.showWhile(display, new Runnable JavaDoc() {
130             public void run() {
131                 fOperationTarget.doOperation(fOperationCode);
132             }
133         });
134     }
135
136     /**
137      * The <code>TextOperationAction</code> implementation of this
138      * <code>IUpdate</code> method discovers the operation through the current
139      * editor's <code>ITextOperationTarget</code> adapter, and sets the
140      * enabled state accordingly.
141      */

142     public void update() {
143         if (!fAllowUpdate)
144             return;
145         
146         super.update();
147
148         if (!fRunsOnReadOnly && !canModifyEditor()) {
149             setEnabled(false);
150             return;
151         }
152
153         ITextEditor editor= getTextEditor();
154         if (fOperationTarget == null && editor!= null && fOperationCode != -1)
155             fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
156
157         boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
158         setEnabled(isEnabled);
159     }
160
161     /*
162      * @see TextEditorAction#setEditor(ITextEditor)
163      */

164     public void setEditor(ITextEditor editor) {
165         super.setEditor(editor);
166         fOperationTarget= null;
167     }
168 }
169
Popular Tags