Line 0
Link Here
|
|
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
16 |
*/ |
17 |
package org.netbeans.modules.java.editor.codegen; |
18 |
|
19 |
import java.awt.event.ActionEvent; |
20 |
import javax.swing.text.BadLocationException; |
21 |
import javax.swing.text.Caret; |
22 |
import javax.swing.text.JTextComponent; |
23 |
import org.netbeans.editor.BaseAction; |
24 |
import org.netbeans.editor.BaseDocument; |
25 |
import org.netbeans.editor.BaseKit; |
26 |
import org.netbeans.editor.Formatter; |
27 |
import org.netbeans.editor.Utilities; |
28 |
import org.openide.util.Exceptions; |
29 |
|
30 |
/** |
31 |
* Action which inserts an appropriate character at line-end without moving |
32 |
* the caret. |
33 |
* |
34 |
* @author Tim Boudreau |
35 |
*/ |
36 |
public final class InsertSemicolonAction extends BaseAction { |
37 |
private final boolean withNewline; |
38 |
private final char what; |
39 |
|
40 |
protected InsertSemicolonAction (String name, char what, boolean withNewline) { |
41 |
super(name); |
42 |
this.withNewline = withNewline; |
43 |
this.what = what; |
44 |
} |
45 |
|
46 |
public InsertSemicolonAction(String name, boolean withNewline) { |
47 |
this (name, ';', withNewline); |
48 |
} |
49 |
|
50 |
public InsertSemicolonAction(boolean withNewLine) { |
51 |
this (withNewLine ? "complete-line-newline" : "complete-line", ';', |
52 |
withNewLine); |
53 |
} |
54 |
|
55 |
@Override |
56 |
public void actionPerformed(ActionEvent evt, final JTextComponent target) { |
57 |
if (!target.isEditable() || !target.isEnabled()) { |
58 |
target.getToolkit().beep(); |
59 |
return; |
60 |
} |
61 |
final BaseDocument doc = (BaseDocument) target.getDocument(); |
62 |
final class R implements Runnable { |
63 |
public void run() { |
64 |
Caret caret = target.getCaret(); |
65 |
int dotpos = caret.getDot(); |
66 |
Formatter formatter = doc.getFormatter(); |
67 |
formatter.indentLock(); |
68 |
try { |
69 |
int eoloffset = Utilities.getRowEnd(target, dotpos); |
70 |
doc.insertString(eoloffset, "" + what, null); |
71 |
if (withNewline) { |
72 |
//This is code from the editor module, but it is |
73 |
//a pretty strange way to do this: |
74 |
doc.insertString(dotpos, "-", null); //NOI18N |
75 |
doc.remove(dotpos, 1); |
76 |
int eolDot = Utilities.getRowEnd(target, caret.getDot()); |
77 |
int newDotPos = formatter.indentNewLine(doc, eolDot); |
78 |
caret.setDot(newDotPos); |
79 |
} |
80 |
} catch (BadLocationException ex) { |
81 |
Exceptions.printStackTrace(ex); |
82 |
} finally { |
83 |
formatter.indentUnlock(); |
84 |
} |
85 |
} |
86 |
}; |
87 |
doc.runAtomicAsUser(new R()); |
88 |
} |
89 |
} |