001/* An attribute used for storing an interface to be used with Chic.
002
003 Copyright (c) 2000-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 */
027package ptolemy.chic;
028
029// Ptolemy imports
030import ptolemy.actor.gui.style.TextStyle;
031import ptolemy.kernel.util.IllegalActionException;
032import ptolemy.kernel.util.NameDuplicationException;
033import ptolemy.kernel.util.NamedObj;
034import ptolemy.kernel.util.StringAttribute;
035
036// Java imports
037///////////////////////////////////////////////////////////////////
038//// ChicAttribute
039
040/**
041 An attribute that has a string value which is meant to be used as an interface
042 in Chic.
043 Use setExpression() to define the value, as in for example
044 <pre>
045 attribute.setExpression("xxx");
046 </pre>
047 <p>The default value of the string contained by this attribute is the empty
048 string.
049
050 <p>By default, an instance of this class is fully visible in a user
051 interface and it is annotated with a TextStyle attribute.  The
052 visibility is indicated to the user interface when the user interface
053 calls the getVisibility() method of this class and the value
054 Settable.FULL is returned to the userInterface.
055
056 <p>Note that the string value within ChicAttribute will not be parsed
057 and you do not have to type a leading and a trailing double quote.
058
059 <p>This class is an attribute that replaces any previously existing
060 attribute of the same class in the container that has the same name.
061
062 @author Eleftherios Matsikoudis
063 @version $Id$
064 @since Ptolemy II 3.0
065 @Pt.ProposedRating Red (cxh)
066 @Pt.AcceptedRating Red (cxh)
067 */
068public class ChicAttribute extends StringAttribute {
069    /** Construct an attribute with the given name contained by the specified
070     *  container and annotate it with a TextStyle attribute. The container
071     *  argument must not be null, or a NullPointerException will be thrown.
072     *  This attribute will use the workspace of the container for
073     *  synchronization and version counts. If the name argument is null,
074     *  then the name is set to the empty string. This attribute that replaces
075     *  any previously existing attribute of the same class in the container
076     *  that has the same name. The object is added to the directory of the
077     *  workspace if the container is null.
078     *  Increment the version of the workspace.
079     *  @param container The container.
080     *  @param name The name of this attribute.
081     *  @exception IllegalActionException If the attribute is not of an
082     *   acceptable class for the container, or if the name contains a period.
083     *  @exception NameDuplicationException If the name coincides with
084     *   an attribute of different class already in the container.
085     */
086    public ChicAttribute(NamedObj container, String name)
087            throws IllegalActionException, NameDuplicationException {
088        super(container, name);
089
090        new TextStyle(this, "style");
091    }
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         public methods                    ////
095
096    /** Remove any previous ChicAttribute in the container that has the
097     *  same name as this attribute, and then call the base class method
098     *  to set the container. If the container has an attribute with the
099     *  same name that is not an instance of this class, throw an exception.
100     *  If the container is not in the same workspace as this attribute,
101     *  throw an exception. If this attribute is already contained by the
102     *  NamedObj, do nothing. If the attribute already has a container, remove
103     *  this attribute from its attribute list first.  Otherwise, remove
104     *  it from the directory of the workspace, if it is there.
105     *  If the argument is null, then remove this attribute from its
106     *  container. It is not added to the workspace directory, so this
107     *  could result in this object being garbage collected.
108     *  <p>
109     *  Note that since an ChicAttribute is a NamedObj, it can itself have
110     *  attributes.  However, recursive containment is not allowed, where
111     *  an attribute is an attribute of itself, or indirectly of any attribute
112     *  it contains.
113     *  <p>
114     *  This method is write-synchronized on the
115     *  workspace and increments its version number.
116     *  @param container The container to attach this attribute to.
117     *  @exception IllegalActionException If this attribute is not of the
118     *   expected class for the container, or it has no name, or there is an
119     *   attribute with the same name that is not an instance of this class,
120     *   or the attribute and container are not in the same workspace, or
121     *   the proposed container would result in recursive containment.
122     *  @exception NameDuplicationException If the container already has
123     *   an attribute with the name of this attribute that is not of class
124     *   ChicAttribute.
125     */
126    @Override
127    public void setContainer(NamedObj container)
128            throws IllegalActionException, NameDuplicationException {
129        ChicAttribute previous = null;
130
131        if (container != null) {
132            previous = (ChicAttribute) container.getAttribute(getName(),
133                    getClass());
134
135            if (previous != null) {
136                previous.setContainer(null);
137            }
138        }
139
140        try {
141            super.setContainer(container);
142        } catch (IllegalActionException ex) {
143            // Restore previous.
144            if (previous != null) {
145                previous.setContainer(container);
146            }
147
148            throw ex;
149        } catch (NameDuplicationException ex) {
150            // Restore previous.
151            if (previous != null) {
152                previous.setContainer(container);
153            }
154
155            throw ex;
156        }
157    }
158
159    ///////////////////////////////////////////////////////////////////
160    ////                         public variables                  ////
161    ///////////////////////////////////////////////////////////////////
162    ////                         private methods                   ////
163    ///////////////////////////////////////////////////////////////////
164    ////                         private variables                 ////
165}