1) Fixed issue #872.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / actions / RemoveBlockCommentAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation 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  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
12
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.ResourceBundle;
16
17 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.BadPartitioningException;
21 import org.eclipse.jface.text.IDocumentExtension3;
22 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.jface.text.ITypedRegion;
24 import org.eclipse.ui.texteditor.ITextEditor;
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
37          *            the resource bundle
38          * @param prefix
39          *            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
43          *            the text editor
44          */
45         public RemoveBlockCommentAction(ResourceBundle bundle, String prefix,
46                         ITextEditor editor) {
47                 super(bundle, prefix, editor);
48         }
49
50         /*
51          * @see net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection,
52          *      org.eclipse.jface.text.IDocumentExtension3,
53          *      net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction.Edit.EditFactory)
54          */
55         protected void runInternal(ITextSelection selection,
56                         IDocumentExtension3 docExtension, Edit.EditFactory factory)
57                         throws BadPartitioningException, BadLocationException {
58                 List edits = new LinkedList();
59                 int tokenLength = getCommentStart().length();
60
61                 int offset = selection.getOffset();
62                 int endOffset = offset + selection.getLength();
63
64                 ITypedRegion partition = docExtension.getPartition(
65                                 IPHPPartitions.PHP_PARTITIONING, offset, false);
66                 int partOffset = partition.getOffset();
67                 int partEndOffset = partOffset + partition.getLength();
68
69                 while (partEndOffset < endOffset) {
70
71                         if (partition.getType() == IPHPPartitions.PHP_MULTILINE_COMMENT) {
72                                 edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
73                                 edits.add(factory.createEdit(partEndOffset - tokenLength,
74                                                 tokenLength, "")); //$NON-NLS-1$
75                         }
76
77                         partition = docExtension.getPartition(
78                                         IPHPPartitions.PHP_PARTITIONING, partEndOffset, false);
79                         partOffset = partition.getOffset();
80                         partEndOffset = partOffset + partition.getLength();
81                 }
82
83                 if (partition.getType() == IPHPPartitions.PHP_MULTILINE_COMMENT) {
84                         edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
85                         edits.add(factory.createEdit(partEndOffset - tokenLength,
86                                         tokenLength, "")); //$NON-NLS-1$
87                 }
88
89                 executeEdits(edits);
90         }
91
92         /*
93          * @see net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
94          */
95         protected boolean isValidSelection(ITextSelection selection) {
96                 return selection != null && !selection.isEmpty();
97         }
98
99 }