001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.awt.Component;
033import java.awt.Dimension;
034
035import javax.swing.Box;
036import javax.swing.JComponent;
037import javax.swing.JLabel;
038import javax.swing.JTextArea;
039import javax.swing.JTextField;
040
041
042/**
043 * A utilities class for creating Swing UI widgets
044 * 
045 * @author Matthew Brooke
046 * @since 27 February 2006
047 */
048public class WidgetFactory {
049
050        private WidgetFactory() {
051        }
052
053        /**
054         * create a non-opaque JLabel containing the given displayText, with the
055         * minimumSize and preferredSize set to the PrefMinDims Dimension
056         * 
057         * @param displayText
058         *            String
059         * @param PrefMinDims
060         *            Dimension
061         * @return JLabel
062         */
063        protected static JLabel makeJLabel(String displayText, Dimension PrefMinDims) {
064
065                JLabel lbl = new JLabel(displayText != null ? displayText : "");
066                lbl.setOpaque(false);
067                setPrefMinSizes(lbl, PrefMinDims);
068                return lbl;
069        }
070
071        /**
072         * create a JTextField containing the given initialValue, with the
073         * minimumSize and preferredSize set to the PrefMinDims Dimension
074         * 
075         * @param initialValue
076         *            String
077         * @param PrefMinDims
078         *            Dimension
079         * @return JLabel
080         */
081        protected static JTextField makeJTextField(String initialValue,
082                        Dimension PrefMinDims) {
083                initialValue = (initialValue != null ? initialValue : "");
084                JTextField tf = new JTextField();
085                setPrefMinSizes(tf, PrefMinDims);
086                tf.setText(initialValue);
087                return tf;
088        }
089
090        /**
091         * create a non-opaque JTextField containing the given initialValue, with
092         * the minimumSize and preferredSize set to the PrefMinDims Dimension
093         * 
094         * @param initialValue
095         *            String
096         * @param PrefMinDims
097         *            Dimension
098         * @return JLabel
099         */
100        protected static JTextArea makeJTextArea(String initialValue) {
101                initialValue = (initialValue != null ? initialValue : "");
102                JTextArea ta = new JTextArea(initialValue);
103                ta.setEditable(true);
104                ta.setLineWrap(true);
105                ta.setWrapStyleWord(true);
106
107                return ta;
108        }
109
110        /**
111         * create a non-opaque JLabel containing the given displayText, with the
112         * minimumSize and preferredSize set to the PrefMinDims Dimension
113         * 
114         * @param displayText
115         *            String
116         * @param PrefMinDims
117         *            Dimension
118         * @return JLabel
119         */
120        protected static Component getDefaultSpacer() {
121
122                return Box.createRigidArea(defaultSpacerDims);
123        }
124
125        /**
126         * sets the minimum and preferred sizes to the passed Dimension
127         * 
128         * @param component
129         *            JComponent
130         * @param dims
131         *            Dimension
132         */
133        protected static void setPrefMinSizes(JComponent component, Dimension dims) {
134                component.setSize(dims);
135                component.setMinimumSize(dims);
136                component.setPreferredSize(dims);
137        }
138
139        /**
140         * sets the minimum, maximum and preferred sizes to the passed Dimension
141         * 
142         * @param component
143         *            JComponent
144         * @param dims
145         *            Dimension
146         */
147        protected static void setPrefMinMaxSizes(JComponent component,
148                        Dimension dims) {
149                setPrefMinSizes(component, dims);
150                component.setMaximumSize(dims);
151        }
152
153        private static final Dimension defaultSpacerDims = StaticGUIResources
154                        .getDimension("general.defaultSpacer.width",
155                                        "general.defaultSpacer.height", 5, 5);
156}