001/* An attribute for specifying that a parameter is edited with a combo menu. 002 003 Copyright (c) 1998-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.style; 029 030import java.util.List; 031 032import ptolemy.actor.gui.PtolemyQuery; 033import ptolemy.kernel.util.IllegalActionException; 034import ptolemy.kernel.util.NameDuplicationException; 035import ptolemy.kernel.util.NamedObj; 036import ptolemy.kernel.util.Settable; 037import ptolemy.kernel.util.Workspace; 038 039/////////////////////////////////////////////////////////////////// 040//// ChoiceStyle 041 042/** 043 This attribute annotates user settable attributes to specify 044 an uneditable combobox style for configuring the containing attribute. 045 An uneditable combobox is restricted to only the values specified as the 046 combobox options. No arbitrary value can be entered. 047 For an editable combobox, use EditableChoiceStyle instead. 048 The choices that are presented in the combobox 049 are given by a set of attributes implementing the Settable interface, 050 such as StringAttribute, contained by this style. 051 052 @see EditableChoiceStyle 053 @see ptolemy.actor.gui.EditorPaneFactory 054 @see ParameterEditorStyle 055 @see ptolemy.kernel.util.StringAttribute 056 @author Steve Neuendorffer 057 @version $Id$ 058 @since Ptolemy II 1.0 059 @Pt.ProposedRating Green (neuendor) 060 @Pt.AcceptedRating Yellow (neuendor) 061 */ 062public class ChoiceStyle extends ParameterEditorStyle { 063 /** Construct an attribute in the default workspace with an empty string 064 * as its name. 065 * The object is added to the directory of the workspace. 066 * Increment the version number of the workspace. 067 */ 068 public ChoiceStyle() { 069 super(); 070 } 071 072 /** Construct an attribute in the given workspace with an empty string 073 * as its name. 074 * The object is added to the directory of the workspace. 075 * Increment the version number of the workspace. 076 * @param workspace The workspace that will contain the attribute 077 * that is being constructed. 078 079 */ 080 public ChoiceStyle(Workspace workspace) { 081 // This constructor is needed for Shallow codegen to work. 082 super(workspace); 083 } 084 085 /** Construct an attribute with the specified container and name. 086 * @param container The container. 087 * @param name The name of the attribute. 088 * @exception IllegalActionException If the attribute is not of an 089 * acceptable attribute for the container, or if the container 090 * is not an instance of Settable. 091 * @exception NameDuplicationException If the name coincides with 092 * an attribute already in the container. 093 */ 094 public ChoiceStyle(NamedObj container, String name) 095 throws IllegalActionException, NameDuplicationException { 096 super(container, name); 097 } 098 099 /////////////////////////////////////////////////////////////////// 100 //// public methods //// 101 102 /** Return true if this style is acceptable for the given attribute. 103 * @param param The attribute that this annotates. 104 * @return True if the style contains some attributes representing the 105 * choices. 106 */ 107 @Override 108 public boolean acceptable(Settable param) { 109 return !attributeList(Settable.class).isEmpty(); 110 } 111 112 /** Create a new uneditable 113 * combo box entry in the given query associated with the 114 * attribute containing this style. The name of the entry is 115 * the name of the attribute. Attach the attribute to the created entry. 116 * 117 * @param query The query into which to add the entry. 118 * @exception IllegalActionException If the containing attribute 119 * has a value that cannot be edited using this style. 120 */ 121 @Override 122 public void addEntry(PtolemyQuery query) throws IllegalActionException { 123 Settable container = (Settable) getContainer(); 124 String name = container.getName(); 125 List paramList = attributeList(Settable.class); 126 Settable[] choices = (Settable[]) paramList 127 .toArray(new Settable[paramList.size()]); 128 String[] values = new String[choices.length]; 129 130 for (int i = 0; i < choices.length; i++) { 131 values[i] = choices[i].getExpression(); 132 } 133 134 String defaultChoice = container.getExpression(); 135 query.addChoice(name, container.getDisplayName(), values, defaultChoice, 136 _isEditable, PtolemyQuery.preferredBackgroundColor(container), 137 PtolemyQuery.preferredForegroundColor(container)); 138 query.attachParameter(container, name); 139 } 140 141 /** Whether or not the combobox is editable. EditableComboBox changes 142 * this to create an editable combo box. In this base class the 143 * value is false. 144 */ 145 protected boolean _isEditable = false; 146}