001/* Interface for parameters that provide web export content.
002
003 Copyright (c) 2011-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 */
028
029package ptolemy.vergil.basic.export.web;
030
031import ptolemy.actor.gui.ColorAttribute;
032import ptolemy.data.BooleanToken;
033import ptolemy.data.expr.FileParameter;
034import ptolemy.data.expr.Parameter;
035import ptolemy.data.expr.StringParameter;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.util.Attribute;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.Settable;
042import ptolemy.util.FileUtilities;
043import ptolemy.util.StringUtilities;
044import ptolemy.vergil.basic.BasicGraphFrame;
045import ptolemy.vergil.basic.ExportParameters;
046
047///////////////////////////////////////////////////////////////////
048//// WebExportParameters
049/**
050 * Container for parameters that customize web export.
051 *
052 * @author Christopher Brooks and Edward A. Lee
053 * @version $Id$
054 * @since Ptolemy II 10.0
055 * @Pt.ProposedRating Red (cxh)
056 * @Pt.AcceptedRating Red (cxh)
057 */
058public class WebExportParameters extends Attribute {
059
060    /** Construct an attribute with the given name contained by the specified
061     *  entity.
062     *  @param container The container.
063     *  @param name The name of this attribute.
064     *  @exception IllegalActionException If the attribute is not of an
065     *   acceptable class for the container, or if the name contains a period.
066     *  @exception NameDuplicationException If the name coincides with
067     *   an attribute already in the container.
068     */
069    public WebExportParameters(NamedObj container, String name)
070            throws IllegalActionException, NameDuplicationException {
071        super(container, name);
072
073        _parameters = new ExportParameters();
074
075        directoryToExportTo = new FileParameter(this, "directoryToExportTo");
076        Parameter allowFiles = new Parameter(directoryToExportTo, "allowFiles");
077        allowFiles.setExpression("false");
078        allowFiles.setVisibility(Settable.NONE);
079        Parameter allowDirectories = new Parameter(directoryToExportTo,
080                "allowDirectories");
081        allowDirectories.setExpression("true");
082        allowDirectories.setVisibility(Settable.NONE);
083
084        backgroundColor = new ColorAttribute(this, "backgroundColor");
085
086        openCompositesBeforeExport = new Parameter(this,
087                "openCompositesBeforeExport");
088        openCompositesBeforeExport.setTypeEquals(BaseType.BOOLEAN);
089        openCompositesBeforeExport.setExpression("false");
090
091        runBeforeExport = new Parameter(this, "runBeforeExport");
092        runBeforeExport.setTypeEquals(BaseType.BOOLEAN);
093        runBeforeExport.setExpression("false");
094
095        showInBrowser = new Parameter(this, "showInBrowser");
096        showInBrowser.setTypeEquals(BaseType.BOOLEAN);
097        showInBrowser.setExpression("true");
098
099        copyJavaScriptFiles = new Parameter(this, "copyJavaScriptFiles");
100        copyJavaScriptFiles.setTypeEquals(BaseType.BOOLEAN);
101        copyJavaScriptFiles.setExpression("false");
102
103        imageFormat = new StringParameter(this, "imageFormat");
104        imageFormat.setExpression("gif");
105        imageFormat.addChoice("gif");
106        imageFormat.addChoice("png");
107        imageFormat.addChoice("jpg");
108
109        usePtWebsite = new Parameter(this, "usePtWebsite");
110        usePtWebsite.setTypeEquals(BaseType.BOOLEAN);
111        usePtWebsite.setExpression("false");
112        usePtWebsite.setVisibility(Settable.EXPERT);
113    }
114
115    ///////////////////////////////////////////////////////////////////
116    ////                         parameters                        ////
117
118    /** Background color. By default this is blank, which indicates
119     *  to use the background color of the model.
120     */
121    public ColorAttribute backgroundColor;
122
123    /** If true, then make an exported web page stand alone.
124     *  Instead of referencing JavaScript and image files on the
125     *  ptolemy.org website, if this parameter is true, then the
126     *  required files will be copied into the target directory.
127     *  This is a boolean that defaults to false.
128     */
129    public Parameter copyJavaScriptFiles;
130
131    /** The directory to export to. If a relative name is given,
132     *  then it is relative to the location of the model file.
133     *  By default, this is blank,
134     *  which will result in writing to a directory with name
135     *  equal to the sanitized name of the model,
136     *  and the directory will be contained in the same location
137     *  where the model that contains this attribute is stored.
138     */
139    public FileParameter directoryToExportTo;
140
141    /** The image format to use, which can be one of "gif" (the default),
142     *  "png", or "jpg".
143     */
144    public StringParameter imageFormat;
145
146    /** If true, hierarchically open all composite actors
147     *  in the model before exporting (so that these also
148     *  get exported, and hyperlinks to them are created).
149     *  This is a boolean that defaults to false.
150     */
151    public Parameter openCompositesBeforeExport;
152
153    /** If true, run the model before exporting (to open plotter
154     *  or other display windows that get exported). Note that
155     *  it is important the model have a finite run. This is a
156     *  boolean that defaults to false.
157     */
158    public Parameter runBeforeExport;
159
160    /** If true, open a web browser to display the resulting
161     *  export. This is a boolean that defaults to true.
162     */
163    public Parameter showInBrowser;
164
165    /** If true, use the server-side includes of the Ptolemy website.
166     *  This is a boolean that defaults to false. This parameter
167     *  is marked as an expert parameter, so by default, it is not
168     *  visible.
169     */
170    public Parameter usePtWebsite;
171
172    ///////////////////////////////////////////////////////////////////
173    ////                         public methods                    ////
174
175    /** React to a change in an attribute.  This method updates the
176     *  local data structure provided by {@link #getExportParameters()}.
177     *  @param attribute The attribute that changed.
178     *  @exception IllegalActionException If the change is not acceptable
179     *   to this container (not thrown in this base class).
180     */
181    @Override
182    public void attributeChanged(Attribute attribute)
183            throws IllegalActionException {
184        if (attribute == backgroundColor) {
185            if (!backgroundColor.getExpression().trim().equals("")) {
186                _parameters.backgroundColor = backgroundColor.asColor();
187            } else {
188                _parameters.backgroundColor = BasicGraphFrame.BACKGROUND_COLOR;
189            }
190        } else if (attribute == copyJavaScriptFiles) {
191            _parameters.copyJavaScriptFiles = ((BooleanToken) copyJavaScriptFiles
192                    .getToken()).booleanValue();
193        } else if (attribute == directoryToExportTo) {
194            _parameters.directoryToExportTo = directoryToExportTo.asFile();
195        } else if (attribute == imageFormat) {
196            _parameters.imageFormat = imageFormat.stringValue();
197        } else if (attribute == openCompositesBeforeExport) {
198            _parameters.openCompositesBeforeExport = ((BooleanToken) openCompositesBeforeExport
199                    .getToken()).booleanValue();
200        } else if (attribute == runBeforeExport) {
201            _parameters.runBeforeExport = ((BooleanToken) runBeforeExport
202                    .getToken()).booleanValue();
203        } else if (attribute == showInBrowser) {
204            _parameters.showInBrowser = ((BooleanToken) showInBrowser
205                    .getToken()).booleanValue();
206        } else if (attribute == usePtWebsite) {
207            _parameters.usePtWebsite = ((BooleanToken) usePtWebsite.getToken())
208                    .booleanValue();
209        } else {
210            super.attributeChanged(attribute);
211        }
212    }
213
214    /** Return the current parameter values in a data structure.
215     *  If a file name has not been specified, then substitute the
216     *  sanitized model name.
217     *  @return The current parameter values.
218     */
219    public ExportParameters getExportParameters() {
220        if (_parameters.directoryToExportTo == null) {
221            // FIXME: This next line seems to sometimes put quotation marks around the file name!
222            _parameters.directoryToExportTo = FileUtilities.nameToFile(
223                    StringUtilities.sanitizeName(getContainer().getName()),
224                    directoryToExportTo.getBaseDirectory());
225        }
226        return _parameters;
227    }
228
229    /** Return true if the parameter values are different from the defaults.
230     *  @return True if the parameter values are different from the defaults.
231     */
232    public boolean parametersChanged() {
233        return !_isMoMLSuppressed(1);
234    }
235
236    ///////////////////////////////////////////////////////////////////
237    ////                         private fields                    ////
238
239    /** The current parameter values. */
240    private ExportParameters _parameters;
241}