001/* A style for parameters that can be specified using a FileChooser.
002
003 Copyright (c) 2001-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.gui.style;
029
030import java.io.File;
031import java.net.URI;
032
033import ptolemy.actor.gui.PtolemyQuery;
034import ptolemy.data.BooleanToken;
035import ptolemy.data.Token;
036import ptolemy.data.expr.Parameter;
037import ptolemy.kernel.attributes.URIAttribute;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.Settable;
042import ptolemy.kernel.util.StringAttribute;
043import ptolemy.kernel.util.Workspace;
044
045///////////////////////////////////////////////////////////////////
046//// FileChooserStyle
047
048/**
049 This attribute annotates user settable attributes to specify
050 that the value of the parameter can be optionally given using a
051 FileChooser.
052
053 @see ptolemy.actor.gui.EditorPaneFactory
054 @author Steve Neuendorffer and Edward A. Lee
055 @version $Id$
056 @since Ptolemy II 2.0
057 @Pt.ProposedRating Yellow (eal)
058 @Pt.AcceptedRating Red (cxh)
059 */
060public class FileChooserStyle extends ParameterEditorStyle {
061    /** Construct an attribute in the default workspace with an empty string
062     *  as its name.
063     *  The object is added to the directory of the workspace.
064     *  Increment the version number of the workspace.
065     */
066    public FileChooserStyle() {
067        super();
068    }
069
070    /** Construct an attribute in the given workspace with an empty string
071     *  as its name.
072     *  The object is added to the directory of the workspace.
073     *  Increment the version number of the workspace.
074     *  @param workspace The workspace that will contain the attribute
075     *  that is being constructed.
076     */
077    public FileChooserStyle(Workspace workspace) {
078        // This constructor is needed for Shallow codegen to work.
079        super(workspace);
080    }
081
082    /** Construct an attribute with the specified container and name.
083     *  @param container The container.
084     *  @param name The name of the attribute.
085     *  @exception IllegalActionException If the attribute is not of an
086     *   acceptable attribute for the container, or if the container
087     *   is not an instance of Settable.
088     *  @exception NameDuplicationException If the name coincides with
089     *   an attribute already in the container.
090     */
091    public FileChooserStyle(NamedObj container, String name)
092            throws IllegalActionException, NameDuplicationException {
093        super(container, name);
094    }
095
096    ///////////////////////////////////////////////////////////////////
097    ////                         public methods                    ////
098
099    /** Return true if this style is acceptable for the given parameter.
100     *  @param param The attribute that this annotates.
101     *  @return True if the argument is a StringAttribute, false otherwise.
102     */
103    @Override
104    public boolean acceptable(Settable param) {
105        if (!(param instanceof StringAttribute)) {
106            return false;
107        } else {
108            return true;
109        }
110    }
111
112    /** Create a new entry in the given query associated with the
113     *  attribute containing this style.  The name of the entry is
114     *  the name of the attribute.  Attach the attribute to the created entry.
115     *  @param query The query into which to add the entry.
116     *  @exception IllegalActionException If thrown when accessing parameters
117     *   specifying whether files or directories should be listed.
118     */
119    @Override
120    public void addEntry(PtolemyQuery query) throws IllegalActionException {
121        Settable container = (Settable) getContainer();
122        String name = container.getName();
123        String defaultValue = container.getExpression();
124        defaultValue = container.getExpression();
125
126        URI modelURI = URIAttribute.getModelURI(this);
127        File directory = null;
128
129        if (modelURI != null) {
130            if (modelURI.getScheme().equals("file")) {
131                File modelFile = new File(modelURI);
132                directory = modelFile.getParentFile();
133            }
134        }
135
136        // Check to see whether the attribute being configured
137        // specifies whether files or directories should be listed.
138        // By default, only files are selectable.
139        boolean allowFiles = true;
140        boolean allowDirectories = false;
141
142        if (container instanceof NamedObj) {
143            Parameter marker = (Parameter) ((NamedObj) container)
144                    .getAttribute("allowFiles", Parameter.class);
145
146            if (marker != null) {
147                Token value = marker.getToken();
148
149                if (value instanceof BooleanToken) {
150                    allowFiles = ((BooleanToken) value).booleanValue();
151                }
152            }
153
154            marker = (Parameter) ((NamedObj) container)
155                    .getAttribute("allowDirectories", Parameter.class);
156
157            if (marker != null) {
158                Token value = marker.getToken();
159
160                if (value instanceof BooleanToken) {
161                    allowDirectories = ((BooleanToken) value).booleanValue();
162                }
163            }
164        }
165
166        // FIXME: What to do when neither files nor directories are allowed?
167        if (!allowFiles && !allowDirectories) {
168            // The given attribute will not have a query in the dialog.
169            return;
170        }
171
172        query.addFileChooser(name, container.getDisplayName(), defaultValue,
173                modelURI, directory, allowFiles, allowDirectories,
174                PtolemyQuery.preferredBackgroundColor(container),
175                PtolemyQuery.preferredForegroundColor(container));
176        query.attachParameter(container, name);
177    }
178}