001/* Attribute specifying a URI to link to from an icon.
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.LiveLink;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034import ptolemy.kernel.util.NamedObj;
035
036///////////////////////////////////////////////////////////////////
037//// IconLink
038/**
039 * Attribute specifying a URI to link to from an icon when a model
040 * is exported to a web page.
041 * Using the <i>linkTarget</i> parameter, you can control how the
042 * link is displayed.
043 * In contrast, {@link LiveLink} provides a link to a model
044 * in Vergil (instead of in an exported web page).
045 * <p>
046 * To use this, drag it onto an icon in your model. Then double
047 * click on that icon to set the URL to link to. The <i>linkTarget</i>
048 * parameter specifies whether the link should be opened in a
049 * new browser window (the default), in the same browser window,
050 * in a lightbox, etc.
051 * <p>
052 * Note that this attribute can be used in combination with
053 * {@link LinkToOpenTableaux}. The latter provides a hyperlink that works
054 * within Vergil, whereas this attribute provides a hyperlink
055 * that works in an exported HTML page.  For example,
056 * LinkToOpenTableaux might be used to provide a hyperlink to another
057 * model (a pointer to its MoML file), so that double clicking
058 * on the container of the LiveLink attribute opens the other
059 * model. If that container also contains an instance of
060 * IconLink, then when the model is exported to a web page,
061 * the container's icon can become a link to the exported page
062 * for the other model.
063 *
064 * @author Edward A. Lee
065 * @version $Id$
066 * @since Ptolemy II 10.0
067 * @Pt.ProposedRating Red (cxh)
068 * @Pt.AcceptedRating Red (cxh)
069 */
070public class IconLink extends WebContent implements WebExportable {
071
072    /** Create an instance of this parameter.
073     *  @param container The container.
074     *  @param name The name.
075     *  @exception IllegalActionException If the superclass throws it.
076     *  @exception NameDuplicationException If the superclass throws it.
077     */
078    public IconLink(NamedObj container, String name)
079            throws IllegalActionException, NameDuplicationException {
080        super(container, name);
081
082        _icon.setIconText("L");
083        displayText.setExpression("http://ptolemy.org");
084        height.setExpression("1");
085
086        linkTarget = new LinkTarget(this, "linkTarget");
087        // Note that the default value and choices are set
088        // in the above constructor call.
089
090        setExpression("http://ptolemy.org");
091    }
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         parameters                        ////
095
096    /** Parameter specifying the target for the link.
097     *  The possibilities are:
098     *  <ul>
099     *  <li><b>_lightbox</b>: Open in a lightbox-style popup frame.
100     *  <li><b>_blank</b>: Open in a new window or tab.
101     *  <li><b>_self</b>: Open in the same frame as it was clicked.
102     *  <li><b>_parent</b>: Open in the parent frameset.
103     *  <li><b>_top</b>: Open in the full body of the window.
104     *  <li><b><i>framename</i></b>: Open in a named frame.
105     *  </ul>
106     *  The default is "_lightbox".
107     */
108    public LinkTarget linkTarget;
109
110    ///////////////////////////////////////////////////////////////////
111    ////                         public methods                    ////
112
113    /** A link is of type text/html.
114     *
115     * @return The string text/html
116     */
117    @Override
118    public String getMimeType() {
119        return "text/html";
120    }
121
122    /** Return true, since new content should overwrite old.
123     *
124     * @return True, since new content should overwrite old
125     */
126    @Override
127    public boolean isOverwriteable() {
128        return true;
129    }
130
131    ///////////////////////////////////////////////////////////////////
132    ////                         protected methods                 ////
133
134    /** Provide content to the specified web exporter to be
135     *  included in a web page for the container of this object.
136     *  This class defines an href attribute to associate with
137     *  the area of the image map corresponding to its container.
138     *
139     *  @param exporter  The web exporter to write content to
140     *  @exception IllegalActionException If evaluating the value
141     *   of this parameter fails, or creating a web attribute fails.
142     */
143    @Override
144    protected void _provideAttributes(WebExporter exporter)
145            throws IllegalActionException {
146
147        WebAttribute webAttribute;
148
149        NamedObj container = getContainer();
150        if (container != null) {
151            // Last argument specifies to overwrite any previous value defined.
152            if (!stringValue().trim().equals("")) {
153
154                // Create link attribute and add to exporter.
155                // Content should only be added once (onceOnly -> true).
156                webAttribute = WebAttribute.createWebAttribute(getContainer(),
157                        "hrefWebAttribute", "href");
158                webAttribute.setExpression(stringValue());
159                exporter.defineAttribute(webAttribute, true);
160            }
161
162            String targetValue = linkTarget.stringValue();
163            if (!targetValue.trim().equals("")) {
164                if (targetValue.equals("_lightbox")) {
165                    // Strangely, the class has to be "iframe".
166                    // I don't understand why it can't be "lightbox".
167
168                    // Create class attribute and add to exporter.
169                    // Content should only be added once (onceOnly -> true).
170                    webAttribute = WebAttribute.appendToWebAttribute(
171                            getContainer(), "classWebAttribute", "class",
172                            "iframe");
173                    exporter.defineAttribute(webAttribute, true);
174                } else {
175
176                    // Create target attribute and add to exporter.
177                    // Content should only be added once (onceOnly -> true).
178                    webAttribute = WebAttribute.createWebAttribute(
179                            getContainer(), "targetWebAttribute", "target");
180                    webAttribute.setExpression(targetValue);
181                    exporter.defineAttribute(webAttribute, true);
182                }
183            }
184        }
185    }
186}