001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2004 Klaus Bartz
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.gui;
023:
024: import javax.swing.Icon;
025: import javax.swing.JLabel;
026: import javax.swing.SwingConstants;
027:
028: /**
029: * <p>
030: * A label factory which can handle modified look like to present icons or present it not.
031: * </p>
032: *
033: * @author Klaus Bartz
034: *
035: */
036: public class LabelFactory implements SwingConstants {
037:
038: private static boolean useLabelIcons = true;
039:
040: /**
041: * Returns whether the factory creates labels with icons or without icons.
042: *
043: * @return whether the factory creates labels with icons or without icons
044: */
045: public static boolean isUseLabelIcons() {
046: return useLabelIcons;
047: }
048:
049: /**
050: * Sets the use icon state.
051: *
052: * @param b flag for the icon state
053: */
054: public static void setUseLabelIcons(boolean b) {
055: useLabelIcons = b;
056: }
057:
058: /**
059: * Returns a new JLabel with the horizontal alignment CENTER. If isUseLabelIcons is true, the
060: * given image will be set to the label, else an empty label returns.
061: *
062: * @param image the image to be used as label icon
063: * @return new JLabel with the given parameters
064: */
065: public static JLabel create(Icon image) {
066: return (create(image, CENTER));
067:
068: }
069:
070: /**
071: * Returns a new JLabel with the given horizontal alignment. If isUseLabelIcons is true, the
072: * given image will be set to the label, else an empty label returns.
073: *
074: * @param image the image to be used as label icon
075: * @param horizontalAlignment horizontal alignment of the label
076: * @return new JLabel with the given parameters
077: */
078: public static JLabel create(Icon image, int horizontalAlignment) {
079: return (create(null, image, horizontalAlignment));
080:
081: }
082:
083: /**
084: * Returns a new JLabel with the horizontal alignment CENTER.
085: *
086: * @param text the text to be set
087: * @return new JLabel with the given parameters
088: */
089: public static JLabel create(String text) {
090: return (create(text, CENTER));
091:
092: }
093:
094: /**
095: * Returns a new JLabel or FullLineLabel with the horizontal alignment CENTER.
096: *
097: * @param text the text to be set
098: * @param isFullLine determines whether a FullLineLabel or a JLabel should be created
099: * @return new JLabel or FullLineLabel with the given parameters
100: */
101: public static JLabel create(String text, boolean isFullLine) {
102: return (create(text, CENTER, isFullLine));
103:
104: }
105:
106: /**
107: * Returns a new JLabel with the given horizontal alignment.
108: *
109: * @param text the text to be set
110: * @param horizontalAlignment horizontal alignment of the label
111: * @return new JLabel with the given parameters
112: */
113: public static JLabel create(String text, int horizontalAlignment) {
114: return (create(text, null, horizontalAlignment));
115:
116: }
117:
118: /**
119: * Returns a new JLabel or FullLineLabel with the given horizontal alignment.
120: *
121: * @param text the text to be set
122: * @param horizontalAlignment horizontal alignment of the label
123: * @param isFullLine determines whether a FullLineLabel or a JLabel should be created
124: * @return new JLabel or FullLineLabel with the given parameters
125: */
126: public static JLabel create(String text, int horizontalAlignment,
127: boolean isFullLine) {
128: return (create(text, null, horizontalAlignment, isFullLine));
129:
130: }
131:
132: /**
133: * Returns a new JLabel with the given horizontal alignment. If isUseLabelIcons is true, the
134: * given image will be set to the label. The given text will be set allways to the label. It is
135: * allowed, that image and/or text are null.
136: *
137: * @param text the text to be set
138: * @param image the image to be used as label icon
139: * @param horizontalAlignment horizontal alignment of the label
140: * @return new JLabel with the given parameters
141: */
142: public static JLabel create(String text, Icon image,
143: int horizontalAlignment) {
144: return (create(text, image, horizontalAlignment, false));
145: }
146:
147: /**
148: * Returns a new JLabel or FullLineLabel with the given horizontal alignment. If isUseLabelIcons
149: * is true, the given image will be set to the label. The given text will be set allways to the
150: * label. It is allowed, that image and/or text are null.
151: *
152: * @param text the text to be set
153: * @param image the image to be used as label icon
154: * @param horizontalAlignment horizontal alignment of the label
155: * @param isFullLine determines whether a FullLineLabel or a JLabel should be created
156: * @return new JLabel or FullLineLabel with the given parameters
157: */
158: public static JLabel create(String text, Icon image,
159: int horizontalAlignment, boolean isFullLine) {
160: JLabel retval = null;
161: if (image != null && isUseLabelIcons()) {
162: if (isFullLine)
163: retval = new FullLineLabel(image);
164: else
165: retval = new JLabel(image);
166: } else {
167: if (isFullLine)
168: retval = new FullLineLabel();
169: else
170: retval = new JLabel();
171: }
172: if (text != null)
173: retval.setText(text);
174: retval.setHorizontalAlignment(horizontalAlignment);
175: return (retval);
176: }
177:
178: /**
179: * This class is only needed to signal a different layout handling. There is no additonal
180: * functionality related to a JLabel. Only the needed constructors are implemented.
181: * A FullLineLabel gets from the IzPanelLayout as default a constraints for a full line.
182: * Therefore the width of this label do not determine the width of a column as a JLable
183: * it do.
184: *
185: * @author Klaus Bartz
186: *
187: */
188: public static class FullLineLabel extends JLabel {
189:
190: /**
191: * Required (serializable)
192: */
193: private static final long serialVersionUID = 2918265795390777147L;
194:
195: /**
196: * Creates a <code>JLabel</code> instance with the specified image.
197: * The label is centered vertically and horizontally
198: * in its display area.
199: *
200: * @param image The image to be displayed by the label.
201: */
202: public FullLineLabel(Icon image) {
203: super (image);
204: }
205:
206: /**
207: * Default constructor.
208: */
209: public FullLineLabel() {
210: super();
211: }
212: }
213:
214: }
|