Skip to content

Package: Item$1

Item$1

nameinstructionbranchcomplexitylinemethod
visit(DetailBarLabel)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
visit(DetailInputBox)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
visit(VariableAssignment)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package networkconfigurator.item;
2:
3: import java.awt.Graphics;
4: import java.awt.Point;
5: import java.awt.Rectangle;
6: import java.io.Serializable;
7: import java.util.ArrayList;
8: import java.util.Iterator;
9: import java.util.List;
10:
11: import model.assignment.VariableAssignment;
12: import networkconfigurator.CompareVaNameVisitor;
13: import networkconfigurator.DetailBarLabel;
14: import networkconfigurator.DetailBarPropertyEntry;
15: import networkconfigurator.DetailDrawPropertyEntry;
16: import networkconfigurator.DetailInputBox;
17:
18: /**
19: * A Node represents a node in a graph.
20: *
21: * @author Erik Arand
22: */
23: public abstract class Item implements Serializable {
24:         /**
25:          * Point of the Node.
26:          */
27:         private Point point;
28:         /**
29:          * radius of the Node.
30:          */
31:         private final int radius = 15;
32:         /**
33:          * Boolean if selected.
34:          */
35:         private boolean selected = false;
36:         /**
37:          * Rectangle if selected of the Node.
38:          */
39:         private final Rectangle boundary = new Rectangle();
40:         /**
41:          * saves the entries for the properties draw.
42:          */
43:         private final List<DetailDrawPropertyEntry> propertyDraw =
44:                         new ArrayList<DetailDrawPropertyEntry>();
45:         /**
46:          * saves the entries for the properties bar.
47:          */
48:         private final List<DetailBarPropertyEntry> propertyBar =
49:                         new ArrayList<DetailBarPropertyEntry>();
50:
51:         /**
52:          * getter for the properties draw.
53:          *
54:          * @return propertyDraw
55:          */
56:         public final List<DetailDrawPropertyEntry> getPropertyDraw() {
57:                 return propertyDraw;
58:         }
59:
60:         /**
61:          * getter for the properties bar.
62:          *
63:          * @return propertyBar
64:          */
65:         public final List<DetailBarPropertyEntry> getPropertyBar() {
66:                 return propertyBar;
67:         }
68:
69:         /**
70:          * Return true if this node contains p.
71:          *
72:          * @param p
73:          * the Point
74:          * @return the answer
75:          */
76:         public boolean contains(final Point p) {
77:                 return boundary.contains(p);
78:         }
79:
80:         /**
81:          * Return true if this node is selected.
82:          *
83:          * @return the answer
84:          */
85:         public boolean getSelected() {
86:                 return selected;
87:         }
88:
89:         /**
90:          * Mark this node as selected.
91:          *
92:          * @param selected
93:          * selects the node.
94:          */
95:         public void setSelected(final boolean selected) {
96:                 this.selected = selected;
97:         }
98:
99:         /**
100:          * Üblicher Getter für das Attribut p.
101:          *
102:          * @return liefert p.
103:          */
104:         public Point getPoint() {
105:                 return point;
106:         }
107:
108:         /**
109:          * Üblicher Setter für das Attribut p.
110:          *
111:          * @param p
112:          * Setzt den Parameter {@code p} in das Feld p.
113:          */
114:         public void setPoint(final Point p) {
115:                 this.point = p;
116:         }
117:
118:         /**
119:          * Üblicher Getter für das Attribut b.
120:          *
121:          * @return liefert b.
122:          */
123:         public Rectangle getBoundary() {
124:                 return boundary;
125:         }
126:
127:         /**
128:          * Calculate this node's rectangular boundary.
129:          *
130:          */
131:         public void setBoundary() {
132:                 boundary.setBounds(getPoint().x - radius, getPoint().y - radius, 2 * radius, 2 * radius);
133:         }
134:
135:         /**
136:          * Draw this item.
137:          *
138:          * @param g
139:          * to be drawn
140:          */
141:         public abstract void draw(Graphics g);
142:
143:         /**
144:          * mark the item when it is selected.
145:          *
146:          * @param g
147:          * the Graphic.
148:          */
149:         public abstract void markAsSelected(Graphics g);
150:
151:         /**
152:          * The comparison method which evaluates if a variable assignment contains the identifier in the
153:          * detail bar.
154:          *
155:          * @param assignment
156:          * the variable assignment
157:          * @return if the variable assignment was found
158:          */
159:         public final Boolean containsVaByIdentifier(final VariableAssignment assignment) {
160:                 final String identifier = assignment.getNameOfAssignment();
161:                 final Iterator<DetailBarPropertyEntry> iterator = getPropertyBar().iterator();
162:                 while (iterator.hasNext()) {
163:                         final boolean found =
164:                                         iterator.next().getPropertyBarEntry().accept(new CompareVaNameVisitor() {
165:
166:                                                 @Override
167:                                                 public boolean visit(final VariableAssignment va) {
168:                                                         return va.getNameOfAssignment().equals(identifier);
169:                                                 }
170:
171:                                                 @Override
172:                                                 public boolean visit(final DetailBarLabel label) {
173:                                                         return false;
174:                                                 }
175:
176:                                                 @Override
177:                                                 public boolean visit(final DetailInputBox detailBarInputBox) {
178:                                                         return false;
179:                                                 }
180:                                         });
181:                         if (found) {
182:                                 return true;
183:                         }
184:                 }
185:                 return false;
186:         }
187:
188:         /**
189:          * Collected all the selected nodes in list.
190:          *
191:          * @param list
192:          * list of all nodes
193:          * @param selected
194:          * list of selected nodes
195:          */
196:         public static void getSelectedNodes(final List<Item> list, final List<Item> selected) {
197:                 selected.clear();
198:                 for (Item n : list) {
199:                         if (n.getSelected()) {
200:                                 selected.add(n);
201:                         }
202:                 }
203:         }
204:
205:         /**
206:          * Select no nodes.
207:          *
208:          * @param list
209:          * List of nodes that should not be selected
210:          */
211:         public static void selectNone(final List<Item> list) {
212:                 for (Item n : list) {
213:                         n.setSelected(false);
214:                 }
215:         }
216:
217:         /**
218:          * Select a single node; return true if not already selected.
219:          *
220:          * @param list
221:          * list of Nodes.
222:          * @param p
223:          * point
224:          * @return if node is already selected
225:          */
226:         public static boolean selectOne(final List<Item> list, final Point p) {
227:                 for (Item n : list) {
228:                         if (n.contains(p)) {
229:                                 if (!n.getSelected()) {
230:                                         Item.selectNone(list);
231:                                         n.setSelected(true);
232:                                 }
233:                                 return true;
234:                         }
235:                 }
236:                 return false;
237:         }
238:
239:         /**
240:          * Returns true if node in List already selected.
241:          *
242:          * @param list
243:          * List of nodes.
244:          * @param p
245:          * the point.
246:          * @return Returns true if node is already selected.
247:          */
248:         public static boolean selectMultiple(final List<Item> list, final Point p) {
249:                 for (Item n : list) {
250:                         if (n.contains(p)) {
251:                                 if (!n.getSelected()) {
252:                                         n.setSelected(true);
253:                                 }
254:                                 return true;
255:                         }
256:                 }
257:                 return false;
258:         }
259:
260:         /**
261:          * Select all items.
262:          *
263:          * @param list
264:          * list of items.
265:          */
266:         public static void selectAll(final List<Item> list) {
267:                 for (Item n : list) {
268:                         n.setSelected(true);
269:                 }
270:         }
271:
272:         /**
273:          * Select each node in r.
274:          *
275:          * @param list
276:          * list of nodes in the rectangle
277:          * @param r
278:          * the rectangle
279:          */
280:         public static void selectRect(final List<Item> list, final Rectangle r) {
281:                 for (Item n : list) {
282:                         n.setSelected(r.contains(n.getPoint()));
283:                 }
284:         }
285:
286:         /**
287:          * Toggle selected state of each node containing p.
288:          *
289:          * @param list
290:          * list of selected nodes that contain point p
291:          * @param p
292:          * the point
293:          */
294:         public static void selectToggle(final List<Item> list, final Point p) {
295:                 for (Item n : list) {
296:                         if (n.contains(p)) {
297:                                 n.setSelected(!n.getSelected());
298:                         }
299:                 }
300:         }
301:
302:         /**
303:          * Update each node's position by d (delta).
304:          *
305:          * @param list
306:          * list of nodes that need to update their position
307:          * @param delta
308:          * delta of the new position and the previous
309:          */
310:         public static void updatePosition(final List<Item> list, final Point delta) {
311:                 for (Item n : list) {
312:                         if (n.getSelected()) {
313:                                 n.getPoint().x += delta.x;
314:                                 n.getPoint().y += delta.y;
315:                                 n.setBoundary();
316:                         }
317:                 }
318:         }
319:
320:         /**
321:          * accept-method for nodevisitor (VisitorPattern).
322:          *
323:          * @param itemVisitor
324:          * the nodevisitor.
325:          */
326:         public abstract void accept(ItemVisitor itemVisitor);
327:
328:         /**
329:          * accept-method for connectvisitor (VisitorPattern).
330:          *
331:          * @param visitor
332:          * the connectvisitor.
333:          */
334:         public abstract void acceptConnectVisitor(ConnectVisitor visitor);
335:
336: }