001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.io.File;
025: import java.io.FilenameFilter;
026:
027: import javax.swing.JFileChooser;
028: import javax.swing.filechooser.FileFilter;
029:
030: import org.beryl.gui.widgets.Dialog;
031: import org.beryl.gui.widgets.Frame;
032: import org.beryl.gui.widgets.Label;
033:
034: public class DialogUtils {
035: public static final int RESULT_YES = 0;
036: public static final int RESULT_NO = 1;
037: public static final int RESULT_CANCEL = 2;
038:
039: /**
040: * Display a file open dialog
041: * @param extension The file extension to look for
042: */
043: public static File showOpenFileDialog(Widget parent,
044: String extension) {
045: JFileChooser chooser = new JFileChooser();
046: chooser.setFileFilter(new ExtensionFilter(extension, true));
047: if (chooser.showOpenDialog(parent.getRealWidget()) == JFileChooser.APPROVE_OPTION) {
048: return chooser.getSelectedFile();
049: } else {
050: return null;
051: }
052: }
053:
054: /**
055: * Display a file save dialog
056: * @param extension The file extension of the destination file
057: */
058: public static File showSaveFileDialog(Widget parent,
059: String extension) {
060: JFileChooser chooser = new JFileChooser();
061: chooser.setFileFilter(new ExtensionFilter(extension, true));
062: if (chooser.showSaveDialog(parent.getRealWidget()) == JFileChooser.APPROVE_OPTION) {
063: String path = chooser.getSelectedFile().getPath();
064: if (!path.endsWith("." + extension))
065: path = path + "." + extension;
066: return new File(path);
067: } else {
068: return null;
069: }
070: }
071:
072: private static class YesNoCancelEventListener implements
073: GUIEventListener {
074: private int result = RESULT_CANCEL;
075:
076: public void eventOccured(GUIEvent event) {
077: String name = event.getName();
078:
079: if (name.equals("yes"))
080: result = RESULT_YES;
081: else if (name.equals("no"))
082: result = RESULT_NO;
083: else if (name.equals("cancel"))
084: result = RESULT_CANCEL;
085:
086: ((Dialog) event.getSource().getParentWidgetByClass(
087: Dialog.class)).dispose();
088: }
089:
090: public int getResult() {
091: return result;
092: }
093: };
094:
095: /**
096: * Show a dialog with a yes/no/cancel button
097: * @param frame The parent frame
098: * @param title The dialog title
099: * @param message The dialog message
100: * @return The result (RESULT_YES/RESULT_NO/RESULT_CANCEL)
101: */
102: public static int showYesNoCancelDialog(Frame frame, String title,
103: String message) throws GUIException {
104: YesNoCancelEventListener listener = new YesNoCancelEventListener();
105: Dialog dialog = (Dialog) WidgetFactory.getInstance()
106: .constructWidget(DialogUtils.class,
107: "YesNoCancelDialog", listener);
108: Label label = (Label) dialog.getWidget("DialogCaption");
109: label.setProperty("text", message);
110: dialog.initDialog(frame);
111: dialog.setProperty("title", title);
112: dialog.show();
113: return listener.getResult();
114: }
115:
116: /**
117: * A filter class which removes every file except
118: * (optionally) directories and files with a given extension
119: */
120: public static class ExtensionFilter extends FileFilter implements
121: FilenameFilter {
122: private String extension = null;
123: private boolean directories = false;
124:
125: public ExtensionFilter(String extension, boolean directories) {
126: this .extension = "." + extension;
127: this .directories = directories;
128: }
129:
130: public boolean accept(File file) {
131: return file.getName().endsWith(extension)
132: || (directories && file.isDirectory());
133: }
134:
135: public boolean accept(File file, String name) {
136: file = new File(name);
137: return file.getName().endsWith(extension)
138: || (directories && file.isDirectory());
139: }
140:
141: public String getDescription() {
142: return "*" + extension;
143: }
144: }
145: }
|