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.expr.StringParameter;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.NamedObj;
039
040///////////////////////////////////////////////////////////////////
041//// DefaultIconLink
042/**
043 * A parameter specifying default hyperlink to associate
044 * with icons in model. Putting this attribute into a model causes
045 * the icons of entities, attributes, or both, to be assigned a
046 * default hyperlink to the URI given by <i>linkTarget</i>.
047 * This will replace any configuration default link that
048 * includes the same objects, and
049 * targets the same instanceOf possibilities.
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 DefaultIconLink extends IconLink {
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 DefaultIconLink(NamedObj container, String name)
066            throws IllegalActionException, NameDuplicationException {
067        super(container, name);
068
069        include = new StringParameter(this, "include");
070        include.addChoice("Entities");
071        include.addChoice("Attributes");
072        include.addChoice("All");
073        include.setExpression("Entities");
074
075        instancesOf = new StringParameter(this, "instancesOf");
076    }
077
078    ///////////////////////////////////////////////////////////////////
079    ////                         parameters                        ////
080
081    /** If non-empty (the default), specifies a class name.
082     *  Only entities or attributes (depending on <i>include</i>)
083     *  implementing the specified
084     *  class will be assigned the control defined by this
085     *  DefaultIconLink parameter.
086     */
087    public StringParameter instancesOf;
088
089    /** Specification of whether to provide the link for
090     *  Attributes, Entities, or both. This is either "Entities" (the
091     *  default), "Attributes", or "All".
092     */
093    public StringParameter include;
094
095    ///////////////////////////////////////////////////////////////////
096    ////                         protected methods                 ////
097
098    /** Override the base class to define an href attribute to associate with
099     *  the area of the image map corresponding to its container.
100     *
101     *  @param exporter  The web exporter to which to write content.
102     *  @exception IllegalActionException If evaluating the value
103     *   of this parameter fails.
104     */
105    @Override
106    protected void _provideAttributes(WebExporter exporter)
107            throws IllegalActionException {
108
109        boolean entities = false, attributes = false;
110        String includeValue = include.stringValue()
111                .toLowerCase(Locale.getDefault());
112        if (includeValue.equals("all")) {
113            entities = true;
114            attributes = true;
115        } else if (includeValue.equals("entities")) {
116            entities = true;
117        } else if (includeValue.equals("attributes")) {
118            attributes = true;
119        }
120        List<NamedObj> objects;
121        String instances = instancesOf.stringValue();
122        NamedObj container = getContainer();
123        if (entities && container instanceof CompositeEntity) {
124            if (instances.trim().equals("")) {
125                objects = ((CompositeEntity) container).entityList();
126            } else {
127                try {
128                    Class restrict = Class.forName(instances);
129                    objects = ((CompositeEntity) container)
130                            .entityList(restrict);
131                } catch (ClassNotFoundException e) {
132                    throw new IllegalActionException(this,
133                            "No such class: " + instances);
134                }
135            }
136            for (NamedObj object : objects) {
137                _provideEachAttribute(exporter, object);
138            }
139        }
140        if (attributes) {
141            if (instances.trim().equals("")) {
142                objects = ((CompositeEntity) container).attributeList();
143            } else {
144                try {
145                    Class restrict = Class.forName(instances);
146                    objects = ((CompositeEntity) container)
147                            .attributeList(restrict);
148                } catch (ClassNotFoundException e) {
149                    throw new IllegalActionException(this,
150                            "No such class: " + instances);
151                }
152            }
153            for (NamedObj object : objects) {
154                _provideEachAttribute(exporter, object);
155            }
156        }
157    }
158
159    /** Provide content to the specified web exporter to be
160     *  included in a web page for the container of this object.
161     *  This class defines an href attribute to associate with
162     *  the area of the image map corresponding to its container.
163     *
164     *  @param exporter The exporter.
165     *  @param object The object.
166     *  @exception IllegalActionException If evaluating the value
167     *   of this parameter fails.
168     */
169    protected void _provideEachAttribute(WebExporter exporter, NamedObj object)
170            throws IllegalActionException {
171
172        WebAttribute webAttribute;
173
174        if (object != null) {
175            // Last argument specifies to overwrite any previous value defined.
176            if (!stringValue().trim().equals("")) {
177
178                // Create link attribute and add to exporter.
179                // Content should only be added once (onceOnly -> true).
180                webAttribute = WebAttribute.createWebAttribute(getContainer(),
181                        "hrefWebAttribute", "href");
182                webAttribute.setExpression(stringValue());
183                exporter.defineAttribute(webAttribute, true);
184
185                String targetValue = linkTarget.stringValue();
186                if (!targetValue.trim().equals("")) {
187                    if (targetValue.equals("_lightbox")) {
188                        // Strangely, the class has to be "iframe".
189                        // I don't understand why it can't be "lightbox".
190
191                        // Create class attribute and add to exporter.
192                        // Content should only be added once (onceOnly -> true).
193                        webAttribute = WebAttribute.appendToWebAttribute(
194                                getContainer(), "classWebAttribute", "class",
195                                "iframe");
196                        exporter.defineAttribute(webAttribute, true);
197                    } else {
198                        // Create target attribute and add to exporter.
199                        // Content should only be added once (onceOnly -> true).
200                        webAttribute = WebAttribute.createWebAttribute(
201                                getContainer(), "targetWebAttribute", "target");
202                        webAttribute.setExpression(targetValue);
203                        exporter.defineAttribute(webAttribute, true);
204                    }
205                }
206            }
207        }
208    }
209}