001/* A GUI property that encloses a JComboBox component. 002 003 Copyright (c) 2008-2018 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.actor.gui.properties; 029 030import java.awt.event.ItemEvent; 031import java.awt.event.ItemListener; 032 033import javax.swing.JComboBox; 034import javax.swing.JComponent; 035 036import ptolemy.data.expr.StringParameter; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.InternalErrorException; 039import ptolemy.kernel.util.NameDuplicationException; 040import ptolemy.kernel.util.NamedObj; 041 042/////////////////////////////////////////////////////////////////// 043//// ComboBox 044 045/** 046 A GUI property that encloses a JComboBox component. 047 048 @author Thomas Huining Feng 049 @version $Id$ 050 @since Ptolemy II 8.0 051 @Pt.ProposedRating Red (tfeng) 052 @Pt.AcceptedRating Red (tfeng) 053 */ 054public class ComboBox extends GUIProperty implements ItemListener { 055 056 /** Construct a GUI property with the given name contained by the specified 057 * entity. The container argument must not be null, or a 058 * NullPointerException will be thrown. This attribute will use the 059 * workspace of the container for synchronization and version counts. 060 * If the name argument is null, then the name is set to the empty string. 061 * Increment the version of the workspace. 062 * @param container The container. 063 * @param name The name of this attribute. 064 * @exception IllegalActionException If the attribute is not of an 065 * acceptable class for the container, or if the name contains a period. 066 * @exception NameDuplicationException If the name coincides with 067 * an attribute already in the container. 068 */ 069 public ComboBox(NamedObj container, String name) 070 throws IllegalActionException, NameDuplicationException { 071 super(container, name); 072 } 073 074 /** Construct a GUI property with the given name contained by the specified 075 * entity with the given Java Swing component. The container argument must 076 * not be null, or a NullPointerException will be thrown. This attribute 077 * will use the workspace of the container for synchronization and version 078 * counts. If the name argument is null, then the name is set to the empty 079 * string. Increment the version of the workspace. 080 * @param container The container. 081 * @param name The name of this attribute. 082 * @param component The Java Swing component. 083 * @exception IllegalActionException If the attribute is not of an 084 * acceptable class for the container, or if the name contains a period. 085 * @exception NameDuplicationException If the name coincides with 086 * an attribute already in the container. 087 */ 088 public ComboBox(NamedObj container, String name, JComponent component) 089 throws IllegalActionException, NameDuplicationException { 090 super(container, name, component); 091 } 092 093 /** Construct a GUI property with the given name contained by the specified 094 * entity with the given Java Swing component and the given layout 095 * constraint. The container argument must not be null, or a 096 * NullPointerException will be thrown. This attribute 097 * will use the workspace of the container for synchronization and version 098 * counts. If the name argument is null, then the name is set to the empty 099 * string. Increment the version of the workspace. 100 * @param container The container. 101 * @param name The name of this attribute. 102 * @param component The Java Swing component. 103 * @param constraint The layout constraint. 104 * @exception IllegalActionException If the attribute is not of an 105 * acceptable class for the container, or if the name contains a period. 106 * @exception NameDuplicationException If the name coincides with 107 * an attribute already in the container. 108 */ 109 public ComboBox(NamedObj container, String name, JComponent component, 110 Object constraint) 111 throws IllegalActionException, NameDuplicationException { 112 super(container, name, component, constraint); 113 } 114 115 /** Construct a GUI property with the given name contained by the specified 116 * entity with the given layout 117 * constraint. The container argument must not be null, or a 118 * NullPointerException will be thrown. This attribute 119 * will use the workspace of the container for synchronization and version 120 * counts. If the name argument is null, then the name is set to the empty 121 * string. Increment the version of the workspace. 122 * @param container The container. 123 * @param name The name of this attribute. 124 * @param constraint The layout constraint. 125 * @exception IllegalActionException If the attribute is not of an 126 * acceptable class for the container, or if the name contains a period. 127 * @exception NameDuplicationException If the name coincides with 128 * an attribute already in the container. 129 */ 130 public ComboBox(NamedObj container, String name, Object constraint) 131 throws IllegalActionException, NameDuplicationException { 132 super(container, name, constraint); 133 } 134 135 /** React to an action of changing the selected item in the combo box. The 136 * item must be an instance of {@link Item} and when it is selected, the 137 * {@link Item#perform()} method is invoked. After that, if the item 138 * specifies the next item to be selected in its {@link Item#next} 139 * attribute, that next item is selected, which may cause this method to be 140 * invoked again. 141 * 142 * @param event The item event representing which item is selected. 143 */ 144 @Override 145 public void itemStateChanged(ItemEvent event) { 146 if (event.getStateChange() != ItemEvent.SELECTED) { 147 return; 148 } 149 150 Item item = (Item) event.getItem(); 151 item.perform(); 152 153 if (item.next == null) { 154 return; 155 } 156 157 try { 158 String next = item.next.stringValue(); 159 if (!next.equals("")) { 160 Item nextItem = (Item) getAttribute(next); 161 ((JComboBox) getComponent()).setSelectedItem(nextItem); 162 } 163 } catch (IllegalActionException e) { 164 throw new InternalErrorException(this, e, 165 "Unable to find the next item."); 166 } 167 } 168 169 /////////////////////////////////////////////////////////////////// 170 //// Item 171 172 /** 173 The base class for an item that can be added to the combo box as a choice. 174 175 @author Thomas Huining Feng 176 @version $Id$ 177 @since Ptolemy II 8.0 178 @Pt.ProposedRating Red (tfeng) 179 @Pt.AcceptedRating Red (tfeng) 180 */ 181 public static class Item extends ActionGUIProperty { 182 183 /** Construct an item with the given name contained by the specified 184 * entity. The container argument must not be null, or a 185 * NullPointerException will be thrown. This attribute will use the 186 * workspace of the container for synchronization and version counts. 187 * If the name argument is null, then the name is set to the empty 188 * string. Increment the version of the workspace. 189 * @param container The container. 190 * @param name The name of this attribute. 191 * @exception IllegalActionException If the attribute is not of an 192 * acceptable class for the container, or if the name contains a 193 * period. 194 * @exception NameDuplicationException If the name coincides with 195 * an attribute already in the container. 196 */ 197 public Item(ComboBox container, String name) 198 throws IllegalActionException, NameDuplicationException { 199 super(container, name); 200 201 next = new StringParameter(this, "next"); 202 } 203 204 /** Specify the container NamedObj, adding this attribute to the 205 * list of attributes in the container. If the container already 206 * contains an attribute with the same name, then throw an exception 207 * and do not make any changes. Similarly, if the container is 208 * not in the same workspace as this attribute, throw an exception. 209 * If this attribute is already contained by the NamedObj, do nothing. 210 * If the attribute already has a container, remove 211 * this attribute from its attribute list first. Otherwise, remove 212 * it from the directory of the workspace, if it is there. 213 * If the argument is null, then remove it from its container. 214 * It is not added to the workspace directory, so this could result in 215 * this object being garbage collected. 216 * Note that since an Attribute is a NamedObj, it can itself have 217 * attributes. However, recursive containment is not allowed, where 218 * an attribute is an attribute of itself, or indirectly of any attribute 219 * it contains. This method is write-synchronized on the 220 * workspace and increments its version number. 221 * <p> 222 * Subclasses may constrain the type of container by overriding 223 * {@link #setContainer(NamedObj)}. 224 * @param container The container to attach this attribute to.. 225 * @exception IllegalActionException If this attribute is not of the 226 * expected class for the container, or it has no name, 227 * or the attribute and container are not in the same workspace, or 228 * the proposed container would result in recursive containment. 229 * @exception NameDuplicationException If the container already has 230 * an attribute with the name of this attribute. 231 * @see #getContainer() 232 */ 233 @Override 234 public void setContainer(NamedObj container) 235 throws IllegalActionException, NameDuplicationException { 236 ComboBox oldContainer = (ComboBox) getContainer(); 237 if (oldContainer != null) { 238 ((JComboBox) oldContainer.getComponent()).removeItem(this); 239 } 240 super.setContainer(container); 241 if (container != null) { 242 ((JComboBox) ((ComboBox) container).getComponent()) 243 .addItem(this); 244 } 245 } 246 247 /** Return the display name of this item, or its name if the display 248 * name is not specified. The returned string is shown in the combo box 249 * for this item. 250 * 251 * @return The display name of this item. 252 */ 253 @Override 254 public String toString() { 255 return getDisplayName(); 256 } 257 258 /** The name of the next item in the same combo box to be selected when 259 * this GUI property is selected, or an empty string if there is no 260 * next item. 261 */ 262 public StringParameter next; 263 264 /** Create a new Java Swing component. In this case, the component is 265 * always null because a component is not needed for a combo box item. 266 * 267 * @return A Swing component that can be enclosed in this GUI property. 268 * @exception IllegalActionException Not thrown in this base class. 269 */ 270 @Override 271 protected JComponent _createComponent() throws IllegalActionException { 272 // The component should not be used. 273 return null; 274 } 275 } 276 277 /** Create a new JComboBox component. 278 * 279 * @return A Swing component that can be enclosed in this GUI property. 280 * @exception IllegalActionException Not thrown in this base class. 281 */ 282 @Override 283 protected JComponent _createComponent() throws IllegalActionException { 284 JComboBox comboBox = new JComboBox(); 285 comboBox.addItemListener(this); 286 return comboBox; 287 } 288}