Skip to content

Commit

Permalink
fixed bug where callbacks where not set in undocked windows
Browse files Browse the repository at this point in the history
  • Loading branch information
GavriYashar committed Sep 15, 2016
1 parent de248f2 commit beeefbb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
93 changes: 70 additions & 23 deletions src/at/justin/matlab/EditorApp.java
Expand Up @@ -4,12 +4,14 @@
import at.justin.matlab.gui.bookmarks.Bookmarks;
import at.justin.matlab.mesr.MESR;
import at.justin.matlab.prefs.Settings;
import at.justin.matlab.util.ComponentUtil;
import com.mathworks.matlab.api.editor.Editor;
import com.mathworks.matlab.api.editor.EditorApplicationListener;
import com.mathworks.matlab.api.editor.EditorEvent;
import com.mathworks.matlab.api.editor.EditorEventListener;
import com.mathworks.mde.editor.EditorSyntaxTextPane;
import com.mathworks.mde.editor.MatlabEditorApplication;
import com.mathworks.widgets.editor.breakpoints.BreakpointView;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
Expand Down Expand Up @@ -100,9 +102,7 @@ public Editor openEditor(File file) {
public void setCallbacks() {
List<Editor> openEditors = getMatlabEditorApplication().getOpenEditors();
for (final Editor editor : openEditors) {
if (editors.contains(editor)) {
continue;
}
if (editors.contains(editor)) continue;
editors.add(editor);
editor.addEventListener(new EditorEventListener() {
@Override
Expand All @@ -113,26 +113,19 @@ public void eventOccurred(EditorEvent editorEvent) {
}
}
});
}

List<Component> list = Matlab.getInstance().getComponents("EditorSyntaxTextPane");
for (Component component : list) {
KeyListener[] keyListeners = component.getKeyListeners();
if (Settings.getPropertyBoolean("verbose")) {
System.out.println("\n" + keyListeners.length + " keylisteners");
for (KeyListener keyListener : keyListeners) {
System.out.println(keyListener.toString());
}
}

EditorSyntaxTextPane editorSyntaxTextPane = ComponentUtil.getEditorSyntaxTextPaneForEditor(editor);
if (editorSyntaxTextPane == null) continue;
KeyListener[] keyListeners = editorSyntaxTextPane.getKeyListeners();
for (KeyListener keyListener1 : keyListeners) {
if (keyListener1.toString().equals(KeyReleasedHandler.getKeyListener().toString())) {
component.removeKeyListener(keyListener1); // this will assure that the new keylistener is added and the previous one is removed
editorSyntaxTextPane.removeKeyListener(keyListener1);
// this will assure that the new keylistener is added and the previous one is removed
// while matlab is still running and the .jar is replaced
}
}
component.addKeyListener(KeyReleasedHandler.getKeyListener());
EditorSyntaxTextPane editorSyntaxTextPane = (EditorSyntaxTextPane) component;

editorSyntaxTextPane.addKeyListener(KeyReleasedHandler.getKeyListener());
editorSyntaxTextPane.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
Expand All @@ -156,6 +149,7 @@ public void changedUpdate(DocumentEvent e) {
}
});
}

if (Settings.containsKey("bpColor")) {
colorizeBreakpointView(Settings.getPropertyColor("bpColor"));
} else {
Expand All @@ -164,17 +158,19 @@ public void changedUpdate(DocumentEvent e) {
}

public void removeCallbacks() {
List<Component> list = Matlab.getInstance().getComponents("EditorSyntaxTextPane");
for (Component component : list) {
component.removeKeyListener(KeyReleasedHandler.getKeyListener());
List<Editor> openEditors = getMatlabEditorApplication().getOpenEditors();
for (Editor editor : openEditors) {
EditorSyntaxTextPane editorSyntaxTextPane = ComponentUtil.getEditorSyntaxTextPaneForEditor(editor);
editorSyntaxTextPane.removeKeyListener(KeyReleasedHandler.getKeyListener());
}
colorizeBreakpointView(DISABLED);
}

public void colorizeBreakpointView(Color color) {
List<Component> list = Matlab.getInstance().getComponents("BreakpointView$2");
for (Component component : list) {
component.setBackground(color);
List<Editor> openEditors = getMatlabEditorApplication().getOpenEditors();
for (Editor editor : openEditors) {
BreakpointView.Background breakpointView = ComponentUtil.getBreakPointViewForEditor(editor);
if (breakpointView != null) breakpointView.setBackground(color);
}
}
}
Expand Down Expand Up @@ -248,3 +244,54 @@ public void colorizeBreakpointView(Color color) {
// }
// });
// }

//
// add callbacks the old way
// List<Component> list = Matlab.getInstance().getComponents("EditorSyntaxTextPane");
// for (Component component : list) {
// KeyListener[] keyListeners = component.getKeyListeners();
// if (Settings.getPropertyBoolean("verbose")) {
// System.out.println("\n" + keyListeners.length + " keylisteners");
// for (KeyListener keyListener : keyListeners) {
// System.out.println(keyListener.toString());
// }
// }
//
// for (KeyListener keyListener1 : keyListeners) {
// if (keyListener1.toString().equals(KeyReleasedHandler.getKeyListener().toString())) {
// component.removeKeyListener(keyListener1);
// // this will assure that the new keylistener is added and the previous one is removed
// // while matlab is still running and the .jar is replaced
// }
// }
// component.addKeyListener(KeyReleasedHandler.getKeyListener());
// EditorSyntaxTextPane editorSyntaxTextPane = (EditorSyntaxTextPane) component;
// editorSyntaxTextPane.getDocument().addDocumentListener(new DocumentListener() {
// @Override
// public void insertUpdate(DocumentEvent e) {
// Bookmarks.getInstance().adjustBookmarks(e, true);
// try {
// String insertString = e.getDocument().getText(e.getOffset(), e.getLength());
// if (insertString.equals("%")) MESR.doYourThing();
// } catch (BadLocationException ignored) {
// ignored.printStackTrace();
// }
// }
//
// @Override
// public void removeUpdate(DocumentEvent e) {
// Bookmarks.getInstance().adjustBookmarks(e, false);
// }
//
// @Override
// public void changedUpdate(DocumentEvent e) {
// EditorWrapper.setIsDirty(true);
// }
// });
// }

// old breakpointview color
// List<Component> list = Matlab.getInstance().getComponents("BreakpointView$2");
// for (Component component : list) {
// component.setBackground(color);
// }
7 changes: 6 additions & 1 deletion src/at/justin/matlab/EditorWrapper.java
@@ -1,6 +1,8 @@
package at.justin.matlab;

import at.justin.matlab.util.ComponentUtil;
import com.mathworks.matlab.api.editor.Editor;
import com.mathworks.mde.editor.EditorSyntaxTextPane;
import com.mathworks.widgets.text.mcode.cell.CellUtils;

import javax.swing.*;
Expand Down Expand Up @@ -174,8 +176,11 @@ public int getSelectionPositionStart() {
return gae().getTextComponent().getSelectionStart();
}

public EditorSyntaxTextPane getEditorSyntaxTextPane() {
return ComponentUtil.getEditorSyntaxTextPaneForEditor(gae());
}

/**
*
* @param line line number
* @param select boolean
*/
Expand Down

1 comment on commit beeefbb

@GavriYashar
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#6

Please sign in to comment.