1 package org.opentrafficsim.simulationengine.properties;
2
3
4
5
6
7
8
9
10
11
12
13 public class SelectionProperty extends AbstractProperty<String>
14 {
15
16 private String shortName;
17
18
19 private String description;
20
21
22 private String[] options;
23
24
25 private int currentOption;
26
27
28 private final boolean readOnly;
29
30
31
32
33
34
35
36
37
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
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
56 @Override
57 public final String getValue()
58 {
59 return this.options[this.currentOption];
60 }
61
62
63
64
65
66
67 public final String getOptionName(final int index)
68 {
69 return this.options[index];
70 }
71
72
73 @Override
74 public final String getShortName()
75 {
76 return this.shortName;
77 }
78
79
80 @Override
81 public final String getDescription()
82 {
83 return this.description;
84 }
85
86
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
107
108
109 public final String[] getOptionNames()
110 {
111 return this.options.clone();
112 }
113
114
115 @Override
116 public final boolean isReadOnly()
117 {
118 return this.readOnly;
119 }
120
121
122 @Override
123 public final String htmlStateDescription()
124 {
125 return getShortName() + ": " + this.options[this.currentOption];
126 }
127
128
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 }