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