EditorContextDispatcher
class added.
+ EditorContextDispatcher
class added, in order to simplify the access to current
+ active elements in the IDE (like current FileObject and editor pane) and
+ to make the events dispatching more efficient - reduce the number of listeners on context switching.
+ EditorContextDispatcher
allows registration of listeners based on files MIME type, therefore
+ listeners obtain change events only when context change concerns a file of the given MIME type.
+
null
when there is no active file.
+ */
+ public synchronized FileObject getCurrentFile() {
+ return currentFile;
+ }
+
+ /**
+ * Get the String representation of URL of the current active file.
+ * @return The String representation of URL of the current active file or
+ * an empty String when there is no active file.
+ */
+ public synchronized String getCurrentURLAsString() {
+ if (currentURL == null) {
+ FileObject fo = getCurrentFile();
+ if (fo != null) {
+ try {
+ currentURL = fo.getURL().toString ();
+ } catch (FileStateInvalidException ex) {}
+ }
+ if (currentURL == null) {
+ currentURL = ""; // NOI18N
+ }
+ }
+ return currentURL;
+ }
+
+ /**
+ * Get the {@link org.openide.cookies.EditorCookie} of currently edited file.
+ * @return The current {@link org.openide.cookies.EditorCookie} or
+ * null
when there is no currently edited file.
+ */
+ public synchronized EditorCookie getCurrentEditorCookie() {
+ if (currentOpenedPane != null) {
+ return currentEditorCookie;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Get the {@link javax.swing.JEditorPane} of currently edited file.
+ * @return The current {@link javax.swing.JEditorPane} or
+ * null
when there is no currently edited file.
+ */
+ public synchronized JEditorPane getCurrentEditor() {
+ return currentOpenedPane;
+ }
+
+ /**
+ * Get the line number of the caret in the current editor.
+ * @return the line number or -1
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;
+ }
+
+ /**
+ * Get the most recent active file. This returns the active file if there's
+ * one, or a file, that was most recently active.
+ * @return The most recent file or null
when there was no recent
+ * active file.
+ */
+ public synchronized FileObject getMostRecentFile() {
+ return mostRecentFileRef.get();
+ }
+
+ /**
+ * Get the String representation of URL of the most recent active file.
+ * @return The String representation of URL of the most recent file or
+ * an empty String when there was no recent active file.
+ */
+ public synchronized String getMostRecentURLAsString() {
+ FileObject fo = getMostRecentFile();
+ if (fo != null) {
+ try {
+ return fo.getURL().toString ();
+ } catch (FileStateInvalidException ex) {}
+ }
+ return ""; // NOI18N
+ }
+
+ private synchronized EditorCookie getMostRecentEditorCookie() {
+ if (getMostRecentEditor() != null) {
+ return mostRecentEditorCookieRef.get();
+ } else {
+ return null;
+ }
+ }
+
+ private synchronized JEditorPane getMostRecentEditor() {
+ return mostRecentOpenedPaneRef.get();
+ }
+
+ /**
+ * 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 -1
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;
+ }
+
+ /**
+ * Add a PropertyChangeListener to this context dispatcher.
+ * @param l The PropertyChangeListener
+ */
+ public void addPropertyChangeListener(PropertyChangeListener l) {
+ pcs.addPropertyChangeListener(l);
+ }
+
+ /**
+ * Remove a PropertyChangeListener from this context dispatcher.
+ * @param l The PropertyChangeListener
+ */
+ public void removePropertyChangeListener(PropertyChangeListener l) {
+ pcs.removePropertyChangeListener(l);
+ // Also remove the listener from all MIME types
+ synchronized (pcsByMIMEType) {
+ Set