1 56 57 package org.objectstyle.cayenne.modeler.util; 58 59 import java.awt.BorderLayout ; 60 import java.awt.Component ; 61 import java.awt.FlowLayout ; 62 63 import javax.swing.BorderFactory ; 64 import javax.swing.JButton ; 65 import javax.swing.JComponent ; 66 import javax.swing.JPanel ; 67 import javax.swing.JScrollPane ; 68 import javax.swing.JTable ; 69 import javax.swing.ListSelectionModel ; 70 71 72 import com.jgoodies.forms.builder.DefaultFormBuilder; 73 import com.jgoodies.forms.layout.FormLayout; 74 75 81 82 public class PanelFactory { 84 85 88 public static JPanel createButtonPanel(JButton [] buttons) { 89 JPanel panel = new JPanel (); 90 panel.setBorder(BorderFactory.createEmptyBorder(3, 20, 3, 7)); 91 panel.setLayout(new FlowLayout (FlowLayout.RIGHT)); 92 93 for (int i = 0; i < buttons.length; i++) { 94 panel.add(buttons[i]); 95 } 96 97 return panel; 98 } 99 100 public static JPanel createForm( 101 String title, 102 String [] labels, 103 Component [] components) { 104 Component [] jlabels = new Component [labels.length]; 105 for (int i = 0; i < labels.length; i++) { 106 jlabels[i] = CayenneWidgetFactory.createLabel(labels[i]); 107 } 108 return createForm(title, jlabels, components); 109 } 110 111 public static JPanel createForm( 112 Component [] leftComponents, 113 Component [] rightComponents) { 114 return createForm(null, leftComponents, rightComponents); 115 } 116 117 120 public static JPanel createForm( 121 String title, 122 Component [] leftComponents, 123 Component [] rightComponents) { 124 125 if (leftComponents.length != rightComponents.length) { 126 throw new IllegalArgumentException ( 127 "Arrays must be the same size, instead got " 128 + leftComponents.length 129 + "and " 130 + rightComponents.length); 131 } 132 133 int numRows = leftComponents.length; 134 if (numRows == 0) { 135 throw new IllegalArgumentException ("Zero components."); 136 } 137 138 FormLayout layout = new FormLayout("right:100, 3dlu, left:300", ""); 139 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 140 builder.setDefaultDialogBorder(); 141 142 if (title != null) { 143 builder.appendSeparator(title); 144 } 145 146 for (int i = 0; i < numRows; i++) { 147 builder.append(leftComponents[i], rightComponents[i]); 148 builder.nextLine(); 149 } 150 151 return builder.getPanel(); 152 } 153 154 159 public static JPanel createTablePanel(JTable table, JButton [] buttons) { 160 JPanel panel = new JPanel (); 161 panel.setLayout(new BorderLayout (5, 5)); 162 163 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 165 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 166 167 panel.add(new JScrollPane (table), BorderLayout.CENTER); 169 170 if (buttons != null) { 172 panel.add(createButtonPanel(buttons), BorderLayout.SOUTH); 173 } 174 return panel; 175 } 176 177 180 public static JPanel createTablePanel( 181 JTable table, 182 JComponent [] components, 183 JButton [] buttons) { 184 JPanel panel = new JPanel (); 185 panel.setLayout(new BorderLayout (5, 5)); 186 187 JPanel temp_panel = new JPanel (new BorderLayout ()); 188 189 table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 191 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 192 JScrollPane scroll_pane = new JScrollPane (table); 193 temp_panel.add(scroll_pane, BorderLayout.CENTER); 194 195 for (int i = 0; i < components.length; i++) { 196 JPanel temp = new JPanel (new BorderLayout ()); 197 temp.add(temp_panel, BorderLayout.CENTER); 198 temp.add(components[i], BorderLayout.SOUTH); 199 temp_panel = temp; 200 } 201 202 panel.add(temp_panel, BorderLayout.CENTER); 203 204 if (buttons != null) { 205 panel.add(createButtonPanel(buttons), BorderLayout.SOUTH); 206 } 207 return panel; 208 } 209 210 } 211 | Popular Tags |