xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/awt/CLCheckbox.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 /**
30  * CLCheckbox.java
31  *
32  * Copyright 1995-1996 Active Software Inc.
33  *
34  * @version @(#)CLCheckbox.java 1.9 96/12/11
35  * @author  Tilman Sporkert
36  */
37 
38 
39 package sunsoft.jws.visual.rt.awt;
40 
41 import java.awt.*;
42 /* BEGIN JSTYLED */
43 
44 /**
45          * A CLCheckbox is a special Object that the ColumnList
46 	 * draws like a flat
47          * checkbox. This has two advantages over putting
48 	 * java.awt.Checkboxes into
49          * the ColumnList:
50          * - under Motif, the java.awt.Checkbox has some
51 	 * extra space around it,
52          *   making the rows very high.
53          * - highlighting a row with a java.awt.Checkbox in it looks ugly
54          * - significant performance improvements (measured 10x
55 	 * in one application)
56          * - the Checkbox is always drawn flat, not in 3D look.
57 	 * Flat is the correct
58          *   look in a scrollable area with a white background.
59          *
60          * Notes on usage:
61          * - if the state of a CLCheckbox gets changed,
62 	 * needsRepaint() should be
63          *   called on the ColumnList to make the change visible
64          * - If a CLCheckbox is in a column list, clicking
65 	 * on the box changes the
66          *   state and sends out an ACTION_EVENT. The row
67 	 * will not get selected.
68          *   Unlike AWT Checkboxes, clicking on the label does not change the
69          *   status. It just selects the row in the
70 	 * ColumnList (which triggers a
71          *   LIST_EVENT).
72          *
73          * @author  Tilman Sporkert
74          */
75             /* END JSTYLED */
76 public class CLCheckbox extends CLComponent
77 {
78     private boolean state = false;
79 
CLCheckbox(String text, boolean state)80     public CLCheckbox(String text, boolean state) {
81         super(text);
82         this.state = state;
83     }
84 
getState()85     public boolean getState() {
86         return state;
87     }
88 
setState(boolean state)89     public void setState(boolean state) {
90         this.state = state;
91     }
92 
paint(Graphics g, int x, int y, int colWidth, int rowHeight, int ascent, int alignment)93     public void paint(Graphics g, int x, int y, int colWidth,
94 		      int rowHeight, int ascent, int alignment)
95     {
96         if (canvas == null)
97             return;
98 
99         if (colWidth >= (rowHeight+4)) {
100             g.drawRect(x + 5, y + 2, rowHeight - 6, rowHeight - 6);
101             if (state) {
102                 g.drawLine(x + 8, y + rowHeight / 2 - 1,
103 			   x + rowHeight / 2 + 2, y + rowHeight - 7);
104                 g.drawLine(x + rowHeight / 2 + 2, y + rowHeight - 7,
105 			   x + rowHeight - 3, y + 4);
106                 g.drawLine(x + 7, y + rowHeight / 2 - 1,
107 			   x + rowHeight / 2 + 2, y + rowHeight - 6);
108                 g.drawLine(x + rowHeight / 2 + 2, y + rowHeight - 6,
109 			   x + rowHeight - 3, y + 5);
110             }
111         }
112         if (text != null) {
113             canvas.drawString(g, text, x + rowHeight + 4, y + ascent,
114 			      colWidth - rowHeight - 8, alignment);
115         }
116     }
117 
textX()118     public int textX() {
119         if (canvas == null)
120             return -1;
121         else
122             return canvas.rowHeight + 4;
123     }
124 
textY()125     public int textY() {
126         if (canvas == null)
127             return -1;
128         else
129             return canvas.rowAscent;
130     }
131 
size()132     public Dimension size() {
133         if (canvas == null)
134             return null;
135 
136         FontMetrics metrics = canvas.getFontMetrics();
137         Dimension size = new Dimension(0, 0);
138 
139         if (text != null)
140             size.width += metrics.stringWidth(text) + 8;
141         size.width += canvas.rowHeight;
142         size.height = canvas.rowHeight;
143 
144         return size;
145     }
146 
mouseDown(Event evt)147     public boolean mouseDown(Event evt) {
148         if (canvas == null)
149             return false;
150 
151         if (evt.x <= canvas.rowHeight) {
152             state = !state;
153             canvas.postEvent(new Event(this, Event.ACTION_EVENT,
154 				       new Boolean(state)));
155             canvas.repaint();
156             return true;
157         }
158 
159         return false;
160     }
161 }
162