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.JGoArea;
023: import com.nwoods.jgo.JGoBrush;
024: import com.nwoods.jgo.JGoPen;
025: import com.nwoods.jgo.JGoRectangle;
026: import com.nwoods.jgo.JGoView;
027: import java.awt.Color;
028: import java.awt.GradientPaint;
029: import java.awt.Graphics2D;
030: import java.awt.Rectangle;
031: import java.util.Collection;
032: import java.util.Iterator;
033: import java.util.LinkedHashMap;
034: import java.util.Map;
035: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.gtk.ICanvasFieldNode;
036: import org.netbeans.modules.soa.mapper.common.gtk.ICanvasGroupNode;
037:
038: /**
039: *
040: * @author jsandusky
041: */
042: public class GradientRectangle extends JGoRectangle {
043:
044: private static final JGoPen PEN_BORDER = JGoPen
045: .makeStockPen(new Color(127, 127, 127));
046:
047: // The gradient colors and boundaries are defined as follows:
048: //
049: // .-----------------. COLOR_5, 100% of span (top of node)
050: // | | |
051: // | | COLOR_4, 95% of span
052: // | | |
053: // | title field & | COLOR_3, 70% of span
054: // | input fields | |
055: // | | COLOR_2, 23.6% of span
056: // | | |
057: // | | COLOR_1, 7.3% of span
058: // | | |
059: // |_________________| COLOR_0, start of span (top of return field)
060: // | return field |
061: // |_________________|
062:
063: private static final Color COLOR_0 = Color.WHITE;
064: private static final Color COLOR_1 = new Color(231, 241, 249);
065: private static final Color COLOR_2 = new Color(184, 215, 255);
066: private static final Color COLOR_3 = new Color(255, 255, 255);
067: private static final Color COLOR_4 = new Color(221, 235, 246);
068: private static final Color COLOR_5 = new Color(169, 197, 235);
069:
070: private Map mColorMap = new LinkedHashMap();
071: private ICanvasGroupNode mGroupNode;
072:
073: public GradientRectangle(ICanvasGroupNode groupNode) {
074: super ();
075: mGroupNode = groupNode;
076: setPen(PEN_BORDER);
077: }
078:
079: private void calculateColorMap() {
080: mColorMap.clear();
081: float spanLength = getSpanLength();
082: float midX = getLeft() + getWidth() / 2;
083: int position = getTop() + (int) spanLength;
084: // From the bottom of the methoid to the top, we define a set of
085: // gradients that start from COLOR_0 and span to COLOR_5.
086: // Thus, COLOR_0 is the color at the bottom, and COLOR_5 is at the top.
087: position = addColorPoint(0.073f, position, COLOR_0, COLOR_1,
088: spanLength, midX);
089: position = addColorPoint(0.236f, position, COLOR_1, COLOR_2,
090: spanLength, midX);
091: position = addColorPoint(0.700f, position, COLOR_2, COLOR_3,
092: spanLength, midX);
093: position = addColorPoint(0.950f, position, COLOR_3, COLOR_4,
094: spanLength, midX);
095: position = addColorPoint(1.000f, position, COLOR_4, COLOR_5,
096: spanLength, midX);
097: }
098:
099: private float getSpanLength() {
100: float spanLength = getHeight();
101:
102: if (mGroupNode.isExpanded()) {
103: // If a result node exists, then start the span length from
104: // the top of the result node.
105: if (mGroupNode.getNodes().size() > 0) {
106: ICanvasFieldNode resultNode = null;
107: Collection nodes = mGroupNode.getNodes();
108: for (Iterator iter = nodes.iterator(); iter.hasNext();) {
109: ICanvasFieldNode iterNode = (ICanvasFieldNode) iter
110: .next();
111: if (iterNode.getFieldNode().isOutput()) {
112: resultNode = iterNode;
113: break;
114: }
115: }
116: if (resultNode != null && resultNode instanceof JGoArea) {
117: spanLength -= ((JGoArea) resultNode).getHeight();
118: }
119: }
120: }
121:
122: return spanLength;
123: }
124:
125: private int addColorPoint(float spanFactor, int spanPosition,
126: Color one, Color two, float spanLength, float midX) {
127: // We work from the bottom of the methoid towards the top.
128: // Each new span position that we calculate has a smaller y
129: // coordinate than the previous. The first color is below
130: // the second color.
131: int newSpanPosition = getTop()
132: + Math.round(spanLength - spanLength * spanFactor);
133: Rectangle rect = new Rectangle(getLeft(), newSpanPosition,
134: getWidth(), (int) spanPosition - newSpanPosition);
135: GradientPaint paint = new GradientPaint(midX, newSpanPosition,
136: two, midX, spanPosition, one);
137: mColorMap.put(rect, paint);
138: return newSpanPosition;
139: }
140:
141: public void setBoundingRect(int left, int top, int width, int height) {
142: super .setBoundingRect(left, top, width, height);
143: calculateColorMap();
144: }
145:
146: public void paint(Graphics2D graphics2d, JGoView jgoview) {
147: Rectangle rectangle = getBoundingRect();
148: drawGradientRect(graphics2d, getPen(), getBrush(), rectangle.x,
149: rectangle.y, rectangle.width, rectangle.height);
150: }
151:
152: public void drawGradientRect(Graphics2D graphics2d, JGoPen jgopen,
153: JGoBrush jgobrush, int x, int y, int w, int h) {
154: for (Iterator iter = mColorMap.keySet().iterator(); iter
155: .hasNext();) {
156: Rectangle rect = (Rectangle) iter.next();
157: GradientPaint paint = (GradientPaint) mColorMap.get(rect);
158: graphics2d.setPaint(paint);
159: graphics2d
160: .fillRect(rect.x, rect.y, rect.width, rect.height);
161: }
162:
163: if (jgopen != null) {
164: java.awt.Stroke stroke = jgopen.getStroke();
165: if (stroke != null) {
166: graphics2d.setStroke(stroke);
167: graphics2d.setColor(jgopen.getColor());
168: graphics2d.drawRect(x, y, w, h);
169: }
170: }
171: }
172: }
|