* Added browser like links (Ctrl+Mouseclick on identifier; same as F3 shortcut)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / actions / AddBlockCommentAction.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 import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
19
20 import org.eclipse.jface.text.Assert;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.BadPartitioningException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IDocumentExtension3;
25 import org.eclipse.jface.text.ITextSelection;
26 import org.eclipse.jface.text.ITypedRegion;
27 import org.eclipse.ui.texteditor.ITextEditor;
28
29 /**
30  * Action that encloses the editor's current selection with Java block comment terminators (<code>&#47;&#42;</code> and
31  * <code>&#42;&#47;</code>).
32  * 
33  * @since 3.0
34  */ 
35 public class AddBlockCommentAction extends BlockCommentAction {
36
37   /**
38    * Creates a new instance.
39    * 
40    * @param bundle
41    *          the resource bundle
42    * @param prefix
43    *          a prefix to be prepended to the various resource keys (described in <code>ResourceAction</code> constructor), or
44    *          <code>null</code> if none
45    * @param editor
46    *          the text editor
47    */
48   public AddBlockCommentAction(ResourceBundle bundle, String 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,
54    *      org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
55    */
56   protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory)
57       throws BadLocationException, BadPartitioningException {
58     int selectionOffset = selection.getOffset();
59     int selectionEndOffset = selectionOffset + selection.getLength();
60     List edits = new LinkedList();
61     ITypedRegion partition = docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, selectionOffset, false);
62
63     handleFirstPartition(partition, edits, factory, selectionOffset);
64
65     while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
66       partition = handleInteriorPartition(partition, edits, factory, docExtension);
67     }
68
69     handleLastPartition(partition, edits, factory, selectionEndOffset);
70
71     executeEdits(edits);
72   }
73
74   /**
75    * Handle the first partition of the selected text.
76    * 
77    * @param partition
78    * @param edits
79    * @param factory
80    * @param offset
81    */
82   private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset)
83       throws BadLocationException {
84
85     int partOffset = partition.getOffset();
86     String partType = partition.getType();
87
88     Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
89
90     // first partition: mark start of comment
91     if (partType == IDocument.DEFAULT_CONTENT_TYPE ||
92         partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE) {
93       // Java code: right where selection starts
94       edits.add(factory.createEdit(offset, 0, getCommentStart()));
95     } else if (isSpecialPartition(partType)) {
96       // special types: include the entire partition
97       edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
98     } // javadoc: no mark, will only start after comment
99
100   }
101
102   /**
103    * Handles the end of the given partition and the start of the next partition, which is returned.
104    *  
105    * @param partition
106    * @param edits
107    * @param factory
108    * @param docExtension
109    * @return
110    * @throws BadLocationException
111    * @throws BadPartitioningException
112    */
113   private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory,
114       IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
115
116     // end of previous partition
117     String partType = partition.getType();
118     int partEndOffset = partition.getOffset() + partition.getLength();
119     int tokenLength = getCommentStart().length();
120
121     boolean wasJavadoc = false; // true if the previous partition is javadoc
122
123     if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
124
125       wasJavadoc = true;
126
127     } else if (partType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
128
129       // already in a comment - remove ending mark
130       edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
131
132     }
133
134     // advance to next partition
135     partition = docExtension.getPartition(IPHPPartitions.PHP_PARTITIONING, partEndOffset, false);
136     partType = partition.getType();
137
138     // start of next partition
139     if (wasJavadoc) {
140
141       // if previous was javadoc, and the current one is not, then add block comment start
142       if (partType == IDocument.DEFAULT_CONTENT_TYPE ||  
143           partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE ||
144           isSpecialPartition(partType)) {
145         edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
146       }
147
148     } else { // !wasJavadoc
149
150       if (partType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
151         // if next is javadoc, end block comment before
152         edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
153       } else if (partType == IPHPPartitions.PHP_MULTILINE_COMMENT) {
154         // already in a comment - remove startToken
155         edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
156       }
157     }
158
159     return partition;
160   }
161
162   /**
163    * Handles the end of the last partition.
164    * 
165    * @param partition
166    * @param edits
167    * @param factory
168    * @param endOffset
169    */
170   private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset)
171       throws BadLocationException {
172
173     String partType = partition.getType();
174
175     if (partType == IDocument.DEFAULT_CONTENT_TYPE  ||
176         partType == PHPDocumentPartitioner.PHP_SCRIPT_CODE) {
177       // normal java: end comment where selection ends
178       edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
179     } else if (isSpecialPartition(partType)) {
180       // special types: consume entire partition
181       edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
182     }
183
184   }
185
186   /**
187    * Returns whether <code>partType</code> is special, i.e. a Java <code>String</code>,<code>Character</code>, or
188    * <code>Line End Comment</code> partition.
189    * 
190    * @param partType
191    *          the partition type to check
192    * @return <code>true</code> if <code>partType</code> is special, <code>false</code> otherwise
193    */
194   private boolean isSpecialPartition(String partType) {
195     //          return partType == IJavaPartitions.JAVA_CHARACTER
196     return partType == IPHPPartitions.PHP_STRING_DQ || partType == IPHPPartitions.PHP_STRING_SQ
197         || partType == IPHPPartitions.PHP_SINGLELINE_COMMENT;
198   }
199
200   /*
201    * @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
202    */
203   protected boolean isValidSelection(ITextSelection selection) {
204     return selection != null && !selection.isEmpty() && selection.getLength() > 0;
205   }
206
207 }