Skip to content

Commit

Permalink
#86 Improved NoteEditor to catch dialog close button, added catch of …
Browse files Browse the repository at this point in the history
…cancel button
  • Loading branch information
raydac committed Aug 10, 2021
1 parent a02ed59 commit ac609fe
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 31 deletions.
9 changes: 9 additions & 0 deletions mind-map/mind-map-swing-panel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.KeyEvent;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
Expand All @@ -70,10 +67,8 @@
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -876,6 +871,33 @@ public static boolean isKeyStrokeEvent(@Nullable final KeyStroke keyStroke, fina
return result;
}

@Nonnull
@MustNotContainNull
private static List<JButton> findAllOptionPaneButtons(@Nonnull final JComponent component) {
final List<JButton> result = new ArrayList<>();
Arrays.stream(component.getComponents())
.filter(x -> x instanceof JComponent)
.map(x -> (JComponent) x)
.forEach(x -> {
if (x instanceof JButton) {
if ("OptionPane.button".equals(x.getName())) {
result.add((JButton) x);
}
} else {
result.addAll(findAllOptionPaneButtons(x));
}
});
return result;
}

private static void replaceActionListenerForButton(@Nonnull final JButton button, @Nonnull final ActionListener listener) {
final ActionListener[] currentListeners = button.getActionListeners();
for (final ActionListener l : currentListeners) {
button.removeActionListener(l);
}
button.addActionListener(listener);
}

@Nonnull
@SafeVarargs
@ReturnsOriginal
Expand All @@ -886,40 +908,69 @@ public static JComponent catchEscInParentDialog(
@Nonnull @MustNotContainNull final Consumer<JDialog>... beforeClose) {
component.addHierarchyListener(new HierarchyListener() {

final Consumer<JDialog> processor = dialog -> {
final boolean close;
if (doClose == null) {
close = true;
} else {
if (doClose.test(dialog)) {
close = dialogProvider
.msgConfirmOkCancel(dialog, BUNDLE.getString("Utils.confirmActionTitle"), BUNDLE.getString("Utils.closeForContentChange"));
} else {
close = true;
}
}
if (close) {
try {
for (final Consumer<JDialog> c : beforeClose) {
try {
c.accept(dialog);
} catch (Exception ex) {
LOGGER.error("Error during before close call", ex);
}
}
} finally {
dialog.dispose();
}
}
};
private final KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);

@Override
public void hierarchyChanged(@Nonnull final HierarchyEvent e) {
final Window window = SwingUtilities.getWindowAncestor(component);
if (window instanceof JDialog) {
if (window instanceof JDialog && (e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0) {
final JDialog dialog = (JDialog) window;

final List<JButton> dialogButtons = findAllOptionPaneButtons(dialog.getRootPane());
dialogButtons.stream()
.filter(x -> "cancel".equalsIgnoreCase(x.getText()))
.forEach(x -> replaceActionListenerForButton(x, be -> {
processor.accept(dialog);
}));

dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

final WindowListener windowListener = new WindowAdapter() {
@Override
public void windowClosing(@Nonnull final WindowEvent e) {
processor.accept(dialog);
}
};

final WindowListener[] windowListeners = dialog.getWindowListeners();
for (final WindowListener w : windowListeners) {
dialog.removeWindowListener(w);
}
dialog.addWindowListener(windowListener);

final InputMap inputMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(escapeKeyStroke, "PRESSING_ESCAPE");
final ActionMap actionMap = dialog.getRootPane().getActionMap();
actionMap.put("PRESSING_ESCAPE", new AbstractAction() {
@Override
public void actionPerformed(@Nonnull final ActionEvent e) {
final boolean close;
if (doClose == null) {
close = true;
} else {
if (doClose.test(dialog)) {
close = dialogProvider
.msgConfirmOkCancel(dialog, BUNDLE.getString("Utils.confirmActionTitle"), BUNDLE.getString("Utils.closeForContentChange"));
} else {
close = true;
}
}
if (close) {
for (final Consumer<JDialog> c : beforeClose) {
try {
c.accept(dialog);
} catch (Exception ex) {
LOGGER.error("Error during before close call", ex);
}
}
dialog.dispose();
}
processor.accept(dialog);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions mind-map/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
Expand Down

0 comments on commit ac609fe

Please sign in to comment.