本文整理匯總了Java中javax.swing.JEditorPane.getCaretPosition方法的典型用法代碼示例。如果您正苦於以下問題:Java JEditorPane.getCaretPosition方法的具體用法?Java JEditorPane.getCaretPosition怎麽用?Java JEditorPane.getCaretPosition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JEditorPane
的用法示例。
在下文中一共展示了JEditorPane.getCaretPosition方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: resolveRoot
import javax.swing.JEditorPane; //導入方法依賴的package包/類
public static void resolveRoot(final Lookup lookup, final boolean searchFromBase, final boolean isCallerGraph, final Task<Call> rootCallback) {
JavaSource js = null;
RootResolver resolver = null;
EditorCookie ec = lookup.lookup(EditorCookie.class);
if (ec != null/*RefactoringActionsProvider.isFromEditor(ec)*/) {
JEditorPane openedPane = NbDocument.findRecentEditorPane(ec);
Document doc = ec.getDocument();
js = JavaSource.forDocument(doc);
resolver = new RootResolver(openedPane.getCaretPosition(), isCallerGraph, searchFromBase);
}
// else {
// XXX resolve Node.class
// }
postResolveRoot(js, resolver, rootCallback);
}
示例2: invokeInstantRename
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private void invokeInstantRename(final Lookup lookup, final EditorCookie ec) {
try {
JEditorPane target = ec.getOpenedPanes()[0];
final int caret = target.getCaretPosition();
String ident = Utilities.getIdentifier(Utilities.getDocument(target), caret);
if (ident == null) {
Utilities.setStatusBoldText(target, WARN_CannotPerformHere());
return;
}
DataObject od = (DataObject) target.getDocument().getProperty(Document.StreamDescriptionProperty);
JavaSource js = od != null ? JavaSource.forFileObject(od.getPrimaryFile()) : null;
if (js == null) {
Utilities.setStatusBoldText(target, WARN_CannotPerformHere());
return;
}
InstantRefactoringUI ui = InstantRefactoringUIImpl.create(js, caret);
if (ui != null) {
if (ui.getRegions().isEmpty() || ui.getKeyStroke() == null) {
doFullRename(lookup);
} else {
doInstantRename(target, js, caret, ui);
}
} else {
Utilities.setStatusBoldText(target, WARN_CannotPerformHere());
}
} catch (BadLocationException e) {
Exceptions.printStackTrace(e);
}
}
示例3: getCurrentClassDeclaration
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/** @return declared class name
*/
public String getCurrentClassDeclaration() {
FileObject fo = contextDispatcher.getCurrentFile();
if (fo == null) {
return null;
}
JEditorPane ep = contextDispatcher.getCurrentEditor();
final int currentOffset = (ep == null) ? 0 : ep.getCaretPosition();
//final int currentOffset = org.netbeans.editor.Registry.getMostActiveComponent().getCaretPosition();
return EditorContextSupport.getClassDeclaredAt(fo, currentOffset);
}
示例4: getCurrentMethodDeclaration
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/** @return { "method name", "method signature", "enclosing class name" }
*/
@Override
public String[] getCurrentMethodDeclaration() {
FileObject fo = contextDispatcher.getCurrentFile();
if (fo == null) {
return null;
}
JEditorPane ep = contextDispatcher.getCurrentEditor();
final int currentOffset = (ep == null) ? 0 : ep.getCaretPosition();
return EditorContextSupport.getMethodDeclaredAt(fo, currentOffset);
}
示例5: 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);
}
示例6: debugCaret
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private static StringBuilder debugCaret(StringBuilder sb, JEditorPane pane) throws Exception {
int caretOffset = pane.getCaretPosition();
Document doc = pane.getDocument();
sb.append("caret[").append(caretOffset).append("]sel<");
sb.append(pane.getSelectionStart()).append(',');
sb.append(pane.getSelectionEnd()).append('>');
int startTextOffset = Math.max(0, caretOffset - 2);
int endTextOffset = Math.min(caretOffset + 2, doc.getLength() + 1);
sb.append(" \"");
CharSequenceUtilities.debugText(sb, doc.getText(startTextOffset, caretOffset - startTextOffset));
sb.append('|');
CharSequenceUtilities.debugText(sb, doc.getText(caretOffset, endTextOffset - caretOffset));
sb.append("\"");
return sb;
}
示例7: runInEDT
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private void runInEDT(Snapshot snapshot, Model model, final FileObject file, String mimeType, int caretOffset) {
LOG.log(Level.FINER, "runInEDT(), file: {0}, caret: {1}", new Object[]{file, caretOffset});
if (cancelled) {
LOG.log(Level.FINER, "cancelled");
return;
}
if (caretOffset == -1) {
try {
//dirty workaround
DataObject dobj = DataObject.find(file);
EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
if (ec != null) {
JEditorPane pane = NbDocument.findRecentEditorPane(ec);
if (pane != null) {
caretOffset = pane.getCaretPosition();
}
}
} catch (DataObjectNotFoundException ex) {
//possibly deleted file, give up
return;
}
LOG.log(Level.INFO, "workarounded caret offset: {0}", caretOffset);
}
//find rule corresponding to the offset
Rule rule = findRuleAtOffset(snapshot, model, caretOffset);
//>>hack, remove once the css.lib css grammar gets finalized
if(rule != null && rule.getSelectorsGroup() == null) {
rule = null;
}
//<<hack
if(rule != null) {
//check whether the rule is virtual
if(snapshot.getOriginalOffset(rule.getSelectorsGroup().getStartOffset()) == -1) {
//virtual selector created for html source element with class or id attribute
LOG.log(Level.FINER, "the found rule is virtual, exiting w/o change of the RuleEditor", caretOffset);
return ;
}
}
if (!mimeType.equals("text/css")) {
//if not a css file,
//update the rule editor only if there's a rule in an embedded css code
if (rule == null) {
LOG.log(Level.FINER, "not a css file and rule not found at {0} offset, exiting w/o change of the RuleEditor", caretOffset);
return;
}
}
final CssStylesTC cssStylesTC = (CssStylesTC) WindowManager.getDefault().findTopComponent(CssStylesTC.ID);
if (cssStylesTC == null) {
return;
}
//update the RuleEditor TC name
RuleEditorController controller = cssStylesTC.getRuleEditorController();
LOG.log(Level.FINER, "SourceTask: calling controller.setModel({0})", model);
controller.setModel(model);
if (rule == null) {
controller.setNoRuleState();
} else {
controller.setRule(rule);
}
}
示例8: updateTextInAWT
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private void updateTextInAWT(EditorCookie es, final String in) throws IOException, BadLocationException {
StyledDocument tmpdoc = es.getDocument();
if (tmpdoc == null)
tmpdoc = es.openDocument();
//sample editor position
JEditorPane[] eps = es.getOpenedPanes();
JEditorPane pane = null;
JViewport port = null;
int caretPosition = 0;
Point viewPosition = null;
if (eps != null) {
pane = eps[0];
caretPosition = pane.getCaretPosition();
port = getParentViewport (pane);
if (port != null)
viewPosition = port.getViewPosition();
}
// prepare modification task
final Exception[] taskEx = new Exception[] {null};
final StyledDocument sdoc = tmpdoc;
Runnable task = new Runnable() {
public void run() {
try {
sdoc.remove (0, sdoc.getLength()); // right alternative
// we are at Unicode level
sdoc.insertString (0, in, null);
} catch (Exception iex) {
taskEx[0] = iex;
}
}
};
// perform document modification
org.openide.text.NbDocument.runAtomicAsUser(sdoc, task);
//??? setModified (true);
//restore editor position
if (eps != null) {
try {
pane.setCaretPosition (caretPosition);
} catch (IllegalArgumentException e) {
}
port.setViewPosition (viewPosition);
}
if (taskEx[0]!=null) {
if (taskEx[0] instanceof IOException) {
throw (IOException)taskEx[0];
}
throw new IOException(taskEx[0]);
}
}
示例9: getUpToDate
import javax.swing.JEditorPane; //導入方法依賴的package包/類
@Override
public UpToDateStatus getUpToDate() {
if (model == null) {
return UpToDateStatus.UP_TO_DATE_OK;
}
FileObject fo = NbEditorUtilities.getFileObject(document);
boolean ok = false;
try {
if (fo.isValid()) {
DataObject dobj = DataObject.find(fo);
EditorCookie ed = dobj.getLookup().lookup(EditorCookie.class);
if (ed != null) {
JEditorPane[] panes = ed.getOpenedPanes();
if (panes != null && panes.length > 0) {
//#214527
JEditorPane pane = panes[0];
if (panes.length > 1) {
for (JEditorPane p : panes) {
if (p.isFocusOwner()) {
pane = p;
break;
}
}
}
//TODO this code is called very often apparently.
//we should only run the checks if something changed..
//something means file + selection start + selection end.
final int selectionStart = pane.getSelectionStart();
final int selectionEnd = pane.getSelectionEnd();
final int caretPosition = pane.getCaretPosition();
RP.post(new Runnable() {
@Override
public void run() {
refreshLinkAnnotations(document, model, selectionStart, selectionEnd);
}
});
if (selectionStart != selectionEnd) { //maybe we want to remove the condition?
RP.post(new Runnable() {
@Override public void run() {
//this condition is important in order not to break any running hints
//the model sync+refresh renders any existing POMComponents people
// might be holding useless
if (!model.isIntransaction()) {
HintsController.setErrors(document, LAYER_POM_SELECTION, findHints(model, project, selectionStart, selectionEnd, caretPosition));
} else {
HintsController.setErrors(document, LAYER_POM_SELECTION, Collections.<ErrorDescription>emptyList());
}
}
});
ok = true;
return UpToDateStatus.UP_TO_DATE_PROCESSING;
}
}
}
}
} catch (DataObjectNotFoundException ex) {
//#166011 just a minor issue, just log, but don't show to user directly
LOG.log(Level.INFO, "Touched somehow invalidated FileObject", ex);
} finally {
if (!ok) {
HintsController.setErrors(document, LAYER_POM_SELECTION, Collections.<ErrorDescription>emptyList());
}
}
return UpToDateStatus.UP_TO_DATE_OK; // XXX should use UP_TO_DATE_PROCESSING if checkHints task is currently running
}