001/* Interface for attributes that can have their values externally set. 002 003 Copyright (c) 1997-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 030import java.util.Collection; 031 032/////////////////////////////////////////////////////////////////// 033//// Settable 034 035/** 036 This is an interface for attributes that can have their values 037 externally set. An attribute class that implements this interface has to 038 be able to have a value set by a string, via the setExpression() 039 method. A string representation is returned by the getExpression() 040 method. An expression may be an ordinary string with no further 041 interpretation, or it may be a string that needs to be evaluated. 042 In the latter case, an implementation of this attribute may not 043 evaluate the string when the setExpression() method is called. 044 It may instead only evaluate the string when the validate() method 045 is called. Often this will not be called until the value of the 046 expression is actually needed (this is known as "lazy evaluation"). 047 Such an implementation will defer notification of listeners and the 048 container until the string is evaluated. In a typical use of this 049 interface, therefore, it is necessary to be sure that validate() 050 is called sometime after setExpression() is called. 051 <p> 052 In addition, an attribute class that implements this interface 053 needs to maintain a list of listeners that are informed whenever 054 the value of the attribute changes. It should inform those 055 listeners whenever setExpression() is called. 056 <p> 057 Among other uses, this interface marks attributes whose value 058 can be set via the value attribute of a MoML property element. 059 For example, if class XXX implements Settable, then the following 060 is valid MoML: 061 <pre> 062 <property name="xxx" class="XXX" value="yyy"/> 063 </pre> 064 <p> 065 This interface also supports annotations that hint to a user 066 interface the level of visibility that an instance should have. 067 The visibility is specified as one of the static instances of 068 the inner class Visibility, currently NONE, EXPERT, FULL, and NOT_EDITABLE 069 NONE indicates that the user should never see the instance, 070 and should not be able to set its value through the user interface. 071 EXPERT means that only expert users should see the instance. 072 FULL means that the instance is always visible, and a user interface 073 should always allow it to be set. 074 NOT_EDITABLE is similar to FULL, except that the value of the 075 expression is visible, but not editable by the user. This is 076 commonly used for feedback from the model. 077 078 @author Edward A. Lee 079 @version $Id$ 080 @since Ptolemy II 1.0 081 @Pt.ProposedRating Green (eal) 082 @Pt.AcceptedRating Green (cxh) 083 */ 084public interface Settable extends Nameable { 085 /////////////////////////////////////////////////////////////////// 086 //// public methods //// 087 088 /** Add a listener to be notified when the value of this settable 089 * object changes. An implementation of this method should ignore 090 * the call if the specified listener is already on the list of 091 * listeners. In other words, it should not be possible for the 092 * same listener to be notified twice of a value update. 093 * @param listener The listener to add. 094 * @see #removeValueListener(ValueListener) 095 */ 096 public void addValueListener(ValueListener listener); 097 098 /** Return the default value of this attribute, if there is 099 * one, or null if there is none. 100 * @return The default value of this attribute, or null 101 * if there is none. 102 */ 103 public String getDefaultExpression(); 104 105 /** Return a name to present to the user. 106 * @return A name to present to the user. 107 */ 108 @Override 109 public String getDisplayName(); 110 111 /** Get the expression of the attribute that has been set by setExpression(), 112 * or null if there is none. 113 * @return The expression. 114 * @see #setExpression(String) 115 */ 116 public String getExpression(); 117 118 /** Get the value of the attribute, which is the evaluated expression. 119 * @return The value. 120 * @see #getExpression() 121 */ 122 public String getValueAsString(); 123 124 /** Get the visibility of this Settable, as set by setVisibility(). 125 * If setVisibility() has not been called, then implementations of 126 * this interface should return some default, not null, indicating 127 * user-level visibility. The returned value is one of the static 128 * instances of the Visibility inner class. 129 * @return The visibility of this Settable. 130 * @see #setVisibility(Settable.Visibility) 131 */ 132 public Settable.Visibility getVisibility(); 133 134 /** Remove a listener from the list of listeners that are 135 * notified when the value of this variable changes. If no such listener 136 * exists, do nothing. 137 * @param listener The listener to remove. 138 * @see #addValueListener(ValueListener) 139 */ 140 public void removeValueListener(ValueListener listener); 141 142 /** Set the value of the attribute by giving some expression. 143 * In some implementations, the listeners and the container will 144 * be notified immediately. However, some implementations may 145 * defer notification until validate() is called. 146 * @param expression The value of the attribute. 147 * @exception IllegalActionException If the expression is invalid. 148 * @see #getExpression() 149 */ 150 public void setExpression(String expression) throws IllegalActionException; 151 152 /** Set the visibility of this Settable. The argument should be one 153 * of the static public instances of the inner class Visibility. 154 * This is enforced by making it impossible to construct instances 155 * of this inner class outside this interface definition. 156 * If this method is not called, then implementations of 157 * this interface should return some default, not null. 158 * @param visibility The visibility of this Settable. 159 * @see #getVisibility() 160 */ 161 public void setVisibility(Settable.Visibility visibility); 162 163 /** Check the validity of the expression set in setExpression(). 164 * Implementations of this method should notify the container 165 * by calling attributeChanged(), unless the container has already 166 * been notified in setExpression(). They should also notify any 167 * registered value listeners if they have not already been notified. 168 * If any other instances of Settable are validated as a side effect, 169 * then an implementation should return a Collection containing those 170 * instances. This can be used by the caller to avoid validating those 171 * again. The list may contain this instance of Settable. 172 * @return A collection of settables that are also validated as a 173 * side effect, or null if there are none. 174 * @exception IllegalActionException If the expression is not valid, or 175 * its value is not acceptable to the container or the listeners. 176 */ 177 public Collection validate() throws IllegalActionException; 178 179 /////////////////////////////////////////////////////////////////// 180 //// public members //// 181 182 /** Indicator that a user interface should not make an instance visible. 183 */ 184 public static Visibility NONE = new Visibility(); 185 186 /** Indicator that a user interface should make an instance visible 187 * only to experts. 188 */ 189 public static Visibility EXPERT = new Visibility(); 190 191 /** Indicator that a user interface should make an instance visible. 192 */ 193 public static Visibility FULL = new Visibility(); 194 195 /** Indicator that a user interface should make an instance 196 * visible, but not allow editing of the variable. 197 */ 198 public static Visibility NOT_EDITABLE = new Visibility(); 199 200 /////////////////////////////////////////////////////////////////// 201 //// inner classes //// 202 203 /** Inner class used for the static enumeration of indicators of 204 * visibility. Instances of this class cannot be constructed outside 205 * the enclosing interface because its constructor is private. 206 */ 207 public static class Visibility { 208 // Private constructor prevents construction outside. 209 // This constructor should not be called! 210 // it is protected to work around a compiler bug in JDK1.2.2 211 protected Visibility() { 212 } 213 } 214}