KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > fakepeer > FakePeerUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.form.fakepeer;
22
23 import java.awt.*;
24
25 /**
26  *
27  * @author Tomas Pavek
28  */

29
30 class FakePeerUtils
31 {
32     static void drawButton(Graphics g,int x,int y,int w,int h) {
33         g.fillRect(x,y,w,h);
34
35         // button has a raised border (Windows)
36
g.setColor(SystemColor.controlHighlight);
37         g.drawLine(x,y+h-2,x,y);
38         g.drawLine(x,y,x+w-2,y);
39         g.setColor(SystemColor.controlDkShadow);
40         g.drawLine(x,y+h-1,x+w-1,y+h-1);
41         g.drawLine(x+w-1,y+h-1,x+w-1,y);
42         if (w >=4 && h >= 4) {
43             g.setColor(SystemColor.controlLtHighlight);
44             g.drawLine(x+1,y+h-3,x+1,y+1);
45             g.drawLine(x+1,y+1,x+w-3,y+1);
46             g.setColor(SystemColor.controlShadow);
47             g.drawLine(x+1,y+h-2,x+w-2,y+h-2);
48             g.drawLine(x+w-2,y+h-2,x+w-2,y+1);
49         }
50     }
51
52     static void drawArrowButton(Graphics g,
53                                 int x, int y, int w, int h,
54                                 int type, boolean enabled) {
55         g.setColor(SystemColor.control);
56         drawButton(g,x,y,w,h);
57
58         int minWH = w < h ? w : h,
59             size; // size of the arrow - from 0 to 4
60
if (minWH >= ABUT_SIZE) size = 4;
61         else if (minWH >= 12) size = 3;
62         else if (minWH >= 8) size = 2;
63         else if (minWH >= 6) size = 1;
64         else size = 0;
65
66         if (enabled)
67             g.setColor(SystemColor.controlText);
68         else {
69             g.setColor(SystemColor.controlLtHighlight);
70             x++;
71             y++;
72         }
73
74         // draw the arrow
75
while (true) {
76             if (type == 1) { // left <
77
int ax = x+w/2-size/2,
78                     ay = y+h/2-1;
79                 for (int i=0; i < size; i++)
80                     g.drawLine(ax+i,ay-i,ax+i,ay+i);
81             } else if (type == 2) { // right >
82
int ax = x+w/2+size/2,
83                     ay = y+h/2-1;
84                 for (int i=0; i < size; i++)
85                     g.drawLine(ax-i,ay-i,ax-i,ay+i);
86             } else if (type == 3) { // upper ^
87
int ax = x+w/2-1,
88                     ay = y+h/2-size/2;
89                 for (int i=0; i < size; i++)
90                     g.drawLine(ax-i,ay+i,ax+i,ay+i);
91             } else if (type == 4) { // lower v
92
int ax = x+w/2-1,
93                     ay = y+h/2+size/2;
94                 for (int i=0; i < size; i++)
95                     g.drawLine(ax-i,ay-i,ax+i,ay-i);
96             }
97
98             if (enabled) break;
99             else {
100                 enabled = true;
101                 g.setColor(SystemColor.controlShadow);
102                 x--;
103                 y--;
104             }
105         }
106     }
107
108     static void drawChoiceButton(Graphics g,int x,int y,int w,int h,boolean enabled) {
109         // Windows-like style - a button with an arrow
110
drawArrowButton(g,x,y,w,h,4,enabled);
111     }
112
113     static void drawScrollThumb(Graphics g,int x,int y,int w,int h) {
114         // Windows-like style - thumb looks just like a button
115
drawButton(g,x,y,w,h);
116     }
117
118     static void drawLoweredBox(Graphics g,int x,int y,int w,int h) {
119         g.fillRect(x,y,w,h);
120
121         g.setColor(SystemColor.controlShadow);
122         g.drawLine(x,y+h-2,x,y);
123         g.drawLine(x,y,x+w-2,y);
124         g.setColor(SystemColor.controlLtHighlight);
125         g.drawLine(x,y+h-1,x+w-1,y+h-1);
126         g.drawLine(x+w-1,y+h-1,x+w-1,y);
127         if (w >=4 && h >= 4) {
128             g.setColor(SystemColor.controlDkShadow);
129             g.drawLine(x+1,y+h-3,x+1,y+1);
130             g.drawLine(x+1,y+1,x+w-3,y+1);
131             g.setColor(SystemColor.controlHighlight);
132             g.drawLine(x+1,y+h-2,x+w-2,y+h-2);
133             g.drawLine(x+w-2,y+h-2,x+w-2,y+1);
134         }
135     }
136
137     static void drawScrollbar(Graphics g,
138                               int x,int y,int w,int h,
139                               int orientation,
140                               boolean enabled,
141                               boolean border,int relValue,int amount,int range) {
142         g.fillRect(x, y, w, h); // color (for background) is expected to be set outside
143

144         if (border) { // border (Windows style)
145
g.setColor(SystemColor.controlShadow);
146             g.drawRect(x,y,w-1,h-1);
147             g.setColor(SystemColor.control);
148             g.drawRect(x+1,y+1,w-3,h-3);
149         } else
150             g.setColor(SystemColor.control);
151
152         if (orientation == Scrollbar.HORIZONTAL) {
153             int butW;
154             if (w >= 2*SCROLL_W) {
155                 butW = SCROLL_W;
156                 int wFT = w - 2*butW; // width that remains for the "thumb"
157
if (wFT >= 4 && enabled) { // paint the thumb
158
int thumbW = range > 0 ? wFT * amount / range : wFT;
159                     if (thumbW < 6) thumbW = 6;
160                     if (thumbW > wFT) thumbW = wFT;
161                     range -= relValue;
162                     int thumbX = (range > 0 ? relValue * (wFT - thumbW) / range : 0) + x + butW;
163
164                     drawScrollThumb(g,thumbX,y,thumbW,h);
165                 }
166             } else butW = w/2;
167             if (butW >= 4) { // paint "arrow" buttons
168
drawArrowButton(g,x,y,butW,h,1,enabled); // the left one <
169
drawArrowButton(g,x+w-butW,y,butW,h,2,enabled); // the right one >
170
}
171         } else { // == Scrollbar.VERTICAL
172
int butH;
173             if (h >= 2*SCROLL_H) {
174                 butH = SCROLL_H;
175                 int hFT = h - 2*butH; // height that remains for the "thumb"
176
if (hFT >= 4 && enabled) { // paint the thumb
177
int thumbH = range > 0 ? hFT * amount / range : hFT;
178                     if (thumbH < 6) thumbH = 6;
179                     if (thumbH > hFT) thumbH = hFT;
180                     range -= relValue;
181                     int thumbY = (range > 0 ? relValue * (hFT - thumbH) / range : 0) + y + butH;
182                         
183                     drawScrollThumb(g,x,thumbY,w,thumbH);
184                 }
185             } else butH = h/2;
186             if (butH >= 4) { // paint "arrow" buttons
187
drawArrowButton(g,x,y,w,butH,3,enabled); // the upper one ^
188
drawArrowButton(g,x,y+h-butH,w,butH,4,enabled); // the lower one v
189
}
190         }
191     }
192
193     private static int ABUT_SIZE = 16; // standard arrow button's width & height
194
static final int SCROLL_W = 16, SCROLL_H = 16;
195 }
196
Popular Tags