AbstractOpenDialog.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: import java.awt.*;
16: import java.awt.event.*;
17: import java.io.*;
18: import java.util.*;
19: import javax.swing.*;
20: import javax.swing.filechooser.*;
21:
22: public abstract class AbstractOpenDialog extends JDialog implements ActionListener {
23:
24:
25: protected JFileChooser2 fileChooser;
26:
27:
28: protected JCheckBox readOnlyCheckBox;
29: protected JRadioButton rebootRadioButton;
30: protected JRadioButton openRadioButton;
31:
32:
33: protected boolean directory;
34: protected boolean readOnly;
35: protected boolean reboot;
36:
37:
38:
39: @SuppressWarnings ("this-escape") public AbstractOpenDialog (
40: Frame owner, String title, String jaTitle, boolean directory,
41: javax.swing.filechooser.FileFilter fileFilter) {
42: super (owner, title, true);
43: this.setUndecorated (true);
44: this.getRootPane ().setWindowDecorationStyle (JRootPane.FRAME);
45: this.setAlwaysOnTop (true);
46: this.setLocationByPlatform (true);
47: this.setDefaultCloseOperation (WindowConstants.HIDE_ON_CLOSE);
48:
49: Multilingual.mlnTitle (this, "ja", jaTitle);
50: this.directory = directory;
51:
52:
53: fileChooser = new JFileChooser2 ();
54: fileChooser.setMultiSelectionEnabled (true);
55: fileChooser.setControlButtonsAreShown (false);
56: fileChooser.setFileFilter (fileFilter);
57: fileChooser.addActionListener (this);
58:
59:
60: ButtonGroup approveGroup = new ButtonGroup ();
61: JComponent component =
62: ComponentFactory.createBorderPanel (
63: 0, 0,
64: ComponentFactory.createVerticalBox (
65: fileChooser,
66: ComponentFactory.createHorizontalBox (
67: Box.createHorizontalStrut (12),
68: Box.createHorizontalGlue (),
69: readOnlyCheckBox =
70: Multilingual.mlnText (
71: ComponentFactory.createCheckBox (
72: readOnly, "Read-only", this),
73: "ja", "書き込み禁止"),
74: Box.createHorizontalGlue (),
75: rebootRadioButton =
76: ComponentFactory.setFixedSize (
77: ComponentFactory.setText (
78: ComponentFactory.createRadioButton (
79: approveGroup, reboot, "approve reboot", this),
80: ""),
81: 20, 20),
82: Multilingual.mlnText (
83: ComponentFactory.createButton (
84: "Reboot from it", KeyEvent.VK_R, this),
85: "ja", "ここから再起動"),
86: Box.createHorizontalStrut (12),
87: openRadioButton =
88: ComponentFactory.setFixedSize (
89: ComponentFactory.setText (
90: ComponentFactory.createRadioButton (
91: approveGroup, !reboot, "approve open", this),
92: ""),
93: 20, 20),
94: Multilingual.mlnText (
95: ComponentFactory.createButton (
96: "Open", KeyEvent.VK_O, this),
97: "ja", "開く"),
98: Box.createHorizontalStrut (20),
99: Multilingual.mlnText (
100: ComponentFactory.createButton (
101: "Cancel", KeyEvent.VK_C, this),
102: "ja", "キャンセル"),
103: Box.createHorizontalStrut (12)
104: ),
105: Box.createVerticalStrut (12)
106: )
107: );
108:
109: this.getContentPane ().add (component, BorderLayout.CENTER);
110: this.pack ();
111: this.setVisible (false);
112: }
113:
114:
115:
116: public boolean getReadOnly () {
117: return readOnly;
118: }
119:
120:
121:
122: public void setReadOnly (boolean readOnly) {
123: this.readOnly = readOnly;
124: if (readOnlyCheckBox != null) {
125: readOnlyCheckBox.setSelected (readOnly);
126: }
127: }
128:
129:
130:
131: public boolean getReboot () {
132: return reboot;
133: }
134:
135:
136:
137: public void setReboot (boolean reboot) {
138: this.reboot = reboot;
139: if (reboot) {
140: if (rebootRadioButton != null) {
141: rebootRadioButton.setSelected (true);
142: }
143: } else {
144: if (openRadioButton != null) {
145: openRadioButton.setSelected (true);
146: }
147: }
148: }
149:
150:
151:
152: public void addHistory (File[] files) {
153: fileChooser.addHistory (files);
154: fileChooser.selectLastFiles ();
155: }
156:
157:
158:
159: public ArrayList<String> getHistory () {
160: return fileChooser.getHistory ();
161: }
162:
163:
164:
165: public void rescanCurrentDirectory () {
166: fileChooser.rescanCurrentDirectory ();
167: }
168:
169:
170:
171: @Override public void actionPerformed (ActionEvent ae) {
172: switch (ae.getActionCommand ()) {
173: case JFileChooser.APPROVE_SELECTION:
174: approve (reboot);
175: break;
176: case "Read-only":
177: readOnly = ((JCheckBox) ae.getSource ()).isSelected ();
178: break;
179: case "approve reboot":
180: reboot = true;
181: break;
182: case "Reboot from it":
183: approve (true);
184: break;
185: case "approve open":
186: reboot = false;
187: break;
188: case "Open":
189: approve (false);
190: break;
191: case JFileChooser.CANCEL_SELECTION:
192: case "Cancel":
193: this.setVisible (false);
194: break;
195: }
196: }
197:
198:
199:
200:
201:
202: protected void approve (boolean reboot) {
203: File[] files = fileChooser.getSelectedFiles ();
204: if (files.length == 0 && directory) {
205: files = new File[] { fileChooser.getCurrentDirectory () };
206: }
207: if (0 < files.length) {
208: openFiles (files, reboot);
209: this.setVisible (false);
210: }
211: }
212:
213:
214:
215: public abstract void openFiles (File[] files, boolean reboot);
216:
217: }