leaks: unregister shortcuts and detach listeners

This commit is contained in:
anna
2012-03-13 20:44:23 +01:00
parent 3b6da2c96d
commit a0d356b830
2 changed files with 42 additions and 26 deletions
@@ -741,35 +741,39 @@ public abstract class DialogWrapper {
final JRootPane rootPane = getRootPane();
// if rootPane = null, dialog has already been disposed
if (rootPane != null) {
new AwtVisitor(rootPane) {
public boolean visit(final Component component) {
if (component instanceof JComponent) {
final JComponent eachComp = (JComponent)component;
final ActionMap actionMap = eachComp.getActionMap();
final KeyStroke[] strokes = eachComp.getRegisteredKeyStrokes();
for (KeyStroke eachStroke : strokes) {
boolean remove = true;
if (actionMap != null) {
for (int i = 0; i < 3; i++) {
final InputMap inputMap = eachComp.getInputMap(i);
final Object key = inputMap.get(eachStroke);
if (key != null) {
final Action action = actionMap.get(key);
if (action instanceof UIResource) remove = false;
}
}
}
if (remove) eachComp.unregisterKeyboardAction(eachStroke);
}
}
return false;
}
};
unregisterKeyboardActions(rootPane);
myPeer.dispose();
}
}
public static void unregisterKeyboardActions(final JRootPane rootPane) {
new AwtVisitor(rootPane) {
public boolean visit(final Component component) {
if (component instanceof JComponent) {
final JComponent eachComp = (JComponent)component;
final ActionMap actionMap = eachComp.getActionMap();
final KeyStroke[] strokes = eachComp.getRegisteredKeyStrokes();
for (KeyStroke eachStroke : strokes) {
boolean remove = true;
if (actionMap != null) {
for (int i = 0; i < 3; i++) {
final InputMap inputMap = eachComp.getInputMap(i);
final Object key = inputMap.get(eachStroke);
if (key != null) {
final Action action = actionMap.get(key);
if (action instanceof UIResource) remove = false;
}
}
}
if (remove) eachComp.unregisterKeyboardAction(eachStroke);
}
}
return false;
}
};
}
/**
* This method is invoked by default implementation of "Cancel" action. It just closes dialog
@@ -129,7 +129,7 @@ public class FrameWrapper implements Disposable, DataProvider {
} else {
((JDialog)frame).setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
WindowAdapter focusListener = new WindowAdapter() {
final WindowAdapter focusListener = new WindowAdapter() {
public void windowOpened(WindowEvent e) {
IdeFocusManager fm = IdeFocusManager.getInstance(myProject);
JComponent toFocus = myPreferedFocus;
@@ -145,6 +145,12 @@ public class FrameWrapper implements Disposable, DataProvider {
}
};
frame.addWindowListener(focusListener);
Disposer.register(this, new Disposable() {
@Override
public void dispose() {
frame.removeWindowListener(focusListener);
}
});
if (myCloseOnEsc) addCloseOnEsc((RootPaneContainer)frame);
((RootPaneContainer)frame).getContentPane().add(myComponent, BorderLayout.CENTER);
if (frame instanceof JFrame) {
@@ -181,6 +187,12 @@ public class FrameWrapper implements Disposable, DataProvider {
if (isDisposed()) return;
Window frame = getFrame();
final JRootPane rootPane = ((RootPaneContainer)frame).getRootPane();
if (rootPane != null) {
DialogWrapper.unregisterKeyboardActions(rootPane);
}
frame.setVisible(false);
frame.dispose();