本文整理汇总了Java中javax.swing.JEditorPane.getCaret方法的典型用法代码示例。如果您正苦于以下问题:Java JEditorPane.getCaret方法的具体用法?Java JEditorPane.getCaret怎么用?Java JEditorPane.getCaret使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JEditorPane
的用法示例。
在下文中一共展示了JEditorPane.getCaret方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSelectedIdentifier
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/**
* Returns identifier currently selected in editor or <code>null</code>.
*
* @return identifier currently selected in editor or <code>null</code>
*/
@Override
public String getSelectedIdentifier () {
JEditorPane ep = contextDispatcher.getCurrentEditor ();
if (ep == null) {
return null;
}
Caret caret = ep.getCaret();
if (caret == null) {
// No caret => no selected text
return null;
}
String s = ep.getSelectedText ();
if (s == null) {
return null;
}
if (Utilities.isJavaIdentifier (s)) {
return s;
}
return null;
}
示例2: setEditable
import javax.swing.JEditorPane; //导入方法依赖的package包/类
public void setEditable(boolean b) {
JEditorPane editor = editor();
if (editor.isEditable() == b) return;
editor.setEditable(b);
if (b) {
if (lastBgColor != null) editor.setBackground(lastBgColor);
if (lastCaret != null) editor.setCaret(lastCaret);
} else {
lastBgColor = editor.getBackground();
lastCaret = editor.getCaret();
editor.setBackground(disabledBgColor);
editor.setCaret(nullCaret);
}
}
示例3: getCurrentLineNumber
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/**
* Get the line number of the caret in the current editor.
* @return the line number or <code>-1</code> when there is no current editor.
*/
public int getCurrentLineNumber() {
EditorCookie e = getCurrentEditorCookie ();
if (e == null) return -1;
JEditorPane ep = getCurrentEditor ();
if (ep == null) return -1;
StyledDocument d = e.getDocument ();
if (d == null) return -1;
Caret caret = ep.getCaret ();
if (caret == null) return -1;
int ln = NbDocument.findLineNumber (
d,
caret.getDot ()
);
return ln + 1;
}
示例4: getMostRecentLineNumber
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/**
* Get the line number of the caret in the most recent editor.
* This returns the current line number in the current editor if there's one,
* or a line number of the caret in the editor, that was most recently active.
* @return the line number or <code>-1</code> when there was no recent active editor.
*/
public int getMostRecentLineNumber() {
EditorCookie e = getMostRecentEditorCookie ();
if (e == null) return -1;
JEditorPane ep = getMostRecentEditor ();
if (ep == null) return -1;
StyledDocument d = e.getDocument ();
if (d == null) return -1;
Caret caret = ep.getCaret ();
if (caret == null) return -1;
int ln = NbDocument.findLineNumber (
d,
caret.getDot ()
);
return ln + 1;
}
示例5: getMostRecentLine
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/**
* Get the line of the caret in the most recent editor.
* This returns the current line in the current editor if there's one,
* or a line of the caret in the editor, that was most recently active.
* @return the line or <code>null</code> when there was no recent active editor.
*/
public Line getMostRecentLine() {
EditorCookie e = getMostRecentEditorCookie ();
if (e == null) return null;
JEditorPane ep = getMostRecentEditor ();
if (ep == null) return null;
StyledDocument d = e.getDocument ();
if (d == null) return null;
Caret caret = ep.getCaret ();
if (caret == null) return null;
int lineNumber = NbDocument.findLineNumber(d, caret.getDot());
Line.Set lineSet = e.getLineSet();
try {
return lineSet.getCurrent(lineNumber);
} catch (IndexOutOfBoundsException ex) {
return null;
}
}
示例6: deleteChar
import javax.swing.JEditorPane; //导入方法依赖的package包/类
protected void deleteChar(String original, String expected) throws Exception {
String source = original;
String reformatted = expected;
Formatter formatter = getFormatter(null);
int sourcePos = source.indexOf('^');
assertNotNull(sourcePos);
source = source.substring(0, sourcePos) + source.substring(sourcePos+1);
int reformattedPos = reformatted.indexOf('^');
assertNotNull(reformattedPos);
reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);
JEditorPane ta = getPane(source);
Caret caret = ta.getCaret();
caret.setDot(sourcePos);
BaseDocument doc = (BaseDocument) ta.getDocument();
if (formatter != null) {
configureIndenters(doc, formatter, true);
}
setupDocumentIndentation(doc, null);
runKitAction(ta, DefaultEditorKit.deletePrevCharAction, "\n");
String formatted = doc.getText(0, doc.getLength());
assertEquals(reformatted, formatted);
if (reformattedPos != -1) {
assertEquals(reformattedPos, caret.getDot());
}
}
示例7: deleteWord
import javax.swing.JEditorPane; //导入方法依赖的package包/类
protected void deleteWord(String original, String expected) throws Exception {
String source = original;
String reformatted = expected;
Formatter formatter = getFormatter(null);
int sourcePos = source.indexOf('^');
assertNotNull(sourcePos);
source = source.substring(0, sourcePos) + source.substring(sourcePos+1);
int reformattedPos = reformatted.indexOf('^');
assertNotNull(reformattedPos);
reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);
JEditorPane ta = getPane(source);
Caret caret = ta.getCaret();
caret.setDot(sourcePos);
BaseDocument doc = (BaseDocument) ta.getDocument();
if (formatter != null) {
configureIndenters(doc, formatter, true);
}
setupDocumentIndentation(doc, null);
runKitAction(ta, BaseKit.removePreviousWordAction, "\n");
String formatted = doc.getText(0, doc.getLength());
assertEquals(reformatted, formatted);
if (reformattedPos != -1) {
assertEquals(reformattedPos, caret.getDot());
}
}
示例8: insertNewline
import javax.swing.JEditorPane; //导入方法依赖的package包/类
public void insertNewline(String source, String reformatted, IndentPrefs preferences) throws Exception {
int sourcePos = source.indexOf('^');
assertNotNull(sourcePos);
source = source.substring(0, sourcePos) + source.substring(sourcePos+1);
Formatter formatter = getFormatter(null);
int reformattedPos = reformatted.indexOf('^');
assertNotNull(reformattedPos);
reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);
JEditorPane ta = getPane(source);
Caret caret = ta.getCaret();
caret.setDot(sourcePos);
BaseDocument doc = (BaseDocument) ta.getDocument();
if (formatter != null) {
configureIndenters(doc, formatter, true);
}
setupDocumentIndentation(doc, preferences);
runKitAction(ta, DefaultEditorKit.insertBreakAction, "\n");
String formatted = doc.getText(0, doc.getLength());
assertEquals(reformatted, formatted);
if (reformattedPos != -1) {
assertEquals(reformattedPos, caret.getDot());
}
}
示例9: getCurrentOffset
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/**
* Returns number of line currently selected in editor or <code>-1</code>.
*
* @return number of line currently selected in editor or <code>-1</code>
*/
public int getCurrentOffset () {
JEditorPane ep = contextDispatcher.getCurrentEditor();
if (ep == null) {
return -1;
}
Caret caret = ep.getCaret ();
if (caret == null) {
return -1;
}
return caret.getDot();
}
示例10: getCurrentElement
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/** throws IllegalComponentStateException when can not return the data in AWT. */
private String getCurrentElement(FileObject fo, JEditorPane ep,
final ElementKind kind, final String[] elementSignaturePtr)
throws java.awt.IllegalComponentStateException {
if (fo == null) {
return null;
}
final int currentOffset;
final String selectedIdentifier;
if (ep != null) {
String s;
Caret caret = ep.getCaret();
if (caret == null) {
s = null;
currentOffset = 0;
} else {
s = ep.getSelectedText ();
currentOffset = ep.getCaretPosition();
if (ep.getSelectionStart() > currentOffset || ep.getSelectionEnd() < currentOffset) {
s = null; // caret outside of the selection
}
}
if (s != null && Utilities.isJavaIdentifier (s)) {
selectedIdentifier = s;
} else {
selectedIdentifier = null;
}
} else {
selectedIdentifier = null;
currentOffset = 0;
}
return EditorContextSupport.getCurrentElement(fo, currentOffset, selectedIdentifier, kind, elementSignaturePtr);
}
示例11: getApplicableFileObject
import javax.swing.JEditorPane; //导入方法依赖的package包/类
private FileObject getApplicableFileObject(int[] caretPosHolder) {
if (!EventQueue.isDispatchThread()) {
// Unsafe to ask for an editor pane from a random thread.
// E.g. org.netbeans.lib.uihandler.LogRecords.write asking for getName().
Collection<? extends FileObject> dobs = Utilities.actionsGlobalContext().lookupAll(FileObject.class);
return dobs.size() == 1 ? dobs.iterator().next() : null;
}
// TODO: Use the new editor library to compute this:
// JTextComponent pane = EditorRegistry.lastFocusedComponent();
TopComponent comp = TopComponent.getRegistry().getActivated();
if(comp == null) {
return null;
}
Node[] nodes = comp.getActivatedNodes();
if (nodes != null && nodes.length == 1) {
if (comp instanceof CloneableEditorSupport.Pane) { //OK. We have an editor
EditorCookie ec = nodes[0].getLookup().lookup(EditorCookie.class);
if (ec != null) {
JEditorPane editorPane = NbDocument.findRecentEditorPane(ec);
if (editorPane != null) {
if (editorPane.getCaret() != null) {
caretPosHolder[0] = editorPane.getCaret().getDot();
}
Document document = editorPane.getDocument();
return Source.create(document).getFileObject();
}
}
} else {
return UICommonUtils.getFileObjectFromNode(nodes[0]);
}
}
return null;
}
示例12: getRecentColumn
import javax.swing.JEditorPane; //导入方法依赖的package包/类
public static int getRecentColumn() {
JEditorPane mostRecentEditor = EditorContextDispatcher.getDefault().getMostRecentEditor();
if (mostRecentEditor != null) {
Caret caret = mostRecentEditor.getCaret();
if (caret != null) {
int offset = caret.getDot();
try {
int rs = javax.swing.text.Utilities.getRowStart(mostRecentEditor, offset);
return offset - rs;
} catch (BadLocationException blex) {}
}
}
return 0;
}
示例13: install
import javax.swing.JEditorPane; //导入方法依赖的package包/类
/**
* Called when the kit is being installed into
* a JEditorPane.
*
* @param c the JEditorPane
*/
public void install(JEditorPane c) {
c.addCaretListener(inputAttributeUpdater);
c.addPropertyChangeListener(inputAttributeUpdater);
Caret caret = c.getCaret();
if (caret != null) {
inputAttributeUpdater.updateInputAttributes
(caret.getDot(), caret.getMark(), c);
}
}
示例14: writeExternal
import javax.swing.JEditorPane; //导入方法依赖的package包/类
@Override
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
// Save environent if support is non-null.
// XXX #13685: When support is null, the tc will be discarded
// after deserialization.
out.writeObject((support != null) ? support.cesEnv() : null);
// #16461 Caret could be null?!,
// hot fix - making it robust for that case.
int pos = 0;
// 19559 Even pane could be null! Better solution would be put
// writeReplace method in place also, but it is a API change. For
// the time be just robust here.
JEditorPane p = pane;
if (p != null) {
Caret caret = p.getCaret();
if (caret != null) {
pos = caret.getDot();
} else {
if (p instanceof QuietEditorPane) {
int lastPos = ((QuietEditorPane) p).getLastPosition();
if (lastPos == -1) {
Logger.getLogger(CloneableEditor.class.getName()).log(Level.WARNING, null,
new java.lang.IllegalStateException("Pane=" +
p +
"was not initialized yet!"));
} else {
pos = lastPos;
}
} else {
Document doc = ((support != null) ? support.getDocument() : null);
// Relevant only if document is non-null?!
if (doc != null) {
Logger.getLogger(CloneableEditor.class.getName()).log(Level.WARNING, null,
new java.lang.IllegalStateException("Caret is null in editor pane=" +
p +
"\nsupport=" +
support +
"\ndoc=" +
doc));
}
}
}
}
out.writeObject(new Integer(pos));
out.writeBoolean(getLookup() == support.getLookup());
}
示例15: insertChar
import javax.swing.JEditorPane; //导入方法依赖的package包/类
protected void insertChar(String original, char insertText, String expected, String selection, boolean codeTemplateMode) throws Exception {
String source = original;
String reformatted = expected;
Formatter formatter = getFormatter(null);
int sourcePos = source.indexOf('^');
assertNotNull(sourcePos);
source = source.substring(0, sourcePos) + source.substring(sourcePos+1);
int reformattedPos = reformatted.indexOf('^');
assertNotNull(reformattedPos);
reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);
JEditorPane ta = getPane(source);
Caret caret = ta.getCaret();
caret.setDot(sourcePos);
if (selection != null) {
int start = original.indexOf(selection);
assertTrue(start != -1);
assertTrue("Ambiguous selection - multiple occurrences of selection string",
original.indexOf(selection, start+1) == -1);
ta.setSelectionStart(start);
ta.setSelectionEnd(start+selection.length());
assertEquals(selection, ta.getSelectedText());
}
BaseDocument doc = (BaseDocument) ta.getDocument();
if (codeTemplateMode) {
// Copied from editor/codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java
String EDITING_TEMPLATE_DOC_PROPERTY = "processing-code-template"; // NOI18N
doc.putProperty(EDITING_TEMPLATE_DOC_PROPERTY, Boolean.TRUE);
}
if (formatter != null) {
configureIndenters(doc, formatter, true);
}
setupDocumentIndentation(doc, null);
runKitAction(ta, DefaultEditorKit.defaultKeyTypedAction, ""+insertText);
String formatted = doc.getText(0, doc.getLength());
assertEquals(reformatted, formatted);
if (reformattedPos != -1) {
assertEquals(reformattedPos, caret.getDot());
}
}