Skip to content

Commit f2608e0

Browse files
committed
AsyncDialog, AsyncFileChooser, JFileChooser fixes
1 parent aff7530 commit f2608e0

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ public void run() {
798798
JSUtil.J2S.getFileFromDialog(/**@j2sNative function(file){r.run$(file)}||*/ null, "java.io.File");
799799
return JDialog.ASYNCHRONOUS_INTEGER;
800800
case SAVE_DIALOG:
801+
if (selectedFile != null)
802+
lastFileName = selectedFile.getName();
801803
String name = JSUtil.prompt((dialogTitle == null ? "File to Save?" : dialogTitle), lastFileName);
802804
if (name == null)
803805
return CANCEL_OPTION;

sources/net.sf.j2s.java.core/src/test/async/AsyncDialog.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import javax.swing.JOptionPane;
1212

1313
/**
14-
* A class to manage asynchronous input and confirmation dialogs.
14+
* A class to manage asynchronous input, option, and confirmation dialogs.
1515
*
1616
* @author Bob Hanson hansonr
1717
*
@@ -21,6 +21,7 @@ public class AsyncDialog extends Window implements PropertyChangeListener {
2121

2222
private ActionListener actionListener;
2323
private Object choice;
24+
private String[] options;
2425

2526
public AsyncDialog(ActionListener a) {
2627
super(null);
@@ -35,51 +36,72 @@ public AsyncDialog(ActionListener a) {
3536
public void propertyChange(PropertyChangeEvent evt) {
3637
switch (evt.getPropertyName()) {
3738
case "value":
38-
process(evt.getNewValue());
39+
Object val = evt.getNewValue();
40+
if (options != null) {
41+
int i;
42+
for (i = 0; i < options.length; i++) {
43+
if (options[i] == val)
44+
break;
45+
}
46+
val = Integer.valueOf(i < options.length ? i : JOptionPane.CLOSED_OPTION);
47+
}
48+
process(val);
3949
}
4050
}
4151

42-
public void showInputDialog(Component frame, String message, String title, int type,
43-
Icon icon, Integer[] choices, Integer initialChoice) {
44-
if (frame != null) {
45-
setBounds(frame.getBounds());
46-
}
52+
// These options can be supplemented as desired.
53+
54+
public void showInputDialog(Component frame, String message, String title, int type, Icon icon, Integer[] choices,
55+
Integer initialChoice) {
56+
setFrame(frame);
4757
process(JOptionPane.showInputDialog(this, message, title, type, icon, choices, initialChoice));
4858
}
4959

60+
public void showOptionDialog(Component frame, String message, String title, int optionType, int messageType,
61+
Icon icon, String[] options, String initialValue) {
62+
setFrame(frame);
63+
this.options = options;
64+
process(JOptionPane.showOptionDialog(this, message, title, optionType, messageType, icon, options,
65+
initialValue));
66+
}
67+
5068
public void showConfirmDialog(Component frame, String message, String title, int optionType) {
5169
showConfirmDialog(frame, message, title, optionType, JOptionPane.QUESTION_MESSAGE);
5270
}
5371

5472
public void showConfirmDialog(Component frame, String message, String title, int optionType, int messageType) {
73+
setFrame(frame);
74+
process(JOptionPane.showConfirmDialog(this, message, title, optionType, messageType));
75+
}
76+
77+
private void setFrame(Component frame) {
5578
if (frame != null) {
5679
setBounds(frame.getBounds());
5780
}
58-
process(JOptionPane.showConfirmDialog(this, message, title, optionType, messageType));
5981
}
6082

6183
private boolean processed;
62-
84+
6385
/**
6486
* Return for confirm dialog.
6587
*
66-
* @param ret may be JavaScript NaN, testable as ret != ret or ret != - -ret
88+
* @param ret may be JavaScript NaN, testable as ret != ret or ret != - -ret
6789
*/
6890
private void process(int ret) {
6991
if (ret != - -ret || processed)
7092
return;
7193
process(new Integer(ret));
7294
}
73-
95+
7496
private void process(Object ret) {
7597
if (ret instanceof javax.swing.plaf.UIResource || processed)
7698
return;
7799
processed = true;
78-
choice = ret;
100+
choice = ret;
79101
actionListener.actionPerformed(new ActionEvent(this, 0, "SelectedOption"));
80102
dispose();
81103
}
82-
104+
83105
/**
84106
* retrieve selection from the ActionEvent, for which "this" is getSource()
85107
*
@@ -95,5 +117,5 @@ public int getOption() {
95117
}
96118
return ((Integer) choice).intValue();
97119
}
98-
120+
99121
}

sources/net.sf.j2s.java.core/src/test/async/AsyncFileChooser.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ public AsyncFileChooser(File file, FileSystemView view) {
3232
super(file, view);
3333
}
3434

35+
@Deprecated
36+
public int showDialog(Component frame) {
37+
return err();
38+
}
39+
40+
@Deprecated
41+
public int showDialog(Component frame, String type) {
42+
return (ok == null ? err() : super.showDialog(frame, type));
43+
}
44+
45+
@Deprecated
46+
public int showOpenDialog(Component frame) {
47+
return err();
48+
}
49+
50+
@Deprecated
51+
public int showSaveDialog(Component frame) {
52+
return err();
53+
}
54+
55+
private int err() {
56+
try {
57+
throw new java.lang.IllegalAccessException("Warning! AsyncFileChooser interface bypassed!");
58+
} catch (IllegalAccessException e) {
59+
e.printStackTrace();
60+
}
61+
return JFileChooser.CANCEL_OPTION;
62+
}
63+
3564
/**
3665
*
3766
* @param frame
@@ -43,19 +72,19 @@ public AsyncFileChooser(File file, FileSystemView view) {
4372
public int showDialog(Component frame, String type, Runnable ok, Runnable cancel) {
4473
this.ok = ok;
4574
this.cancel = cancel;
46-
return process(showDialog(frame, type));
75+
return process(super.showDialog(frame, type));
4776
}
4877

4978
public void showOpenDialog(Component frame, Runnable ok, Runnable cancel) {
5079
this.ok = ok;
5180
this.cancel = cancel;
52-
process(showOpenDialog(frame));
81+
process(super.showOpenDialog(frame));
5382
}
5483

5584
public void showSaveDialog(Component frame, Runnable ok, Runnable cancel) {
5685
this.ok = ok;
5786
this.cancel = cancel;
58-
process(showSaveDialog(frame));
87+
process(super.showSaveDialog(frame));
5988
}
6089

6190
@Override

0 commit comments

Comments
 (0)