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 java.util.List;
032import java.util.Locale;
033
034import ptolemy.data.BooleanToken;
035import ptolemy.data.expr.Parameter;
036import ptolemy.data.expr.StringParameter;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.NamedObj;
042
043///////////////////////////////////////////////////////////////////
044//// DefaultTitle
045/**
046 * A parameter specifying default title to associate
047 * with a model and with components in the model.
048 * By default, this attribute uses the model name
049 * and component names as the title.
050 *
051 * @author Edward A. Lee
052 * @version $Id$
053 * @since Ptolemy II 10.0
054 * @Pt.ProposedRating Red (cxh)
055 * @Pt.AcceptedRating Red (cxh)
056 */
057public class DefaultTitle extends WebContent implements WebExportable {
058
059    /** Create an instance of this parameter.
060     *  @param container The container.
061     *  @param name The name.
062     *  @exception IllegalActionException If the superclass throws it.
063     *  @exception NameDuplicationException If the superclass throws it.
064     */
065    public DefaultTitle(NamedObj container, String name)
066            throws IllegalActionException, NameDuplicationException {
067        super(container, name);
068
069        _icon.setIconText("T");
070        displayText
071                .setExpression("Default title to give to icons in the model.");
072
073        showTitleInHTML = new Parameter(this, "showTitleInHTML");
074        showTitleInHTML.setExpression("true");
075        showTitleInHTML.setTypeEquals(BaseType.BOOLEAN);
076
077        include = new StringParameter(this, "include");
078        include.addChoice("Entities");
079        include.addChoice("Attributes");
080        include.addChoice("All");
081        include.addChoice("None");
082        include.setExpression("Entities");
083
084        instancesOf = new StringParameter(this, "instancesOf");
085    }
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         parameters                        ////
089
090    /** If non-empty (the default), specifies a class name.
091     *  Only entities or attributes (depending on <i>include</i>)
092     *  implementing the specified
093     *  class will be assigned the title defined by this
094     *  DefaultTitle parameter.
095     */
096    public StringParameter instancesOf;
097
098    /** Specification of whether to provide the title for
099     *  Attributes, Entities, or both. This is either "Entities" (the
100     *  default), "Attributes", "All", or "None".
101     */
102    public StringParameter include;
103
104    /** If set to true, then the title given by this parameter
105     *  will be shown in the HTML prior to the image of the model
106     *  (as well as in the image of the model, if it is visible
107     *  when the export to web occurs). This is a boolean that
108     *  defaults to true.
109     */
110    public Parameter showTitleInHTML;
111
112    ///////////////////////////////////////////////////////////////////
113    ////                         public methods                    ////
114
115    /** A title is of type text/html.
116     *
117     * @return The string text/html
118     */
119    @Override
120    public String getMimeType() {
121        return "text/html";
122    }
123
124    /** Return true, since new title content should overwrite old title content.
125     *
126     * @return True, since new title content should overwrite old title content.
127     */
128    @Override
129    public boolean isOverwriteable() {
130        return true;
131    }
132
133    ///////////////////////////////////////////////////////////////////
134    ////                         protected methods                 ////
135
136    /** Provide content to the specified web exporter to be
137     *  included in a web page. This class provides a default title for the
138     *  web page and for each object
139     *  as specified by <i>include</i> and <i>instancesOf</i>.
140     *
141     *  @param exporter  The web exporter to which to write content.
142     *  @exception IllegalActionException If something is wrong with the web
143     *  content or the object already has an attribute with the same name as the
144     *  the created WebAttribute
145     */
146    @Override
147    protected void _provideAttributes(WebExporter exporter)
148            throws IllegalActionException {
149
150        WebAttribute webAttribute;
151
152        // Set the title for the model object.
153        String titleValue = stringValue();
154        if (titleValue == null || titleValue.equals("")) {
155            // Use the model name as the default title.
156            titleValue = toplevel().getDisplayName();
157        }
158
159        // FIXME:  Refactor so we don't need this method
160        exporter.setTitle(titleValue,
161                ((BooleanToken) showTitleInHTML.getToken()).booleanValue());
162
163        // Create a WebAttribute for title and add to exporter.
164        // Content should only be added once (onceOnly -> true).
165        /* title attribute is now used for displaying parameter table.
166        webAttribute = WebAttribute.createWebAttribute(getContainer(),
167                "titleWebAttribute", "title");
168        webAttribute.setExpression(titleValue);
169        exporter.defineAttribute(webAttribute, true);
170         */
171
172        boolean entities = false, attributes = false;
173        String includeValue = include.stringValue()
174                .toLowerCase(Locale.getDefault());
175        if (includeValue.equals("all")) {
176            entities = true;
177            attributes = true;
178        } else if (includeValue.equals("entities")) {
179            entities = true;
180        } else if (includeValue.equals("attributes")) {
181            attributes = true;
182        }
183        List<NamedObj> objects;
184        String instances = instancesOf.stringValue();
185        NamedObj container = getContainer();
186        if (entities && container instanceof CompositeEntity) {
187            if (instances.trim().equals("")) {
188                objects = ((CompositeEntity) container).entityList();
189            } else {
190                try {
191                    Class restrict = Class.forName(instances);
192                    objects = ((CompositeEntity) container)
193                            .entityList(restrict);
194                } catch (ClassNotFoundException e) {
195                    throw new IllegalActionException(this,
196                            "No such class: " + instances);
197                }
198            }
199            /* title attribute is now used for displaying parameter table.
200            for (NamedObj object : objects) {
201                // Create a WebAttribute for each object's title and add to
202                // exporter.   Content should only be added once
203                // (onceOnly -> true).
204                webAttribute = WebAttribute.createWebAttribute(object,
205                        "titleWebAttribute", "title");
206                webAttribute.setExpression(object.getDisplayName());
207                exporter.defineAttribute(webAttribute, true);
208            }
209             */
210        }
211        if (attributes) {
212            if (instances.trim().equals("")) {
213                objects = ((CompositeEntity) container).attributeList();
214            } else {
215                try {
216                    Class restrict = Class.forName(instances);
217                    objects = ((CompositeEntity) container)
218                            .attributeList(restrict);
219                } catch (ClassNotFoundException e) {
220                    throw new IllegalActionException(this,
221                            "No such class: " + instances);
222                }
223            }
224            for (NamedObj object : objects) {
225                // Create a WebAttribute for each object's title and add to
226                // exporter.   Content should only be added once
227                // (onceOnly -> true).
228                webAttribute = WebAttribute.createWebAttribute(object,
229                        "titleWebAttribute", "title");
230                webAttribute.setExpression(object.getDisplayName());
231                exporter.defineAttribute(webAttribute, true);
232            }
233        }
234    }
235
236}