~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to languages/engine/src/org/netbeans/modules/languages/features/CodeCommentAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.languages.features;
 
43
 
 
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;
 
60
 
 
61
/**
 
62
 *
 
63
 * @author Dan
 
64
 */
 
65
public class CodeCommentAction extends CommentAction {
 
66
 
 
67
    public CodeCommentAction() {
 
68
        super(""); // NOI18N
 
69
    }
 
70
 
 
71
    public void actionPerformed(ActionEvent evt, JTextComponent target) {
 
72
        if (target != null) {
 
73
            if (!target.isEditable() || !target.isEnabled()) {
 
74
                target.getToolkit().beep();
 
75
                return;
 
76
            }
 
77
            BaseDocument doc = (BaseDocument)target.getDocument();
 
78
            Caret caret = target.getCaret();
 
79
            TokenHierarchy th = TokenHierarchy.get (doc);
 
80
            if (th == null) {
 
81
                return;
 
82
            }
 
83
            TokenSequence ts = th.tokenSequence();
 
84
            try {
 
85
                if (caret.isSelectionVisible()) {
 
86
                    int startPos = Utilities.getRowStart(doc, target.getSelectionStart());
 
87
                    int endPos = target.getSelectionEnd();
 
88
                    doc.atomicLock();
 
89
                    try {
 
90
                        if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) {
 
91
                            endPos--;
 
92
                        }
 
93
                        int lineCnt = Utilities.getRowCount(doc, startPos, endPos);
 
94
                        List mimeTypes = new ArrayList(lineCnt);
 
95
                        int pos = startPos;
 
96
                        for (int x = lineCnt ; x > 0; x--) {
 
97
                            mimeTypes.add(getRealMimeType(ts, pos));
 
98
                            pos = Utilities.getRowStart(doc, pos, 1);
 
99
                        }
 
100
                        
 
101
                        pos = startPos;
 
102
                        for (Iterator iter = mimeTypes.iterator(); iter.hasNext(); ) {
 
103
                            modifyLine(doc, (String)iter.next(), pos);
 
104
                            pos = Utilities.getRowStart(doc, pos, 1);
 
105
                        }
 
106
                    } finally {
 
107
                        doc.atomicUnlock();
 
108
                    }
 
109
                } else { // selection not visible
 
110
                    int pos = Utilities.getRowStart(doc, target.getSelectionStart());
 
111
                    String mt = getRealMimeType(ts, pos);
 
112
                    doc.atomicLock();
 
113
                    try {
 
114
                        modifyLine(doc, mt, pos);
 
115
                    } finally {
 
116
                        doc.atomicUnlock();
 
117
                    }
 
118
                }
 
119
            } catch (BadLocationException e) {
 
120
                target.getToolkit().beep();
 
121
            }
 
122
        }
 
123
    }
 
124
    
 
125
    private String getRealMimeType(TokenSequence ts, int offset) {
 
126
        while (true) {
 
127
            ts.move(offset);
 
128
            if (!ts.moveNext())
 
129
                break;
 
130
            offset = ts.offset();
 
131
            TokenSequence ts2 = ts.embedded();
 
132
            if (ts2 == null) break;
 
133
            ts = ts2;
 
134
        }
 
135
        return ts.language().mimeType();
 
136
    }
 
137
    
 
138
    private void modifyLine(BaseDocument doc, String mimeType, int offset) throws BadLocationException {
 
139
        Feature feature = null;
 
140
        try {
 
141
            Language language = LanguagesManager.getDefault().getLanguage(mimeType);
 
142
            feature = language.getFeature(Language.COMMENT_LINE);
 
143
        } catch (LanguageDefinitionNotFoundException e) {
 
144
        }
 
145
        if (feature != null) {
 
146
            String prefix = (String) feature.getValue("prefix"); // NOI18N
 
147
            if (prefix == null) {
 
148
                return;
 
149
            }
 
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);
 
154
            }
 
155
            doc.insertString(offset, prefix, null);
 
156
        }
 
157
    }
 
158
    
 
159
}