1 56 package org.objectstyle.cayenne.modeler.dialog.classgen; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Component ; 60 import java.awt.Dimension ; 61 import java.awt.FlowLayout ; 62 import java.awt.event.ItemEvent ; 63 64 import javax.swing.Box ; 65 import javax.swing.JPanel ; 66 import javax.swing.JScrollPane ; 67 import javax.swing.JTable ; 68 import javax.swing.table.DefaultTableCellRenderer ; 69 import javax.swing.table.TableCellRenderer ; 70 71 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog; 72 import org.scopemvc.core.PropertyManager; 73 import org.scopemvc.core.Selector; 74 import org.scopemvc.view.swing.SAction; 75 import org.scopemvc.view.swing.SButton; 76 import org.scopemvc.view.swing.SCheckBox; 77 import org.scopemvc.view.swing.SPanel; 78 import org.scopemvc.view.swing.STable; 79 import org.scopemvc.view.swing.STableModel; 80 import org.scopemvc.view.swing.STextField; 81 import org.scopemvc.view.swing.SwingView; 82 83 import com.jgoodies.forms.builder.PanelBuilder; 84 import com.jgoodies.forms.layout.CellConstraints; 85 import com.jgoodies.forms.layout.FormLayout; 86 87 93 public class ClassGeneratorDialog extends SPanel { 94 95 protected STable table; 96 97 public ClassGeneratorDialog() { 98 99 final STextField superClassPackage = new STextField(30); 101 superClassPackage.setSelector("superClassPackage"); 102 103 final STextField superClassTemplate = new STextField(30); 104 superClassTemplate.setSelector("customSuperclassTemplate"); 105 106 STextField classTemplate = new STextField(30); 107 classTemplate.setSelector("customClassTemplate"); 108 109 STextField folder = new STextField(30); 110 folder.setSelector("outputDir"); 111 112 SButton chooseButton = new SButton(new SAction( 113 ClassGeneratorController.CHOOSE_LOCATION_CONTROL)); 114 chooseButton.setEnabled(true); 115 116 SButton chooseTemplateButton = new SButton(new SAction( 117 ClassGeneratorController.CHOOSE_TEMPLATE_CONTROL)); 118 chooseTemplateButton.setEnabled(true); 119 120 final SButton chooseSuperTemplateButton = new SButton(new SAction( 121 ClassGeneratorController.CHOOSE_SUPERTEMPLATE_CONTROL)); 122 chooseSuperTemplateButton.setEnabled(true); 123 124 SButton selectAllButton = new SButton(new SAction( 125 ClassGeneratorController.SELECT_ALL_CONTROL)); 126 selectAllButton.setEnabled(true); 127 128 SButton generateButton = new SButton(new SAction( 129 ClassGeneratorController.GENERATE_CLASSES_CONTROL)); 130 generateButton.setEnabled(true); 131 132 SButton cancelButton = new SButton(new SAction( 133 ClassGeneratorController.CANCEL_CONTROL)); 134 cancelButton.setEnabled(true); 135 136 SCheckBox generateSuperclass = new SCheckBox() { 137 138 public void itemStateChanged(ItemEvent inEvent) { 140 boolean enabled = inEvent.getStateChange() == ItemEvent.SELECTED; 141 superClassPackage.setEnabled(enabled); 142 superClassTemplate.setEnabled(enabled); 143 chooseSuperTemplateButton.setEnabled(enabled); 144 145 super.itemStateChanged(inEvent); 146 } 147 }; 148 generateSuperclass.setSelector("pairs"); 149 150 table = new ClassGeneratorTable(); 152 table.setRowHeight(25); 153 table.setRowMargin(3); 154 ClassGeneratorModel model = new ClassGeneratorModel(table); 155 model.setSelector("entities"); 156 model.setColumnNames(new String [] { 157 "Entity", "Class", "Generate", "Problems" 158 }); 159 model.setColumnSelectors(new String [] { 160 "entity.name", "entity.className", "selected", "validationMessage" 161 }); 162 163 table.setModel(model); 164 table.getColumnModel().getColumn(2).setPreferredWidth(30); 165 166 168 CellConstraints cc = new CellConstraints(); 169 PanelBuilder builder = new PanelBuilder( 170 new FormLayout( 171 "right:max(50dlu;pref), 3dlu, fill:pref:grow, 3dlu, left:70", 172 "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, fill:100dlu:grow")); 173 builder.setDefaultDialogBorder(); 174 175 builder.addSeparator("Generated Class Settings", cc.xywh(1, 1, 5, 1)); 176 builder.addLabel("Output Directory:", cc.xy(1, 3)); 177 builder.add(folder, cc.xy(3, 3)); 178 builder.add(chooseButton, cc.xy(5, 3)); 179 builder.addLabel("Custom Template:", cc.xy(1, 5)); 180 builder.add(classTemplate, cc.xy(3, 5)); 181 builder.add(chooseTemplateButton, cc.xy(5, 5)); 182 builder.addSeparator("Generated Superclass Settings", cc.xywh(1, 7, 5, 1)); 183 builder.addLabel("Generate Superclass:", cc.xy(1, 9)); 184 builder.add(generateSuperclass, cc.xywh(3, 9, 3, 1)); 185 builder.addLabel("Superclass Package:", cc.xy(1, 11)); 186 builder.add(superClassPackage, cc.xy(3, 11)); 187 builder.addLabel("Custom Template:", cc.xy(1, 13)); 188 builder.add(superClassTemplate, cc.xy(3, 13)); 189 builder.add(chooseSuperTemplateButton, cc.xy(5, 13)); 190 191 builder.add(new JScrollPane ( 192 table, 193 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 194 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), cc.xywh(1, 15, 5, 1)); 195 196 JPanel buttons = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 197 buttons.add(selectAllButton); 198 buttons.add(Box.createHorizontalStrut(20)); 199 buttons.add(cancelButton); 200 buttons.add(generateButton); 201 202 setDisplayMode(SwingView.MODAL_DIALOG); 203 setTitle("Generate Java Classes"); 204 setLayout(new BorderLayout ()); 205 206 add(builder.getPanel(), BorderLayout.CENTER); 207 add(buttons, BorderLayout.SOUTH); 208 } 209 210 public STable getTable() { 211 return table; 212 } 213 214 class ClassGeneratorModel extends STableModel { 215 216 Selector enabledSelector = Selector.fromString("enabled"); 217 218 223 public ClassGeneratorModel(JTable table) { 224 super(table); 225 } 226 227 public boolean isEnabledRow(int rowIndex) { 228 Object row = getElementAt(rowIndex); 230 PropertyManager manager = getItemsManager(); 231 if (manager == null || row == null) { 232 return false; 233 } 234 235 try { 236 Boolean enabled = (Boolean ) manager.get(row, enabledSelector); 237 return enabled != null && enabled.booleanValue(); 238 } 239 catch (Exception e) { 240 return false; 241 } 242 } 243 244 247 public boolean isCellEditable(int rowIndex, int columnIndex) { 248 if (columnIndex != 2) { 250 return false; 251 } 252 253 return isEnabledRow(rowIndex); 254 } 255 } 256 257 class ClassGeneratorTable extends STable { 258 259 final Dimension preferredSize = new Dimension (500, 300); 260 261 DefaultTableCellRenderer problemRenderer; 262 263 ClassGeneratorTable() { 264 problemRenderer = new ClassGeneratorProblemRenderer(); 265 problemRenderer.setBackground(ValidatorDialog.WARNING_COLOR); 266 } 267 268 public TableCellRenderer getCellRenderer(int row, int column) { 269 ClassGeneratorModel model = (ClassGeneratorModel) getModel(); 270 271 return (model.isEnabledRow(row)) 272 ? super.getCellRenderer(row, column) 273 : problemRenderer; 274 } 275 276 public Dimension getPreferredScrollableViewportSize() { 277 return preferredSize; 278 } 279 } 280 281 class ClassGeneratorProblemRenderer extends DefaultTableCellRenderer { 282 283 public Component getTableCellRendererComponent( 284 JTable table, 285 Object value, 286 boolean isSelected, 287 boolean hasFocus, 288 int row, 289 int column) { 290 291 if (value instanceof Boolean ) { 292 value = ""; 293 } 294 295 return super.getTableCellRendererComponent( 296 table, 297 value, 298 isSelected, 299 hasFocus, 300 row, 301 column); 302 } 303 304 } 305 306 } | Popular Tags |