/* ========================
* 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-2005, by :
* Corporate:
* Astrium SAS
* Individual:
* Ronan Ogor
*
* $Id: LoaderHandler.java,v 1.3 2008/09/29 10:06:38 ogor Exp $
*
* Changes
* -------
* 14 juin 2005 : Creation date (RO);
*
*/
package syn3d.nodes;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import simtools.ui.AggregateFileFilter;
public abstract class LoaderHandler implements Serializable {
protected HashMap classes=null;
protected JFileChooser chooser = null;
protected HashMap filters= null;
protected AggregateFileFilter aggregateFilter=null;
protected String[][] extDescClass = null;
protected File selectedFile=null;
protected Class loader=null;
public boolean select(){
if(chooser==null||filters==null
||chooser.showOpenDialog(null)!=JFileChooser.APPROVE_OPTION){
return false;
}
File f=chooser.getSelectedFile();
FileFilter ff=chooser.getFileFilter();
if(ff==aggregateFilter){
ff=aggregateFilter.getFilterForFile(null, f);
}
String ext=(String)filters.get(ff);
if(ext==null){
return false;
}
Class c=(Class)classes.get(ext);
if(c==null){
return false;
}
selectedFile=f;
loader=c;
return true;
}
public abstract boolean load();
protected void setFileChooser(){
// check availability of the loaders and generate
// a file chooser accordingly
for(int i=0;i<extDescClass.length;i++){
String className=extDescClass[i][2];
Class c;
try {
c = Class.forName(className);
if(classes==null){
classes=new HashMap();
}
classes.put(extDescClass[i][0],c);
if(chooser==null){
chooser=new JFileChooser();
chooser.setAcceptAllFileFilterUsed(false); // don't want all
filters=new HashMap();
}
final int r=i;
FileFilter ff=new FileFilter(){
public boolean accept(File f) {
if (f.isDirectory())
return true;
String name = f.getName();
return name.toLowerCase().endsWith(
extDescClass[r][0]);
}
public String getDescription() {
return extDescClass[r][1];
}
};
filters.put(ff,extDescClass[i][0]);
chooser.addChoosableFileFilter(ff);
}
catch (ClassNotFoundException cnfe) {
}
}
if(chooser!=null && filters.size()>1){
aggregateFilter=new AggregateFileFilter(chooser.getChoosableFileFilters());
chooser.addChoosableFileFilter(aggregateFilter);
chooser.setFileFilter(aggregateFilter);
}
}
}