001    package market.swing;
002    
003    import java.awt.Color;
004    
005    import javax.swing.BorderFactory;
006    import javax.swing.JTextField;
007    import javax.swing.border.Border;
008    
009    /**
010     * Factory class for borders, JTextFields und {@link JTFCheckable JTFCheckables}.
011     */
012    public class ComponentFactory {
013    
014        public static final int LEFT = JTextField.LEFT;
015        public static final int CENTER = JTextField.CENTER;
016        public static final int RIGHT = JTextField.RIGHT;
017    
018    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
019    // borders
020    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
021    
022        /**
023         * Creates a line border out that leaves some space around the object to which the border is set.
024         * This is achieved with the help of an empty border between the object and the line border.
025         *
026         * @return the border.
027         */
028        public static Border createInsetBorder() {
029            return createInsetBorder(BorderFactory.createLineBorder(Color.gray), 5, 10, 10, 10);
030        }
031    
032        /**
033         * Creates a title border out that leaves some space around the object to which the border is set.
034         * This is achieved with the help of an empty border between the object and the title border.
035         *
036         * @param label the caption of the title border.
037         * @return the border.
038         */
039        public static Border createInsetBorder(String label) {
040            return createInsetBorder(BorderFactory.createTitledBorder(label), 5, 10, 10, 10);
041        }
042    
043        /**
044         * Creates a compound border out that leaves some space around the object to which the border is set.
045         * This is achieved with the help of an empty border between the object and the title border.
046         *
047         * @param border the outer border.
048         * @param top the top inset.
049         * @param left the left inset.
050         * @param bottom the bottom inset.
051         * @param right the right inset.
052         * @return the border.
053         */
054        public static Border createInsetBorder(Border border, int top, int left, int bottom, int right) {
055            return BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(top, left, bottom,
056                    right));
057        }
058    
059    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
060    // JTextFields
061    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
062    
063        /**
064         * Creates a JTextField.
065         * @param text the text to be set.
066         * @param size the text field's size
067         * @param editable defines if text field is editable or not
068         * @param alignment the text's alignment in the text field (left, center, right)
069         * @param border defines if the text field should have a border.
070         * @return the created JTextField.
071         */
072        public static JTextField createTextField(String text, int size, boolean editable, int alignment,
073                boolean border) {
074            return createTextField(text, size, editable, alignment, Color.black, border);
075        }
076    
077        /**
078         * Creates a JTextField.
079         * @param value the number to be set as text.
080         * @param size the text field's size
081         * @param editable defines if text field is editable or not
082         * @param alignment the text's alignment in the text field (left, center, right)
083         * @param border defines if the text field should have a border.
084         * @return the created JTextField.
085         */
086        public static JTextField createTextField(double value, int size, boolean editable, int alignment,
087                boolean border) {
088            return createTextField(Double.toString(value), size, editable, alignment, Color.black, border);
089        }
090    
091        /**
092         * Creates a JTextField.
093         * @param text the text to be set.
094         * @param size the text field's size
095         * @param editable defines if text field is editable or not
096         * @param alignment the text's alignment in the text field (left, center, right)
097         * @param color the color of the text field's text.
098         * @param border defines if the text field should have a border.
099         * @return the created JTextField.
100         */
101        public static JTextField createTextField(String text, int size, boolean editable, int alignment,
102                Color color, boolean border) {
103            JTextField jtf = new JTextField(text, size);
104            jtf.setEditable(editable);
105            jtf.setHorizontalAlignment(alignment);
106            jtf.setForeground(color);
107            if (!border) {
108                jtf.setBorder(null);
109            }
110            return jtf;
111        }
112    }