KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > gtk > AdaptiveMatteBorder


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 package org.netbeans.swing.plaf.gtk;
20
21 import javax.swing.*;
22 import javax.swing.border.Border JavaDoc;
23 import java.awt.*;
24
25 /**
26  * A matte border with a few twists - can do drop shadow; also, for toolbars,
27  * will check the position of the component and return taller insets for the
28  * top row, so it's offset from the menu, but not present a doubled border
29  * for lower rows; same thing for toolbars adjacent to each other.
30  *
31  * @author Tim Boudreau
32  */

33 public class AdaptiveMatteBorder implements Border JavaDoc {
34     private Insets insets;
35     private int shadowDepth;
36     private boolean topLeftInsets;
37     
38     /** Creates a new instance of AdaptiveMatteBorder */
39     public AdaptiveMatteBorder(boolean t, boolean l, boolean b, boolean r, int shadowDepth, boolean topLeftInsets) {
40         insets = new Insets (t ? topLeftInsets ? shadowDepth + 1 : 1 : 0, l ? topLeftInsets ? shadowDepth + 1: 1 : 0, b ? 1 + shadowDepth : shadowDepth, r ? 1 + shadowDepth : shadowDepth);
41         this.shadowDepth = shadowDepth;
42         this.topLeftInsets = topLeftInsets;
43     }
44     
45     public AdaptiveMatteBorder(boolean t, boolean l, boolean b, boolean r, int shadowDepth) {
46         this (t, l, b, r, shadowDepth, false);
47     }
48     
49     private Insets maybeOmitInsets (Insets ins, Component c) {
50         if (shadowDepth <= 0 || !topLeftInsets) {
51             return ins;
52         }
53         Insets result = new Insets(ins.top, ins.left, ins.right, ins.bottom);
54         if (topLeftInsets) {
55             Point p = c.getLocation();
56             if (p.x > 10) {
57                 result.left = 1;
58             }
59             if (p.y > 10) {
60                 result.top = 1;
61             }
62         }
63         return result;
64     }
65     
66     public Insets getBorderInsets(Component c) {
67         return maybeOmitInsets(insets, c);
68     }
69     
70     public boolean isBorderOpaque() {
71         return false;
72     }
73
74     
75     public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
76         Color color = g.getColor();
77         Insets ins = getBorderInsets(c);
78         Point p = c.getLocation();
79         
80         //This will always really come from the theme on GTK
81
g.setColor (UIManager.getColor("controlShadow")); //NOI18N
82
w -= shadowDepth;
83         h -= shadowDepth;
84         if (topLeftInsets) {
85             if (p.y <= 10) {
86                 y += shadowDepth;
87                 h -= shadowDepth;
88             }
89             if (p.x <= 10) {
90                 x += shadowDepth;
91                 w -= shadowDepth;
92             }
93         }
94         if (ins.top > 0) {
95             g.fillRect(x, y, w, 1);
96         }
97         if (ins.left > 0) {
98             g.fillRect(x, y, 1, h);
99         }
100         if (ins.right > 0) {
101             g.fillRect(x + w - 1, y, 1, h);
102         }
103         if (ins.bottom > 0) {
104             g.fillRect(x, y + h - 1, w, 1);
105         }
106         
107         boolean isViewTab = isViewTab(c);
108         
109         if (shadowDepth > 1) {
110             Rectangle clip = g.getClipBounds();
111             boolean clipTouchesRight = ((clip.x + clip.width) >= (x + w));
112             boolean clipTouchesBottom = ((clip.y + clip.height) >= (y + h));
113
114             if (clipTouchesBottom || clipTouchesRight) {
115                 Color ctrl = UIManager.getColor ("control"); //NOI18N
116
Color base = UIManager.getColor("controlShadow");
117             
118                 Color curr;
119                 for (int i = 1; i < shadowDepth; i++) {
120                     curr = colorTowards (base, ctrl, shadowDepth, i+1);
121                     g.setColor (curr);
122                     if (clipTouchesRight && ins.right > 0) {
123                         g.fillRect(x + w - 1 + i, y + (isViewTab ? 0 : i), 1, h);
124                     }
125                     if (clipTouchesBottom && ins.bottom > 0) {
126                         g.fillRect(x + i, y + h - 1 + i, w - 1, 1);
127                     }
128                 }
129             }
130         }
131         g.setColor (color);
132     }
133     
134 // private static int[] xpoints = new int[4];
135
// private static int[] ypoints = new int[4];
136

137     static boolean isViewTab (Component c) {
138         if (c.getParent() instanceof JComponent) {
139             JComponent jc = (JComponent) c.getParent();
140             Object JavaDoc o = jc.getClientProperty("viewType");
141             if (o != null && o instanceof Integer JavaDoc) {
142                 return ((Integer JavaDoc) o).intValue() == 0;
143             }
144         }
145         return false;
146     }
147
148     private static final float[] comps = new float[4];
149     private static final float[] targs = new float[4];
150     
151     static final Color colorTowards (Color base, Color target, float steps, float step) {
152         base.getColorComponents(comps);
153         target.getColorComponents(targs);
154         
155         comps[3] = 1.0f; //No transparency, performance problems
156

157         float factor = (step / steps);
158         
159         for (int i=0; i < 3; i++) {
160             comps[i] = saturate(comps[i] - (factor * (comps[i] - targs[i])));
161         }
162         
163         
164 // comps[3] = 1f - (step / steps);
165
Color result = new Color (comps[0], comps[1], comps[2], comps[3]);
166         return result;
167     }
168     
169     private static final float saturate (float f) {
170         float orig = f;
171         if (f > 1) {
172             f = 1;
173         }
174         if (f < 0) {
175             f = 0;
176         }
177         return f;
178     }
179     
180 }
181
182
Popular Tags