1
28 package demo.view.networkmonitoring;
29
30 import y.view.GenericNodeRealizer;
31 import y.view.ImageNodePainter;
32 import y.view.NodeRealizer;
33
34 import java.awt.Color;
35 import java.awt.Graphics2D;
36 import java.net.URL;
37
38
41 public class NetworkNodePainter implements GenericNodeRealizer.Painter, GenericNodeRealizer.ContainsTest {
42 static final int SIDE_WARNING_SIGN = 20;
43
44 private final ImageNodePainter enabledNetworkNodePainter;
45 private final ImageNodePainter disabledNetworkNodePainter;
46
47 public NetworkNodePainter(URL enabledImage, URL disableImage) {
48 enabledNetworkNodePainter = new ImageNodePainter(enabledImage);
50 disabledNetworkNodePainter = new ImageNodePainter(disableImage);
51 }
52
53 public void paint(NodeRealizer context, Graphics2D graphics) {
54 final NetworkData networkData = NetworkMonitoringFactory.getNetworkData(context);
55 if (networkData.isOK()) {
56 enabledNetworkNodePainter.paint(context, graphics);
57 } else {
58 disabledNetworkNodePainter.paint(context, graphics);
59 }
60
61 if (networkData.isBroken()) {
63 NetworkMonitoringFactory.paintWarningSign(context.getX(),
64 context.getY() + context.getHeight() - SIDE_WARNING_SIGN, SIDE_WARNING_SIGN, SIDE_WARNING_SIGN, graphics);
65 }
66 }
67
68 public void paintSloppy(NodeRealizer context, Graphics2D graphics) {
69 final Color oldColor = graphics.getColor();
70 try {
71 final NetworkData data = NetworkMonitoringFactory.getNetworkData(context);
73 graphics.setColor(NetworkMonitoringFactory.getStatusColor(data));
74 graphics.fill(context.getBoundingBox());
75 } finally {
76 graphics.setColor(oldColor);
77 }
78 }
79
80 public boolean contains(NodeRealizer context, double x, double y) {
81 final NetworkData networkData = NetworkMonitoringFactory.getNetworkData(context);
82 if (networkData.isOK()) {
83 return enabledNetworkNodePainter.contains(context, x, y);
84 } else {
85 return disabledNetworkNodePainter.contains(context, x, y) || hitWarningSign(context, x, y);
88 }
89 }
90
91
98 public void setAlphaImageUsed(boolean useAlphaImage) {
99 enabledNetworkNodePainter.setAlphaImageUsed(useAlphaImage);
100 disabledNetworkNodePainter.setAlphaImageUsed(useAlphaImage);
101 }
102
103
106 public static boolean hitWarningSign(NodeRealizer context, double x, double y) {
107 final NetworkData networkData = NetworkMonitoringFactory.getNetworkData(context);
108 return networkData.isBroken()
109 && x > context.getX() && x < context.getX() + SIDE_WARNING_SIGN
110 && y > context.getY() + context.getHeight() - SIDE_WARNING_SIGN && y < context.getY() + context.getHeight();
111 }
112 }
113