001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings.extra;
022:
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Frame;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.Insets;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.ItemEvent;
032: import java.awt.event.ItemListener;
033: import java.awt.event.KeyEvent;
034: import java.awt.event.MouseEvent;
035: import java.awt.event.MouseListener;
036: import java.awt.event.WindowEvent;
037: import java.io.File;
038: import java.io.IOException;
039: import java.lang.reflect.Method;
040: import java.util.Vector;
041:
042: import com.javujavu.javux.util.SortMap;
043: import com.javujavu.javux.wings.ShortcutAdapter;
044: import com.javujavu.javux.wings.WingButton;
045: import com.javujavu.javux.wings.WingCombo;
046: import com.javujavu.javux.wings.WingDialog;
047: import com.javujavu.javux.wings.WingImage;
048: import com.javujavu.javux.wings.WingLabel;
049: import com.javujavu.javux.wings.WingList;
050: import com.javujavu.javux.wings.WingPanel;
051: import com.javujavu.javux.wings.WingScrollPane;
052: import com.javujavu.javux.wings.WingSkin;
053: import com.javujavu.javux.wings.WingTextField;
054: import com.javujavu.javux.wings.dialogs.WingMessageBox;
055: import com.javujavu.javux.wings.item.LabelItem;
056:
057: /**
058: * This class creates, displays, and operates a message box.
059: * The message box contains an application-defined message and title,
060: * plus any combination of push buttons
061: * <br>
062: * <b>This class is thread safe.</b>
063: **/
064: public class WingFileDialog extends WingDialog implements
065: ActionListener, ItemListener, MouseListener {
066: public static final String[] LABELS = { "Choose File", "Open",
067: "Cancel", "Look in:", "File name:", "Files of type:",
068: "Warning", "Overwrite existing file ?", "Yes", "No" };
069:
070: public static final int FILE = 1;
071: public static final int DIRECTORY = 2;
072: public static final int EXISTING = 4;
073: public static final int NO_WARN = 8;
074:
075: protected WingButton bOpen;
076: protected WingButton bCancel;
077: protected ShortcutAdapter sListEnter;
078: public boolean result;
079: private WingCombo cbLookIn;
080: private WingButton bUp;
081: private WingButton bHome;
082: private WingList list;
083: private WingTextField tfFileName;
084: private WingCombo cbFilesOfType;
085: private WingLabel l1, l2, l3;
086: private Object[][] filters;
087: private String[] labels;
088: private String dir;
089: private int type;
090:
091: public WingFileDialog(Frame owner) {
092: super (owner, true);
093:
094: WingPanel content = new WingPanel(new GridBagLayout());
095: content.setPreferredSize(new Dimension(500, 340));
096:
097: GridBagConstraints c = new GridBagConstraints();
098: int gap = 5;
099: c.insets = new Insets(gap, gap, gap, gap);
100: c.fill = GridBagConstraints.NONE;
101: c.anchor = GridBagConstraints.EAST;
102:
103: c.anchor = GridBagConstraints.CENTER;
104: c.gridwidth = GridBagConstraints.REMAINDER;
105: WingPanel p1 = new WingPanel(new GridBagLayout());
106: content.add(p1, c);
107:
108: GridBagConstraints c2 = new GridBagConstraints();
109: c2.insets = new Insets(0, 0, 0, gap);
110: p1.add(l1 = new WingLabel(), c2);
111: p1.add(cbLookIn = new WingCombo(), c2);
112: p1.add(bUp = new WingButton(), c2);
113: bUp.setStyleId("file.up.tool");
114: c2.insets = new Insets(0, 0, 0, 0);
115: p1.add(bHome = new WingButton(), c2);
116: bHome.setStyleId("file.home.tool");
117:
118: c.fill = GridBagConstraints.BOTH;
119: c.insets = new Insets(0, gap, gap, gap);
120: c.weightx = 1.0;
121: c.weighty = 1.0;
122: content.add(new WingScrollPane(list = new WingList(
123: WingList.VERTICAL_WRAP)), c);
124: list.addMouseListener(this );
125:
126: c.anchor = GridBagConstraints.EAST;
127: c.fill = GridBagConstraints.NONE;
128: c.weightx = 0.0;
129: c.weighty = 0.0;
130: c.gridwidth = 1;
131: content.add(l2 = new WingLabel(), c);
132:
133: c.fill = GridBagConstraints.HORIZONTAL;
134: c.insets = new Insets(0, 0, gap, gap);
135: c.weightx = 1.0;
136: content.add(tfFileName = new WingTextField(), c);
137:
138: c.weightx = 0.0;
139: c.gridwidth = GridBagConstraints.REMAINDER;
140: content.add(bOpen = new WingButton(), c);
141: bOpen.setStyleId("file.open");
142: bOpen.setShortcut(KeyEvent.VK_ENTER, 0);
143:
144: c.fill = GridBagConstraints.NONE;
145: c.gridwidth = 1;
146: content.add(l3 = new WingLabel(), c);
147:
148: c.fill = GridBagConstraints.HORIZONTAL;
149: c.insets = new Insets(0, 0, gap, gap);
150: c.weightx = 1.0;
151: content.add(cbFilesOfType = new WingCombo(), c);
152:
153: c.weightx = 0.0;
154: c.gridwidth = GridBagConstraints.REMAINDER;
155: content.add(bCancel = new WingButton(), c);
156: bCancel.setStyleId("file.cancel");
157: bCancel.setShortcut(KeyEvent.VK_ESCAPE, 0);
158:
159: list.addShortcut(sListEnter = new ShortcutAdapter(
160: KeyEvent.VK_ENTER, 0, this ));
161:
162: setContentPane(content);
163: content.addActionListener(this );
164: content.addItemListener(this );
165: enableEvents(WindowEvent.WINDOW_EVENT_MASK);
166: }
167:
168: public boolean showDialog(Container origin, int type,
169: Object[][] filters, String title) {
170: return showDialog(origin, type, filters, new String[] { title });
171: }
172:
173: public boolean showDialog(Container origin, int type,
174: Object[][] filters, String[] labels) {
175: this .type = type;
176:
177: this .labels = (String[]) LABELS.clone();
178: if ((type & EXISTING) == 0)
179: this .labels[1] = "Save";
180: for (int i = 0; labels != null && i < labels.length
181: && i < this .labels.length; i++) {
182: if (labels[i] != null)
183: this .labels[i] = labels[i];
184: }
185:
186: String h = null;
187: try {
188: h = System.getProperty("user.home");
189: } catch (Exception e) {
190: }
191: ;
192: bHome.setEnabled(h != null);
193:
194: setTitle(this .labels[0]);
195: bOpen.setLabel(this .labels[1]);
196: bCancel.setLabel(this .labels[2]);
197: l1.setLabel(this .labels[3]);
198: l2.setLabel(this .labels[4]);
199: l3.setLabel(this .labels[5]);
200:
201: this .filters = filters;
202: cbFilesOfType.removeAllItems();
203: if ((type & FILE) != 0 && filters != null && filters.length > 0) {
204: cbFilesOfType.setEnabled(true);
205: for (int i = 0; i < filters.length; i++) {
206: cbFilesOfType.addItem(filters[i][0]);
207: }
208: } else {
209: cbFilesOfType.setEnabled(false);
210: cbFilesOfType.addItem(" ");
211: }
212:
213: if (dir == null) {
214: dir = (new File("")).getAbsolutePath();
215: }
216: if ((type & DIRECTORY) != 0
217: && tfFileName.getText().trim().length() == 0) {
218: tfFileName.setText(dir);
219: }
220: listFiles();
221:
222: super .packAndCenter(origin, 10, 10);
223: setVisible(true);
224:
225: return result;
226: }
227:
228: public void setPath(String path) {
229: if (path == null)
230: path = "";
231: File f = new File(path);
232: try {
233: path = f.getCanonicalPath();
234: } catch (IOException e) {
235: }
236: if (f.isDirectory()) {
237: dir = path;
238: tfFileName.setText(null);
239: } else {
240: File f2 = f;
241: while (true) {
242: dir = f2.getParent();
243: if (dir == null)
244: break;
245: f2 = new File(dir);
246: if (f2.isDirectory())
247: break;
248: }
249: if (dir == null)
250: dir = (new File("")).getAbsolutePath();
251: tfFileName.setText(f.getName());
252: }
253: }
254:
255: public String getPath() {
256: String r = tfFileName.getText().trim();
257: if (r.length() > 0) {
258: if (!(new File(r)).isAbsolute())
259: r = sum(dir, r);
260: } else
261: r = dir;
262: try {
263: r = (new File(r)).getCanonicalPath();
264: } catch (IOException e) {
265: }
266: return r;
267: }
268:
269: private void setDir(String ndir) {
270: dir = ndir;
271: if ((type & DIRECTORY) != 0)
272: tfFileName.setText(dir);
273: listFiles();
274: }
275:
276: private void listFiles() {
277: list.removeAllItems();
278: File f = new File(dir), f2;
279: String[] ll = f.list();
280:
281: WingImage imgUp = WingSkin.getImage("file.icon.up", null, null);
282: WingImage imgDir = WingSkin.getImage("file.icon.dir", null,
283: null);
284: WingImage imgDrive = WingSkin.getImage("file.icon.drive", null,
285: null);
286: WingImage imgFile = WingSkin.getImage("file.icon.file", null,
287: null);
288:
289: if (f.getParent() != null) {
290: list.addItem(new LabelItem("..", imgUp));
291: }
292:
293: SortMap m = new SortMap(true);
294: for (int i = 0; ll != null && i < ll.length; i++) {
295: f2 = new File(sum(dir, ll[i]));
296: if (f2.isDirectory())
297: m.set(ll[i], ll[i]);
298: }
299: for (int i = 0; i < m.size(); i++) {
300: list.addItem(new LabelItem(m.key(i), imgDir));
301: }
302: m.removeAllElements();
303: for (int i = 0; (type & FILE) != 0 && ll != null
304: && i < ll.length; i++) {
305: f2 = new File(sum(dir, ll[i]));
306: if (f2.isFile()) {
307: boolean add = false;
308: if (filters == null || filters.length == 0)
309: add = true;
310: else {
311: add = false;
312: int k = cbFilesOfType.getSelectedIndex();
313: Object o = filters[k][1];
314: String lll = ll[i].toLowerCase();
315: if (o instanceof String[]) {
316: for (int n = 0; n < ((String[]) o).length
317: && !add; n++) {
318: String ext = ((String[]) o)[n];
319: if (lll.endsWith(ext)) {
320: add = true;
321: }
322: }
323: } else if (o instanceof String) {
324: add = lll.endsWith((String) o);
325: } else
326: add = true;
327: }
328: if (add)
329: m.set(ll[i], ll[i]);
330: }
331: }
332: for (int i = 0; i < m.size(); i++) {
333: list.addItem(new LabelItem(m.key(i), imgFile));
334: }
335:
336: cbLookIn.removeAllItems();
337: Vector v = new Vector();
338: String p2 = dir, n;
339: while (p2 != null) {
340: f2 = new File(p2);
341: n = f2.getName();
342: if (n.length() == 0)
343: n = f2.getAbsolutePath();
344: v.addElement(n);
345: p2 = f2.getParent();
346: }
347: String ind = "";
348: for (int i = v.size() - 1; i >= 0; i--) {
349: cbLookIn.addItem(new LabelItem(ind + v.elementAt(i), (ind
350: .length() == 0) ? imgDrive : imgDir));
351: ind += " ";
352: }
353: bUp.setEnabled(v.size() > 1);
354: cbLookIn.setSelectedIndex(v.size() - 1);
355: try {
356: Method mm = File.class.getMethod("listRoots",
357: new Class[] {});
358: File[] rr = (File[]) mm.invoke(null, new Object[] {});
359: for (int i = 0; i < rr.length; i++) {
360: cbLookIn.addItem(new LabelItem(rr[i].getPath(),
361: imgDrive));
362: }
363: } catch (Exception e) {
364: }
365:
366: checkOpen();
367: }
368:
369: private void checkOpen() {
370: String r = getPath();
371: File f = new File(r);
372: bOpen
373: .setEnabled(((type & FILE) != 0 && f.isFile())
374: || ((type & DIRECTORY) != 0 && f.isDirectory())
375: || ((type & EXISTING) == 0
376: && ((type & FILE) != 0 || !f.isFile()) && ((type & DIRECTORY) != 0 || !f
377: .isDirectory())));
378: }
379:
380: public void actionPerformed(ActionEvent e) {
381: Object src = e.getSource();
382: if (src == bOpen)
383: accept();
384: else if (src == bCancel)
385: close(false);
386: else if (src == bHome) {
387: setDir(System.getProperty("user.home"));
388: } else if (src == bUp) {
389: setDir((new File(dir)).getParent());
390: } else if (src == tfFileName) {
391: checkOpen();
392: } else if (src == sListEnter) {
393: listOpen();
394: }
395: }
396:
397: private static String sum(String p1, String p2) {
398: if (p1.endsWith(File.separator))
399: return p1 + p2;
400: else
401: return p1 + File.separator + p2;
402: }
403:
404: public void itemStateChanged(ItemEvent e) {
405: Object src = e.getSource();
406: if (src == cbLookIn) {
407: String d = null;
408: int i = cbLookIn.getSelectedIndex();
409: while (true) {
410: String p = cbLookIn.getItem(i).toString();
411: if (d == null)
412: d = p.trim();
413: else
414: d = sum(p.trim(), d);
415: if (!p.startsWith(" "))
416: break;
417: i--;
418: }
419: setDir(d);
420: } else if (src == list) {
421: String n = list.getSelectedItem().toString();
422: if (n.equals(".."))
423: return;
424: File f = new File(sum(dir, n));
425: if (f.isFile() && (type & FILE) != 0) {
426: tfFileName.setText(n);
427: } else if (f.isDirectory() && (type & DIRECTORY) != 0) {
428: tfFileName.setText(sum(dir, n));
429: }
430: checkOpen();
431: } else if (src == cbFilesOfType) {
432: listFiles();
433: }
434: }
435:
436: protected void processWindowEvent(WindowEvent e) {
437: if (e.getID() == WindowEvent.WINDOW_CLOSING)
438: close(false);
439: super .processWindowEvent(e);
440: }
441:
442: public void accept() {
443: if ((type & (EXISTING | NO_WARN)) == 0
444: && (new File(getPath())).isFile()) {
445: if ((new WingMessageBox((Frame) getParent(), labels[6],
446: labels[7] + "\n" + getPath(), labels[8], null,
447: labels[9])).showMessage(this ) != WingMessageBox.OKYES) {
448: return;
449: }
450: }
451: close(true);
452: }
453:
454: public void close(boolean result) {
455: this .result = result;
456: setVisible(false);
457: dispose();
458: }
459:
460: private void listOpen() {
461: Object o = list.getSelectedItem();
462: if (o == null)
463: return;
464: String n = o.toString();
465: if (n.equals("..")) {
466: setDir((new File(dir)).getParent());
467: return;
468: }
469: File f = new File(sum(dir, n));
470: if (f.isFile()) {
471: if ((type & FILE) != 0) {
472: accept();
473: }
474: } else {
475: setDir(sum(dir, n));
476: }
477: }
478:
479: public void mouseClicked(MouseEvent e) {
480: if (e.getClickCount() > 1) {
481: listOpen();
482: }
483: }
484:
485: public void mouseEntered(MouseEvent e) {
486: }
487:
488: public void mouseExited(MouseEvent e) {
489: }
490:
491: public void mousePressed(MouseEvent e) {
492: }
493:
494: public void mouseReleased(MouseEvent e) {
495: }
496: }
|