Skip to content

Package: ConfigurationGraph$RunClass

ConfigurationGraph$RunClass

nameinstructionbranchcomplexitylinemethod
ConfigurationGraph.RunClass(RouterConfiguration)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
run()
M: 95 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package networkconfigurator;
2:
3: import java.awt.BorderLayout;
4: import java.awt.Color;
5: import java.awt.Dimension;
6: import java.awt.EventQueue;
7: import java.awt.Graphics;
8: import java.awt.Point;
9: import java.awt.Rectangle;
10: import java.awt.event.InputEvent;
11: import java.awt.event.KeyAdapter;
12: import java.awt.event.KeyEvent;
13: import java.awt.event.MouseAdapter;
14: import java.awt.event.MouseEvent;
15: import java.awt.event.MouseMotionAdapter;
16: import java.awt.event.WindowAdapter;
17: import java.awt.event.WindowEvent;
18: import java.io.IOException;
19: import java.util.ArrayList;
20: import java.util.List;
21: import java.util.Observable;
22: import java.util.Observer;
23: import java.util.Set;
24:
25: import javax.swing.ImageIcon;
26: import javax.swing.JComponent;
27: import javax.swing.JFrame;
28: import javax.swing.JOptionPane;
29: import javax.swing.JPanel;
30: import javax.swing.JScrollPane;
31: import javax.swing.JToolBar;
32: import javax.swing.KeyStroke;
33:
34: import basic.GuiConstants;
35: import model.RouterConfiguration;
36: import model.assignment.AssignmentFile;
37: import networkconfigurator.actions.DeleteAction;
38: import networkconfigurator.actions.LoadTask;
39: import networkconfigurator.actions.RenameAction;
40: import networkconfigurator.actions.ResetAction;
41: import networkconfigurator.actions.ResetTask;
42: import networkconfigurator.actions.SaveAction;
43: import networkconfigurator.actions.SaveTask;
44: import networkconfigurator.actions.SelectAllAction;
45: import networkconfigurator.item.ConfigRouter;
46: import networkconfigurator.item.Edge;
47: import networkconfigurator.item.Item;
48:
49: /**
50: * @author Erik Arand
51: */
52: public class ConfigurationGraph extends JComponent implements Observer {
53:
54:         /**
55:          * Width of window.
56:          */
57:         private static final int WIDE = 640;
58:         /**
59:          * Height of window.
60:          */
61:         private static final int HIGH = 480;
62:         /**
63:          * Radius of Icons.
64:          */
65:         private static final int RADIUS = 15;
66:         /**
67:          * Backgroundcolor of window.
68:          */
69:         private final Color backgroundColor = new Color(0x00f0f0f0);
70:         /**
71:          * Controls of the window.
72:          */
73:         private final ControlPanel control = new ControlPanel(this);
74:         /**
75:          * Detailbar for devices.
76:          */
77:         private final DetailBar devicesdetailView = new DetailBar(this);
78:         /**
79:          * radius.
80:          */
81:         private final int radius = RADIUS;
82:         /**
83:          * List of all items added to the screen.
84:          */
85:         private final List<Item> items = new ArrayList<Item>();
86:
87:         /**
88:          * Gets all items added to the graph.
89:          *
90:          * @return all nodes
91:          */
92:         public List<Item> getItems() {
93:                 return items;
94:         }
95:
96:         /**
97:          * Der Router, der konfiguriert wird.
98:          */
99:         private ConfigRouter theConfigRouter;
100:
101:         /**
102:          * get the config router.
103:          *
104:          * @return the config router
105:          */
106:         public ConfigRouter getTheConfigRouter() {
107:                 return theConfigRouter;
108:         }
109:
110:         /**
111:          * set the config router.
112:          *
113:          * @param configRouter
114:          * the router to be set.
115:          *
116:          */
117:         public void setTheConfigRouter(final ConfigRouter configRouter) {
118:                 theConfigRouter = configRouter;
119:         }
120:
121:         /**
122:          * List of all node that are selected.
123:          */
124:         private final List<Item> selected = new ArrayList<Item>();
125:
126:         /**
127:          * gets the List of selected nodes.
128:          *
129:          * @return list of selected nodes.
130:          */
131:         public List<Item> getSelected() {
132:                 Item.getSelectedNodes(items, selected);
133:                 return selected;
134:         }
135:
136:         /**
137:          * default point.
138:          */
139:         private Point mousePt = new Point(WIDE / 2, HIGH / 2);
140:
141:         /**
142:          * gets the point of the mouse.
143:          *
144:          * @return mouse point
145:          */
146:         public Point getMousePt() {
147:                 return mousePt;
148:         }
149:
150:         /**
151:          * Retangle of Icons.
152:          */
153:         private final Rectangle mouseRect = new Rectangle();
154:         /**
155:          * Boolean for selects.
156:          */
157:         private boolean selecting = false;
158:         /**
159:          * Boolean if key "ctrl" is Pressed.
160:          */
161:         private boolean ctrlPressed = false;
162:         /**
163:          * SelectAll String.
164:          */
165:         private final String selectAll = "selectAll";
166:         /**
167:          * Save String.
168:          */
169:         private final String save = "save";
170:         /**
171:          * Save String.
172:          */
173:         private final String reset = "reset";
174:         /**
175:          * Delete String.
176:          */
177:         private final String delete = "delete";
178:         /**
179:          * F2 String.
180:          */
181:         private final String f2 = "F2";
182:         /**
183:          * the associated router configuration.
184:          */
185:         private final RouterConfiguration rc;
186:
187:         /**
188:          * Running the configuration graph.
189:          *
190:          * @param routerConfiguration
191:          * Adjustments can be made with the help of loadConfig.
192:          */
193:         public static void executeConfigurationGraph(final RouterConfiguration routerConfiguration) {
194:                 EventQueue.invokeLater(new RunClass(routerConfiguration));
195:         }
196:
197:         /**
198:          * Der ConfigurationGraph with configurations to be loaded.
199:          *
200:          * @param rc
201:          * preconfigurations to be loaded.
202:          * @throws LoadConfigurationGraphException
203:          * the error is thrown if a problem occurs during loading.
204:          *
205:          */
206:         public ConfigurationGraph(final RouterConfiguration rc)
207:                         throws LoadConfigurationGraphException {
208:                 this.rc = rc;
209:                 prepareConfigurationGraph();
210:
211:                 for (AssignmentFile assignmentFile : this.rc.getAssignmentFileManager()
212:                                 .getAssignmentFiles()) {
213:                         assignmentFile.addObserver(this);
214:                 }
215:
216:                 // this.theConfigRouter = new ConfigRouter(this.getMousePt().getLocation(),
217:                 // new VariableAssignment(null, null, "ConfigRouter", null));
218:
219:                 loadConfig();
220:         }
221:
222:         /**
223:          * prepare the configuration graph.
224:          */
225:         private void prepareConfigurationGraph() {
226:                 this.setOpaque(true);
227:                 this.setFocusable(true);
228:                 this.addMouseListener(new MouseHandler());
229:                 this.addMouseMotionListener(new MouseMotionHandler());
230:                 this.addKeyListener(new KeyHandler());
231:                 this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK),
232:                                 selectAll);
233:                 this.getActionMap().put(selectAll, new SelectAllAction(selectAll, this));
234:                 this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK), save);
235:                 this.getActionMap().put(save, new SaveAction(this));
236:                 this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK),
237:                                 reset);
238:                 this.getActionMap().put(reset, new ResetAction(this));
239:                 this.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_DELETE, 0), delete);
240:                 this.getActionMap().put(delete, new DeleteAction(delete, this));
241:                 this.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_F2, 0), f2);
242:                 this.getActionMap().put(f2, new RenameAction(f2, this));
243:         }
244:
245:         /**
246:          * serializes the item list to the xml file.
247:          *
248:          * @throws IOException
249:          * if the changes in the xml file can not be saved
250:          */
251:         public void saveConfig() throws IOException {
252:                 final SaveTask saveTask = new SaveTask();
253:                 saveTask.saveGraphConfiguration(this.rc.getPath(), this.getItems());
254:         }
255:
256:         /**
257:          * loads the item list to the xml file.
258:          *
259:          * @throws LoadConfigurationGraphException
260:          * the error is thrown if a problem occurs during loading.
261:          */
262:         public void loadConfig() throws LoadConfigurationGraphException {
263:                 final LoadTask load = new LoadTask(this);
264:                 load.loadGraphConfiguration(this.rc);
265:         }
266:
267:         /**
268:          * resets the config.
269:          *
270:          * @throws IOException
271:          * if the changes in the xml file can not be loaded.
272:          * @throws LoadConfigurationGraphException
273:          * the error is thrown if a problem occurs during loading.
274:          */
275:         public void resetConfig() throws IOException, LoadConfigurationGraphException {
276:                 final ResetTask resetTask = new ResetTask(this);
277:                 resetTask.resetGraphConfiguration(this.rc);
278:         }
279:
280:         /**
281:          * Setter of Edges.
282:          *
283:          * @param eS
284:          * Set of Edges.
285:          */
286:         public void setEdges(final Set<Edge> eS) {
287:                 for (Edge e : eS) {
288:                         this.items.add(e.getN1());
289:                         this.items.add(e.getN2());
290:                         this.items.add(e);
291:                 }
292:         }
293:
294:         @Override
295:         public Dimension getPreferredSize() {
296:                 return new Dimension(WIDE, HIGH);
297:         }
298:
299:         @Override
300:         public void paintComponent(final Graphics g) {
301:                 g.setColor(this.backgroundColor);
302:                 g.fillRect(0, 0, getWidth(), getHeight());
303:                 for (Item n : items) {
304:                         n.draw(g);
305:                 }
306:                 if (selecting) {
307:                         g.setColor(Color.darkGray);
308:                         g.drawRect(mouseRect.x, mouseRect.y, mouseRect.width, mouseRect.height);
309:                 }
310:         }
311:
312:         /**
313:          * The Run-Class.
314:          *
315:          * @author Erik Arand
316:          *
317:          */
318:         private static final class RunClass implements Runnable {
319:                 /**
320:                  * The Run Configuration.
321:                  */
322:                 private final RouterConfiguration rc;
323:
324:                 /**
325:                  * The Constructor.
326:                  *
327:                  * @param routerConfiguration
328:                  * the routerConfiguration to load the assignmentfiles and references.
329:                  */
330:                 private RunClass(final RouterConfiguration routerConfiguration) {
331:                         this.rc = routerConfiguration;
332:                 }
333:
334:                 @Override
335:                 public void run() {
336:                         final JFrame f = new JFrame("fli4l Network configuration");
337:                         final ImageIcon img =
338:                                         new ImageIcon(NetworkconfiguratorConstants.PACKAGE_ICONS + "fli4l.png");
339:                         f.setIconImage(img.getImage());
340:
341:                         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
342:
343:                         try {
344:                                 final ConfigurationGraph gp = new ConfigurationGraph(rc);
345:                                 f.add(gp.control, BorderLayout.NORTH);
346:
347:                                 final JPanel p = new JPanel(new BorderLayout());
348:                                 p.add(gp.devicesdetailView, BorderLayout.EAST);
349:                                 f.add(p, BorderLayout.EAST);
350:
351:                                 final TextfieldListener deviceNameListener =
352:                                                 new TextfieldListener(gp, gp.devicesdetailView);
353:                                 gp.devicesdetailView.getNameText().addKeyListener(deviceNameListener);
354:
355:                                 f.add(new JScrollPane(gp), BorderLayout.CENTER);
356:                                 f.pack();
357:                                 f.setLocationByPlatform(true);
358:                                 f.setVisible(true);
359:                                 // TODO gab es Changes? Sonst kein Aufruf
360:                                 f.addWindowListener(new WindowAdapter() {
361:                                         @Override
362:                                         public void windowClosing(final WindowEvent we) {
363:                                                 final int result = JOptionPane.showConfirmDialog(f,
364:                                                                 GuiConstants.DO_YOU_WANT_TO_SAVE_CHANGES,
365:                                                                 GuiConstants.EXIT_CONFIRMATION, JOptionPane.YES_NO_OPTION);
366:                                                 if (result == JOptionPane.YES_OPTION) {
367:                                                         new SaveAction(gp).actionPerformed(we);
368:                                                         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
369:                                                 } else if (result == JOptionPane.NO_OPTION) {
370:                                                         new ResetAction(gp).actionPerformed(we);
371:                                                         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
372:
373:                                                 }
374:
375:                                         }
376:                                 });
377:                         } catch (final LoadConfigurationGraphException e) {
378:                                 JOptionPane.showMessageDialog(f, e.getMessage(),
379:                                                 NetworkconfiguratorConstants.ERROR_TITEL, JOptionPane.ERROR_MESSAGE);
380:
381:                         }
382:                 }
383:         }
384:
385:         /**
386:          * Mouse Handler for pressing and releasing the mouse.
387:          */
388:         private class MouseHandler extends MouseAdapter {
389:
390:                 @Override
391:                 public void mouseReleased(final MouseEvent e) {
392:                         selecting = false;
393:                         mouseRect.setBounds(0, 0, 0, 0);
394:                         if (e.isPopupTrigger()) {
395:                                 showPopup(e);
396:                         }
397:                         e.getComponent().repaint();
398:                 }
399:
400:                 @Override
401:                 public void mousePressed(final MouseEvent e) {
402:                         mousePt = e.getPoint();
403:                         if (e.isShiftDown()) {
404:                                 Item.selectToggle(items, mousePt);
405:                         } else if (ctrlPressed) {
406:                                 Item.selectMultiple(items, mousePt);
407:                         } else if (e.isControlDown()) {
408:                                 Item.selectOne(items, mousePt);
409:                         } else if (e.isPopupTrigger()) {
410:                                 Item.selectOne(items, mousePt);
411:                                 showPopup(e);
412:                         } else if (Item.selectOne(items, mousePt)) {
413:                                 selecting = false;
414:                         } else {
415:                                 Item.selectNone(items);
416:                                 selecting = true;
417:                         }
418:                         showDetails();
419:                         e.getComponent().repaint();
420:                 }
421:
422:                 @Override
423:                 public void mouseClicked(final MouseEvent e) {
424:                         requestFocusInWindow();
425:                 }
426:
427:                 /**
428:                  * Shows the pop-up menu on the right click.
429:                  *
430:                  * @param e
431:                  * The right click event.
432:                  */
433:                 private void showPopup(final MouseEvent e) {
434:                         control.getPopup(selected).show(e.getComponent(), e.getX(), e.getY());
435:                 }
436:         }
437:
438:         /**
439:          * Key handler for pressing and releasing the ctrl-key.
440:          *
441:          */
442:         private class KeyHandler extends KeyAdapter {
443:                 @Override
444:                 public void keyPressed(final KeyEvent e) {
445:                         ctrlPressed = e.isControlDown();
446:                 }
447:
448:                 @Override
449:                 public void keyReleased(final KeyEvent e) {
450:                         ctrlPressed = e.isControlDown();
451:                 }
452:         }
453:
454:         /**
455:          * Drag and Drop Engine.
456:          */
457:         private class MouseMotionHandler extends MouseMotionAdapter {
458:                 /**
459:                  * Delta of the current position and the previous.
460:                  */
461:                 private final Point delta = new Point();
462:
463:                 @Override
464:                 public void mouseDragged(final MouseEvent e) {
465:                         if (selecting) {
466:                                 mouseRect.setBounds(Math.min(mousePt.x, e.getX()), Math.min(mousePt.y, e.getY()),
467:                                                 Math.abs(mousePt.x - e.getX()), Math.abs(mousePt.y - e.getY()));
468:                                 Item.selectRect(items, mouseRect);
469:                         } else {
470:                                 delta.setLocation(e.getX() - mousePt.x, e.getY() - mousePt.y);
471:                                 Item.updatePosition(items, delta);
472:                                 mousePt = e.getPoint();
473:                         }
474:                         e.getComponent().repaint();
475:                 }
476:         }
477:
478:         /**
479:          * getter of the Toolbar.
480:          *
481:          * @return the toolbar
482:          */
483:         public JToolBar getControlPanel() {
484:                 return control;
485:         }
486:
487:         /**
488:          * gets the radius.
489:          *
490:          * @return the radius
491:          */
492:         public int getRadius() {
493:                 return radius;
494:         }
495:
496:         /**
497:          * method that show the details of the selected icon in the detailview.
498:          */
499:         public void showDetails() {
500:                 Item.getSelectedNodes(items, selected);
501:                 if (selected.size() == 1) {
502:
503:                         activateDetailBar(selected.get(0));
504:                 } else {
505:                         devicesdetailView.setVisible(false);
506:                 }
507:
508:         }
509:
510:         /**
511:          * üblicher Getter für das Attribut getDetailView.
512:          *
513:          * @return liefert getDetailView.
514:          */
515:         public DetailBar getDetailView() {
516:                 return devicesdetailView;
517:         }
518:
519:         /**
520:          * activate the DevicesDetailBar.
521:          *
522:          * @param item
523:          * device that should be displayed in the detailsbar.
524:          */
525:         private void activateDetailBar(final Item item) {
526:                 devicesdetailView.refreshDetails(item);
527:                 devicesdetailView.setVisible(true);
528:         }
529:
530:         /**
531:          * getter for the router configuration.
532:          *
533:          * @return the router configuration
534:          */
535:         public RouterConfiguration getRc() {
536:                 return rc;
537:         }
538:
539:         @Override
540:         public void update(final Observable o, final Object arg) {
541:                 this.repaint();
542:         }
543:
544: }