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: /**
015: * Skeleton of a standard text editor action. The action is
016: * initially associated with a text editor via the constructor,
017: * but can subsequently be changed using <code>setEditor</code>.
018: * Subclasses must implement the <code>run</code> method and if
019: * required override the <code>update</code> method.
020: * <p>
021: * Subclasses that may modify the editor content should use {@link #canModifyEditor()}
022: * in their <code>update</code> code to check whether updating the editor is most
023: * likely possible (even if it is read-only - this may change for editor contents
024: * that are under version control) and {@link #validateEditorInputState()} before
025: * actually modifying the editor contents.
026: * </p>
027: */
028: public abstract class TextEditorAction extends ResourceAction implements
029: IUpdate {
030:
031: /** The action's editor */
032: private ITextEditor fTextEditor;
033:
034: /**
035: * Creates and initializes the action for the given text editor. The action
036: * configures its visual representation from the given resource bundle.
037: *
038: * @param bundle the resource bundle
039: * @param prefix a prefix to be prepended to the various resource keys
040: * (described in <code>ResourceAction</code> constructor), or
041: * <code>null</code> if none
042: * @param editor the text editor
043: * @see ResourceAction#ResourceAction(ResourceBundle, String)
044: */
045: protected TextEditorAction(ResourceBundle bundle, String prefix,
046: ITextEditor editor) {
047: super (bundle, prefix);
048: setEditor(editor);
049: update();
050: }
051:
052: /**
053: * Creates and initializes the action for the given text editor. The action
054: * configures its visual representation from the given resource bundle.
055: *
056: * @param bundle the resource bundle
057: * @param prefix a prefix to be prepended to the various resource keys
058: * (described in <code>ResourceAction</code> constructor), or
059: * <code>null</code> if none
060: * @param editor the text editor
061: * @param style the style of this action
062: * @see ResourceAction#ResourceAction(ResourceBundle, String, int)
063: * @since 3.0
064: */
065: protected TextEditorAction(ResourceBundle bundle, String prefix,
066: ITextEditor editor, int style) {
067: super (bundle, prefix, style);
068: setEditor(editor);
069: update();
070: }
071:
072: /**
073: * Returns the action's text editor.
074: *
075: * @return the action's text editor
076: */
077: protected ITextEditor getTextEditor() {
078: return fTextEditor;
079: }
080:
081: /**
082: * Retargets this action to the given editor.
083: *
084: * @param editor the new editor, or <code>null</code> if none
085: */
086: public void setEditor(ITextEditor editor) {
087: fTextEditor = editor;
088: }
089:
090: /**
091: * Always enables this action if it is connected to a text editor.
092: * If the associated editor is <code>null</code>, the action is disabled.
093: * Subclasses may override.
094: */
095: public void update() {
096: setEnabled(getTextEditor() != null);
097: }
098:
099: /**
100: * Checks the editor's modifiable state. Returns <code>true</code> if the editor can be modified,
101: * taking in account the possible editor extensions.
102: *
103: * <p>If the editor implements <code>ITextEditorExtension2</code>,
104: * this method returns {@link ITextEditorExtension2#isEditorInputModifiable()};<br> else if the editor
105: * implements <code>ITextEditorExtension</code>, it returns {@link ITextEditorExtension#isEditorInputReadOnly()};<br>
106: * else, {@link ITextEditor#isEditable()} is returned, or <code>false</code> if the editor is <code>null</code>.</p>
107: *
108: * <p>There is only a difference to {@link #validateEditorInputState()} if the editor implements
109: * <code>ITextEditorExtension2</code>.</p>
110: *
111: * @return <code>true</code> if a modifying action should be enabled, <code>false</code> otherwise
112: * @since 3.0
113: */
114: protected boolean canModifyEditor() {
115: ITextEditor editor = getTextEditor();
116: if (editor instanceof ITextEditorExtension2)
117: return ((ITextEditorExtension2) editor)
118: .isEditorInputModifiable();
119: else if (editor instanceof ITextEditorExtension)
120: return !((ITextEditorExtension) editor)
121: .isEditorInputReadOnly();
122: else if (editor != null)
123: return editor.isEditable();
124: else
125: return false;
126: }
127:
128: /**
129: * Checks and validates the editor's modifiable state. Returns <code>true</code> if an action
130: * can proceed modifying the editor's input, <code>false</code> if it should not.
131: *
132: * <p>If the editor implements <code>ITextEditorExtension2</code>,
133: * this method returns {@link ITextEditorExtension2#validateEditorInputState()};<br> else if the editor
134: * implements <code>ITextEditorExtension</code>, it returns {@link ITextEditorExtension#isEditorInputReadOnly()};<br>
135: * else, {@link ITextEditor#isEditable()} is returned, or <code>false</code> if the editor is <code>null</code>.</p>
136: *
137: * <p>There is only a difference to {@link #canModifyEditor()} if the editor implements
138: * <code>ITextEditorExtension2</code>.</p>
139: *
140: * @return <code>true</code> if a modifying action can proceed to modify the underlying document, <code>false</code> otherwise
141: * @since 3.0
142: */
143: protected boolean validateEditorInputState() {
144: ITextEditor editor = getTextEditor();
145: if (editor instanceof ITextEditorExtension2)
146: return ((ITextEditorExtension2) editor)
147: .validateEditorInputState();
148: else if (editor instanceof ITextEditorExtension)
149: return !((ITextEditorExtension) editor)
150: .isEditorInputReadOnly();
151: else if (editor != null)
152: return editor.isEditable();
153: else
154: return false;
155: }
156: }
|