View Javadoc
1   package org.opentrafficsim.simulationengine.properties;
2   
3   /**
4    * Property that is described by a set of Strings where exactly one can (and must) be true.
5    * <p>
6    * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
8    * <p>
9    * $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, @version $Revision: 1155 $, by $Author: averbraeck $,
10   * initial version 19 dec. 2014 <br>
11   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
12   */
13  public class SelectionProperty extends AbstractProperty<String>
14  {
15      /** Short description of this SelectionProperty. */
16      private String shortName;
17  
18      /** Description of this SelectionProperty. */
19      private String description;
20  
21      /** The set of options to select from. */
22      private String[] options;
23  
24      /** Index of the currently selected option. */
25      private int currentOption;
26  
27      /** Indicates if this is SelectionProperty is read-only. */
28      private final boolean readOnly;
29  
30      /**
31       * Construct a new SelectionProperty.
32       * @param shortName String; name of the new SelectionProperty
33       * @param description String; description of the new SelectionProperty (may use HTML mark up)
34       * @param options String[]; the possible values of the SelectionProperty
35       * @param initialDefaultOption int; the index of the initially selected option
36       * @param readOnly boolean; if true the selection cannot be altered.
37       * @param displayPriority int; the display priority of the new SelectionProperty
38       */
39      public SelectionProperty(final String shortName, final String description, final String[] options,
40          final int initialDefaultOption, final boolean readOnly, final int displayPriority)
41      {
42          super(displayPriority);
43          this.shortName = shortName;
44          this.description = description;
45          // Make a deep copy of options
46          this.options = new String[options.length];
47          for (int i = 0; i < options.length; i++)
48          {
49              this.options[i] = options[i];
50          }
51          this.currentOption = initialDefaultOption;
52          this.readOnly = readOnly;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public final String getValue()
58      {
59          return this.options[this.currentOption];
60      }
61  
62      /**
63       * Retrieve the name of one of the options of this SelectionProperty.
64       * @param index int; the index of the value
65       * @return String; the name of the value at the requested index
66       */
67      public final String getOptionName(final int index)
68      {
69          return this.options[index];
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public final String getShortName()
75      {
76          return this.shortName;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public final String getDescription()
82      {
83          return this.description;
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public final void setValue(final String newValue) throws PropertyException
89      {
90          if (this.readOnly)
91          {
92              throw new PropertyException("Cannot modify a read-only SelectionProperty");
93          }
94          for (int index = 0; index < this.options.length; index++)
95          {
96              if (this.options[index].equals(newValue))
97              {
98                  this.currentOption = index;
99                  return;
100             }
101         }
102         throw new PropertyException("The value " + newValue + " is not among the valid options");
103     }
104 
105     /**
106      * Return the names of the options of this SelectionProperty.
107      * @return String[]; the names of the options of this SelectionProperty
108      */
109     public final String[] getOptionNames()
110     {
111         return this.options.clone();
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public final boolean isReadOnly()
117     {
118         return this.readOnly;
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public final String htmlStateDescription()
124     {
125         return getShortName() + ": " + this.options[this.currentOption];
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public AbstractProperty<String> deepCopy()
131     {
132         return new SelectionProperty(this.shortName, this.description, this.options, this.currentOption, this.readOnly,
133             getDisplayPriority());
134     }
135 
136 }