2 * Copyright (c) 2003-2004 Christopher Lenz and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial API and implementation
11 * $Id: CommentAction.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.editor;
16 import java.util.ResourceBundle;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRewriteTarget;
21 import org.eclipse.jface.text.ITextSelection;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.texteditor.IDocumentProvider;
26 import org.eclipse.ui.texteditor.ITextEditor;
27 import org.eclipse.ui.texteditor.ITextEditorExtension2;
28 import org.eclipse.ui.texteditor.TextEditorAction;
31 * (Heavily inspired by the AddBlockComment class in JDT-UI)
33 public class CommentAction extends TextEditorAction {
35 // Constructors ------------------------------------------------------------
40 * @param bundle the resource bundle
41 * @param prefix a prefix to be prepended to the various resource keys
42 * (described in <code>ResourceAction</code> constructor), or
43 * <code>null</code> if none
44 * @param editor the text editor
46 public CommentAction(ResourceBundle bundle, String prefix,
48 super(bundle, prefix, editor);
51 // TextEditorAction Implementation -----------------------------------------
54 * @see org.eclipse.jface.action.Action#run()
60 ITextEditor editor = getTextEditor();
61 if ((editor == null) || !ensureEditable(editor)) {
64 ITextSelection selection = getCurrentSelection();
65 if (!validSelection(selection)) {
68 IDocumentProvider docProvider = editor.getDocumentProvider();
69 IEditorInput input = editor.getEditorInput();
70 if (docProvider == null || input == null) {
73 IDocument document = docProvider.getDocument(input);
74 if (document == null) {
77 IRewriteTarget target = (IRewriteTarget)
78 editor.getAdapter(IRewriteTarget.class);
80 target.beginCompoundChange();
83 int offset = selection.getOffset();
84 String start = "/*"; //$NON-NLS-1$
85 document.replace(offset, 0, start);
86 document.replace(offset + selection.getLength() + start.length(), 0,
88 } catch (BadLocationException e) {
92 target.endCompoundChange();
98 * @see org.eclipse.ui.texteditor.TextEditorAction#update()
100 public void update() {
103 if (!validSelection(getCurrentSelection())) {
109 // Private Methods ---------------------------------------------------------
112 * Ensures that the editor is modifyable. If the editor is an instance of
113 * <code>ITextEditorExtension2</code>, its
114 * <code>validateEditorInputState</code> method is called, otherwise, the
115 * result of <code>isEditable</code> is returned.
117 * @param editor the editor to be checked
118 * @return <code>true</code> if the editor is editable, <code>false</code>
121 private boolean ensureEditable(ITextEditor editor) {
122 if (editor instanceof ITextEditorExtension2) {
123 ITextEditorExtension2 extension = (ITextEditorExtension2) editor;
124 return extension.validateEditorInputState();
126 return editor.isEditable();
130 * Returns the editor's selection, or <code>null</code> if no selection can
131 * be obtained or the editor is <code>null</code>.
133 * @return the selection of the action's editor, or <code>null</code>
135 private ITextSelection getCurrentSelection() {
136 ITextEditor editor = getTextEditor();
137 if (editor != null) {
138 ISelectionProvider provider = editor.getSelectionProvider();
139 if (provider != null) {
140 ISelection selection = provider.getSelection();
141 if (selection instanceof ITextSelection) {
142 return (ITextSelection) selection;
150 * Checks whether <code>selection</code> is valid, i.e. neither
151 * <code>null</code> or empty.
153 * @param selection the selection to check
154 * @return <code>true</code> if the selection is valid, <code>false</code>
157 private boolean validSelection(ITextSelection selection) {
158 if (selection != null) {
159 return (!selection.isEmpty() && (selection.getLength() > 0));