001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.filechooser;
020:
021: import java.io.File;
022:
023: import javax.swing.JFileChooser;
024: import javax.swing.JOptionPane;
025:
026: import com.jeta.open.i18n.I18N;
027: import com.jeta.open.registry.JETARegistry;
028: import com.jeta.swingbuilder.interfaces.userprops.TSUserProperties;
029:
030: /**
031: * This class is used to invoke a file chooser dialog. The type of dialog
032: * depends on the user preferences. It can either be a JFileChooser or a
033: * TSFileChooser. The TSFileChooser is a more advanced dialog that follows the
034: * Linux and Emacs type file selection.
035: *
036: * @author Jeff Tassin
037: */
038: public class TSFileChooserFactory {
039:
040: public static final String LAST_DIRECTORY = "jeta.filechooser.lastdirectory";
041:
042: /**
043: * Invokes a file open dialog. The type of dialog depends on the user
044: * preferences. It will be either a JFileChooser or a TSFileChooser.
045: *
046: * @return the file selected by the user, or null if the operation was
047: * canceled
048: */
049: public static File showOpenDialog() {
050: return showOpenDialog(JFileChooser.FILES_ONLY);
051: }
052:
053: /**
054: * Invokes a file open dialog. Allows the caller to specify a type of file
055: * used.
056: */
057: public static File showOpenDialog(String type) {
058: return showOpenDialog(type, (TSFileFilter) null);
059: }
060:
061: /**
062: * Invokes a file open dialog. Allows the caller to specify a type of file
063: * used.
064: */
065: public static File showOpenDialog(String type, TSFileFilter filter) {
066: return showOpenDialog(new FileChooserConfig(type,
067: JFileChooser.FILES_ONLY, filter));
068: }
069:
070: /**
071: * Invokes a file open dialog. Allows the caller to specify a type of file
072: * used.
073: */
074: public static File showOpenDialog(String type,
075: TSFileFilter[] filters) {
076: return showOpenDialog(new FileChooserConfig(type,
077: JFileChooser.FILES_ONLY, filters));
078: }
079:
080: /**
081: * Invokes a file open dialog. The type of dialog depends on the user
082: * preferences. It will be either a JFileChooser or a TSFileChooser.
083: *
084: * @return the file selected by the user, or null if the operation was
085: * canceled
086: */
087: public static File showOpenDialog(int mode) {
088: TSUserProperties userprops = (TSUserProperties) JETARegistry
089: .lookup(TSUserProperties.COMPONENT_ID);
090: return showOpenDialog(new FileChooserConfig(userprops
091: .getProperty(LAST_DIRECTORY, null), null, mode,
092: (TSFileFilter[]) null));
093: }
094:
095: /**
096: * Invokes a file open dialog. The type of dialog depends on the user
097: * preferences. It will be either a JFileChooser or a TSFileChooser.
098: *
099: * @return the file selected by the user, or null if the operation was
100: * canceled
101: */
102: public static File showOpenDialog(int mode, TSFileFilter filter) {
103: TSUserProperties userprops = (TSUserProperties) JETARegistry
104: .lookup(TSUserProperties.COMPONENT_ID);
105: return showOpenDialog(new FileChooserConfig(userprops
106: .getProperty(LAST_DIRECTORY, null), null, mode, filter));
107:
108: // return showOpenDialog( mode, userprops.getProperty( LAST_DIRECTORY,
109: // null ), new TSFileFilter[] { filter} );
110: }
111:
112: /**
113: * Invokes a file open dialog. The type of dialog depends on the user
114: * preferences. It will be either a JFileChooser or a TSFileChooser.
115: *
116: * @return the file selected by the user, or null if the operation was
117: * canceled
118: */
119: public static File showOpenDialog(int mode, String last_dir,
120: TSFileFilter[] filters) {
121: return showOpenDialog(new FileChooserConfig(last_dir, null,
122: mode, filters));
123: }
124:
125: /**
126: * Invokes a file open dialog. The type of dialog depends on the user
127: * preferences. It will be either a JFileChooser or a TSFileChooser.
128: *
129: * @return the file selected by the user, or null if the operation was
130: * canceled
131: */
132: public static File showOpenDialog(FileChooserConfig fcc) {
133: TSUserProperties userprops = (TSUserProperties) JETARegistry
134: .lookup(TSUserProperties.COMPONENT_ID);
135: String last_dir = fcc.getInitialDirectory();
136:
137: if (last_dir != null) {
138: File f = new File(last_dir);
139: if (!f.exists()) {
140: last_dir = f.getParent();
141: if (last_dir != null) {
142: f = new File(last_dir);
143: if (!f.exists()) {
144: last_dir = userprops.getProperty(
145: LAST_DIRECTORY, null);
146: }
147: }
148: }
149: }
150:
151: String ftype = fcc.getFileType();
152: if (last_dir == null) {
153: String default_dir = userprops.getProperty(LAST_DIRECTORY,
154: null);
155: if (ftype == null)
156: ftype = "";
157:
158: String last_dir_key = LAST_DIRECTORY + ftype;
159: last_dir = userprops.getProperty(last_dir_key, default_dir);
160: if (last_dir != null) {
161: File f = new File(last_dir);
162: if (!f.exists()) {
163: last_dir = f.getParent();
164: if (last_dir != null) {
165: f = new File(last_dir);
166: if (!f.exists()) {
167: last_dir = userprops.getProperty(
168: LAST_DIRECTORY, null);
169: }
170: }
171: }
172: }
173: }
174:
175: JFileChooser chooser = new JFileChooser(last_dir);
176: TSFileFilter[] filters = fcc.getFileFilters();
177: if (filters != null) {
178: for (int index = 0; index < filters.length; index++) {
179: chooser.addChoosableFileFilter(filters[index]);
180: }
181: }
182: chooser.setFileSelectionMode(fcc.getMode());
183: int returnVal = chooser
184: .showOpenDialog(fcc.getParentComponent());
185: if (returnVal == JFileChooser.APPROVE_OPTION) {
186: File f = chooser.getSelectedFile();
187: String dir = "";
188: if (f.isDirectory())
189: dir = f.getPath();
190: else {
191: dir = f.getParent();
192: }
193:
194: if (ftype != null) {
195: String last_dir_key = LAST_DIRECTORY + ftype;
196: userprops.setProperty(last_dir_key, dir);
197: }
198: userprops.setProperty(LAST_DIRECTORY, dir);
199:
200: return f;
201: } else {
202: return null;
203: }
204: }
205:
206: public static File showSaveDialog(String type) {
207: return showSaveDialog(type, (TSFileFilter[]) null);
208: }
209:
210: public static File showSaveDialog(String type, TSFileFilter filter) {
211: return showSaveDialog(new FileChooserConfig(type,
212: JFileChooser.FILES_ONLY, filter));
213: }
214:
215: public static File showSaveDialog(String type, TSFileFilter[] filter) {
216: return showSaveDialog(new FileChooserConfig(type,
217: JFileChooser.FILES_ONLY, filter));
218: }
219:
220: public static File showSaveDialog() {
221: TSUserProperties userprops = (TSUserProperties) JETARegistry
222: .lookup(TSUserProperties.COMPONENT_ID);
223: return showSaveDialog(new FileChooserConfig(userprops
224: .getProperty(LAST_DIRECTORY, null), null,
225: JFileChooser.FILES_ONLY, (TSFileFilter[]) null));
226: }
227:
228: public static File showSaveDialog(FileChooserConfig fcc) {
229: TSUserProperties userprops = (TSUserProperties) JETARegistry
230: .lookup(TSUserProperties.COMPONENT_ID);
231: String last_dir = fcc.getInitialDirectory();
232: File selected_file = null;
233: if (last_dir != null) {
234: File f = new File(last_dir);
235: if (!f.exists()) {
236: last_dir = f.getParent();
237: if (last_dir != null) {
238: f = new File(last_dir);
239: if (!f.exists()) {
240: last_dir = userprops.getProperty(
241: LAST_DIRECTORY, null);
242: }
243: }
244: } else if (f.isFile()) {
245: selected_file = f;
246: }
247: }
248:
249: String ftype = fcc.getFileType();
250: if (last_dir == null) {
251: String default_dir = userprops.getProperty(LAST_DIRECTORY,
252: null);
253: if (ftype == null)
254: ftype = "";
255:
256: String last_dir_key = LAST_DIRECTORY + ftype;
257: last_dir = userprops.getProperty(last_dir_key, default_dir);
258: if (last_dir != null) {
259: File f = new File(last_dir);
260: if (!f.exists()) {
261: last_dir = f.getParent();
262: if (last_dir != null) {
263: f = new File(last_dir);
264: if (!f.exists()) {
265: last_dir = userprops.getProperty(
266: LAST_DIRECTORY, null);
267: }
268: }
269: }
270: }
271: }
272:
273: JFileChooser chooser = new JFileChooser(last_dir) {
274: public void approveSelection() {
275: // check if file exists
276: File f = getSelectedFile();
277: if (f.exists()) {
278: String title = I18N.getLocalizedMessage("Warning");
279: String msg = I18N
280: .getLocalizedMessage("File_exists_overwrite?");
281: int result = JOptionPane.showConfirmDialog(this ,
282: msg, title, JOptionPane.YES_NO_OPTION);
283: if (result == JOptionPane.YES_OPTION)
284: super .approveSelection();
285: } else
286: super .approveSelection();
287: }
288: };
289:
290: TSFileFilter[] filters = fcc.getFileFilters();
291: if (filters != null) {
292: for (int index = 0; index < filters.length; index++) {
293: chooser.addChoosableFileFilter(filters[index]);
294: }
295: }
296:
297: if (selected_file != null)
298: chooser.setSelectedFile(selected_file);
299:
300: int returnVal = chooser
301: .showSaveDialog(fcc.getParentComponent());
302: if (returnVal == JFileChooser.APPROVE_OPTION) {
303: File f = chooser.getSelectedFile();
304: String dir = "";
305: if (f.isDirectory())
306: dir = f.getPath();
307: else
308: dir = f.getParent();
309:
310: if (ftype != null) {
311: String last_dir_key = LAST_DIRECTORY + ftype;
312: userprops.setProperty(last_dir_key, dir);
313: }
314: userprops.setProperty(LAST_DIRECTORY, dir);
315: return f;
316: } else
317: return null;
318: }
319: }
|