001/*
002 * Copyright (c) 2014 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2014-09-28 17:57:53 +0000 (Sun, 28 Sep 2014) $' 
007 * '$Revision: 32976 $'
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 */
029package org.kepler.cloudsharing.actor;
030
031import java.util.HashSet;
032import java.util.Set;
033
034import ptolemy.actor.TypedAtomicActor;
035import ptolemy.actor.TypedIOPort;
036import ptolemy.data.StringToken;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.Workspace;
042
043/** An actor that records file names to be uploaded to the cloud.
044 *  The inputs to the actor are names of files that will be uploaded
045 *  to the cloud when the Share To Cloud button is pressed on the
046 *  toolbar.
047 * 
048 *  @author Daniel Crawl
049 *  @version $Id: ShareToCloud.java 32976 2014-09-28 17:57:53Z crawl $
050 * 
051 */
052public class ShareToCloud extends TypedAtomicActor {
053
054    /** Create a new ShareToCloud in the specified workspace. */
055    public ShareToCloud(Workspace workspace) {
056        super(workspace);
057    }
058
059    /** Create a new ShareToCloud in specified container with the specified name. */
060    public ShareToCloud(CompositeEntity container, String name)
061            throws IllegalActionException, NameDuplicationException {
062        super(container, name);
063        
064        input = new TypedIOPort(this, "input", true, false);
065        input.setTypeEquals(BaseType.STRING);
066        input.setMultiport(true);
067    }
068    
069    /** Clone this actor into the specified workspace. */
070    @Override
071    public Object clone(Workspace workspace) throws CloneNotSupportedException {
072        
073        ShareToCloud newObject = (ShareToCloud)super.clone(workspace);
074        newObject._files = new HashSet<String>();
075        return newObject;
076    }
077
078    /** Get the file names to be uploaded. */
079    public Set<String> getFiles() {
080        Set<String> files = new HashSet<String>(_files);
081        _files.clear();
082        return files;
083    }
084    
085    /** Read the set of file names. */
086    @Override
087    public void fire() throws IllegalActionException {
088        
089        super.fire();
090        
091        for(int i = 0; i < input.getWidth(); i++) {
092            _files.add(((StringToken)input.get(i)).stringValue());
093        }
094        
095    }
096    
097    /** Clear the set of file names. */
098    @Override
099    public void preinitialize() throws IllegalActionException {
100        
101        super.preinitialize();
102        
103        _files.clear();
104    }
105    
106    /** The name of a file to be uploaded to the cloud. */
107    public TypedIOPort input;
108    
109    /** The set of file names to be uploaded. */
110    private Set<String> _files = new HashSet<String>();
111    
112}