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