KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jface.text.BadLocationException;
18 import org.eclipse.jface.text.BadPartitioningException;
19 import org.eclipse.jface.text.IDocumentExtension3;
20 import org.eclipse.jface.text.ITextSelection;
21 import org.eclipse.jface.text.ITypedRegion;
22
23 import org.eclipse.ui.texteditor.ITextEditor;
24
25 import org.eclipse.jdt.ui.text.IJavaPartitions;
26
27
28 /**
29  * Action that removes the enclosing comment marks from a Java block comment.
30  *
31  * @since 3.0
32  */

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

44     public RemoveBlockCommentAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
45         super(bundle, prefix, editor);
46     }
47     
48     /*
49      * @see org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction.Edit.EditFactory)
50      */

51     protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
52         List JavaDoc edits= new LinkedList JavaDoc();
53         int tokenLength= getCommentStart().length();
54         
55         int offset= selection.getOffset();
56         int endOffset= offset + selection.getLength();
57
58         ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, offset, false);
59         int partOffset= partition.getOffset();
60         int partEndOffset= partOffset + partition.getLength();
61         
62         while (partEndOffset < endOffset) {
63             
64             if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
65                 edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
66
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
67
}
68             
69             partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
70             partOffset= partition.getOffset();
71             partEndOffset= partOffset + partition.getLength();
72         }
73
74         if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
75             edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
76
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
77
}
78
79         executeEdits(edits);
80     }
81
82     /*
83      * @see org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
84      */

85     protected boolean isValidSelection(ITextSelection selection) {
86         return selection != null && !selection.isEmpty();
87     }
88
89 }
90
Popular Tags