JFileChooser2.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30: package xeij;
31:
32: import java.awt.*;
33: import java.awt.event.*;
34: import java.io.*;
35: import java.lang.*;
36: import java.util.*;
37: import java.util.regex.*;
38: import javax.swing.*;
39: import javax.swing.event.*;
40: import javax.swing.text.*;
41:
42: public class JFileChooser2 extends JFileChooser {
43:
44: private static final boolean DEBUG = false;
45:
46:
47: public static final int MAXIMUM_HISTORY_COUNT = 100;
48: public static final int MAXIMUM_ROW_COUNT = 10;
49: protected ArrayList<File[]> history;
50:
51:
52: protected JTextField formerTextField;
53: protected JComboBox<String> comboBox;
54: protected JTextField comboBoxTextField;
55: protected int ignoreItemEvent;
56:
57:
58:
59:
60:
61:
62:
63: protected static final Pattern NAME_PATTERN = Pattern.compile ("\\s*+(?:,\\s*+)*+(?:([^\",]++)|\"([^\"]++)\"?+)");
64:
65:
66: public JFileChooser2 () {
67: this (new File[0]);
68: }
69: public JFileChooser2 (File file) {
70: this (new File[] { file });
71: }
72: @SuppressWarnings ("this-escape") public JFileChooser2 (File[] files) {
73: history = new ArrayList<File[]> ();
74: history.add (files);
75: formerTextField = null;
76: comboBox = null;
77: comboBoxTextField = null;
78: ignoreItemEvent = 0;
79:
80:
81: if (false) {
82: ComponentFactory.printComponentTree (this);
83:
84:
85:
86:
87: }
88: JPanel fileNamePanel;
89: try {
90: fileNamePanel = (JPanel) ((JPanel) getComponent (3)).getComponent (0);
91: formerTextField = (JTextField) fileNamePanel.getComponent (1);
92: } catch (Exception e) {
93: e.printStackTrace ();
94: return;
95: }
96:
97:
98:
99:
100: comboBox = new JComboBox<String> (new String[0]);
101: comboBox.setEditable (true);
102: comboBox.setMaximumRowCount (MAXIMUM_ROW_COUNT);
103:
104: comboBoxTextField = (JTextField) comboBox.getEditor ().getEditorComponent ();
105: comboBoxTextField.setColumns (formerTextField.getColumns ());
106: comboBoxTextField.setText (formerTextField.getText ());
107:
108: fileNamePanel.remove (formerTextField);
109:
110: fileNamePanel.add (comboBox);
111:
112: fileNamePanel.validate ();
113:
114: ((AbstractDocument) formerTextField.getDocument ()).addDocumentListener (new DocumentListener () {
115: @Override public void changedUpdate (DocumentEvent de) {
116: if (DEBUG) {
117: System.out.println ("formerTextField.changedUpdate");
118: System.out.println (" getText()=\"" + formerTextField.getText () + "\"");
119: }
120: }
121: @Override public void insertUpdate (DocumentEvent de) {
122: if (DEBUG) {
123: System.out.println ("formerTextField.insertUpdate");
124: System.out.println (" getText()=\"" + formerTextField.getText () + "\"");
125: }
126: if (ignoreItemEvent == 0) {
127: ignoreItemEvent++;
128: comboBoxTextField.setText (formerTextField.getText ());
129: ignoreItemEvent--;
130: }
131: }
132: @Override public void removeUpdate (DocumentEvent de) {
133: if (DEBUG) {
134: System.out.println ("formerTextField.removeUpdate");
135: System.out.println (" getText()=\"" + formerTextField.getText () + "\"");
136: }
137: if (ignoreItemEvent == 0) {
138: ignoreItemEvent++;
139: comboBoxTextField.setText (formerTextField.getText ());
140: ignoreItemEvent--;
141: }
142: }
143: });
144:
145: ((AbstractDocument) comboBoxTextField.getDocument ()).addDocumentListener (new DocumentListener () {
146: @Override public void changedUpdate (DocumentEvent de) {
147: if (DEBUG) {
148: System.out.println ("comboBoxTextField.changedUpdate");
149: System.out.println (" getText()=\"" + formerTextField.getText () + "\"");
150: }
151: }
152: @Override public void insertUpdate (DocumentEvent de) {
153: if (DEBUG) {
154: System.out.println ("comboBoxTextField.insertUpdate");
155: System.out.println (" getText()=\"" + formerTextField.getText () + "\"");
156: }
157: if (ignoreItemEvent == 0) {
158: ignoreItemEvent++;
159: setSelectedFiles (namesToFiles (comboBoxTextField.getText ()));
160: ignoreItemEvent--;
161: }
162: }
163: @Override public void removeUpdate (DocumentEvent de) {
164: if (DEBUG) {
165: System.out.println ("comboBoxTextField.removeUpdate");
166: System.out.println (" getText()=\"" + formerTextField.getText () + "\"");
167: }
168: if (ignoreItemEvent == 0) {
169: ignoreItemEvent++;
170: setSelectedFiles (namesToFiles (comboBoxTextField.getText ()));
171: ignoreItemEvent--;
172: }
173: }
174: });
175:
176: comboBox.addItemListener (new ItemListener () {
177: @Override public void itemStateChanged (ItemEvent ie) {
178: if (DEBUG) {
179: System.out.println ("comboBox.itemStateChanged");
180: System.out.println (" getSelectedIndex()=" + comboBox.getSelectedIndex ());
181: System.out.println (" getText()=" + comboBoxTextField.getText ());
182: }
183: if (ignoreItemEvent == 0) {
184: ignoreItemEvent++;
185: if (ie.getStateChange () == ItemEvent.SELECTED) {
186: int i = comboBox.getSelectedIndex ();
187: if (0 <= i) {
188: if (i < history.size ()) {
189: File[] files = history.get (i);
190: history.remove (i);
191: history.add (0, files);
192: } else {
193: clearHistory ();
194:
195: }
196: historyToComboBox ();
197: selectLastFiles ();
198: }
199: }
200: ignoreItemEvent--;
201: }
202: }
203: });
204: historyToComboBox ();
205: selectLastFiles ();
206: }
207:
208:
209:
210:
211: public void clearHistory () {
212: if (JOptionPane.showConfirmDialog (
213: this,
214: Multilingual.mlnJapanese ? "履歴を消去しますか?" : "Do you want to clear history?",
215: Multilingual.mlnJapanese ? "確認" : "Confirmation",
216: JOptionPane.YES_NO_OPTION,
217: JOptionPane.PLAIN_MESSAGE) == JOptionPane.YES_OPTION) {
218: history.clear ();
219: history.add (0, new File[0]);
220: }
221: }
222:
223:
224:
225: public void selectLastFiles () {
226: if (DEBUG) {
227: System.out.println ("selectLastFiles");
228: if (history.size () != 0) {
229: System.out.println (" " + filesToPaths (history.get (0)));
230: }
231: }
232: if (history.size () != 0) {
233: File[] files = history.get (0);
234: if (files.length == 0) {
235: setCurrentDirectory (new File (".").getAbsoluteFile ().getParentFile ());
236: }
237: setSelectedFiles (files);
238: }
239: }
240:
241:
242:
243:
244: @Override public File getSelectedFile () {
245: if (DEBUG) {
246: System.out.println ("getSelectedFile");
247: }
248: File[] files = getSelectedFiles ();
249: return files.length == 0 ? null : files[0];
250: }
251:
252:
253:
254:
255: @Override public File[] getSelectedFiles () {
256: if (DEBUG) {
257: System.out.println ("getSelectedFiles");
258: }
259: if (comboBox == null) {
260: return super.getSelectedFiles ();
261: }
262: File[] files = namesToFiles (comboBoxTextField.getText ());
263: if (DEBUG) {
264: for (int i = 0; i < files.length; i++) {
265: File file = files[i];
266: System.out.println (" " + file.getPath ());
267: }
268: }
269: return files;
270: }
271:
272:
273:
274: @Override public void setSelectedFile (File file) {
275: if (DEBUG) {
276: System.out.println ("setSelectedFile");
277: if (file != null) {
278: System.out.println (" " + file.getPath ());
279: }
280: }
281:
282:
283: super.setSelectedFile (file);
284:
285: }
286:
287:
288:
289: @Override public void setSelectedFiles (File[] files) {
290: if (DEBUG) {
291: System.out.println ("setSelectedFiles");
292: if (files != null) {
293: for (File file : files) {
294: System.out.println (" " + file.getPath ());
295: }
296: }
297: }
298:
299: File[] dotFiles = null;
300: if (files != null) {
301: dotFiles = new File[files.length];
302: for (int i = 0; i < files.length; i++) {
303: File file = files[i];
304: dotFiles[i] = file.isDirectory () ? new File (file, ".") : file;
305: }
306: }
307:
308: super.setSelectedFiles (dotFiles);
309:
310: }
311:
312:
313:
314: public void addHistory (File file) {
315: if (DEBUG) {
316: System.out.println ("addHistory");
317: if (file != null) {
318: System.out.println (" " + file.getPath ());
319: }
320: }
321: if (file != null) {
322: addHistory (new File[] { file });
323: }
324: }
325:
326:
327:
328: public void addHistory (String paths) {
329: addHistory (pathsToFiles (paths));
330: }
331:
332:
333:
334: public void addHistory (File[] files) {
335: if (DEBUG) {
336: System.out.println ("addHistory");
337: if (files != null) {
338: for (File file : files) {
339: System.out.println (" " + file.getPath ());
340: }
341: }
342: }
343: if (files == null || files.length == 0) {
344: return;
345: }
346: for (int i = 0; i < history.size (); i++) {
347: File[] files2 = history.get (i);
348: if (files2.length == 0 ||
349: Arrays.equals (files, files2)) {
350: history.remove (i);
351: i--;
352: }
353: }
354: if (history.size () == MAXIMUM_HISTORY_COUNT) {
355: history.remove (MAXIMUM_HISTORY_COUNT - 1);
356: }
357: history.add (0, files);
358: historyToComboBox ();
359: }
360:
361:
362:
363: public ArrayList<String> getHistory () {
364: if (DEBUG) {
365: System.out.println ("getHistory");
366: }
367: ArrayList<String> pathsList = new ArrayList<String> ();
368: for (File[] files : history) {
369: pathsList.add (filesToPaths (files));
370: }
371: return pathsList;
372: }
373:
374:
375:
376: public void setHistory (ArrayList<String> pathsList) {
377: if (DEBUG) {
378: System.out.println ("setHistory");
379: if (pathsList != null) {
380: for (String paths : pathsList) {
381: System.out.println (" " + paths);
382: }
383: }
384: }
385: history.clear ();
386: list:
387: for (String paths : pathsList) {
388: File[] files = pathsToFiles (paths);
389: if (files.length == 0) {
390: continue list;
391: }
392: for (File[] newerFiles : history) {
393: if (Arrays.equals (files, newerFiles)) {
394: continue list;
395: }
396: }
397: history.add (files);
398: if (history.size () == MAXIMUM_HISTORY_COUNT) {
399: break list;
400: }
401: }
402: historyToComboBox ();
403: }
404:
405:
406:
407: protected void historyToComboBox () {
408: if (DEBUG) {
409: System.out.println ("historyToComboBox");
410: for (File[] files : history) {
411: System.out.println (" " + filesToPaths (files));
412: }
413: }
414: ignoreItemEvent++;
415: comboBox.removeAllItems ();
416: for (File[] files : history) {
417: comboBox.addItem (filesToNames (files));
418: }
419: comboBox.addItem (Multilingual.mlnJapanese ?
420: "---------- 履歴を消去する ----------" :
421: "---------- Clear history ----------");
422: ignoreItemEvent--;
423: }
424:
425:
426:
427: public String filesToNames (File[] files) {
428: StringBuilder sb = new StringBuilder ();
429: int n = files.length;
430: if (n == 0) {
431: } else if (n == 1) {
432: sb.append (files[0].getName ());
433: } else {
434: for (int i = 0; i < n; i++) {
435: if (0 < i) {
436: sb.append (' ');
437: }
438: sb.append ('"').append (files[i].getName ()).append ('"');
439: }
440: }
441: return sb.toString ();
442: }
443:
444:
445:
446:
447: public File[] namesToFiles (String names) {
448: File directory = getCurrentDirectory ();
449: ArrayList<File> fileList = new ArrayList<File> ();
450: Matcher matcher = NAME_PATTERN.matcher (names);
451: while (matcher.find ()) {
452: String name = matcher.group (1) != null ? matcher.group (1) : matcher.group (2);
453: File file = new File (directory, name).getAbsoluteFile ();
454: if (!fileList.contains (file)) {
455: fileList.add (file);
456: }
457: }
458: return fileList.toArray (new File[fileList.size ()]);
459: }
460:
461:
462:
463: public static String filesToPaths (File[] files) {
464: StringBuilder sb = new StringBuilder ();
465: int n = files.length;
466: if (n == 0) {
467: } else if (n == 1) {
468: sb.append (files[0].getPath ());
469: } else {
470: for (int i = 0; i < n; i++) {
471: if (0 < i) {
472: sb.append (' ');
473: }
474: sb.append ('"').append (files[i].getPath ()).append ('"');
475: }
476: }
477: return sb.toString ();
478: }
479:
480:
481:
482:
483: public static File[] pathsToFiles (String paths) {
484: ArrayList<File> fileList = new ArrayList<File> ();
485: Matcher matcher = NAME_PATTERN.matcher (paths);
486: while (matcher.find ()) {
487: String path = matcher.group (1) != null ? matcher.group (1) : matcher.group (2);
488: File file = new File (path).getAbsoluteFile ();
489: if (!fileList.contains (file)) {
490: fileList.add (file);
491: }
492: }
493: return fileList.toArray (new File[fileList.size ()]);
494: }
495:
496: }
497:
498:
499: