/* FileChooser actor enables the user to choose a file in the local file system.
*
* Copyright (c) 2004 The Regents of the University of California.
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
* OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
* UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* PT_COPYRIGHT_VERSION_2
* COPYRIGHTENDKEY
*/
package org.cipres.kepler;
// Ptolemy package
import ptolemy.actor.TypedAtomicActor;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.*;
import ptolemy.actor.TypedIOPort;
import ptolemy.data.BooleanToken;
import ptolemy.data.StringToken;
import ptolemy.data.type.BaseType;
import ptolemy.data.expr.Parameter;
import ptolemy.data.expr.FileParameter;
import ptolemy.data.expr.SingletonParameter;
import java.io.File;
// Cipres package
import org.cipres.kepler.registry.Globals;
import javax.swing.JFileChooser;
//////////////////////////////////////////////////////////////////////////
//// FileChooser
/**
* The FileChooser actor helps the user to specify a file and outputs the
* file name with full path. This actor has a outputFileName port for sending
* out the output file name. Two parameters can be set in this actor: useInputFileName
* helps the user to define if the input file name (got from the inputFileName
* input port) is going to be used as the initial value of the output file name;
* needConfirm helps the user to define if the input file name need to be
* confirmed (with the Java File Chooser Dialog) before being set directly as
* the output file name.
*
* @author Zhijie Guan
* @version $Id: $
*/
public class FileChooser extends TypedAtomicActor {
// TODO: assign a proper hash value to this
static final long serialVersionUID = 1L;
/** Construct a FileChooser actor with the given container and name.
* @param container The container.
* @param name The name of this actor.
* by the proposed container.
* @exception NameDuplicationException If the container already has an
* actor with this name.
*/
public FileChooser(CompositeEntity container, String name)
throws NameDuplicationException, IllegalActionException {
super(container, name);
// construct the useInputFileName parameter
// set it to false means we do not use input file name
useInputFileName = new Parameter(this, "useInputFileName", new BooleanToken(false));
useInputFileName.setDisplayName("Use Input File Name");
useInputFileName.setTypeEquals(BaseType.BOOLEAN);
// construct the needConfirm parameter
// set it to true since we want the user to specify the output file name
needConfirm = new Parameter(this, "needConfirm", new BooleanToken(true));
needConfirm.setDisplayName("Confirm the Output File Name");
needConfirm.setTypeEquals(BaseType.BOOLEAN);
needConfirm.setVisibility(Settable.NOT_EDITABLE);
// use the "hide" parameter to set it hidden
// hideNeedConfirm = new SingletonParameter(needConfirm, "hideNeedConfirm");
// hideNeedConfirm.setToken(BooleanToken.TRUE);
// construct the input port inputTrigger
inputFileName = new TypedIOPort(this, "inputFileName", true, false);
inputFileName.setDisplayName("Input File Name");
inputFileName.setTypeEquals(BaseType.GENERAL);
// use the "hide" parameter to set it hidden
hideInputFileName = new SingletonParameter(inputFileName, "_hide");
hideInputFileName.setToken(BooleanToken.TRUE);
// construct the output port
outputFileName = new TypedIOPort(this, "outputFileName", false, true);
//outputFileName.setDisplayName("File Name");
// Set the type constraint.
outputFileName.setTypeEquals(BaseType.STRING);
//outputFileName.setUseful(true);
new Parameter(outputFileName, "_showName", BooleanToken.TRUE);
// construct the parmeter fileNamePar
// set the default value of this parameter as an empty string ("")
// since this parameter should only accept a string value
fileNamePar = new FileParameter(this, "fileNamePar");
fileNamePar.setDisplayName("File Name");
_attachText("_iconDescription", "\n");
}
///////////////////////////////////////////////////////////////////
//// ports and parameters ////
/**
* This parameter decides if the inputFileName port is shown up and
* if the input file name is set as the initial value of the output
* file name. If this parameter is true, the inputFileName port will
* be displayed and the file name value got from it will be set as
* the initial value of the output file name in fileNamePar. If this
* parameter is false, the inputFileName port will be hidden and the
* actor will not read token from that port.
*/
public Parameter useInputFileName = null;
/**
* This parameter decides if the user needs to confirm the output
* file name setting. If this parameter's value is true, a dialog
* will pop up to let the user confirm the output file name value.
* If this parameter's value is false, the initial value in the
* fileNamePar will be used directly as the output file name.
* This parameter is associated with the useInputFileName parameter.
* That is, if the value of the useInputFileName is false, this
* parameter is hidden and set to be true since the user must set
* the output file name. If the value of the useInputFileName is
* true, this parameter is displayed and ready for the user's setting.
*/
public Parameter needConfirm = null;
/**
* An initial value of the file name (with path) is sent into
* This port is an input port of type GENERAL.
*/
public TypedIOPort inputFileName = null;
/**
* The specified file name is sent out through this output port.
* This port is an output port of type STRING.
*/
public TypedIOPort outputFileName = null;
/**
* The file name is displayed in this parameter. The user can also set
* the file name by clicking the "browse" button. When clicked, a file
* chooser dialog will pop up to let the user specify the file in the
* local file system.
*/
public FileParameter fileNamePar = null;
/**
* This parameter is used to control if the inputFileName port should be displayed.
*/
public SingletonParameter hideInputFileName = null;
/**
* This parameter is used to control if the needConfirm parameter should be displayed.
*/
// public SingletonParameter hideNeedConfirm = null;
///////////////////////////////////////////////////////////////////
//// functional variables ////
///////////////////////////////////////////////////////////////////
//// public methods ////
/** If the specified attribute is useInputFileName, then get
* the value of it and re-render the inputFileName port and the
* needConfirm parameter. If it is true, display the inputFileName
* port, enable the needConfirm parameter, and set needConfirm
* parameter to be false. If it is false, hide the inputFileName
* port and the needConfirm parameter, and set needConfirm parameter
* to be true.
* @param attribute The attribute that has changed.
* @exception IllegalActionException.
*/
public void attributeChanged(Attribute attribute)
throws IllegalActionException {
if (attribute == useInputFileName) {
boolean inputFlag = ((BooleanToken) useInputFileName.getToken())
.booleanValue();
if (inputFlag) { // useInputFileName is true
try {
hideInputFileName.setToken(BooleanToken.FALSE);
// System.out.println("Here we set the needConfirm to be false");
// BooleanToken tmpToken =(BooleanToken) needConfirm.getToken();
// while (tmpToken != null)
// tmpToken = (BooleanToken) needConfirm.getToken();
// needConfirm.setToken(BooleanToken.FALSE);
needConfirm.setVisibility(Settable.FULL);
// hideNeedConfirm.setToken(BooleanToken.FALSE);
} catch (Exception ndex) {
ndex.printStackTrace();
}
} else {
// List inPortList = this.inputPortList();
// Iterator ports = inPortList.iterator();
// while (ports.hasNext()) {
// IOPort p = (IOPort) ports.next();
// if (p.isInput()) {
try {
// if (p.getName().equals("inputFileName")) {
hideInputFileName.setToken(BooleanToken.TRUE);
needConfirm.setToken(BooleanToken.TRUE);
needConfirm.setVisibility(Settable.NOT_EDITABLE);
// hideNeedConfirm.setToken(BooleanToken.TRUE);
// }
} catch (Exception e) {
e.printStackTrace();
}
// }
// }//while
}// else
} else {
super.attributeChanged(attribute);
}
}// attributeChanged
/**
* Let the user specify the file name (with path) or use the input file
* name directly, according to the parameter settings in the actor.
*
* @exception IllegalActionException
* If it is thrown by the send() method sending out the
* token.
*/
public void fire() throws IllegalActionException {
super.fire();
String outputFile = null;
if ( ((BooleanToken)useInputFileName.getToken()).booleanValue() ) { // use the input file name
String filename;
if (inputFileName.numberOfSources() != 0)
filename = ((StringToken)inputFileName.get(0)).stringValue(); // get file name from input port
else
filename = ((StringToken)fileNamePar.getToken()).stringValue(); // get file name from the parameter
if ( ((BooleanToken)needConfirm.getToken()).booleanValue() ) { // need confirm for the output file name
// System.out.println("Here we get to confirm the output file name");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setSelectedFile(new File(filename));
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
outputFile = fileChooser.getSelectedFile().getAbsolutePath();
}
} else { // don't need the confirm, set the input file name directly to the output file name
outputFile = filename;
}
} else { // don't use the input file name
String nexusFileDir = Globals.getInstance().getRegistry().getDefaultInputFileDir();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setCurrentDirectory(new File(nexusFileDir));
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
outputFile = fileChooser.getSelectedFile().getAbsolutePath();
}
}
if ( (outputFile != null) && (outputFile.length() > 0) ) {
outputFileName.send(0, new StringToken(outputFile));
}
}
/** Post fire the actor. Return false to indicated that the
* process has finished. If it returns true, the process will
* continue indefinitely.
*/
public boolean postfire() {
return false;
}
}