001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.components.border;
031:
032: import java.util.HashMap;
033:
034: import javax.swing.JButton;
035: import javax.swing.JCheckBox;
036: import javax.swing.JComboBox;
037: import javax.swing.JComponent;
038: import javax.swing.JLabel;
039: import javax.swing.JList;
040: import javax.swing.JRadioButton;
041: import javax.swing.JScrollPane;
042: import javax.swing.JSpinner;
043: import javax.swing.JTable;
044: import javax.swing.JTextArea;
045: import javax.swing.JTextField;
046: import javax.swing.JTree;
047: import javax.swing.LookAndFeel;
048: import javax.swing.UIManager;
049: import javax.swing.border.Border;
050:
051: /**
052: * This class maintains a list of default borders for swing components. The
053: * runtime uses the BorderManager to obtain default borders for components in
054: * cases where the user has specified a default border in addition to other
055: * borders in the border properties designer.
056: *
057: * If the look and feel changes, this class will automatically update the list
058: * based on the new look and feel.
059: *
060: * @author Jeff Tassin
061: */
062: public class BorderManager {
063: /**
064: * A map of default borders for component classes. m_default_borders<Class,Border>
065: */
066: private static HashMap m_default_borders = new HashMap();
067:
068: /**
069: * The current look and feel
070: */
071: private static LookAndFeel m_look_and_feel;
072:
073: /**
074: * @return the default border for the given component.
075: */
076: public static Border getDefaultBorder(JComponent comp) {
077: synchronized (m_default_borders) {
078: if (comp != null) {
079: if (m_default_borders.size() == 0
080: || isLookAndFeelChanged()) {
081: resetDefaultBorders();
082: }
083:
084: Border default_border = (Border) m_default_borders
085: .get(comp.getClass());
086: if (default_border == null) {
087: Class comp_class = comp.getClass();
088: try {
089: JComponent new_comp = (JComponent) comp_class
090: .newInstance();
091: default_border = new_comp.getBorder();
092: m_default_borders.put(comp_class,
093: default_border);
094: } catch (Exception e) {
095: e.printStackTrace();
096: m_default_borders.put(comp_class, null);
097: }
098: }
099: return default_border;
100: }
101: }
102: return null;
103: }
104:
105: private static Border getDefaultBorder(String borderResource) {
106: Border b = UIManager.getBorder(borderResource);
107: return b;
108: }
109:
110: private static boolean isLookAndFeelChanged() {
111: LookAndFeel lf = UIManager.getLookAndFeel();
112: if (m_look_and_feel != lf) {
113: m_look_and_feel = lf;
114: return true;
115: }
116: return false;
117: }
118:
119: /**
120: * Resets the default borders.
121: */
122: private static void resetDefaultBorders() {
123: m_default_borders.clear();
124: m_default_borders.put(JButton.class,
125: getDefaultBorder("Button.border"));
126: m_default_borders.put(JComboBox.class,
127: getDefaultBorder("ComboBox.border"));
128: m_default_borders.put(JLabel.class, new JLabel().getBorder());
129: m_default_borders.put(JList.class,
130: getDefaultBorder("List.border"));
131: m_default_borders.put(JSpinner.class,
132: getDefaultBorder("Spinner.border"));
133: m_default_borders.put(JTable.class, new JTable().getBorder());
134: m_default_borders.put(JTextArea.class,
135: getDefaultBorder("TextArea.border"));
136: m_default_borders.put(JTextField.class,
137: getDefaultBorder("TextField.border"));
138: m_default_borders.put(JTree.class, new JTree().getBorder());
139: m_default_borders.put(JScrollPane.class, new JScrollPane()
140: .getBorder());
141:
142: m_default_borders.put(JCheckBox.class,
143: new javax.swing.border.EmptyBorder(1, 0, 1, 2));
144: m_default_borders.put(JRadioButton.class,
145: new javax.swing.border.EmptyBorder(1, 0, 1, 2));
146:
147: /**
148: * JCheckBox and JRadioButton have overriden borders because their
149: * default borders have a horizontal offset that produces a rather
150: * unpleasant looking GUI.
151: */
152: }
153: }
|