01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
11:
12: import java.util.LinkedList;
13: import java.util.List;
14: import java.util.ResourceBundle;
15:
16: import org.eclipse.jface.text.BadLocationException;
17: import org.eclipse.jface.text.BadPartitioningException;
18: import org.eclipse.jface.text.IDocumentExtension3;
19: import org.eclipse.jface.text.ITextSelection;
20: import org.eclipse.jface.text.ITypedRegion;
21:
22: import org.eclipse.ui.texteditor.ITextEditor;
23:
24: import org.eclipse.jdt.ui.text.IJavaPartitions;
25:
26: /**
27: * Action that removes the enclosing comment marks from a Java block comment.
28: *
29: * @since 3.0
30: */
31: public class RemoveBlockCommentAction extends BlockCommentAction {
32:
33: /**
34: * Creates a new instance.
35: *
36: * @param bundle the resource bundle
37: * @param prefix a prefix to be prepended to the various resource keys
38: * (described in <code>ResourceAction</code> constructor), or
39: * <code>null</code> if none
40: * @param editor the text editor
41: */
42: public RemoveBlockCommentAction(ResourceBundle bundle,
43: String prefix, ITextEditor editor) {
44: super (bundle, prefix, editor);
45: }
46:
47: /*
48: * @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)
49: */
50: protected void runInternal(ITextSelection selection,
51: IDocumentExtension3 docExtension, Edit.EditFactory factory)
52: throws BadPartitioningException, BadLocationException {
53: List edits = new LinkedList();
54: int tokenLength = getCommentStart().length();
55:
56: int offset = selection.getOffset();
57: int endOffset = offset + selection.getLength();
58:
59: ITypedRegion partition = docExtension.getPartition(
60: IJavaPartitions.JAVA_PARTITIONING, offset, false);
61: int partOffset = partition.getOffset();
62: int partEndOffset = partOffset + partition.getLength();
63:
64: while (partEndOffset < endOffset) {
65:
66: if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
67: edits.add(factory.createEdit(partOffset, tokenLength,
68: "")); //$NON-NLS-1$
69: edits.add(factory.createEdit(partEndOffset
70: - tokenLength, tokenLength, "")); //$NON-NLS-1$
71: }
72:
73: partition = docExtension.getPartition(
74: IJavaPartitions.JAVA_PARTITIONING, partEndOffset,
75: false);
76: partOffset = partition.getOffset();
77: partEndOffset = partOffset + partition.getLength();
78: }
79:
80: if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
81: edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
82: edits.add(factory.createEdit(partEndOffset - tokenLength,
83: tokenLength, "")); //$NON-NLS-1$
84: }
85:
86: executeEdits(edits);
87: }
88:
89: /*
90: * @see org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
91: */
92: protected boolean isValidSelection(ITextSelection selection) {
93: return selection != null && !selection.isEmpty();
94: }
95:
96: }
|