1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.mlc.swing.layout;
31
32 import com.jgoodies.forms.factories.ButtonBarFactory;
33 import java.beans.IntrospectionException;
34 import java.lang.reflect.InvocationTargetException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38 import javax.swing.JButton;
39
40 /***
41 * This is the component builder for the JGoodies
42 * ButtonBar.
43 *
44 * @author Michael Connor
45 */
46 public class ButtonBarComponentBuilder implements ComponentBuilder
47 {
48 private static final String left = "left";
49 private static final String right = "right";
50 private static final String center = "center";
51 private static final String justification = "justification";
52 List <BeanProperty> properties = new ArrayList <BeanProperty> ();
53
54 /*** Creates a new instance of ButtonBarComponentBuilder */
55 public ButtonBarComponentBuilder()
56 throws IntrospectionException
57 {
58 properties.add ( new BeanProperty ("justification", String.class));
59 properties.add ( new BeanProperty ("button1Name", String.class));
60 properties.add ( new BeanProperty ("button1Text", String.class));
61 properties.add ( new BeanProperty ("button2Name", String.class));
62 properties.add ( new BeanProperty ("button2Text", String.class));
63 properties.add ( new BeanProperty ("button3Name", String.class));
64 properties.add ( new BeanProperty ("button3Text", String.class));
65 properties.add ( new BeanProperty ("button4Name", String.class));
66 properties.add ( new BeanProperty ("button4Text", String.class));
67 properties.add ( new BeanProperty ("button5Name", String.class));
68 properties.add ( new BeanProperty ("button5Text", String.class));
69 }
70
71 public String getDeclaration(String name, Map <String, Object> properties)
72 {
73 StringBuffer declaration = new StringBuffer();
74 StringBuffer buttonAdds = new StringBuffer ("new javax.swing.JButton[] {");
75
76 int buttonCount = 0;
77
78 for ( int i = 1; i < 6; i++ )
79 {
80 String buttonText = (String) properties.get ("button" + i + "Text");
81 String buttonName = (String) properties.get ("button" + i + "Name");
82 if ( buttonText != null && buttonText.trim ().length() > 0 )
83 {
84 buttonCount++;
85 if ( buttonName == null )
86 buttonName = name + "Button" + i;
87
88 declaration.append ("javax.swing.JButton " + buttonName + " = new javax.swing.JButton (\"" + buttonText + "\");\n");
89 buttonAdds.append (buttonName + ",");
90 }
91 }
92
93
94 if ( buttonCount > 1 )
95 buttonAdds.deleteCharAt(buttonAdds.length() - 1);
96
97 buttonAdds.append ("}");
98 declaration.append ("java.awt.Component " + name + " = com.jgoodies.forms.factories.ButtonBarFactory.");
99 String justificationValue = (String) properties.get (justification);
100
101 if ( justificationValue == null || justificationValue.trim().length() == 0 )
102 justificationValue = right;
103
104 if ( left.equals (justificationValue))
105 declaration.append ("buildRightAlignedBar");
106 else if ( center.equals (justificationValue) )
107 declaration.append ("buildCenteredBar");
108 else
109 declaration.append ("buildRightAlignedBar");
110
111 declaration.append ("(" + buttonAdds.toString() + ");\n");
112 return declaration.toString();
113 }
114
115 public String toString()
116 {
117 return "ButtonBar";
118 }
119
120 public java.awt.Component getInstance(java.util.Map <String, Object> properties)
121 throws InstantiationException, IllegalAccessException, InvocationTargetException
122 {
123 List <JButton> buttons = new ArrayList <JButton> ();
124
125 for ( int i = 1; i < 6; i++ )
126 {
127 String buttonText = (String) properties.get ("button" + i + "Text");
128 if ( buttonText != null && buttonText.trim ().length() > 0 )
129 buttons.add (new JButton (buttonText));
130 }
131
132 JButton[] buttonArray = new JButton[buttons.size()];
133 buttonArray = buttons.toArray (buttonArray);
134
135 String justification = (String) properties.get ("justification");
136 if ( justification == null || justification.trim().length() == 0 )
137 justification = left;
138 else if ( (! justification.equals (left)) &&
139 (! justification.equals (right)) &&
140 (! justification.equals (center)))
141 throw new InstantiationException ("justification should be either left, right, or center");
142
143 if ( justification.equals (right) )
144 {
145 return ButtonBarFactory.buildRightAlignedBar(buttonArray);
146 } else if ( justification.equals (center) )
147 return ButtonBarFactory.buildCenteredBar(buttonArray);
148 else
149 return ButtonBarFactory.buildLeftAlignedBar(buttonArray);
150 }
151
152 public boolean isComponentALayoutContainer ()
153 {
154 return false;
155 }
156
157 public List <BeanProperty> getProperties()
158 {
159 return properties;
160 }
161
162 public ComponentDef getComponentDef(String name, Map<String, Object> beanProperties)
163 {
164 String decl = getDeclaration("${name}", beanProperties);
165 String add = "${container}.add(${name}, \"${name}\");";
166 ComponentDef cd = new ComponentDef(name,"",decl,add);
167 return cd;
168 }
169
170 }