intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / editor / CommentAction.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CommentAction.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.editor;
15
16 import java.util.ResourceBundle;
17
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;
29
30 /**
31  * (Heavily inspired by the AddBlockComment class in JDT-UI)
32  */
33 public class CommentAction extends TextEditorAction {
34
35         // Constructors ------------------------------------------------------------
36
37         /**
38          * Constructor.
39          * 
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
45          */
46         public CommentAction(ResourceBundle bundle, String prefix,
47                 ITextEditor editor) {
48                 super(bundle, prefix, editor);
49         }
50         
51         // TextEditorAction Implementation -----------------------------------------
52
53         /**
54          * @see org.eclipse.jface.action.Action#run()
55          */
56         public void run() {
57                 if (!isEnabled()) {
58                         return;
59                 }
60                 ITextEditor editor = getTextEditor();
61                 if ((editor == null) || !ensureEditable(editor)) {
62                         return;
63                 }
64                 ITextSelection selection = getCurrentSelection();
65                 if (!validSelection(selection)) {
66                         return;
67                 }
68                 IDocumentProvider docProvider = editor.getDocumentProvider();
69                 IEditorInput input = editor.getEditorInput();
70                 if (docProvider == null || input == null) {
71                         return;
72                 }
73                 IDocument document = docProvider.getDocument(input);
74                 if (document == null) {
75                         return;
76                 }
77                 IRewriteTarget target = (IRewriteTarget) 
78                         editor.getAdapter(IRewriteTarget.class);
79                 if (target != null) {
80                         target.beginCompoundChange();
81                 }
82                 try {
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,
87                                 "*/"); //$NON-NLS-1$
88                 } catch (BadLocationException e) {
89                         // ignore
90                 } finally {
91                         if (target != null) {
92                                 target.endCompoundChange();
93                         }
94                 }
95         }
96         
97         /**
98          * @see org.eclipse.ui.texteditor.TextEditorAction#update()
99          */
100         public void update() {
101                 super.update();
102                 if (isEnabled()) {
103                         if (!validSelection(getCurrentSelection())) {
104                                 setEnabled(false);
105                         }
106                 }
107         }
108         
109         // Private Methods ---------------------------------------------------------
110
111         /**
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.
116          * 
117          * @param editor the editor to be checked
118          * @return <code>true</code> if the editor is editable, <code>false</code> 
119          *         otherwise
120          */
121         private boolean ensureEditable(ITextEditor editor) {
122                 if (editor instanceof ITextEditorExtension2) {
123                         ITextEditorExtension2 extension = (ITextEditorExtension2) editor;
124                         return extension.validateEditorInputState();
125                 }
126                 return editor.isEditable();
127         }
128
129         /**
130          * Returns the editor's selection, or <code>null</code> if no selection can 
131          * be obtained or the editor is <code>null</code>.
132          * 
133          * @return the selection of the action's editor, or <code>null</code>
134          */
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;
143                                 }
144                         }
145                 }
146                 return null;
147         }
148
149         /**
150          * Checks whether <code>selection</code> is valid, i.e. neither 
151          * <code>null</code> or empty.
152          * 
153          * @param selection the selection to check
154          * @return <code>true</code> if the selection is valid, <code>false</code> 
155          *         otherwise
156          */
157         private boolean validSelection(ITextSelection selection) {
158                 if (selection != null) {
159                         return (!selection.isEmpty() && (selection.getLength() > 0));
160                 }
161                 return false;
162         }
163
164 }