| NetworkNodePainter.java |
1 /****************************************************************************
2 * This demo file is part of yFiles for Java 2.14.
3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4 * 72070 Tuebingen, Germany. All rights reserved.
5 *
6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7 * of demo files in source code or binary form, with or without
8 * modification, is not permitted.
9 *
10 * Owners of a valid software license for a yFiles for Java version that this
11 * demo is shipped with are allowed to use the demo source code as basis
12 * for their own yFiles for Java powered applications. Use of such programs is
13 * governed by the rights and conditions as set out in the yFiles for Java
14 * license agreement.
15 *
16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 ***************************************************************************/
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 /**
39 * A {@link GenericNodeRealizer.Painter painter} that paints a network node either in enabled or disabled state.
40 */
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 // image node painter delegates for the enabled and the disabled state
49 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 // paint state icon above the image if the network node is broken
62 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 // paint rectangle in current status color
72 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 // also include hit test for warning sign in case alpha image is used and the warning sign lies in the transparent
86 // area of the image
87 return disabledNetworkNodePainter.contains(context, x, y) || hitWarningSign(context, x, y);
88 }
89 }
90
91 /**
92 * Determines whether the {@link #contains(NodeRealizer,double,double)} method should use the alpha transparency of
93 * the image to determine whether this realizer "contains" points. This influences hit testing and edge intersection
94 * calculation.
95 *
96 * @param useAlphaImage whether to use the alpha transparency of the image
97 */
98 public void setAlphaImageUsed(boolean useAlphaImage) {
99 enabledNetworkNodePainter.setAlphaImageUsed(useAlphaImage);
100 disabledNetworkNodePainter.setAlphaImageUsed(useAlphaImage);
101 }
102
103 /**
104 * Checks if the warning sign of the realizer contains the given coordinates.
105 */
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