001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.extension.openide.util;
042:
043: import java.awt.*;
044:
045: /**
046: * Extends Trace class to add paint debugging fuctionality. This is a separate
047: * class so that a headless module that does not import AWT can use the Trace
048: * functionality without linking in AWT.
049: *
050: * @author Joe Nuxoll
051: */
052: public class TraceUI extends Trace {
053:
054: /**
055: * Paints a rectangle within the specified bounds - with hashmarks. As subsequent
056: * (often quickly repetitive) calls are made to this method, the hash direction
057: * alternates from top-right/bottom-left to top-left/bottom-right and cycles colors
058: * and hashmark spacing so that it is easy to distinguish between the different calls,
059: *
060: * @param g Graphics object to paint into
061: * @param x Rectangle.X
062: * @param y Rectangle.y
063: * @param width Rectangle.width
064: * @param height Rectangle.height
065: */
066: public static void debugRect(Graphics g, int x, int y, int width,
067: int height) {
068: if (g == null)
069: return;
070: Rectangle clip = g.getClipBounds();
071: if (clip == null)
072: return;
073: g.setClip(x, y, width, height);
074: Color c = g.getColor();
075: if (colorWheel == null)
076: colorWheel = new TraceUI.ColorWheel();
077: g.setColor(colorWheel.next());
078: g.drawRect(x, y, width - 1, height - 1);
079: g.drawRect(x + 1, y + 1, width - 3, height - 3);
080: // alternate hash directions
081: if (debugRectHashLeft) {
082: for (int i = 0; i < x + width + height; i += debugRectInc)
083: g.drawLine(x, y + i, x + i, y);
084: } else {
085: for (int i = 0; i < x + width + height; i += debugRectInc)
086: g.drawLine(x + width, y + i, x + width - i, y);
087: }
088: debugRectHashLeft = !debugRectHashLeft;
089: if (debugRectInc > 15)
090: debugRectInc = 5;
091: else
092: debugRectInc += 2;
093: g.setColor(c);
094: if (clip != null)
095: g.setClip(clip.x, clip.y, clip.width, clip.height);
096: }
097:
098: private static TraceUI.ColorWheel colorWheel;
099: private static int debugRectInc = 10;
100: private static boolean debugRectHashLeft = true;
101:
102: //------------------------------------------------------------------------------
103: // ColorWheel class - for cycling colors on debugRect calls
104: //------------------------------------------------------------------------------
105: private static class ColorWheel {
106: public static final int RED_TO_YELLOW = 1; // 0xFF0000 --> 0xFFFF00 (adding green)
107: public static final int YELLOW_TO_GREEN = 2; // 0xFFFF00 --> 0x00FF00 (subtracting red)
108: public static final int GREEN_TO_CYAN = 3; // 0x00FF00 --> 0x00FFFF (adding blue)
109: public static final int CYAN_TO_BLUE = 4; // 0x00FFFF --> 0x0000FF (subtracting green)
110: public static final int BLUE_TO_MAGENTA = 5; // 0x0000FF --> 0xFF00FF (adding red)
111: public static final int MAGENTA_TO_RED = 6; // 0xFF00FF --> 0xFF0000 (subtracting blue)
112:
113: public ColorWheel() {
114: }
115:
116: public ColorWheel(Color startColor) {
117: current = startColor;
118: }
119:
120: public ColorWheel(Color startColor, int startCycle) {
121: current = startColor;
122: cycle = startCycle;
123: }
124:
125: public ColorWheel(Color startColor, int startCycle,
126: int increment) {
127: current = startColor;
128: cycle = startCycle;
129: this .increment = increment;
130: }
131:
132: private int increment = 50;
133: private int cycle = RED_TO_YELLOW;
134: private Color current = Color.red;
135:
136: public int getIncrement() {
137: return increment;
138: }
139:
140: public void setIncrement(int newIncrement) {
141: if (newIncrement >= 1 && newIncrement <= 255)
142: increment = newIncrement;
143: else
144: throw new IllegalArgumentException(
145: "ColorWheel increment must be in range (1-255)");
146: }
147:
148: public int getCycle() {
149: return cycle;
150: }
151:
152: public void setCycle(int newCycle) {
153: if (newCycle >= RED_TO_YELLOW && newCycle <= MAGENTA_TO_RED)
154: cycle = newCycle;
155: else
156: throw new IllegalArgumentException(
157: "Invalid ColorWheel cycle: " + newCycle);
158: }
159:
160: public Color getColor() {
161: return current;
162: }
163:
164: public void setColor(Color newColor) {
165: if (newColor != null)
166: current = newColor;
167: }
168:
169: public Color next() {
170: return next(current);
171: }
172:
173: public Color next(Color color) {
174: int r = color.getRed();
175: int g = color.getGreen();
176: int b = color.getBlue();
177: switch (cycle) {
178: case RED_TO_YELLOW:
179: g += increment;
180: if (g > 255) {
181: g = 255;
182: cycle = YELLOW_TO_GREEN;
183: }
184: break;
185: case YELLOW_TO_GREEN:
186: r -= increment;
187: if (r < 0) {
188: r = 0;
189: cycle = GREEN_TO_CYAN;
190: }
191: break;
192: case GREEN_TO_CYAN:
193: b += increment;
194: if (b > 255) {
195: b = 255;
196: cycle = CYAN_TO_BLUE;
197: }
198: break;
199: case CYAN_TO_BLUE:
200: g -= increment;
201: if (g < 0) {
202: g = 0;
203: cycle = BLUE_TO_MAGENTA;
204: }
205: break;
206: case BLUE_TO_MAGENTA:
207: r += increment;
208: if (r > 255) {
209: r = 255;
210: cycle = MAGENTA_TO_RED;
211: }
212: break;
213: case MAGENTA_TO_RED:
214: b -= increment;
215: if (b < 0) {
216: b = 0;
217: cycle = RED_TO_YELLOW;
218: }
219: break;
220: default:
221: cycle = RED_TO_YELLOW;
222: break;
223: }
224: current = new Color(r, g, b);
225: return current;
226: }
227: }
228: }
|