001/*
002 * Copyright (c) 2005-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.resurgence.actor;
031
032import java.io.File;
033
034import ptolemy.actor.TypedAtomicActor;
035import ptolemy.actor.TypedIOPort;
036import ptolemy.actor.parameters.PortParameter;
037import ptolemy.data.StringToken;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043//////////////////////////////////////////////////////////////////////////
044//// DirectoryMaker
045
046/**
047 * This actor creates a new directory. The corresponding path is given out.
048 * 
049 * @author Wibke Sudholt, University and ETH Zurich, November 2004
050 * @version $Id: DirectoryMaker.java 24234 2010-05-06 05:21:26Z welker $
051 */
052public class DirectoryMaker extends TypedAtomicActor {
053
054        /**
055         * Construct a DirectoryMaker with the given container and name.
056         * 
057         * @param container
058         *            The container.
059         * @param name
060         *            The name of this actor.
061         * @exception IllegalActionException
062         *                If the entity cannot be contained by the proposed
063         *                container.
064         * @exception NameDuplicationException
065         *                If the container already has an actor with this name.
066         */
067        public DirectoryMaker(CompositeEntity container, String name)
068                        throws NameDuplicationException, IllegalActionException {
069                super(container, name);
070
071                trigger = new TypedIOPort(this, "trigger", true, false);
072                trigger.setTypeEquals(BaseType.UNKNOWN);
073                trigger.setMultiport(true);
074
075                path = new TypedIOPort(this, "path", false, true);
076                path.setTypeEquals(BaseType.STRING);
077
078                directory = new PortParameter(this, "Directory name");
079                directory.setStringMode(true);
080                directory.getPort().setTypeEquals(BaseType.STRING);
081
082                _attachText("_iconDescription", "<svg>\n"
083                                + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" "
084                                + "style=\"fill:white\"/>\n"
085                                + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10"
086                                + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n"
087                                + "</svg>\n");
088        }
089
090        // /////////////////////////////////////////////////////////////////
091        // // ports and parameters ////
092
093        /**
094         * The input port, which is a trigger.
095         */
096        public TypedIOPort trigger = null;
097        /**
098         * The output port, which contains the new directory path.
099         */
100        public TypedIOPort path = null;
101        /**
102         * The parameter, which is a string with the directory name.
103         */
104        public PortParameter directory = null;
105
106        // /////////////////////////////////////////////////////////////////
107        // // public methods ////
108
109        /**
110         * Create the new directory.
111         * 
112         * @exception IllegalActionException
113         *                If there's no director or if directory making does not
114         *                work.
115         */
116        public void fire() throws IllegalActionException {
117                super.fire();
118                for (int i = 0; i < trigger.getWidth(); i++) {
119                        if (trigger.hasToken(i)) {
120                                trigger.get(i);
121                        }
122                }
123                directory.update();
124                _dirName = ((StringToken)directory.getToken()).stringValue();
125                if (_dirName.length() > 0) {
126                        _dir = new File(_dirName);
127                        if (!_dir.exists()) {
128                                _mkdirsSuccess = _dir.mkdirs();
129                                if (!_mkdirsSuccess) {
130                                        throw new IllegalActionException(this, "Directory " + _dir
131                                                        + " was not successfully made.");
132                                }
133                        } else {
134                                if (!_dir.isDirectory()) {
135                                        throw new IllegalActionException(this, _dir
136                                                        + " exists and is not a directory.");
137                                }
138                        }
139                        try {
140                                _dirName = _dir.getCanonicalPath();
141                        } catch (Exception ex) {
142                                _debug("Cannot get directory path.");
143                        }
144                }
145                path.send(0, new StringToken(_dirName));
146        }
147
148        // /////////////////////////////////////////////////////////////////
149        // // protected members ////
150
151        // /////////////////////////////////////////////////////////////////
152        // // private methods ////
153
154        // /////////////////////////////////////////////////////////////////
155        // // private members ////
156
157        private File _dir;
158        private boolean _mkdirsSuccess;
159        private String _dirName;
160}