KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > swing > CLabel


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.swing;
15
16 import java.awt.Color JavaDoc;
17
18 import javax.swing.Icon JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20
21 import org.compiere.plaf.CompierePLAF;
22
23 /**
24  * Label with Mnemonics interpretation
25  *
26  * @author Jorg Janke
27  * @version $Id: CLabel.java,v 1.7 2003/09/27 11:08:52 jjanke Exp $
28  */

29 public class CLabel extends JLabel JavaDoc
30 {
31     public static int DEFAULT_ALIGNMENT = JLabel.TRAILING;
32
33     /**
34      * Creates a <code>Label</code> instance with the specified
35      * text, image, and horizontal alignment.
36      * The label is centered vertically in its display area.
37      * The text is on the trailing edge of the image.
38      *
39      * @param text The text to be displayed by the label.
40      * @param icon The image to be displayed by the label.
41      * @param horizontalAlignment One of the following constants
42      * defined in <code>SwingConstants</code>:
43      * <code>LEFT</code>,
44      * <code>CENTER</code>,
45      * <code>RIGHT</code>,
46      * <code>LEADING</code> or
47      * <code>TRAILING</code>.
48      */

49     public CLabel (String JavaDoc text, Icon JavaDoc icon, int horizontalAlignment)
50     {
51         super (text, icon, horizontalAlignment);
52         init();
53     }
54
55     /**
56      * Creates a <code>Label</code> instance with the specified
57      * text and horizontal alignment.
58      * The label is centered vertically in its display area.
59      *
60      * @param text The text to be displayed by the label.
61      * @param horizontalAlignment One of the following constants
62      * defined in <code>SwingConstants</code>:
63      * <code>LEFT</code>,
64      * <code>CENTER</code>,
65      * <code>RIGHT</code>,
66      * <code>LEADING</code> or
67      * <code>TRAILING</code>.
68      */

69     public CLabel (String JavaDoc text, int horizontalAlignment)
70     {
71         super(text, horizontalAlignment);
72         init();
73     }
74
75     /**
76      * Creates a <code>Label</code> instance with the specified text.
77      * The label is aligned against the leading edge of its display area,
78      * and centered vertically.
79      *
80      * @param text The text to be displayed by the label.
81      */

82     public CLabel (String JavaDoc text)
83     {
84         super(text, DEFAULT_ALIGNMENT);
85         init();
86     }
87
88     /**
89      * Creates a <code>Label</code> instance with the specified
90      * image and horizontal alignment.
91      * The label is centered vertically in its display area.
92      *
93      * @param image The image to be displayed by the label.
94      * @param horizontalAlignment One of the following constants
95      * defined in <code>SwingConstants</code>:
96      * <code>LEFT</code>,
97      * <code>CENTER</code>,
98      * <code>RIGHT</code>,
99      * <code>LEADING</code> or
100      * <code>TRAILING</code>.
101      */

102     public CLabel (Icon JavaDoc image, int horizontalAlignment)
103     {
104         super (image, horizontalAlignment);
105         init();
106     }
107
108     /**
109      * Creates a <code>Label</code> instance with the specified image.
110      * The label is centered vertically and horizontally
111      * in its display area.
112      *
113      * @param image The image to be displayed by the label.
114      */

115     public CLabel (Icon JavaDoc image)
116     {
117         super (image, DEFAULT_ALIGNMENT);
118         init();
119     }
120
121     /**
122      * Creates a <code>JLabel</code> instance with
123      * no image and with an empty string for the title.
124      * The label is centered vertically
125      * in its display area.
126      * The label's contents, once set, will be displayed on the leading edge
127      * of the label's display area.
128      */

129     public CLabel ()
130     {
131         super("", DEFAULT_ALIGNMENT);
132         init();
133     }
134
135     /**
136      * Creates a <code>Label</code> instance with the specified text.
137      * The label is aligned against the leading edge of its display area,
138      * and centered vertically.
139      *
140      * @param label The text to be displayed by the label.
141      * @param toolTip The optional Tooltip text
142      */

143     public CLabel (String JavaDoc label, String JavaDoc toolTip)
144     {
145         super (label, DEFAULT_ALIGNMENT);
146         if (toolTip != null && toolTip.length() > 0)
147             super.setToolTipText(toolTip);
148         init();
149     } // CLabel
150

151     /**
152      * Common init
153      */

154     private void init()
155     {
156         setFocusable (false);
157         setOpaque(false);
158         //
159
setForeground(CompierePLAF.getTextColor_Label());
160         setFont(CompierePLAF.getFont_Label());
161     } // init
162

163
164     /**
165      * Set Background
166      * @param bg background
167      */

168     public void setBackground (Color JavaDoc bg)
169     {
170         if (bg.equals(getBackground()))
171             return;
172         super.setBackground(bg);
173     } // setBackground
174

175     /*************************************************************************/
176
177     /**
178      * Set label text - if it includes &, the next character is the Mnemonic
179      * @param mnemonicLabel Label containing Mnemonic
180      */

181     public void setText (String JavaDoc mnemonicLabel)
182     {
183         super.setText (createMnemonic (mnemonicLabel));
184     } // setText
185

186     /**
187      * Set label text directly (w/o mnemonics)
188      * @param label Label
189      */

190     public void setTextDirect (String JavaDoc label)
191     {
192         super.setText (label);
193     } // setTextDirect
194

195     /**
196      * Create Mnemonics of text containing "&".
197      * Based on MS notation of &Help => H is Mnemonics
198      * @param text test with Mnemonics
199      * @return text w/o &
200      * @see #setLabelFor
201      */

202     private String JavaDoc createMnemonic(String JavaDoc text)
203     {
204         if (text == null)
205             return text;
206         int pos = text.indexOf("&");
207         if (pos != -1) // We have a nemonic
208
{
209             char ch = text.charAt(pos+1);
210             if (ch != ' ') // &_ - is the & character
211
{
212                 setDisplayedMnemonic(ch);
213                 return text.substring(0, pos) + text.substring(pos+1);
214             }
215         }
216         return text;
217     } // createMnemonic
218

219     /**
220      * Set ReadWrite
221      * @param rw enabled
222      */

223     public void setReadWrite (boolean rw)
224     {
225         this.setEnabled(rw);
226     } // setReadWrite
227

228 } // CLabel
229
Popular Tags