/* NexusFileReader actor reads a Nexus file from the 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.FileParameter; import ptolemy.data.expr.Parameter; import java.io.File; // Cipres package import org.cipres.kepler.registry.Globals; import org.cipres.util.CipresFile; import javax.swing.JFileChooser; ////////////////////////////////////////////////////////////////////////// //// NexusFileReader /** * The NexusFileReader actor reads a Nexus file from the local file system * and sends the file content as a string token. * * @author Zhijie Guan * @version $Id: NexusFileReader.java,v 1.2 2006/04/26 00:43:04 guan Exp $ */ public class NexusFileReader extends TypedAtomicActor { // TODO: assign a proper hash value to this static final long serialVersionUID = 1L; /** Construct a NexusFileReader 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 NexusFileReader(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { super(container, name); // construct the output ports outputFileContent and outputFileName outputFileContent = new TypedIOPort(this, "outputFileContent", false, true); //outputFileContent.setDisplayName("File Content"); outputFileName = new TypedIOPort(this, "outputFileName", false, true); //outputFileName.setDisplayName("File Name"); // Set the type constraint. outputFileContent.setTypeEquals(BaseType.GENERAL); outputFileName.setTypeEquals(BaseType.STRING); //outputFileContent.setUseful(true); //outputFileName.setUseful(true); new Parameter(outputFileContent, "_showName", BooleanToken.TRUE); new Parameter(outputFileName, "_showName", BooleanToken.TRUE); // construct the parameter 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("Nexus File Name"); new Parameter(fileNamePar, "_showName", BooleanToken.TRUE); _attachText("_iconDescription", "\n" + "\n" + "\n"); } /////////////////////////////////////////////////////////////////// //// ports and parameters //// /** * The Nexus file content is sent out through this output port. * This port is an output port of type GENERAL. */ public TypedIOPort outputFileContent = null; /** * The file name is sent out through this output port. * This port is an output port of type STRING. */ public TypedIOPort outputFileName = null; /** * The Nexus file name is set in this parameter. The NexusFileReader * actor opens and reads the file specified by this parameter and sends * out the content through the outputFileContent port. If the user * leaves this parameter empty, the NexusFileReader will pop up a file * chooser dialog to let the user specify the file in the local file * system. */ public FileParameter fileNamePar = null; /////////////////////////////////////////////////////////////////// //// functional variables //// /////////////////////////////////////////////////////////////////// //// public methods //// /** Send the content of the Nexus file to the output. * @exception IllegalActionException If it is thrown by the * send() method sending out the token. */ public void fire() throws IllegalActionException { super.fire(); // get the nexus file name String fileNameStr = ((StringToken)fileNamePar.getToken()).stringValue(); if (fileNameStr.length() == 0) { // if the file name is empty, the actor pops up a file chooser dialog String nexusFileDir = Globals.getInstance().getRegistry().getDefaultInputFileDir(); JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Choose Nexus File"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setCurrentDirectory(new File(nexusFileDir)); if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { fileNameStr = fileChooser.getSelectedFile().getAbsolutePath(); } } try { // This block is for sending file content CipresFile cf = new CipresFile(fileNameStr); StringToken fileContentToken = new StringToken(cf.getContent()); outputFileContent.send(0,fileContentToken); outputFileName.send(0, new StringToken(fileNameStr)); } catch (Exception ex) { ex.printStackTrace(); } } /** 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; } }