Skip to content

Package: ConnectAction

ConnectAction

nameinstructionbranchcomplexitylinemethod
ConnectAction(String, ConfigurationGraph)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
actionPerformed(ActionEvent)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createNewConnection(Adapter, Adapter)
M: 7 C: 19
73%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 1 C: 5
83%
M: 0 C: 1
100%
createNewConnectionBetweeenRouterAndInternet(Adapter, Adapter, ConnectingType, List)
M: 37 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
showWarningMessage()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
typeInList(ConnectingType, List)
M: 4 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.actions;
2:
3: import java.awt.event.ActionEvent;
4: import java.util.List;
5:
6: import javax.swing.JOptionPane;
7:
8: import networkconfigurator.ConfigurationGraph;
9: import networkconfigurator.item.Adapter;
10: import networkconfigurator.item.ConfigRouter;
11: import networkconfigurator.item.ConnectVisitor;
12: import networkconfigurator.item.ConnectingType;
13: import networkconfigurator.item.ConnectingTypeVisitor;
14: import networkconfigurator.item.Internet;
15: import networkconfigurator.item.Item;
16: import networkconfigurator.item.NetworkAdapter;
17: import networkconfigurator.item.NetworkCable;
18: import networkconfigurator.item.Node;
19: import networkconfigurator.item.Slot;
20:
21: /**
22: * Action for connecting selected nodes.
23: */
24: public final class ConnectAction extends AbstractConfigurationGraphAction {
25:         /**
26:          * Constructor.
27:          *
28:          * @param name
29:          * the name.
30:          * @param graph
31:          * the graph.
32:          */
33:         public ConnectAction(final String name, final ConfigurationGraph graph) {
34:                 super(name, graph);
35:         }
36:
37:         @Override
38:         public void actionPerformed(final ActionEvent e) {
39:                 final List<Item> selected = getGraph().getSelected();
40:                 Item.getSelectedNodes(getGraph().getItems(), selected);
41:
42:•                if (selected.size() == 2) {
43:                         selected.get(0).acceptConnectVisitor(new ConnectVisitor() {
44:
45:                                 @Override
46:                                 public void slot(final Slot slot) {
47:                                         slot.getOwner().acceptConnectingTypeVisitor(
48:                                                         new VisitOwnerOfFirstAdapter(selected, slot));
49:                                 }
50:
51:                                 @Override
52:                                 public void adapter(final NetworkAdapter adapter) {
53:                                         adapter.getOwner().acceptConnectingTypeVisitor(
54:                                                         new VisitOwnerOfFirstAdapter(selected, adapter));
55:                                 }
56:                         });
57:                 }
58:                 getGraph().repaint();
59:         }
60:
61:         /**
62:          * Method for showing the Warning Message.
63:          */
64:         private void showWarningMessage() {
65:                 JOptionPane.showMessageDialog(getGraph(), NcActionsConstants.WARNING_MESSAGE,
66:                                 NcActionsConstants.WARNING_TITEL, JOptionPane.WARNING_MESSAGE);
67:         }
68:
69:         /**
70:          * true if list contains type.
71:          *
72:          * @param type
73:          * the type.
74:          * @param list
75:          * the list.
76:          * @return true/false
77:          */
78:         private boolean typeInList(final ConnectingType type, final List<ConnectingType> list) {
79:                 return list.contains(type);
80:         }
81:
82:         /**
83:          * creates a new connection between adaper1 and adapter2 if there are not already connected and
84:          * their owner have the same connectingtype.
85:          *
86:          * @param adapter1
87:          * the adapter.
88:          * @param adapter2
89:          * the adapter.
90:          * @param type
91:          * type which has to be in the list.
92:          * @param list
93:          * the list.
94:          */
95:         private void createNewConnectionBetweeenRouterAndInternet(final Adapter adapter1,
96:                         final Adapter adapter2, final ConnectingType type, final List<ConnectingType> list) {
97:•                if (adapter2.getState().isConnected() && adapter1.getState().isConnected()) {
98:                         showWarningMessage();
99:                 } else {
100:•                        if (list.contains(type)) {
101:                                 getGraph().getItems().add(new NetworkCable(adapter2, adapter1));
102:                                 adapter1.setConnected();
103:                                 adapter2.setConnected();
104:                         } else {
105:                                 JOptionPane.showMessageDialog(getGraph(),
106:                                                 "Connection Type from the internet is not supported by the router.",
107:                                                 "Connection Type is not supported", JOptionPane.WARNING_MESSAGE);
108:                         }
109:                 }
110:         }
111:
112:         /**
113:          * creates a new connection between adaper1 and adapter2 if there are not already connected.
114:          *
115:          * @param adapter1
116:          * the adapter.
117:          * @param adapter2
118:          * the adapter.
119:          */
120:         private void createNewConnection(final Adapter adapter1, final Adapter adapter2) {
121:•                if (adapter2.getState().isConnected() && adapter1.getState().isConnected()) {
122:                         showWarningMessage();
123:                 } else {
124:                         getGraph().getItems().add(new NetworkCable(adapter2, adapter1));
125:                         adapter1.setConnected();
126:                         adapter2.setConnected();
127:                 }
128:         }
129:
130:         /**
131:          * Visitor for visiting the secondAdapter.
132:          *
133:          * @author Erik Ole Arand
134:          *
135:          */
136:         private final class VisitOwnerOfFirstAdapter implements ConnectingTypeVisitor {
137:                 /**
138:                  * The selected items.
139:                  */
140:                 private final List<Item> selected;
141:                 /**
142:                  * The adapter.
143:                  */
144:                 private final Adapter adapter1;
145:
146:                 /**
147:                  * The Constructor.
148:                  *
149:                  * @param selected
150:                  * the selected items.
151:                  * @param adapter
152:                  * the Adapter.
153:                  */
154:                 private VisitOwnerOfFirstAdapter(final List<Item> selected, final Adapter adapter) {
155:                         this.selected = selected;
156:                         this.adapter1 = adapter;
157:                 }
158:
159:                 @Override
160:                 public void router(final ConfigRouter router) {
161:                         final List<ConnectingType> list1 =
162:                                         router.getConnectingTypes().getConnectingTypeList();
163:                         selected.get(1).acceptConnectVisitor(
164:                                         new VisitOwnerOfSecondAdapterWithListOfTypes(list1, adapter1));
165:                 }
166:
167:                 @Override
168:                 public void internet(final Internet internet) {
169:                         final ConnectingType type1 = internet.getConnectingType();
170:                         selected.get(1).acceptConnectVisitor(
171:                                         new VisitOwnerOfSecondAdapterWithOneType(type1, adapter1));
172:                 }
173:
174:                 @Override
175:                 public void others(final Node node) {
176:                         selected.get(1).acceptConnectVisitor(new ConnectVisitor() {
177:                                 @Override
178:                                 public void slot(final Slot slot) {
179:                                         createNewConnection(slot, adapter1);
180:                                 }
181:
182:                                 @Override
183:                                 public void adapter(final NetworkAdapter networkAdapter) {
184:                                         createNewConnection(networkAdapter, adapter1);
185:                                 }
186:                         });
187:                 }
188:         }
189:
190:         /**
191:          * Visits the owner of the second Adapter, that should be connected, if the owner of the first
192:          * adapter was a configRouter node.
193:          *
194:          * @author Erik Ole Arand
195:          *
196:          */
197:         private final class VisitOwnerOfSecondAdapterWithListOfTypes implements ConnectVisitor {
198:                 /**
199:                  * The list of connecting types that is.
200:                  */
201:                 private final List<ConnectingType> list1;
202:
203:                 /**
204:                  * the first adapter which owner is an internet node.
205:                  */
206:                 private final Adapter adapter1;
207:
208:                 /**
209:                  * The Constructor of the Visitor.
210:                  *
211:                  * @param list1
212:                  * list of types of the configrouter which adapter should be connected.
213:                  * @param adapter1
214:                  * adapter1 which should be connected.
215:                  */
216:                 private VisitOwnerOfSecondAdapterWithListOfTypes(final List<ConnectingType> list1,
217:                                 final Adapter adapter1) {
218:                         this.list1 = list1;
219:                         this.adapter1 = adapter1;
220:                 }
221:
222:                 @Override
223:                 public void slot(final Slot slot) {
224:                         slot.getOwner().acceptConnectingTypeVisitor(new ConnectingTypeVisitor() {
225:
226:                                 @Override
227:                                 public void router(final ConfigRouter router) {
228:                                 }
229:
230:                                 @Override
231:                                 public void internet(final Internet internet) {
232:                                         final ConnectingType type2 = internet.getConnectingType();
233:                                         typeInList(type2, list1);
234:                                         createNewConnectionBetweeenRouterAndInternet(slot, adapter1, type2, list1);
235:                                 }
236:
237:                                 @Override
238:                                 public void others(final Node node) {
239:                                         createNewConnection(slot, adapter1);
240:                                 }
241:                         });
242:                 }
243:
244:                 @Override
245:                 public void adapter(final NetworkAdapter adapter) {
246:                         adapter.getOwner().acceptConnectingTypeVisitor(new ConnectingTypeVisitor() {
247:
248:                                 @Override
249:                                 public void router(final ConfigRouter router) {
250:                                         createNewConnection(adapter, adapter1);
251:                                 }
252:
253:                                 @Override
254:                                 public void internet(final Internet internet) {
255:                                 }
256:
257:                                 @Override
258:                                 public void others(final Node node) {
259:                                         createNewConnection(adapter, adapter1);
260:                                 }
261:
262:                         });
263:                 }
264:         }
265:
266:         /**
267:          * Visits the owner of the second Adapter, that should be connected, if the owner of the first
268:          * adapter was a internet node.
269:          *
270:          * @author Erik Ole Arand
271:          *
272:          */
273:         private final class VisitOwnerOfSecondAdapterWithOneType implements ConnectVisitor {
274:                 /**
275:                  * the type of the first adapter, that owner was a internet node.
276:                  */
277:                 private final ConnectingType type1;
278:                 /**
279:                  * the first adapter which owner is an internet node.
280:                  */
281:                 private final Adapter adapter1;
282:
283:                 /**
284:                  * The Constructor of the Visitor.
285:                  *
286:                  * @param type1
287:                  * type of the internet which adapter should be connected.
288:                  * @param adapter1
289:                  * adapter1 which should be connected.
290:                  */
291:                 private VisitOwnerOfSecondAdapterWithOneType(final ConnectingType type1,
292:                                 final Adapter adapter1) {
293:                         this.type1 = type1;
294:                         this.adapter1 = adapter1;
295:                 }
296:
297:                 @Override
298:                 public void slot(final Slot slot) {
299:                         slot.getOwner().acceptConnectingTypeVisitor(new ConnectingTypeVisitor() {
300:
301:                                 @Override
302:                                 public void router(final ConfigRouter router) {
303:                                 }
304:
305:                                 @Override
306:                                 public void internet(final Internet internet) {
307:                                         JOptionPane.showMessageDialog(getGraph(),
308:                                                         "Makes no sense to connect Internet with internet",
309:                                                         NcActionsConstants.WARNING_TITEL, JOptionPane.WARNING_MESSAGE);
310:                                 }
311:
312:                                 @Override
313:                                 public void others(final Node node) {
314:                                         createNewConnection(slot, adapter1);
315:                                 }
316:                         });
317:                 }
318:
319:                 @Override
320:                 public void adapter(final NetworkAdapter adapter) {
321:                         adapter.getOwner().acceptConnectingTypeVisitor(new ConnectingTypeVisitor() {
322:
323:                                 @Override
324:                                 public void router(final ConfigRouter router) {
325:                                         final List<ConnectingType> list1 =
326:                                                         router.getConnectingTypes().getConnectingTypeList();
327:                                         createNewConnectionBetweeenRouterAndInternet(adapter, adapter1, type1, list1);
328:                                 }
329:
330:                                 @Override
331:                                 public void internet(final Internet internet) {
332:                                 }
333:
334:                                 @Override
335:                                 public void others(final Node node) {
336:                                         createNewConnection(adapter, adapter1);
337:
338:                                 }
339:                         });
340:                 }
341:         }
342:
343: }