View Javadoc

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  //				DeviceCell dev1 = new DeviceCell(new Device(22));
31  //				insertDevice(dev1);
32  //				DeviceCell dev2 = new DeviceCell("dev2");
33  //				insertDevice(dev2);
34  //				DeviceCell dev3 = new DeviceCell("dev3");
35  //				insertDevice(dev3);
36  //				DeviceCell dev4 = new DeviceCell("dev4");
37  //				insertDevice(dev4);	
38  //				DeviceCell dev5 = new DeviceCell("dev5");
39  //				insertDevice(dev5);
40  //				DeviceCell dev6 = new DeviceCell("dev6");
41  //				insertDevice(dev6);
42  //				DeviceCell dev7 = new DeviceCell("dev7");
43  //				insertDevice(dev7);
44  //				DeviceCell dev8 = new DeviceCell("dev8");
45  //				insertDevice(dev8);
46  //				
47  //				ConnectionEdge connection1 = new ConnectionEdge("", dev1, dev2);
48  //				insertConnection(connection1);
49  //				ConnectionEdge connection2 = new ConnectionEdge("", dev1, dev3);
50  //				insertConnection(connection2);
51  //				ConnectionEdge connection3 = new ConnectionEdge("", dev4, dev5);
52  //				insertConnection(connection3);
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 }