KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > AddBlockCommentAction


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.jdt.internal.ui.actions;
12
13 import java.util.LinkedList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.BadPartitioningException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.text.ITypedRegion;
25
26 import org.eclipse.ui.texteditor.ITextEditor;
27
28 import org.eclipse.jdt.ui.text.IJavaPartitions;
29
30
31 /**
32  * Action that encloses the editor's current selection with Java block comment terminators
33  * (<code>&#47;&#42;</code> and <code>&#42;&#47;</code>).
34  *
35  * @since 3.0
36  */

37 public class AddBlockCommentAction extends BlockCommentAction {
38
39     /**
40      * Creates a new instance.
41      *
42      * @param bundle the resource bundle
43      * @param prefix a prefix to be prepended to the various resource keys
44      * (described in <code>ResourceAction</code> constructor), or
45      * <code>null</code> if none
46      * @param editor the text editor
47      */

48     public AddBlockCommentAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
49         super(bundle, prefix, editor);
50     }
51     
52     /*
53      * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
54      */

55     protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException {
56         int selectionOffset= selection.getOffset();
57         int selectionEndOffset= selectionOffset + selection.getLength();
58         List JavaDoc edits= new LinkedList JavaDoc();
59         ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, selectionOffset, false);
60
61         handleFirstPartition(partition, edits, factory, selectionOffset);
62
63         while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
64             partition= handleInteriorPartition(partition, edits, factory, docExtension);
65         }
66         
67         handleLastPartition(partition, edits, factory, selectionEndOffset);
68         
69         executeEdits(edits);
70     }
71
72     /**
73      * Handle the partition under the start offset of the selection.
74      *
75      * @param partition the partition under the start of the selection
76      * @param edits the list of edits to later execute
77      * @param factory the factory for edits
78      * @param offset the start of the selection, which must lie inside
79      * <code>partition</code>
80      */

81     private void handleFirstPartition(ITypedRegion partition, List JavaDoc edits, Edit.EditFactory factory, int offset) throws BadLocationException {
82         
83         int partOffset= partition.getOffset();
84         String JavaDoc partType= partition.getType();
85         
86         Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
87

88         // first partition: mark start of comment
89
if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
90             // Java code: right where selection starts
91
edits.add(factory.createEdit(offset, 0, getCommentStart()));
92         } else if (isSpecialPartition(partType)) {
93             // special types: include the entire partition
94
edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
95         } // javadoc: no mark, will only start after comment
96

97     }
98
99     /**
100      * Handles partition boundaries within the selection. The end of the current
101      * partition and the start of the next partition are examined for whether
102      * they contain comment tokens that interfere with the created comment.
103      * <p>
104      * Comment tokens are removed from interior multi-line comments. Javadoc
105      * comments are left as is; instead, multi-line comment tokens are inserted
106      * before and after Javadoc partitions to ensure that the entire selected
107      * area is commented.
108      * </p>
109      * <p>
110      * The next partition is returned.
111      * </p>
112      *
113      * @param partition the current partition
114      * @param edits the list of edits to add to
115      * @param factory the edit factory
116      * @param docExtension the document to get the partitions from
117      * @return the next partition after the current
118      * @throws BadLocationException if accessing the document fails - this can
119      * only happen if the document gets modified concurrently
120      * @throws BadPartitioningException if the document does not have a Java
121      * partitioning
122      */

123     private ITypedRegion handleInteriorPartition(ITypedRegion partition, List JavaDoc edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
124
125         // end of previous partition
126
String JavaDoc partType= partition.getType();
127         int partEndOffset= partition.getOffset() + partition.getLength();
128         int tokenLength= getCommentStart().length();
129         
130         boolean wasJavadoc= false; // true if the previous partition is javadoc
131

132         if (partType == IJavaPartitions.JAVA_DOC) {
133             
134             wasJavadoc= true;
135             
136         } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
137             
138             // already in a comment - remove ending mark
139
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
140

141         }
142
143         // advance to next partition
144
partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
145         partType= partition.getType();
146
147         // start of next partition
148
if (wasJavadoc) {
149             
150             // if previous was javadoc, and the current one is not a comment,
151
// then add a block comment start
152
if (partType == IDocument.DEFAULT_CONTENT_TYPE
153                     || isSpecialPartition(partType)) {
154                 edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
155             }
156             
157         } else { // !wasJavadoc
158

159             if (partType == IJavaPartitions.JAVA_DOC) {
160                 // if next is javadoc, end block comment before
161
edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
162             } else if (partType == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
163                 // already in a comment - remove startToken
164
edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
165
}
166         }
167         
168         return partition;
169     }
170
171     /**
172      * Handles the partition under the end of the selection. For normal java
173      * code, the comment end token is inserted at the selection end; if the
174      * selection ends inside a special (i.e. string, character, line comment)
175      * partition, the entire partition is included inside the comment.
176      *
177      * @param partition the partition under the selection end offset
178      * @param edits the list of edits to add to
179      * @param factory the edit factory
180      * @param endOffset the end offset of the selection
181      */

182     private void handleLastPartition(ITypedRegion partition, List JavaDoc edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
183
184         String JavaDoc partType= partition.getType();
185         
186         if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
187             // normal java: end comment where selection ends
188
edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
189         } else if (isSpecialPartition(partType)) {
190             // special types: consume entire partition
191
edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
192         }
193         
194     }
195
196     /**
197      * Returns whether <code>partType</code> is special, i.e. a Java
198      * <code>String</code>,<code>Character</code>, or
199      * <code>Line End Comment</code> partition.
200      *
201      * @param partType the partition type to check
202      * @return <code>true</code> if <code>partType</code> is special,
203      * <code>false</code> otherwise
204      */

205     private boolean isSpecialPartition(String JavaDoc partType) {
206         return partType == IJavaPartitions.JAVA_CHARACTER
207                 || partType == IJavaPartitions.JAVA_STRING
208                 || partType == IJavaPartitions.JAVA_SINGLE_LINE_COMMENT;
209     }
210
211     /*
212      * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
213      */

214     protected boolean isValidSelection(ITextSelection selection) {
215         return selection != null && !selection.isEmpty() && selection.getLength() > 0;
216     }
217
218 }
219
Popular Tags