/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2006, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Ronan Ogor
*
* $Id: DataSourceProvider.java,v 1.9 2007/06/05 14:29:23 ogor Exp $
*
* Changes
* -------
*
*/
package jsynoptic.data;
import java.io.File;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import jsynoptic.base.Plugin;
import jsynoptic.ui.JSynoptic;
import jsynoptic.ui.Run;
import simtools.data.FileDataSourceProvider;
import simtools.data.DataInfo;
import simtools.data.DataSourceCollection;
import simtools.ui.BasicMessageWriter;
import simtools.ui.ResourceFinder;
import simtools.util.CurrentPathProvider;
import simtools.util.FileSerializer;
/**
* This extension of BasicDataSourceProvider is able to display graphical or batch user interfaces for file providing prosses
* @author zxpletran007
*
*/
public abstract class DataSourceProvider extends FileDataSourceProvider{
public static BasicMessageWriter resources = ResourceFinder.getMessages((DataSourceProvider.class));
protected JFileChooser fileChooser;
public DataSourceProvider(FileFilter filter, String marker){
super(filter, marker);
}
/**
* Allow sub classes to initialise fileChooser
*/
protected void initializeFileChooser(){}
/* (non-Javadoc)
* @see simtools.data.BasicDataSourceProvider#chooseUseCollection(simtools.data.DataSourceCollection, java.lang.String, java.lang.String)
*/
public int chooseUseCollection(DataSourceCollection existingDsc, String dscId, String id) {
if (JSynoptic.gui!=null) {
return JOptionPane.showConfirmDialog(
JSynoptic.gui.getOwner(),
new String[] {
resources.print0args("dscReplaceMsg1"),
id,
resources.print0args("dscReplaceMsg2"),
DataInfo.getId(existingDsc),
resources.print0args("dscReplaceMsg3"),
dscId
},
resources.print0args("dscReplaceTitle"),
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE
);
}else {
return super.chooseUseCollection(existingDsc, dscId, id);
}
}
/* (non-Javadoc)
* @see simtools.data.BasicDataSourceProvider#chooseUseCollectionIfMissingDs(java.lang.String, java.lang.String)
*/
protected int chooseUseCollectionIfMissingDs(String dscId, String dsMissingId){
if (JSynoptic.gui!=null) {
if (!JSynoptic.gui.getIsOpenProcessCancelled()){
int answer = JOptionPane.showConfirmDialog(JSynoptic.gui.getOwner(), resources.print2args("forceOpening",dsMissingId, dscId));
if (answer== NO_OPTION){
setChooseFileAgain(true);
}else if (answer == CANCEL_OPTION){
JSynoptic.gui.setIsOpenProcessCancelled(true);
setChooseFileAgain(false);
}
return answer;
}
}
return super.chooseUseCollectionIfMissingDs(dscId, dsMissingId);
}
/* (non-Javadoc)
* @see simtools.data.BasicDataSourceProvider#chooseFile(java.lang.String, java.lang.String)
*/
public File chooseFile(String fileName, String reason) {
if (JSynoptic.gui==null) {
return FileSerializer.readFromString(fileName, CurrentPathProvider.currentPathProvider.getCurrentPath());
}else {
if (fileChooser==null){
fileChooser = new JFileChooser();
initializeFileChooser();
if (filter!=null){
fileChooser.setFileFilter(filter);
// Add an accessory panel
Plugin plugin=null;
for (int i = 0; i < Run.plugins.size(); ++i) {
Plugin p = (Plugin) Run.plugins.get(i);
FileFilter[] fft = p.getFileFilters(Plugin.OPEN);
if (fft == null)
continue;
for (int j = 0; j < fft.length; j++) {
if (filter.equals(fft[j]))
plugin = p;
}
}
if (plugin!=null){
JComponent accessory = plugin.getOptionPanelForFilter(filter);
if (accessory != null) {
fileChooser.setAccessory(accessory);
}
}
}
}
if (!JSynoptic.gui.getIsOpenProcessCancelled()){
fileChooser.setDialogTitle(resources.print2args("enterFile", fileName, reason));
File currentPath = CurrentPathProvider.currentPathProvider.getCurrentPath();
File file =FileSerializer.readFromString(fileName, CurrentPathProvider.currentPathProvider.getCurrentPath());
// If no parent file, set the default file with the currentPath
if ( file!=null && file.exists())
fileChooser.setSelectedFile(file);
else
fileChooser.setCurrentDirectory(currentPath);
int answer = fileChooser.showDialog(JSynoptic.gui.getOwner(),null);
if (answer==JFileChooser.APPROVE_OPTION) {
return fileChooser.getSelectedFile();
} else {
JSynoptic.gui.setIsOpenProcessCancelled(true);
setChooseFileAgain(false);
}
}
}
return null;
}
}