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: XEiJ.pnlExitFullScreen (true);
213: if (JOptionPane.showConfirmDialog (
214: this,
215: Multilingual.mlnJapanese ? "履歴を消去しますか?" : "Do you want to clear history?",
216: Multilingual.mlnJapanese ? "確認" : "Confirmation",
217: JOptionPane.YES_NO_OPTION,
218: JOptionPane.PLAIN_MESSAGE) == JOptionPane.YES_OPTION) {
219: history.clear ();
220: history.add (0, new File[0]);
221: }
222: }
223:
224:
225:
226: public void selectLastFiles () {
227: if (DEBUG) {
228: System.out.println ("selectLastFiles");
229: if (history.size () != 0) {
230: System.out.println (" " + filesToPaths (history.get (0)));
231: }
232: }
233: if (history.size () != 0) {
234: File[] files = history.get (0);
235: if (files.length == 0) {
236: setCurrentDirectory (new File (".").getAbsoluteFile ().getParentFile ());
237: }
238: setSelectedFiles (files);
239: }
240: }
241:
242:
243:
244:
245: @Override public File getSelectedFile () {
246: if (DEBUG) {
247: System.out.println ("getSelectedFile");
248: }
249: File[] files = getSelectedFiles ();
250: return files.length == 0 ? null : files[0];
251: }
252:
253:
254:
255:
256: @Override public File[] getSelectedFiles () {
257: if (DEBUG) {
258: System.out.println ("getSelectedFiles");
259: }
260: if (comboBox == null) {
261: return super.getSelectedFiles ();
262: }
263: File[] files = namesToFiles (comboBoxTextField.getText ());
264: if (DEBUG) {
265: for (int i = 0; i < files.length; i++) {
266: File file = files[i];
267: System.out.println (" " + file.getPath ());
268: }
269: }
270: return files;
271: }
272:
273:
274:
275: @Override public void setSelectedFile (File file) {
276: if (DEBUG) {
277: System.out.println ("setSelectedFile");
278: if (file != null) {
279: System.out.println (" " + file.getPath ());
280: }
281: }
282:
283:
284: super.setSelectedFile (file);
285:
286: }
287:
288:
289:
290: @Override public void setSelectedFiles (File[] files) {
291: if (DEBUG) {
292: System.out.println ("setSelectedFiles");
293: if (files != null) {
294: for (File file : files) {
295: System.out.println (" " + file.getPath ());
296: }
297: }
298: }
299:
300: File[] dotFiles = null;
301: if (files != null) {
302: dotFiles = new File[files.length];
303: for (int i = 0; i < files.length; i++) {
304: File file = files[i];
305: dotFiles[i] = file.isDirectory () ? new File (file, ".") : file;
306: }
307: }
308:
309: super.setSelectedFiles (dotFiles);
310:
311: }
312:
313:
314:
315: public void addHistory (File file) {
316: if (DEBUG) {
317: System.out.println ("addHistory");
318: if (file != null) {
319: System.out.println (" " + file.getPath ());
320: }
321: }
322: if (file != null) {
323: addHistory (new File[] { file });
324: }
325: }
326:
327:
328:
329: public void addHistory (String paths) {
330: addHistory (pathsToFiles (paths));
331: }
332:
333:
334:
335: public void addHistory (File[] files) {
336: if (DEBUG) {
337: System.out.println ("addHistory");
338: if (files != null) {
339: for (File file : files) {
340: System.out.println (" " + file.getPath ());
341: }
342: }
343: }
344: if (files == null || files.length == 0) {
345: return;
346: }
347: for (int i = 0; i < history.size (); i++) {
348: File[] files2 = history.get (i);
349: if (files2.length == 0 ||
350: Arrays.equals (files, files2)) {
351: history.remove (i);
352: i--;
353: }
354: }
355: if (history.size () == MAXIMUM_HISTORY_COUNT) {
356: history.remove (MAXIMUM_HISTORY_COUNT - 1);
357: }
358: history.add (0, files);
359: historyToComboBox ();
360: }
361:
362:
363:
364: public ArrayList<String> getHistory () {
365: if (DEBUG) {
366: System.out.println ("getHistory");
367: }
368: ArrayList<String> pathsList = new ArrayList<String> ();
369: for (File[] files : history) {
370: pathsList.add (filesToPaths (files));
371: }
372: return pathsList;
373: }
374:
375:
376:
377: public void setHistory (ArrayList<String> pathsList) {
378: if (DEBUG) {
379: System.out.println ("setHistory");
380: if (pathsList != null) {
381: for (String paths : pathsList) {
382: System.out.println (" " + paths);
383: }
384: }
385: }
386: history.clear ();
387: list:
388: for (String paths : pathsList) {
389: File[] files = pathsToFiles (paths);
390: if (files.length == 0) {
391: continue list;
392: }
393: for (File[] newerFiles : history) {
394: if (Arrays.equals (files, newerFiles)) {
395: continue list;
396: }
397: }
398: history.add (files);
399: if (history.size () == MAXIMUM_HISTORY_COUNT) {
400: break list;
401: }
402: }
403: historyToComboBox ();
404: }
405:
406:
407:
408: protected void historyToComboBox () {
409: if (DEBUG) {
410: System.out.println ("historyToComboBox");
411: for (File[] files : history) {
412: System.out.println (" " + filesToPaths (files));
413: }
414: }
415: ignoreItemEvent++;
416: comboBox.removeAllItems ();
417: for (File[] files : history) {
418: comboBox.addItem (filesToNames (files));
419: }
420: comboBox.addItem (Multilingual.mlnJapanese ?
421: "---------- 履歴を消去する ----------" :
422: "---------- Clear history ----------");
423: ignoreItemEvent--;
424: }
425:
426:
427:
428: public String filesToNames (File[] files) {
429: StringBuilder sb = new StringBuilder ();
430: int n = files.length;
431: if (n == 0) {
432: } else if (n == 1) {
433: sb.append (files[0].getName ());
434: } else {
435: for (int i = 0; i < n; i++) {
436: if (0 < i) {
437: sb.append (' ');
438: }
439: sb.append ('"').append (files[i].getName ()).append ('"');
440: }
441: }
442: return sb.toString ();
443: }
444:
445:
446:
447:
448: public File[] namesToFiles (String names) {
449: File directory = getCurrentDirectory ();
450: ArrayList<File> fileList = new ArrayList<File> ();
451: Matcher matcher = NAME_PATTERN.matcher (names);
452: while (matcher.find ()) {
453: String name = matcher.group (1) != null ? matcher.group (1) : matcher.group (2);
454: File file = new File (directory, name).getAbsoluteFile ();
455: if (!fileList.contains (file)) {
456: fileList.add (file);
457: }
458: }
459: return fileList.toArray (new File[fileList.size ()]);
460: }
461:
462:
463:
464: public static String filesToPaths (File[] files) {
465: StringBuilder sb = new StringBuilder ();
466: int n = files.length;
467: if (n == 0) {
468: } else if (n == 1) {
469: sb.append (files[0].getPath ());
470: } else {
471: for (int i = 0; i < n; i++) {
472: if (0 < i) {
473: sb.append (' ');
474: }
475: sb.append ('"').append (files[i].getPath ()).append ('"');
476: }
477: }
478: return sb.toString ();
479: }
480:
481:
482:
483:
484: public static File[] pathsToFiles (String paths) {
485: ArrayList<File> fileList = new ArrayList<File> ();
486: Matcher matcher = NAME_PATTERN.matcher (paths);
487: while (matcher.find ()) {
488: String path = matcher.group (1) != null ? matcher.group (1) : matcher.group (2);
489: File file = new File (path).getAbsoluteFile ();
490: if (!fileList.contains (file)) {
491: fileList.add (file);
492: }
493: }
494: return fileList.toArray (new File[fileList.size ()]);
495: }
496:
497: }
498:
499:
500: