001: package com.vividsolutions.jump.workbench.datasource;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Arrays;
006: import java.util.Collection;
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: import javax.swing.JFileChooser;
013: import javax.swing.filechooser.FileFilter;
014:
015: import com.vividsolutions.jump.util.Block;
016: import com.vividsolutions.jump.util.CollectionUtil;
017: import com.vividsolutions.jump.util.FileUtil;
018: import com.vividsolutions.jump.workbench.WorkbenchContext;
019: import com.vividsolutions.jump.workbench.ui.GUIUtil;
020: import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
021:
022: public abstract class LoadSaveDatasetFileMixin {
023: protected void setSelectedFormat(String selectedFormat) {
024: this .selectedFormat = selectedFormat;
025: }
026:
027: private String selectedFormat = "";
028:
029: protected String getSelectedFormat() {
030: return selectedFormat;
031: }
032:
033: protected Collection showDialog(JFileChooser fileChooser,
034: Class fileDataSourceQueryChooserClass,
035: WorkbenchContext context) {
036: try {
037: return showDialogProper(fileChooser,
038: fileDataSourceQueryChooserClass, context);
039: } catch (Exception e) {
040: throw new RuntimeException(e);
041: }
042: }
043:
044: protected abstract String getName();
045:
046: protected abstract String getLastDirectoryKey();
047:
048: private Collection showDialogProper(final JFileChooser fileChooser,
049: Class fileDataSourceQueryChooserClass,
050: WorkbenchContext context) throws IOException {
051: fileChooser.setDialogTitle(getName());
052: fileChooser.setCurrentDirectory(new File(
053: (String) PersistentBlackboardPlugIn.get(context).get(
054: getLastDirectoryKey(),
055: fileChooser.getCurrentDirectory()
056: .getCanonicalPath())));
057: fileChooser.setSelectedFile(initiallySelectedFile(fileChooser
058: .getCurrentDirectory()));
059: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
060: GUIUtil.removeChoosableFileFilters(fileChooser);
061: final Map fileFilterToFileDataSourceQueryChooserMap = new HashMap();
062: for (Iterator i = CollectionUtil.concatenate(
063: DataSourceQueryChooserManager.get(
064: context.getWorkbench().getBlackboard())
065: .getLoadDataSourceQueryChoosers(),
066: DataSourceQueryChooserManager.get(
067: context.getWorkbench().getBlackboard())
068: .getSaveDataSourceQueryChoosers()).iterator(); i
069: .hasNext();) {
070: DataSourceQueryChooser dataSourceQueryChooser = (DataSourceQueryChooser) i
071: .next();
072: if (!fileDataSourceQueryChooserClass
073: .isInstance(dataSourceQueryChooser)) {
074: continue;
075: }
076: FileDataSourceQueryChooser fileDataSourceQueryChooser = (FileDataSourceQueryChooser) dataSourceQueryChooser;
077: fileChooser
078: .addChoosableFileFilter(fileDataSourceQueryChooser
079: .getFileFilter());
080: fileFilterToFileDataSourceQueryChooserMap.put(
081: fileDataSourceQueryChooser.getFileFilter(),
082: fileDataSourceQueryChooser);
083: }
084: for (Iterator i = fileFilterToFileDataSourceQueryChooserMap
085: .keySet().iterator(); i.hasNext();) {
086: FileFilter fileFilter = (FileFilter) i.next();
087: if (fileFilter.getDescription().equals(selectedFormat)) {
088: fileChooser.setFileFilter(fileFilter);
089: }
090: }
091: if (JFileChooser.APPROVE_OPTION != fileChooser.showDialog(
092: context.getWorkbench().getFrame(), null)) {
093: return null;
094: }
095: PersistentBlackboardPlugIn.get(context).put(
096: getLastDirectoryKey(),
097: fileChooser.getCurrentDirectory().getCanonicalPath());
098: selectedFormat = fileChooser.getFileFilter().getDescription();
099: return CollectionUtil.collect(fileChooser
100: .isMultiSelectionEnabled() ? Arrays.asList(fileChooser
101: .getSelectedFiles()) : Collections
102: .singletonList(fileChooser.getSelectedFile()),
103: new Block() {
104: public Object yield(Object file) {
105: FileDataSourceQueryChooser fileDataSourceQueryChooser = ((FileDataSourceQueryChooser) fileFilterToFileDataSourceQueryChooserMap
106: .get(fileChooser.getFileFilter()));
107: return fileDataSourceQueryChooser
108: .toDataSourceQuery(addExtensionIfRequested(
109: (File) file,
110: fileDataSourceQueryChooser
111: .getExtensions()[0]));
112: }
113: });
114: }
115:
116: public abstract File initiallySelectedFile(File currentDirectory);
117:
118: private File addExtensionIfRequested(File file, String extension) {
119: return isAddingExtensionIfRequested() ? FileUtil
120: .addExtensionIfNone(file, extension) : file;
121: }
122:
123: public abstract boolean isAddingExtensionIfRequested();
124: }
|