001/* Attribute for inserting HTML text into the page exported by Export to Web. 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.awt.Color; 032 033import ptolemy.actor.gui.style.TextStyle; 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.SingletonAttribute; 042import ptolemy.kernel.util.Workspace; 043import ptolemy.vergil.icon.TextIcon; 044import ptolemy.vergil.toolbox.VisibleParameterEditorFactory; 045 046/////////////////////////////////////////////////////////////////// 047//// WebContent 048/** 049 * Base class for attributes defining content for export to web. 050 * This class provides parameters that control the size of the text 051 * box for editing the content and also a parameter that defines what 052 * to display in the model. 053 * 054 * @author Edward A. Lee 055 * @version $Id$ 056 * @since Ptolemy II 10.0 057 * @Pt.ProposedRating Red (cxh) 058 * @Pt.AcceptedRating Red (cxh) 059 */ 060public abstract class WebContent extends StringParameter 061 implements WebExportable { 062 063 /** Create an instance of this parameter. 064 * @param container The container. 065 * @param name The name. 066 * @exception IllegalActionException If the superclass throws it. 067 * @exception NameDuplicationException If the superclass throws it. 068 */ 069 public WebContent(NamedObj container, String name) 070 throws IllegalActionException, NameDuplicationException { 071 super(container, name); 072 073 _icon = new TextIcon(this, "_icon"); 074 _icon.setTextColor(Color.RED); 075 _icon.setIconText("H"); 076 077 displayText = new StringParameter(this, "displayText"); 078 displayText.setExpression("Content for Export to Web"); 079 080 height = new Parameter(this, "height"); 081 height.setTypeEquals(BaseType.INT); 082 height.setExpression("20"); 083 084 width = new Parameter(this, "width"); 085 width.setTypeEquals(BaseType.INT); 086 width.setExpression("60"); 087 088 TextStyle style = new TextStyle(this, "style"); 089 style.height.setExpression("height"); 090 style.width.setExpression("width"); 091 092 new SingletonAttribute(this, "_hideName"); 093 new VisibleParameterEditorFactory(this, "_editorFactory"); 094 } 095 096 /////////////////////////////////////////////////////////////////// 097 //// parameters //// 098 099 /** Parameter giving the text to display in the Ptolemy model. 100 * This defaults to "Content for Export to Web". 101 */ 102 public StringParameter displayText; 103 104 /** Parameter specifying the height of the editing box. 105 * This is an int that defaults to 20. 106 */ 107 public Parameter height; 108 109 /** Parameter specifying the width of the editing box. 110 * This is an int that defaults to 60. 111 */ 112 public Parameter width; 113 114 /////////////////////////////////////////////////////////////////// 115 //// public methods //// 116 117 /** Override the base class to update the icon. 118 * @param attribute The attribute that changed. 119 * @exception IllegalActionException If thrown while setting the 120 * icon text or by the superclass. 121 */ 122 @Override 123 public void attributeChanged(Attribute attribute) 124 throws IllegalActionException { 125 if (attribute == displayText) { 126 _icon.setText(displayText.stringValue()); 127 } else { 128 super.attributeChanged(attribute); 129 } 130 } 131 132 /** Clone the object into the specified workspace. 133 * @param workspace The workspace for the new object. 134 * @return A new NamedObj. 135 * @exception CloneNotSupportedException If any of the attributes 136 * cannot be cloned. 137 */ 138 @Override 139 public Object clone(Workspace workspace) throws CloneNotSupportedException { 140 WebContent newObject = (WebContent) super.clone(workspace); 141 try { 142 newObject.getAttribute("_icon", TextIcon.class).setContainer(null); 143 newObject._icon = new TextIcon(newObject, "_icon"); 144 newObject._icon.setTextColor(Color.RED); 145 newObject._icon.setIconText("H"); 146 } catch (Throwable throwable) { 147 throw new CloneNotSupportedException( 148 getFullName() + ": Failed to clone: " + throwable); 149 } 150 return newObject; 151 } 152 153 /** Provide content to the specified web exporter. 154 * This may include, for example, HTML pages and fragments, Javascript 155 * function definitions and calls, CSS styling, and more. Throw an 156 * IllegalActionException if something is wrong with the web content. 157 * 158 * Calls two methods, _provideAttributes() for generating content that 159 * would be an attribute of an element and _provideElements() for 160 * generating standalone elements. Note that attributes may refer to 161 * other elements; therefore, the provideContent() method always calls both 162 * _provideAttributes() and _provideElements(). For example, a HTML 163 * attribute for a Javascript method call: 164 * <code>onclick="runMethod()"</code> 165 * requires that a 166 * <code><script> function runMethod() { } </script></code> element be defined. 167 * Subclasses should override _provideAttributes() and _provideElements(). 168 * 169 * @param exporter The web exporter to be used 170 * @exception IllegalActionException If something is wrong with the web 171 * content. 172 */ 173 @Override 174 public void provideContent(WebExporter exporter) 175 throws IllegalActionException { 176 _provideAttributes(exporter); 177 _provideElements(exporter); 178 } 179 180 /////////////////////////////////////////////////////////////////// 181 //// protected methods //// 182 183 /** Generate attribute web content. Should call WebExporter's 184 * defineAttribute() method. Subclasses should override. Please also see 185 * {@link ptolemy.vergil.basic.export.web.WebAttribute} 186 * 187 * @param exporter The WebExporter to write content to 188 * @exception IllegalActionException If there is a problem creating the web 189 * content. 190 */ 191 protected void _provideAttributes(WebExporter exporter) 192 throws IllegalActionException { 193 194 } 195 196 /** Generate element web content. Should call WebExporter's 197 * defineElement() method. Subclasses should override. Please also see 198 * {@link ptolemy.vergil.basic.export.web.WebElement} 199 * 200 * @param exporter The WebExporter to write content to 201 * @exception IllegalActionException If there is a problem creating the web 202 * content. 203 */ 204 protected void _provideElements(WebExporter exporter) 205 throws IllegalActionException { 206 207 } 208 209 // Add createResource() method here that returns a URI? Not every class 210 // would need this. 211 212 /////////////////////////////////////////////////////////////////// 213 //// protected variables //// 214 215 /** Icon. */ 216 protected TextIcon _icon; 217}