2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.languages.features;
44
import java.awt.event.ActionEvent;
45
import java.util.ArrayList;
46
import java.util.Iterator;
47
import java.util.List;
48
import javax.swing.text.BadLocationException;
49
import javax.swing.text.Caret;
50
import javax.swing.text.JTextComponent;
51
import org.netbeans.api.languages.LanguageDefinitionNotFoundException;
52
import org.netbeans.api.lexer.TokenHierarchy;
53
import org.netbeans.api.lexer.TokenSequence;
54
import org.netbeans.editor.BaseDocument;
55
import org.netbeans.editor.Utilities;
56
import org.netbeans.editor.ext.ExtKit.CommentAction;
57
import org.netbeans.modules.languages.Feature;
58
import org.netbeans.modules.languages.Language;
59
import org.netbeans.modules.languages.LanguagesManager;
65
public class CodeCommentAction extends CommentAction {
67
public CodeCommentAction() {
71
public void actionPerformed(ActionEvent evt, JTextComponent target) {
73
if (!target.isEditable() || !target.isEnabled()) {
74
target.getToolkit().beep();
77
BaseDocument doc = (BaseDocument)target.getDocument();
78
Caret caret = target.getCaret();
79
TokenHierarchy th = TokenHierarchy.get (doc);
83
TokenSequence ts = th.tokenSequence();
85
if (caret.isSelectionVisible()) {
86
int startPos = Utilities.getRowStart(doc, target.getSelectionStart());
87
int endPos = target.getSelectionEnd();
90
if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) {
93
int lineCnt = Utilities.getRowCount(doc, startPos, endPos);
94
List mimeTypes = new ArrayList(lineCnt);
96
for (int x = lineCnt ; x > 0; x--) {
97
mimeTypes.add(getRealMimeType(ts, pos));
98
pos = Utilities.getRowStart(doc, pos, 1);
102
for (Iterator iter = mimeTypes.iterator(); iter.hasNext(); ) {
103
modifyLine(doc, (String)iter.next(), pos);
104
pos = Utilities.getRowStart(doc, pos, 1);
109
} else { // selection not visible
110
int pos = Utilities.getRowStart(doc, target.getSelectionStart());
111
String mt = getRealMimeType(ts, pos);
114
modifyLine(doc, mt, pos);
119
} catch (BadLocationException e) {
120
target.getToolkit().beep();
125
private String getRealMimeType(TokenSequence ts, int offset) {
130
offset = ts.offset();
131
TokenSequence ts2 = ts.embedded();
132
if (ts2 == null) break;
135
return ts.language().mimeType();
138
private void modifyLine(BaseDocument doc, String mimeType, int offset) throws BadLocationException {
139
Feature feature = null;
141
Language language = LanguagesManager.getDefault().getLanguage(mimeType);
142
feature = language.getFeature(Language.COMMENT_LINE);
143
} catch (LanguageDefinitionNotFoundException e) {
145
if (feature != null) {
146
String prefix = (String) feature.getValue("prefix"); // NOI18N
147
if (prefix == null) {
150
String suffix = (String) feature.getValue("suffix"); // NOI18N
151
if (suffix != null) {
152
int end = Utilities.getRowEnd(doc, offset);
153
doc.insertString(end, suffix, null);
155
doc.insertString(offset, prefix, null);