001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo.util;
021:
022: import com.nwoods.jgo.JGoRectangle;
023: import com.nwoods.jgo.JGoView;
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.Graphics2D;
027: import java.awt.Point;
028: import java.awt.RenderingHints;
029: import org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo.BasicCanvasFieldNode;
030: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.gtk.ICanvasMapperLink;
031:
032: /**
033: *
034: * @author jsandusky
035: */
036: public class DrawPort extends JGoRectangle {
037:
038: public static final Color COLOR_NORMAL = new Color(0, 24, 127); // navy
039: public static final Color COLOR_ACTIVATED = ICanvasMapperLink.DEFAULT_LINK_SELECTED_COLOR;
040: public static final Dimension SIZE_NORMAL = new Dimension(8, 8);
041: public static final Dimension SIZE_HOVER = new Dimension(10, 10);
042:
043: private BasicCanvasFieldNode mFieldNode;
044: private boolean mIsHovering;
045: private boolean mIsActivated;
046: private boolean mIsConnected;
047: private int mSideLength;
048: private Dimension mDrawSize;
049: private Point mDrawLocation = new Point(0, 0);
050:
051: public DrawPort(BasicCanvasFieldNode fieldNode) {
052: mFieldNode = fieldNode;
053: setIsHovering(false);
054: }
055:
056: public BasicCanvasFieldNode getFieldNode() {
057: return mFieldNode;
058: }
059:
060: public void setIsHovering(boolean isHovering) {
061: mIsHovering = isHovering;
062: mDrawSize = isHovering ? SIZE_HOVER : SIZE_NORMAL;
063: mSideLength = mDrawSize.width;
064: }
065:
066: public boolean isHovering() {
067: return mIsHovering;
068: }
069:
070: public void setIsActivated(boolean isActivated) {
071: mIsActivated = isActivated;
072: }
073:
074: public boolean isActivated() {
075: return mIsActivated;
076: }
077:
078: public void setIsConnected(boolean isConnected) {
079: mIsConnected = isConnected;
080: }
081:
082: public boolean isConnected() {
083: return mIsConnected;
084: }
085:
086: public void setDrawLocation(int x, int y) {
087: mDrawLocation = new Point(x, y);
088: }
089:
090: public Dimension getDrawSize() {
091: return mDrawSize;
092: }
093:
094: protected static void setAntiAliasing(Graphics2D graphics2d,
095: boolean isEnable) {
096: graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
097: isEnable ? RenderingHints.VALUE_ANTIALIAS_ON
098: : RenderingHints.VALUE_ANTIALIAS_OFF);
099: }
100:
101: protected static void drawDashedSquare(Graphics2D graphics2d,
102: Point loc, int sideLength, int dash) {
103: int solid = (sideLength - dash) / 2;
104: int end1 = solid - 1;
105: int end2 = sideLength - solid;
106: int lastX = loc.x + (sideLength - 1);
107: int lastY = loc.y + (sideLength - 1);
108:
109: // horizontal lines
110: graphics2d.drawLine(loc.x, loc.y, loc.x + end1, loc.y);
111: graphics2d.drawLine(loc.x + end2, loc.y, lastX, loc.y);
112: graphics2d.drawLine(loc.x, lastY, loc.x + end1, lastY);
113: graphics2d.drawLine(loc.x + end2, lastY, lastX, lastY);
114:
115: // vertical lines
116: graphics2d.drawLine(loc.x, loc.y, loc.x, loc.y + end1);
117: graphics2d.drawLine(loc.x, loc.y + end2, loc.x, lastY);
118: graphics2d.drawLine(lastX, loc.y, lastX, loc.y + end1);
119: graphics2d.drawLine(lastX, loc.y + end2, lastX, lastY);
120: }
121:
122: public void paint(Graphics2D graphics2d, JGoView jgoview) {
123:
124: Point loc = mDrawLocation;
125: graphics2d.setColor(mIsActivated ? COLOR_ACTIVATED
126: : COLOR_NORMAL);
127:
128: if (mIsConnected) {
129: // Connected.
130: setAntiAliasing(graphics2d, false);
131: if (mIsHovering) {
132: graphics2d.fillRect(loc.x + 3, loc.y + 3, 4, 4);
133: drawDashedSquare(graphics2d, loc, mSideLength, 4);
134: } else {
135: graphics2d.fillRect(loc.x + 2, loc.y + 2, 4, 4);
136: drawDashedSquare(graphics2d, loc, mSideLength, 2);
137: }
138: setAntiAliasing(graphics2d, true);
139: } else {
140: // Disconnected.
141: if (mIsHovering) {
142: setAntiAliasing(graphics2d, false);
143: drawDashedSquare(graphics2d, loc, mSideLength, 4);
144: setAntiAliasing(graphics2d, true);
145: } else {
146: // normal disconnected ports are not visible
147: }
148: }
149: }
150: }
|