View Javadoc

1   package com.bluecove.emu.gui.graph;
2   
3   import java.awt.Rectangle;
4   import java.awt.geom.Rectangle2D;
5   import java.io.IOException;
6   import java.util.ArrayList;
7   
8   import javax.imageio.ImageIO;
9   import javax.swing.ImageIcon;
10  import javax.swing.SwingConstants;
11  
12  import org.jgraph.graph.AttributeMap;
13  import org.jgraph.graph.DefaultGraphCell;
14  import org.jgraph.graph.GraphConstants;
15  
16  import com.bluecove.emu.gui.BluecoveEmulatorUI;
17  import com.bluecove.emu.gui.model.Device;
18  
19  public class DeviceCell extends DefaultGraphCell {
20  
21  	private static final long serialVersionUID = 1L;
22  
23  	private Device device;
24  	
25  	private static ArrayList<Spot> spots = new ArrayList<Spot>();
26  	
27  	private static double positionRadians[] = {0, Math.PI, Math.PI/2, Math.PI*3/2, Math.PI/4, Math.PI*5/4, Math.PI*3/4, Math.PI*7/4};
28  	
29  	public DeviceCell(Device device)  {
30  		super(" " + device.getId());
31  		this.device = device;
32  		attributes = new AttributeMap();
33  		addPort(null, "JGraph/Center");
34  		GraphConstants.setVerticalTextPosition (attributes, SwingConstants.BOTTOM);
35  		
36  		try {
37  			ImageIcon icon = new ImageIcon(ImageIO.read(BluecoveEmulatorUI.class
38  					.getResource("/images/phone.png")));
39  		    GraphConstants.setIcon(attributes, icon);
40  		} catch (IOException e) {
41  			throw new RuntimeException(e);
42  		}
43  		GraphConstants.setOpaque(attributes, false);
44  		GraphConstants.setBendable(attributes,false);
45  		GraphConstants.setEditable(attributes,false);
46  		GraphConstants.setSizeable(attributes,false); 
47  		GraphConstants.setAutoSize(attributes, true);
48  		
49  		DeviceCellViewFactory.setViewClass(attributes, DeviceCellView.class.getCanonicalName());
50  	}
51  	
52  	public void beforeInsert() {
53  		GraphConstants.setBounds(attributes, allocateSpot(this).getBounds());
54  	}
55  
56  	public void afterRemove() {
57  		deallocateSpot(this);
58  	}
59  
60  	private static synchronized Spot allocateSpot(DeviceCell device) {
61  		for (int i = 0; i < spots.size(); i++) {
62  			Spot spot = spots.get(i);
63  			if(spot.getDevice() == null) {
64  				spot.setDevice(device);
65  				return spot;
66  			}
67  		}
68  		
69  		Spot spot = new Spot(calculateSpotBounds(spots.size()) ,device);
70  		spots.add(spot);
71  		return spot;
72  	}
73  	
74  	private static Rectangle calculateSpotBounds(int index) {
75  		int x = 0;
76  		int y = 0;
77  		if (index < 8) {
78  			x = (int)(GraphPane.PANE_WIDTH/2 - Math.cos(positionRadians[index])*(GraphPane.PANE_WIDTH/2-10));
79  			y = (int)(GraphPane.PANE_HEIGHT/2 - Math.sin(positionRadians[index])*(GraphPane.PANE_HEIGHT/2-10));
80  		} else {
81  			throw new Error("Max 8 devices is currently supported.");
82  		}
83      	return new Rectangle(x, y ,DeviceCellView.IMAGE_WIDTH, DeviceCellView.IMAGE_HEIGHT); 
84      } 
85  	
86  	private static synchronized void deallocateSpot(DeviceCell device) {
87  		for (int i = 0; i < spots.size(); i++) {
88  			Spot spot = spots.get(i);
89  			if(spot.getDevice().equals(device)) {
90  				spot.setDevice(null);
91  				return;
92  			}
93  		}
94  
95  	}
96  	
97  	private static class Spot {
98  		
99  		Rectangle2D bounds;
100 		DeviceCell device;
101 		
102 		public Spot(Rectangle2D bounds, DeviceCell device) {
103 			super();
104 			this.bounds = bounds;
105 			this.device = device;
106 		}
107 
108 		public DeviceCell getDevice() {
109 			return device;
110 		}
111 
112 		public void setDevice(DeviceCell device) {
113 			this.device = device;
114 		}
115 
116 		public Rectangle2D getBounds() {
117 			return bounds;
118 		}
119 		
120 	}
121 
122 	public Device getDevice() {
123 		return device;
124 	}
125 }