001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
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
022package com.izforge.izpack.gui;
023
024import javax.swing.Icon;
025import javax.swing.JLabel;
026import 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 */
036public class LabelFactory implements SwingConstants
037{
038
039    private static boolean useLabelIcons = true;
040
041    /**
042     * Returns whether the factory creates labels with icons or without icons.
043     * 
044     * @return whether the factory creates labels with icons or without icons
045     */
046    public static boolean isUseLabelIcons()
047    {
048        return useLabelIcons;
049    }
050
051    /**
052     * Sets the use icon state.
053     * 
054     * @param b flag for the icon state
055     */
056    public static void setUseLabelIcons(boolean b)
057    {
058        useLabelIcons = b;
059    }
060
061    /**
062     * Returns a new JLabel with the horizontal alignment CENTER. If isUseLabelIcons is true, the
063     * given image will be set to the label, else an empty label returns.
064     * 
065     * @param image the image to be used as label icon
066     * @return new JLabel with the given parameters
067     */
068    public static JLabel create(Icon image)
069    {
070        return (create(image, CENTER));
071
072    }
073
074    /**
075     * Returns a new JLabel with the given horizontal alignment. If isUseLabelIcons is true, the
076     * given image will be set to the label, else an empty label returns.
077     * 
078     * @param image the image to be used as label icon
079     * @param horizontalAlignment horizontal alignment of the label
080     * @return new JLabel with the given parameters
081     */
082    public static JLabel create(Icon image, int horizontalAlignment)
083    {
084        return (create(null, image, horizontalAlignment));
085
086    }
087
088    /**
089     * Returns a new JLabel with the horizontal alignment CENTER.
090     * 
091     * @param text the text to be set
092     * @return new JLabel with the given parameters
093     */
094    public static JLabel create(String text)
095    {
096        return (create(text, CENTER));
097
098    }
099
100    /**
101     * Returns a new JLabel with the given horizontal alignment.
102     * 
103     * @param text the text to be set
104     * @param horizontalAlignment horizontal alignment of the label
105     * @return new JLabel with the given parameters
106     */
107    public static JLabel create(String text, int horizontalAlignment)
108    {
109        return (create(text, null, horizontalAlignment));
110
111    }
112
113    /**
114     * Returns a new JLabel with the given horizontal alignment. If isUseLabelIcons is true, the
115     * given image will be set to the label. The given text will be set allways to the label. It is
116     * allowed, that image and/or text are null.
117     * 
118     * @param text the text to be set
119     * @param image the image to be used as label icon
120     * @param horizontalAlignment horizontal alignment of the label
121     * @return new JLabel with the given parameters
122     */
123    public static JLabel create(String text, Icon image, int horizontalAlignment)
124    {
125        JLabel retval = null;
126        if (image != null && isUseLabelIcons())
127        {
128            retval = new JLabel(image);
129        }
130        else
131        {
132            retval = new JLabel();
133        }
134        if (text != null) retval.setText(text);
135        retval.setHorizontalAlignment(horizontalAlignment);
136        return (retval);
137    }
138}