001/* A nonpersistent configurable singleton attribute.
002
003 Copyright (c) 1998-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 */
028package ptolemy.kernel.util;
029
030///////////////////////////////////////////////////////////////////
031//// TransientSingletonConfigurableAttribute
032
033/**
034 This class is a nonpersistent configurable singleton attribute.
035 By "nonpersistent" we mean that it does not export MoML.
036 A major application of this class is to define a default icon
037 description.  An icon description is XML code in the SVG
038 (scalable vector graphics) schema, set in a configure element
039 in MoML.
040
041 @deprecated Use SingletonConfigurableAttribute instead with setPersistent(false).
042 @author Steve Neuendorffer and Edward A. Lee
043 @version $Id$
044 @since Ptolemy II 1.0
045 @Pt.ProposedRating Green (eal)
046 @Pt.AcceptedRating Green (bilung)
047 */
048@Deprecated
049public class TransientSingletonConfigurableAttribute
050        extends ConfigurableAttribute implements Singleton {
051    // NOTE: This class does not extend SingletonConfigurableAttribute
052    // even though the setContainer() method is identical.  The reason
053    // is subtle.  The base classes in the Ptolemy kernel all create
054    // instances of this class with the name "_iconDescription" to
055    // give a default icon.  However, when one wishes to replace
056    // this with another icon, the natural way to do it is to create
057    // an instance of SingletonConfigurableAttribute in the MoML
058    // data that goes in the library.  However, when the MoML parser
059    // reads this MoML, it finds a pre-existing attribute that is
060    // an instance of TransientSingletonConfigurableAttribute.
061    // If that is also an instance of SingletonConfigurableAttribute,
062    // then the MoML parser will not replace the attribute.
063    // Thus, it is important that a TransientSingletonConfigurableAttribute
064    // not be an instance of SingletonConfigurableAttribute.
065
066    /** Construct a new attribute with no
067     *  container and an empty string as its name. Add the attribute to the
068     *  workspace directory.
069     *  Increment the version number of the workspace.
070     */
071    public TransientSingletonConfigurableAttribute() {
072        super();
073        setPersistent(false);
074    }
075
076    /** Construct a new attribute with
077     *  no container and an empty string as a name. You can then change
078     *  the name with setName(). If the workspace argument is null, then
079     *  use the default workspace.
080     *  Add the attribute to the workspace directory.
081     *  Increment the version number of the workspace.
082     *  @param workspace The workspace that will list the attribute.
083     */
084    public TransientSingletonConfigurableAttribute(Workspace workspace) {
085        super(workspace);
086        setPersistent(false);
087    }
088
089    /** Construct an attribute with the given container and name.
090     *  If an attribute already exists with the same name as the one
091     *  specified here, and of class SingletonConfigurableAttribute, then that
092     *  attribute is removed before this one is inserted in the container.
093     *  @param container The container.
094     *  @param name The name of this attribute.
095     *  @exception IllegalActionException If the attribute cannot be contained
096     *   by the proposed container.
097     *  @exception NameDuplicationException If the container already has an
098     *   attribute with this name, and the class of that container is not
099     *   SingletonConfigurableAttribute.
100     */
101    public TransientSingletonConfigurableAttribute(NamedObj container,
102            String name)
103            throws NameDuplicationException, IllegalActionException {
104        super(container, name);
105        setPersistent(false);
106    }
107
108    ///////////////////////////////////////////////////////////////////
109    ////                         public methods                    ////
110
111    /** Remove any previous attribute in the container that has the same
112     *  name as this attribute, and then call the base class method to set
113     *  the container. If the container is not in the same workspace as
114     *  this attribute, throw an exception.
115     *  If this attribute is already contained by the NamedObj, do nothing.
116     *  If the attribute already has a container, remove
117     *  this attribute from its attribute list first.  Otherwise, remove
118     *  it from the directory of the workspace, if it is there.
119     *  If the argument is null, then remove this attribute from its
120     *  container. It is not added to the workspace directory, so this
121     *  could result in this object being garbage collected.
122     *  <p>
123     *  Note that since an Attribute is a NamedObj, it can itself have
124     *  attributes.  However, recursive containment is not allowed, where
125     *  an attribute is an attribute of itself, or indirectly of any attribute
126     *  it contains.
127     *  <p>
128     *  This method is write-synchronized on the
129     *  workspace and increments its version number.
130     *  @param container The container to attach this attribute to.
131     *  @exception IllegalActionException If this attribute is not of the
132     *   expected class for the container, or it has no name,
133     *   or the attribute and container are not in the same workspace, or
134     *   the proposed container would result in recursive containment.
135     *  @exception NameDuplicationException If the container already has
136     *   an attribute with the name of this attribute that is of class
137     *   SingletonConfigurableAttribute.
138     */
139    @Override
140    public void setContainer(NamedObj container)
141            throws IllegalActionException, NameDuplicationException {
142        Attribute previous = null;
143
144        if (container != null) {
145            previous = container.getAttribute(getName());
146
147            if (previous != null) {
148                previous.setContainer(null);
149            }
150        }
151
152        try {
153            super.setContainer(container);
154        } catch (IllegalActionException ex) {
155            // Restore previous.
156            if (previous != null) {
157                previous.setContainer(container);
158            }
159
160            throw ex;
161        } catch (NameDuplicationException ex) {
162            // Restore previous.
163            if (previous != null) {
164                previous.setContainer(container);
165            }
166
167            throw ex;
168        }
169    }
170}