KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > gui > LabelFactory


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Klaus Bartz
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.gui;
23
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.SwingConstants JavaDoc;
27
28 /**
29  * <p>
30  * A label factory which can handle modified look like to present icons or present it not.
31  * </p>
32  *
33  * @author Klaus Bartz
34  *
35  */

36 public class LabelFactory implements SwingConstants JavaDoc
37 {
38
39     private static boolean useLabelIcons = true;
40
41     /**
42      * Returns whether the factory creates labels with icons or without icons.
43      *
44      * @return whether the factory creates labels with icons or without icons
45      */

46     public static boolean isUseLabelIcons()
47     {
48         return useLabelIcons;
49     }
50
51     /**
52      * Sets the use icon state.
53      *
54      * @param b flag for the icon state
55      */

56     public static void setUseLabelIcons(boolean b)
57     {
58         useLabelIcons = b;
59     }
60
61     /**
62      * Returns a new JLabel with the horizontal alignment CENTER. If isUseLabelIcons is true, the
63      * given image will be set to the label, else an empty label returns.
64      *
65      * @param image the image to be used as label icon
66      * @return new JLabel with the given parameters
67      */

68     public static JLabel JavaDoc create(Icon JavaDoc image)
69     {
70         return (create(image, CENTER));
71
72     }
73
74     /**
75      * Returns a new JLabel with the given horizontal alignment. If isUseLabelIcons is true, the
76      * given image will be set to the label, else an empty label returns.
77      *
78      * @param image the image to be used as label icon
79      * @param horizontalAlignment horizontal alignment of the label
80      * @return new JLabel with the given parameters
81      */

82     public static JLabel JavaDoc create(Icon JavaDoc image, int horizontalAlignment)
83     {
84         return (create(null, image, horizontalAlignment));
85
86     }
87
88     /**
89      * Returns a new JLabel with the horizontal alignment CENTER.
90      *
91      * @param text the text to be set
92      * @return new JLabel with the given parameters
93      */

94     public static JLabel JavaDoc create(String JavaDoc text)
95     {
96         return (create(text, CENTER));
97
98     }
99
100     /**
101      * Returns a new JLabel or FullLineLabel with the horizontal alignment CENTER.
102      *
103      * @param text the text to be set
104      * @param isFullLine determines whether a FullLineLabel or a JLabel should be created
105      * @return new JLabel or FullLineLabel with the given parameters
106      */

107     public static JLabel JavaDoc create(String JavaDoc text, boolean isFullLine)
108     {
109         return (create(text, CENTER, isFullLine));
110
111     }
112
113     /**
114      * Returns a new JLabel with the given horizontal alignment.
115      *
116      * @param text the text to be set
117      * @param horizontalAlignment horizontal alignment of the label
118      * @return new JLabel with the given parameters
119      */

120     public static JLabel JavaDoc create(String JavaDoc text, int horizontalAlignment)
121     {
122         return (create(text, null, horizontalAlignment));
123
124     }
125
126     /**
127      * Returns a new JLabel or FullLineLabel with the given horizontal alignment.
128      *
129      * @param text the text to be set
130      * @param horizontalAlignment horizontal alignment of the label
131      * @param isFullLine determines whether a FullLineLabel or a JLabel should be created
132      * @return new JLabel or FullLineLabel with the given parameters
133      */

134     public static JLabel JavaDoc create(String JavaDoc text, int horizontalAlignment, boolean isFullLine)
135     {
136         return (create(text, null, horizontalAlignment, isFullLine));
137
138     }
139
140     /**
141      * Returns a new JLabel with the given horizontal alignment. If isUseLabelIcons is true, the
142      * given image will be set to the label. The given text will be set allways to the label. It is
143      * allowed, that image and/or text are null.
144      *
145      * @param text the text to be set
146      * @param image the image to be used as label icon
147      * @param horizontalAlignment horizontal alignment of the label
148      * @return new JLabel with the given parameters
149      */

150     public static JLabel JavaDoc create(String JavaDoc text, Icon JavaDoc image, int horizontalAlignment)
151     {
152         return( create(text, image, horizontalAlignment, false));
153     }
154     
155     /**
156      * Returns a new JLabel or FullLineLabel with the given horizontal alignment. If isUseLabelIcons
157      * is true, the given image will be set to the label. The given text will be set allways to the
158      * label. It is allowed, that image and/or text are null.
159      *
160      * @param text the text to be set
161      * @param image the image to be used as label icon
162      * @param horizontalAlignment horizontal alignment of the label
163      * @param isFullLine determines whether a FullLineLabel or a JLabel should be created
164      * @return new JLabel or FullLineLabel with the given parameters
165      */

166     public static JLabel JavaDoc create(String JavaDoc text, Icon JavaDoc image, int horizontalAlignment, boolean isFullLine)
167     {
168         JLabel JavaDoc retval = null;
169         if (image != null && isUseLabelIcons())
170         {
171             if (isFullLine)
172                 retval = new FullLineLabel(image);
173             else
174                 retval = new JLabel JavaDoc(image);
175         }
176         else
177         {
178             if (isFullLine)
179                 retval = new FullLineLabel();
180             else
181                 retval = new JLabel JavaDoc();
182         }
183         if (text != null) retval.setText(text);
184         retval.setHorizontalAlignment(horizontalAlignment);
185         return (retval);
186     }
187
188     /**
189      * This class is only needed to signal a different layout handling. There is no additonal
190      * functionality related to a JLabel. Only the needed constructors are implemented.
191      * A FullLineLabel gets from the IzPanelLayout as default a constraints for a full line.
192      * Therefore the width of this label do not determine the width of a column as a JLable
193      * it do.
194      *
195      * @author Klaus Bartz
196      *
197      */

198     public static class FullLineLabel extends JLabel JavaDoc
199     {
200
201         /**
202          * Creates a <code>JLabel</code> instance with the specified image.
203          * The label is centered vertically and horizontally
204          * in its display area.
205          *
206          * @param image The image to be displayed by the label.
207          */

208         public FullLineLabel(Icon JavaDoc image)
209         {
210             super(image);
211         }
212
213         /**
214          * Default constructor.
215          */

216         public FullLineLabel()
217         {
218             super();
219         }
220     }
221
222 }
223
Popular Tags