|
How do I get the element under the caret? |
|
|
Null checks, class match checks and exception handling omitted:
JTextComponent editor = Registry.getMostActiveComponent();
BaseDocument baseDoc = (BaseDocument) ed.getDocument();
int offset = ed.getCaret().getDot();
JavaModel.getJavaRepository().beginTrans(false);
JMIUtils utils = JMIUtils.get(baseDoc);
Resource resource = utils.getResource();
JavaModel.setClassPath(resource);
Element el = resource.getElementByOffset(offset);
|
To iterate backwards and find, say, the method the caret is in (if any), use a while loop ala:
while (!(el instanceof Feature) && el != null) {
el = (Element) el.refImmediateComposite();
}
|
Source: NetBeans FAQ
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.