/* ========================
* 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:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: JDialogFile.java,v 1.2 2006/09/27 13:10:23 booba_skaya Exp $
*
* Changes
* -------
* 18 sept. 06 : Initial public release (CC);
*
*/
package simtools.logging.ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* Class JDialogFile
* This class is a dialog that permits to select a file.
* It will be used both for saving a opening actions.
* It first displays a textField, that permits to enter the path to the file.
* If the user wants to browse, he has just to push the browse button.
* The textField will be filled.
*/
public class JDialogFile extends JDialog {
public static final String RESOURCE_KEY = "JDialogFile" ;
/**(<b>String</b>) TYPE_OPEN: The open type dialog.*/
public static final String TYPE_OPEN = "open"+RESOURCE_KEY;
/**(<b>String</b>) TYPE_SAVE: The save type dialog.*/
public static final String TYPE_SAVE = "save"+RESOURCE_KEY;
/**(<b>String</b>) TYPE_UNKNOWN: The unknown type dialog.*/
public static final String TYPE_UNKNOWN = "unknown"+RESOURCE_KEY;
/**(<b>String[]</b>) LOGGING_FILE_EXTENSIONS_OPEN: the array of the accepted file extension for the logging viewer for open action.*/
private static final String[] LOGGING_FILE_EXTENSIONS_OPEN = new String[]{"logb", "LOGB", "xml", "XML"};
/**(<b>String[]</b>) LOGGING_FILE_EXTENSIONS_SAVE: the array of the accepted file extension for the logging viewer for open action.*/
private static final String[] LOGGING_FILE_EXTENSIONS_SAVE = new String[]{"logb", "LOGB"};
/**(<b>String[]</b>) LOGGING_FILE_EXTENSIONS_UNKNOW: the array of the accepted file extension for the logging viewer for open action.*/
private static final String[] LOGGING_FILE_EXTENSIONS_UNKNOW = new String[]{""};
/**(<b>JButton</b>) jButtonBrowse the button that permits to browse.*/
private JButton jButtonBrowse;
/**(<b>int</b>) TEXTFIELD_WIDTH: the width of the textField to enter the path to file.*/
private static final int TEXTFIELD_WIDTH = 30;
/**(<b>JTextField</b>) jTextFieldFile: The jtextField that contains the path to the file.*/
private JTextField jTextFieldFile;
/**(<b>boolean</b>) validationStatus: permits to know if users validates the dialog.*/
private boolean validationStatus;
/**(<b>String</b>) type: the type of the dialog.*/
private String type;
/**
* Contructor JDialogFile
* <br><b>Summary:</b><br>
* The constructor of the class JDialogFile.
*/
public JDialogFile(){
this(TYPE_UNKNOWN);
}
/**
* Contructor JDialogFile
* <br><b>Summary:</b><br>
* The constructor of the class JDialogFile.
* @param type The type of the JDialogFile.
*/
public JDialogFile(String type){
//construct the JDIalog, using the good title, and set it modal.
super(Viewer.instance, Viewer.instance.getString(type+"Title"), true);
this.type = type;
//Construct the Jbutton.
jButtonBrowse = new JButton(new BrowseViewerAction());
//add the button
getContentPane().setLayout(new BorderLayout());
getContentPane().add(constructMainPanel(), BorderLayout.CENTER);
getContentPane().add(constructButtonPanel(), BorderLayout.SOUTH);
pack();
setLocationRelativeTo(Viewer.instance);
}
/**
* Method constructButtonPanel
* <br><b>Summary:</b><br>
* This method construct the JButton panel, that contains the Ok and the cancel button.
* @return <b>(JPanel)</b> the JButton panel, that contains the Ok and the cancel button.
*/
private JPanel constructButtonPanel() {
JPanel jButtonPanel = new JPanel();
jButtonPanel.add(new JButton(new OKViewerAction()));
jButtonPanel.add(new JButton(new CancelViewerAction()));
return jButtonPanel;
}
/**
* Method constructMainPanel
* <br><b>Summary:</b><br>
* This method contructs the mainPanel.
*/
private JPanel constructMainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.add(jButtonBrowse);
//create the textField.
jTextFieldFile = new JTextField(TEXTFIELD_WIDTH);
mainPanel.add(jTextFieldFile);
return mainPanel;
}
/**
* Class BrowseViewerAction
* The action to browse.
*/
private class BrowseViewerAction extends AbstractViewerAction{
public BrowseViewerAction(){
super(type);
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
browseAction();
}
}
/**
* Method browseAction
* <br><b>Summary:</b><br>
* Browse folders.
*/
private void browseAction(){
//Retrieve previous opened DIR in userProperties.
String folder = Viewer.instance.userProperties.getProperty(Viewer.PROPERTY_FOLDER, "");
//Show a JFileChooser and fill the textField using the result.
JFileChooser chooser = null;
//Use the saved properties FOLDER to open the JFileChooser in the previous opened folder.
if(folder != null && !folder.equals("")){
chooser = new JFileChooser(folder);
}else{
//If folder was not set, use default home folder.
chooser = new JFileChooser();
}
chooser.setFileFilter(new LoggingFileFilter(getFileExtensions(),Viewer.instance.getString("fileDescription")));
//show the fileChooser.
int result = chooser.showDialog(Viewer.instance, Viewer.instance.getString(type+"Action"));
//check validation status of the JFileChooser.
if(result == JFileChooser.APPROVE_OPTION){
jTextFieldFile.setText(chooser.getSelectedFile().getPath());
}//if no file has been selected, do nothing.
}
/**
* Method getFileExtensions
* <br><b>Summary:</b><br>
* this method the file extensions string array allowed by the JDialog type.
* @return <b>(String[])</b> A String[] the file extensions string array allowed by the JDialog type.
*/
private String[] getFileExtensions() {
//The result of the method
String[] result = null;
if(type.equals(TYPE_OPEN)){
result = LOGGING_FILE_EXTENSIONS_OPEN;
}else if(type.equals(TYPE_SAVE)){
result = LOGGING_FILE_EXTENSIONS_SAVE;
}else if(type.equals(TYPE_UNKNOWN)){
result = LOGGING_FILE_EXTENSIONS_UNKNOW;
}
//return the result
return result;
}
/**
* Class OKViewerAction
* The action to press OKButton.
*/
private class OKViewerAction extends AbstractViewerAction{
public OKViewerAction(){
super("okButton");
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
okAction();
}
}
/**
* Method okAction
* <br><b>Summary:</b><br>
* This method validates the dialog.
*/
private void okAction() {
validationStatus = true;
dispose();
}
/**
* Class CancelViewerAction
* The action to press cancelButton.
*/
private class CancelViewerAction extends AbstractViewerAction{
public CancelViewerAction(){
super("cancelButton");
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
cancelAction();
}
}
/**
* Method okAction
* <br><b>Summary:</b><br>
* This method validates the dialog.
*/
private void cancelAction() {
validationStatus = false;
dispose();
}
/**
* Method hasBeenValidated
* <br><b>Summary:</b><br>
* This method returns true if user validates the dialog.
* @return <b>(boolean)</b> true if user validates the dialog.
*/
public boolean hasBeenValidated(){
return validationStatus;
}
public File getResult(){
//the result of the method.
File result = null;
if(hasBeenValidated()){
//retrieve the file in the JTextField.
String choosenFile = jTextFieldFile.getText();
if(choosenFile != null && !choosenFile.equals("")){
result = new File(choosenFile);
}
}
//return the result
return result;
}
}