Skip to content

Commit a952aa7

Browse files
committed
Better test.Async* classes
1 parent 273b390 commit a952aa7

File tree

14 files changed

+753
-103
lines changed

14 files changed

+753
-103
lines changed
567 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20191103225630
1+
20191104164911
567 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20191103225630
1+
20191104164911
567 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/doc/Differences.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,46 @@ closed, just as for Java.
557557

558558
Developers are encouraged to create a separate class that handles general calls.
559559
An example class can be found in the SwingJS distribution as src/test/async/AsyncDialog.java.
560+
Very simple modifications to the Java allows asynchronous operation using AsyncDialog. Here
561+
is a simple "do you want to close this frame" example, where you can see that what we have
562+
done is to set the reply into an ActionListener that is defined in the constructor of
563+
the AsyncDisplay object:
564+
565+
// Original:
566+
//
567+
// private void promptQuit() {
568+
// int sel = JOptionPane.showConfirmDialog(null, PROMPT_EXIT, NAME, JOptionPane.YES_NO_OPTION);
569+
// switch (sel) {
570+
// case JOptionPane.YES_OPTION:
571+
// resultsTab.clean();
572+
// seqs.dispose();
573+
// if (fromMain) {
574+
// System.exit(0);
575+
// }
576+
// break;
577+
// }
578+
// }
579+
580+
private void promptQuitAsync() {
581+
new AsyncDialog(new ActionListener() {
582+
583+
@Override
584+
public void actionPerformed(ActionEvent e) {
585+
int sel = ((AsyncDialog)e.getSource()).getOption();
586+
switch (sel) {
587+
case JOptionPane.YES_OPTION:
588+
resultsTab.clean();
589+
seqs.dispose();
590+
if (fromMain) {
591+
System.exit(0);
592+
}
593+
break;
594+
}
595+
}}).showConfirmDialog(null, PROMPT_EXIT, NAME, JOptionPane.YES_NO_OPTION);
596+
}
597+
598+
Very simple!
599+
560600

561601
native methods
562602
--------------

sources/net.sf.j2s.java.core/src/javax/swing/JColorChooser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
public class JColorChooser extends JComponent {
116116

117117

118+
static Component listener;
119+
118120
private ColorSelectionModel selectionModel;
119121

120122
private JComponent previewPanel;
@@ -172,7 +174,8 @@ static class ASYNCHRONOUS_COLOR extends Color implements UIResource {}
172174
*/
173175
public static Color showDialog(Component component, String title, Color initialColor) {
174176

175-
if (!(component instanceof PropertyChangeListener)) {
177+
Component c = (listener == null ? component : listener);
178+
if (!(c instanceof PropertyChangeListener)) {
176179
System.err.println("JOptionPanel: parentComponent is not a PropertyChangeListener");
177180
return null;
178181
}
@@ -663,7 +666,7 @@ protected void initColorChooserDialog(Component c, JColorChooser chooserPane,
663666
}
664667

665668
if (okListener == null && cancelListener == null)
666-
秘ensurePropertyChangeListener(this, c);
669+
秘ensurePropertyChangeListener(this, JColorChooser.listener == null ? c : JColorChooser.listener);
667670

668671
this.chooserPane = chooserPane;
669672

sources/net.sf.j2s.java.core/src/javax/swing/JOptionPane.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ public class JOptionPane extends JComponent {
496496

497497
private static boolean USE_HTML5_MODAL_FOR_WARNINGS_AND_ERRORS = true;
498498

499+
private static Component listener;
500+
499501
/**
500502
* Shows a question-message dialog requesting input from the user. The
501503
* dialog uses the default frame, which usually means it is centered on the
@@ -640,7 +642,7 @@ public static Object showInputDialog(Component parentComponent, Object message,
640642

641643
Object value;
642644

643-
if (!(parentComponent instanceof PropertyChangeListener)) {
645+
if (!isListener(parentComponent)) {
644646

645647
if (!(message instanceof String)) {
646648
warnJSDeveloper();
@@ -754,9 +756,8 @@ public static void showMessageDialog(Component parentComponent, Object message,
754756

755757
boolean simplify = USE_HTML5_MODAL_FOR_WARNINGS_AND_ERRORS
756758
&& (messageType == WARNING_MESSAGE || messageType == ERROR_MESSAGE);
757-
boolean isPropertyListener = parentComponent instanceof PropertyChangeListener;
758759

759-
if (simplify || !isPropertyListener) {
760+
if (simplify || !isListener(parentComponent)) {
760761
if (!simplify && !(message instanceof String))
761762
warnJSDeveloper();
762763
String s = getMessageTypeString(messageType, ": ") + (title == "Message" ? "" : title + "\n\n")
@@ -767,6 +768,10 @@ public static void showMessageDialog(Component parentComponent, Object message,
767768
showOptionDialog(parentComponent, message, title, DEFAULT_OPTION, messageType, icon, null, null);
768769
}
769770

771+
private static boolean isListener(Component parentComponent) {
772+
return (listener != null ? listener : parentComponent) instanceof PropertyChangeListener;
773+
}
774+
770775
private static void warnJSDeveloper() {
771776
System.err.println("JOptionPane: Component does not implement PropertyChangeListener.");
772777
}
@@ -909,7 +914,7 @@ public static int showConfirmDialog(Component parentComponent, Object message, S
909914

910915
boolean jsReturn = true;
911916

912-
if (!(parentComponent instanceof PropertyChangeListener)) {
917+
if (!isListener(parentComponent)) {
913918
if (!(message instanceof String)) {
914919
warnJSDeveloper();
915920
message = "?";
@@ -985,7 +990,7 @@ public static int showConfirmDialog(Component parentComponent, Object message, S
985990
public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
986991
int messageType, Icon icon, Object[] options, Object initialValue) {
987992

988-
if (!(parentComponent instanceof PropertyChangeListener)) {
993+
if (!isListener(parentComponent)) {
989994
warnJSDeveloper();
990995
return CANCEL_OPTION;
991996
}
@@ -1132,7 +1137,7 @@ public void componentShown(ComponentEvent ce) {
11321137
setValue(UNINITIALIZED_VALUE);
11331138
}
11341139
});
1135-
秘ensurePropertyChangeListener(this, parentComponent);
1140+
秘ensurePropertyChangeListener(this, (listener == null ? parentComponent : listener));
11361141

11371142
addPropertyChangeListener(new PropertyChangeListener() {
11381143
@Override

0 commit comments

Comments
 (0)