1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.ResourceBundle;
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;
23 import org.eclipse.ui.texteditor.ITextEditor;
25 import org.eclipse.jdt.ui.text.IJavaPartitions;
29 * Action that removes the enclosing comment marks from a Java block comment.
33 public class RemoveBlockCommentAction extends BlockCommentAction {
36 * Creates a new instance.
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
44 public RemoveBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
45 super(bundle, prefix, editor);
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)
52 protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
53 List<Edit> edits= new LinkedList<Edit>();
54 int tokenLength= getCommentStart().length();
56 int offset= selection.getOffset();
57 int endOffset= offset + selection.getLength();
59 ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, offset, false);
60 int partOffset= partition.getOffset();
61 int partEndOffset= partOffset + partition.getLength();
63 while (partEndOffset < endOffset) {
65 if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
66 edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
67 edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
70 partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
71 partOffset= partition.getOffset();
72 partEndOffset= partOffset + partition.getLength();
75 if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
76 edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
77 edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
84 * @see org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
87 protected boolean isValidSelection(ITextSelection selection) {
88 return selection != null && !selection.isEmpty();