KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > Separator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.awt;
21
22 import java.awt.Canvas JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.SystemColor JavaDoc;
28
29 /**
30  * <p>
31  * Simple component to draw an etched separator line.
32  * </p>
33  *
34  * @author $Author: james $
35  */

36
37 public class Separator extends Canvas JavaDoc {
38
39     /**
40      * Horizontal
41      */

42     public final static int HORIZONTAL = 0;
43
44     /**
45      * Vertical
46      */

47     public final static int VERTICAL = 1;
48
49     // Private instance variables
50
private int orientation;
51     private Color JavaDoc background;
52     private Dimension JavaDoc preferredSize;
53
54     /**
55      * <p>
56      * Construct a new Separator with a given orientation. Can be one of :-
57      * </p>
58      *
59      * <ul>
60      * <li>{@link Seperator.HORIZONTAL}</li>
61      * <li>{@link Seperator.VERTICAL}</li>
62      * </ul>
63      *
64      * @param orientation
65      * orientation
66      */

67     public Separator(int orientation) {
68         super();
69         setOrientation(orientation);
70     }
71
72     public void setBackground(Color JavaDoc background) {
73         super.setForeground(background);
74         this.background = background;
75     }
76
77     /**
78      * <p>
79      * Set the orientation of the separator. Can be one of :-
80      * </p>
81      *
82      * <ul>
83      * <li>{@link Seperator.HORIZONTAL}</li>
84      * <li>{@link Seperator.VERTICAL}</li>
85      * </ul>
86      *
87      * @param orientation
88      * orientation
89      */

90     public void setOrientation(int orientation) {
91         this.orientation = orientation;
92         repaint();
93     }
94
95     public void paint(Graphics JavaDoc g) {
96         Dimension JavaDoc d = getSize();
97         
98         // First try basing color on background color of this component
99
Color JavaDoc fg = background;
100         
101         // No background, traverse parents until a background color is found
102
Component JavaDoc co = getParent();
103         while( fg == null && co != null) {
104             fg = co.getBackground();
105             co = co.getParent();
106         }
107         
108         // Use system color
109
Color JavaDoc l1 = null;
110         Color JavaDoc l2 = null;
111         if(fg == null) {
112             l1 = SystemColor.controlHighlight;
113             l2 = SystemColor.controlShadow;
114         }
115         else {
116             float[] hsbvals = new float[3];
117             Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), hsbvals);
118             l1 = Color.getHSBColor(hsbvals[0], hsbvals[1], hsbvals[2] * 0.9f);
119             l2 = Color.getHSBColor(hsbvals[0], hsbvals[1], hsbvals[2] * 1.1f);
120         }
121         
122         g.setColor(l1);
123         switch (orientation) {
124         case HORIZONTAL:
125             int c = ( d.height - 1 ) / 2;
126             g.drawLine(0, c, d.width, c);
127             g.setColor(l2);
128             g.drawLine(0, c - 1, d.width, c - 1);
129             break;
130         case VERTICAL:
131             int m = ( d.width - 1 ) / 2;
132             g.drawLine(m, 0, m, d.height);
133             g.setColor(l2);
134             g.drawLine(m - 1, 0, m - 1, d.height - 1);
135             break;
136         }
137     }
138
139     public Dimension JavaDoc getPreferredSize() {
140         if(preferredSize != null) {
141             return preferredSize;
142         }
143         else {
144             switch (orientation) {
145             case HORIZONTAL:
146                 return new Dimension JavaDoc(0, 2);
147             default:
148                 return new Dimension JavaDoc(2, 0);
149             }
150         }
151     }
152     
153     public void setPreferredSize(Dimension JavaDoc preferredSize) {
154         this.preferredSize = preferredSize;
155     }
156 }
Popular Tags