KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.texteditor;
12
13
14 import java.util.ResourceBundle JavaDoc;
15
16 /**
17  * Skeleton of a standard text editor action. The action is
18  * initially associated with a text editor via the constructor,
19  * but can subsequently be changed using <code>setEditor</code>.
20  * Subclasses must implement the <code>run</code> method and if
21  * required override the <code>update</code> method.
22  * <p>
23  * Subclasses that may modify the editor content should use {@link #canModifyEditor()}
24  * in their <code>update</code> code to check whether updating the editor is most
25  * likely possible (even if it is read-only - this may change for editor contents
26  * that are under version control) and {@link #validateEditorInputState()} before
27  * actually modifying the editor contents.
28  * </p>
29  */

30 public abstract class TextEditorAction extends ResourceAction implements IUpdate {
31
32     /** The action's editor */
33     private ITextEditor fTextEditor;
34
35     /**
36      * Creates and initializes the action for the given text editor. The action
37      * configures its visual representation from the given resource bundle.
38      *
39      * @param bundle the resource bundle
40      * @param prefix a prefix to be prepended to the various resource keys
41      * (described in <code>ResourceAction</code> constructor), or
42      * <code>null</code> if none
43      * @param editor the text editor
44      * @see ResourceAction#ResourceAction(ResourceBundle, String)
45      */

46     protected TextEditorAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
47         super(bundle, prefix);
48         setEditor(editor);
49         update();
50     }
51
52     /**
53      * Creates and initializes the action for the given text editor. The action
54      * configures its visual representation from the given resource bundle.
55      *
56      * @param bundle the resource bundle
57      * @param prefix a prefix to be prepended to the various resource keys
58      * (described in <code>ResourceAction</code> constructor), or
59      * <code>null</code> if none
60      * @param editor the text editor
61      * @param style the style of this action
62      * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
63      * @since 3.0
64      */

65     protected TextEditorAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, int style) {
66         super(bundle, prefix, style);
67         setEditor(editor);
68         update();
69     }
70
71     /**
72      * Returns the action's text editor.
73      *
74      * @return the action's text editor
75      */

76     protected ITextEditor getTextEditor() {
77         return fTextEditor;
78     }
79
80     /**
81      * Retargets this action to the given editor.
82      *
83      * @param editor the new editor, or <code>null</code> if none
84      */

85     public void setEditor(ITextEditor editor) {
86         fTextEditor= editor;
87     }
88
89     /**
90      * Always enables this action if it is connected to a text editor.
91      * If the associated editor is <code>null</code>, the action is disabled.
92      * Subclasses may override.
93      */

94     public void update() {
95         setEnabled(getTextEditor() != null);
96     }
97
98     /**
99      * Checks the editor's modifiable state. Returns <code>true</code> if the editor can be modified,
100      * taking in account the possible editor extensions.
101      *
102      * <p>If the editor implements <code>ITextEditorExtension2</code>,
103      * this method returns {@link ITextEditorExtension2#isEditorInputModifiable()};<br> else if the editor
104      * implements <code>ITextEditorExtension</code>, it returns {@link ITextEditorExtension#isEditorInputReadOnly()};<br>
105      * else, {@link ITextEditor#isEditable()} is returned, or <code>false</code> if the editor is <code>null</code>.</p>
106      *
107      * <p>There is only a difference to {@link #validateEditorInputState()} if the editor implements
108      * <code>ITextEditorExtension2</code>.</p>
109      *
110      * @return <code>true</code> if a modifying action should be enabled, <code>false</code> otherwise
111      * @since 3.0
112      */

113     protected boolean canModifyEditor() {
114         ITextEditor editor= getTextEditor();
115         if (editor instanceof ITextEditorExtension2)
116             return ((ITextEditorExtension2) editor).isEditorInputModifiable();
117         else if (editor instanceof ITextEditorExtension)
118             return !((ITextEditorExtension) editor).isEditorInputReadOnly();
119         else if (editor != null)
120             return editor.isEditable();
121         else
122             return false;
123     }
124
125     /**
126      * Checks and validates the editor's modifiable state. Returns <code>true</code> if an action
127      * can proceed modifying the editor's input, <code>false</code> if it should not.
128      *
129      * <p>If the editor implements <code>ITextEditorExtension2</code>,
130      * this method returns {@link ITextEditorExtension2#validateEditorInputState()};<br> else if the editor
131      * implements <code>ITextEditorExtension</code>, it returns {@link ITextEditorExtension#isEditorInputReadOnly()};<br>
132      * else, {@link ITextEditor#isEditable()} is returned, or <code>false</code> if the editor is <code>null</code>.</p>
133      *
134      * <p>There is only a difference to {@link #canModifyEditor()} if the editor implements
135      * <code>ITextEditorExtension2</code>.</p>
136      *
137      * @return <code>true</code> if a modifying action can proceed to modify the underlying document, <code>false</code> otherwise
138      * @since 3.0
139      */

140     protected boolean validateEditorInputState() {
141         ITextEditor editor= getTextEditor();
142         if (editor instanceof ITextEditorExtension2)
143             return ((ITextEditorExtension2) editor).validateEditorInputState();
144         else if (editor instanceof ITextEditorExtension)
145             return !((ITextEditorExtension) editor).isEditorInputReadOnly();
146         else if (editor != null)
147             return editor.isEditable();
148         else
149             return false;
150     }
151 }
152
Popular Tags