001/* 002 * Copyright (c) 2004-2007 by Michael Connor. All Rights Reserved. 003 * 004 * Redistribution and use in source and binary forms, with or without 005 * modification, 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 FormLayoutBuilder or Michael Connor nor the names of 015 * its contributors may be used to endorse or promote products derived 016 * from this software without 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, 020 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 021 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 027 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.mlc.swing.layout; 031 032import java.beans.IntrospectionException; 033import java.lang.reflect.InvocationTargetException; 034import java.util.ArrayList; 035import java.util.List; 036import java.util.Map; 037 038import javax.swing.JButton; 039 040import com.jgoodies.forms.factories.ButtonBarFactory; 041 042/** 043 * This is the component builder for the JGoodies 044 * ButtonBar. 045 * 046 * @author Michael Connor 047@version $Id$ 048@since Ptolemy II 8.0 049 */ 050public class ButtonBarComponentBuilder implements ComponentBuilder { 051 private static final String left = "left"; 052 private static final String right = "right"; 053 private static final String center = "center"; 054 private static final String justification = "justification"; 055 List<BeanProperty> properties = new ArrayList<BeanProperty>(); 056 057 /** Creates a new instance of ButtonBarComponentBuilder */ 058 public ButtonBarComponentBuilder() throws IntrospectionException { 059 properties.add(new BeanProperty("justification", String.class)); 060 properties.add(new BeanProperty("button1Name", String.class)); 061 properties.add(new BeanProperty("button1Text", String.class)); 062 properties.add(new BeanProperty("button2Name", String.class)); 063 properties.add(new BeanProperty("button2Text", String.class)); 064 properties.add(new BeanProperty("button3Name", String.class)); 065 properties.add(new BeanProperty("button3Text", String.class)); 066 properties.add(new BeanProperty("button4Name", String.class)); 067 properties.add(new BeanProperty("button4Text", String.class)); 068 properties.add(new BeanProperty("button5Name", String.class)); 069 properties.add(new BeanProperty("button5Text", String.class)); 070 } 071 072 @Override 073 public String getDeclaration(String name, Map<String, Object> properties) { 074 StringBuffer declaration = new StringBuffer(); 075 StringBuffer buttonAdds = new StringBuffer( 076 "new javax.swing.JButton[] {"); 077 078 int buttonCount = 0; 079 080 for (int i = 1; i < 6; i++) { 081 String buttonText = (String) properties.get("button" + i + "Text"); 082 String buttonName = (String) properties.get("button" + i + "Name"); 083 if (buttonText != null && buttonText.trim().length() > 0) { 084 buttonCount++; 085 if (buttonName == null) { 086 buttonName = name + "Button" + i; 087 } 088 089 declaration.append("javax.swing.JButton " + buttonName 090 + " = new javax.swing.JButton (\"" + buttonText 091 + "\");\n"); 092 buttonAdds.append(buttonName + ","); 093 } 094 } 095 096 // let's remove the last comma from the buttonAdds 097 if (buttonCount > 1) { 098 buttonAdds.deleteCharAt(buttonAdds.length() - 1); 099 } 100 101 buttonAdds.append("}"); 102 declaration.append("java.awt.Component " + name 103 + " = com.jgoodies.forms.factories.ButtonBarFactory."); 104 String justificationValue = (String) properties.get(justification); 105 106 if (justificationValue == null 107 || justificationValue.trim().length() == 0) { 108 justificationValue = right; 109 } 110 111 if (left.equals(justificationValue)) { 112 declaration.append("buildRightAlignedBar"); 113 } else if (center.equals(justificationValue)) { 114 declaration.append("buildCenteredBar"); 115 } else { 116 declaration.append("buildRightAlignedBar"); 117 } 118 119 declaration.append("(" + buttonAdds.toString() + ");\n"); 120 return declaration.toString(); 121 } 122 123 @Override 124 public String toString() { 125 return "ButtonBar"; 126 } 127 128 @Override 129 public java.awt.Component getInstance( 130 java.util.Map<String, Object> properties) 131 throws InstantiationException, IllegalAccessException, 132 InvocationTargetException { 133 List<JButton> buttons = new ArrayList<JButton>(); 134 135 for (int i = 1; i < 6; i++) { 136 String buttonText = (String) properties.get("button" + i + "Text"); 137 if (buttonText != null && buttonText.trim().length() > 0) { 138 buttons.add(new JButton(buttonText)); 139 } 140 } 141 142 JButton[] buttonArray = new JButton[buttons.size()]; 143 buttonArray = buttons.toArray(buttonArray); 144 145 String justification = (String) properties.get("justification"); 146 if (justification == null || justification.trim().length() == 0) { 147 justification = left; 148 } else if (!justification.equals(left) && !justification.equals(right) 149 && !justification.equals(center)) { 150 throw new InstantiationException( 151 "justification should be either left, right, or center"); 152 } 153 154 if (justification.equals(right)) { 155 return ButtonBarFactory.buildRightAlignedBar(buttonArray); 156 } else if (justification.equals(center)) { 157 return ButtonBarFactory.buildCenteredBar(buttonArray); 158 } else { 159 return ButtonBarFactory.buildLeftAlignedBar(buttonArray); 160 } 161 } 162 163 @Override 164 public boolean isComponentALayoutContainer() { 165 return false; 166 } 167 168 @Override 169 public List<BeanProperty> getProperties() { 170 return properties; 171 } 172 173 @Override 174 public ComponentDef getComponentDef(String name, 175 Map<String, Object> beanProperties) { 176 String decl = getDeclaration("${name}", beanProperties); 177 String add = "${container}.add(${name}, \"${name}\");"; 178 ComponentDef cd = new ComponentDef(name, "", decl, add); 179 return cd; 180 } 181 182}