The EditorManager is also responsible for creating and managing keyMaps, and you can read all about that in the KeymapConcepts document.
Of course EditorPanes are not the only classes who would want to know about global editor changes, so the EditorManager has general support for adding and removing property change listeners. You can either add a class as the listener of a single property change (EditorManager.addPropertyChangeListener(String, PropertyChangeListener)), or add a class as the listener of all property changes (EditorManager.addPropertyChangeListener(PropertyChangeListener)). The class that is added as the listener should implement the java/Beans/PropertyChangeListener interface, which basically means it should have the function: void propertyChange(PropertyChangeEvent evt);
Let's practice and write code for a class so it will know when the tab
size has changed:
public class KnowsTabSize implements PropertyChangeListener {
int tabSize;
public KnowsTabSize() {
tabSize = 0;
}
public static void initOpenTool(byte majorVersion, byte minorVersion) {
// Make sure the OpenTools API is compatible
if (majorVersion != PrimeTime.CURRENT_MAJOR_VERSION)
return;
// Create our class and add it as a listener of the tabSize EditorManager changes
KnowsTabSize knowItAll = new KnowsTabSize();
EditorManager.addPropertyChangeListener(EditorManager.tabSizeAttribute, knowItAll);
}
// Implement to satisfy the PropertyChangeListener interface
// The EditorManager will call this function anytime it fires a
// tab size property change
public void propertyChange(PropertyChangeEvent e) {
// Update our own tab size
tabSize = EditorManager.getTabSize();
}
}
EditorPane editor = EditorAction.getFocusedEditor();
Of course open nodes in the browser that don't have the focus might
have an EditorPane
associated with them, in which case you can use the getEditor function
from EditorManager
:
Node[] nodes = Browser.getActiveBrowser().getOpenNodes();
for (int i = 0; i < nodes.length; i++) {
EditorPane editor = EditorManager.getEditor(nodes[i]);
if (editor != null) {
// Do something with the editor
}
}
// First get the document EditorPane editor = EditorAction.getFocusedEditor(); Document doc = editor.getDocument(); // Get the base of all Elements Element baseElement = doc.getDefaultRootElement(); // Just for fun, how many lines are there in the document int totalLines = baseElement.getElementCount(); // Where is my caret now int caretIndex = editor.getCaretPosition(); // On what line is my caret and get the Element of that line int lineIndex = baseElement.getElementIndex(caretIndex); Element lineElement = baseElement.getElement(lineIndex); // Get the boundaries of that line int startingIndex = lineElement.getStartOffset(); int endingIndex = lineElement.getEndOffset(); //Get the actual text of the line String lineText = doc.getText(startingIndex, endingIndex - startingIndex);
// Get the editor and the caret
EditorPane editor = EditorAction.getFocusedEditor();
Caret caret = editor.getCaret();
// Where is the caret
int caretPosition = editor.getCaretPosition();
// Does the caret say there is a highlight?
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
// There is an hightlighted selection
}
// Alternate way to find out about selections
int selectBegin = editor.getSelectionStart();
int selectEnd = editor.getSelectionEnd();
if (selectBegin != selectEnd) {
// There is an hightlighted selection
}