View Javadoc

1   /*
2    * Copyright (c) 2004-2005 by Michael Connor. All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without 
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    *  o Redistributions of source code must retain the above copyright notice, 
8    *    this list of conditions and the following disclaimer. 
9    *     
10   *  o Redistributions in binary form must reproduce the above copyright notice, 
11   *    this list of conditions and the following disclaimer in the documentation 
12   *    and/or other materials provided with the distribution. 
13   *     
14   *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of 
15   *    its contributors may be used to endorse or promote products derived 
16   *    from this software without specific prior written permission. 
17   *     
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
19   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
20   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
21   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
22   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
23   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
24   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
25   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
26   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
27   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
28   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
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          // let's remove the last comma from the buttonAdds
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 }