1 package com.bluecove.emu.gui.graph;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Observable;
6 import java.util.Observer;
7
8 import org.jgraph.graph.AttributeMap;
9 import org.jgraph.graph.GraphLayoutCache;
10
11 import com.bluecove.emu.gui.model.DatumNotification;
12 import com.bluecove.emu.gui.model.Model;
13 import com.bluecove.emu.gui.model.Device;
14
15 public class GraphView extends GraphLayoutCache implements Observer {
16
17 private static final long serialVersionUID = 1L;
18
19 private Map<Device, DeviceCell> devices = new HashMap<Device, DeviceCell>();
20
21 public GraphView() {
22 super();
23 Model.instance().addObserver(this);
24 setFactory(new DeviceCellViewFactory());
25 }
26
27 public void addStaff() {
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 }
55
56
57 public void insertDevice(DeviceCell device) {
58 device.beforeInsert();
59 HashMap<DeviceCell, AttributeMap> at = new HashMap<DeviceCell, AttributeMap>();
60 at.put(device, device.getAttributes());
61 insert(new Object[] { device }, at, null,
62 null, null);
63 devices.put(device.getDevice(), device);
64 }
65
66 public void removeDevice(DeviceCell device) {
67 device.afterRemove();
68 remove(new Object[] { device });
69 devices.remove(device.getDevice());
70 }
71
72 public void insertConnection(ConnectionEdge connection) {
73 HashMap<ConnectionEdge, AttributeMap> at = new HashMap<ConnectionEdge, AttributeMap>();
74 at.put(connection, connection.getAttributes());
75 insert(new Object[] { connection }, at, connection.getConnectionSet(),
76 null, null);
77 }
78
79 public void removeConnection(ConnectionEdge connection) {
80 remove(new Object[] { connection });
81
82 }
83
84 public void update(Observable o, Object arg) {
85 System.out.println("Observable-"+o);
86 System.out.println("Object-"+arg);
87 if (DatumNotification.class.isAssignableFrom(arg.getClass())) {
88 DatumNotification notif = (DatumNotification)arg;
89 switch (notif.getType()) {
90 case REMOVED:
91 if (Device.class.isAssignableFrom(notif.getDatum().getClass())) {
92 removeDevice(devices.get((Device)notif.getDatum()));
93 }
94 break;
95 case ADDED:
96 if (Device.class.isAssignableFrom(notif.getDatum().getClass())) {
97 insertDevice(new DeviceCell((Device)notif.getDatum()));
98 }
99 break;
100 default:
101 break;
102 }
103 }
104
105 }
106
107 }