/* ========================
* 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-2008, by :
* Corporate:
* EADS Astrium
* Individual:
* Claude Cazenave
*
* $Id: ImageSelectorPanel.java,v 1.2 2008/10/16 17:14:41 cazenave Exp $
*
* Changes
* -------
* 14 oct. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.media.j3d.SceneGraphObject;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import simtools.ui.ImageMapper;
import simtools.ui.ImagePreview;
import simtools.util.CurrentPathProvider;
import jsynoptic.plugins.java3d.edit.PropertyEdit;
public abstract class ImageSelectorPanel<T extends SceneGraphObject>
extends JPanel implements PropertyEdit.UndoRedoListener,
PropertiesPanel.Editor<T,Object>, ActionListener {
/**
* The file chooser common to all image selector
*/
protected static JFileChooser fileChooser;
/**
* The button to start file selection
*/
protected final JButton bChoose;
/**
* A text field to display current file name
*/
protected final JTextField tName;
/**
* The button to remove selected file if any
*/
protected final JButton bClear;
/**
* The current image, can be null, a image reference or a file or a URL
*/
protected Object currentImage;
public ImageSelectorPanel(){
super(new BorderLayout());
this.setBorder(BorderFactory.createEtchedBorder());// TODO i18n
bChoose = new JButton("Choose..."); // TODO i18n
bChoose.addActionListener(this);
if (fileChooser == null) {
fileChooser = new JFileChooser();
// Add a custom file filter and disable the default
// (Accept All) file filter.
fileChooser.addChoosableFileFilter(new ImageMapper.ImageFileFilter());
fileChooser.setAcceptAllFileFilterUsed(false);
// Add the preview pane.
fileChooser.setAccessory(new ImagePreview(fileChooser));
// Set image file chooser directory
fileChooser.setCurrentDirectory(CurrentPathProvider.currentPathProvider.getCurrentPath());
}
tName=new JTextField(20);
tName.setEditable(false);
bClear = new JButton("Clear"); // TODO i18n
bClear.addActionListener(this);
JPanel pb=new JPanel();
pb.add(bChoose);
pb.add(bClear);
add(BorderLayout.SOUTH,pb);
JPanel pt=new JPanel();
pt.add(tName);
add(BorderLayout.NORTH,pt);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bChoose){
File currentFile=null;
if(currentImage instanceof File){
currentFile=(File)currentImage;
}
fileChooser.setSelectedFile(currentFile);
// Show it.
// TODO i18n
int returnVal = fileChooser.showDialog(this, "SelectImage");
// Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
currentFile = fileChooser.getSelectedFile();
if (currentFile != null) {
load(currentFile);
}
}
}
else if(e.getSource()==bClear){
load(null);
}
}
protected abstract void load(File imageFile);
protected void update(Object image){
String name="";
if(image instanceof File){
name=((File)image).getAbsolutePath();
}
else if(image!=null){
name=image.toString();
}
tName.setText(name);
bClear.setEnabled(name.length()>0);
currentImage=image;
}
}