1 package cz.cuni.amis.pogamut.sposh.elements;
2
3 import cz.cuni.amis.pogamut.sposh.engine.PoshEngine;
4 import cz.cuni.amis.pogamut.sposh.exceptions.CycleException;
5 import cz.cuni.amis.pogamut.sposh.exceptions.DuplicateNameException;
6 import cz.cuni.amis.pogamut.sposh.exceptions.InvalidNameException;
7 import java.awt.datatransfer.DataFlavor;
8 import java.util.ArrayList;
9 import java.util.List;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public final class DriveElement extends PoshDummyElement<DriveElement, DriveCollection> implements INamedElement, IConditionElement<DriveElement> {
36
37
38
39
40 private String name;
41
42
43
44 private final Trigger<DriveElement> trigger = new Trigger<DriveElement>(this);
45
46
47
48 private final TriggeredAction action;
49
50
51
52
53
54
55 @Deprecated
56 private Freq freq;
57
58
59
60
61
62 @Deprecated
63 private String comment;
64
65
66
67 public static final String deName = "deName";
68
69
70
71 public static final DataFlavor dataFlavor = new DataFlavor(DriveElement.class, "drive-element");
72
73
74
75
76
77
78
79
80
81 DriveElement(String name, List<Sense> triggerSenses, PrimitiveCall actionCall, Freq freq, String comment) {
82 assert name != null;
83 this.name = name;
84
85 assert freq != null;
86 this.freq = freq;
87
88 assert comment != null;
89 this.comment = comment;
90
91 for (Sense triggerSense : triggerSenses) {
92 assert !triggerSense.isChildOfParent();
93 trigger.add(triggerSense);
94 }
95
96 this.action = LapElementsFactory.createAction(actionCall);
97 this.action.setParent(this);
98 }
99
100
101
102
103 public Trigger<DriveElement> getTrigger() {
104 return trigger;
105 }
106
107
108
109
110
111
112
113 @Override
114 public Trigger<DriveElement> getCondition() {
115 return getTrigger();
116 }
117
118
119
120
121
122
123
124 public TriggeredAction getAction() {
125 return action;
126 }
127
128 @Override
129 public String toString() {
130 StringBuilder sb = new StringBuilder();
131
132 sb.append('(');
133 sb.append(name);
134 if (!trigger.isEmpty()) {
135 sb.append(" (trigger ");
136 sb.append(trigger.toString());
137 sb.append(')');
138 }
139 sb.append(' ');
140 sb.append(this.action.toString());
141 sb.append(')');
142
143 return sb.toString();
144 }
145
146 @Override
147 public List<PoshElement> getChildDataNodes() {
148 List<PoshElement> children = new ArrayList<PoshElement>();
149 children.addAll(trigger);
150 children.add(this.action);
151
152 return children;
153 }
154
155 @Override
156 public String getName() {
157 return name;
158 }
159
160
161
162
163
164
165
166
167 public void setName(String newName) throws InvalidNameException, DuplicateNameException {
168 newName = newName.trim();
169 if (!newName.matches(IDENT_PATTERN)) {
170 throw InvalidNameException.create(newName);
171 }
172 DriveCollection dc = getParent();
173 for (DriveElement drive : dc.getDrives()) {
174 if (drive.getName().equals(newName) && drive != this) {
175 throw new DuplicateNameException("There already is a drive with name " + newName);
176 }
177 }
178
179
180 String oldName = this.name;
181 this.name = newName;
182
183 firePropertyChange(deName, oldName, newName);
184 }
185
186
187
188
189
190
191
192 @Override
193 public void rename(String newName) throws InvalidNameException, CycleException, DuplicateNameException {
194 setName(newName);
195 }
196
197 @Override
198 public boolean moveChild(int newIndex, PoshElement child) {
199 throw new UnsupportedOperationException();
200 }
201
202 @Override
203 public DataFlavor getDataFlavor() {
204 return dataFlavor;
205 }
206
207 @Override
208 public LapType getType() {
209 return LapType.DRIVE_ELEMENT;
210 }
211
212
213
214
215
216
217
218
219
220
221 @Deprecated
222 public Freq getFreq() {
223 return freq;
224 }
225
226
227
228
229
230
231
232 @Deprecated
233 public String getComment() {
234 return comment;
235 }
236 }