1 56 package org.objectstyle.cayenne.modeler.dialog.objentity; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Component ; 60 import java.awt.Dimension ; 61 62 import javax.swing.DefaultCellEditor ; 63 import javax.swing.DefaultComboBoxModel ; 64 import javax.swing.JButton ; 65 import javax.swing.JComboBox ; 66 import javax.swing.JScrollPane ; 67 import javax.swing.JTable ; 68 import javax.swing.table.TableCellEditor ; 69 70 import org.apache.log4j.Logger; 71 import org.objectstyle.cayenne.modeler.util.PanelFactory; 72 import org.scopemvc.core.Selector; 73 import org.scopemvc.view.swing.SAction; 74 import org.scopemvc.view.swing.SButton; 75 import org.scopemvc.view.swing.SComboBox; 76 import org.scopemvc.view.swing.SLabel; 77 import org.scopemvc.view.swing.SListCellRenderer; 78 import org.scopemvc.view.swing.SPanel; 79 import org.scopemvc.view.swing.STable; 80 import org.scopemvc.view.swing.STableModel; 81 import org.scopemvc.view.swing.STextField; 82 import org.scopemvc.view.swing.SwingView; 83 84 import com.jgoodies.forms.builder.PanelBuilder; 85 import com.jgoodies.forms.layout.CellConstraints; 86 import com.jgoodies.forms.layout.FormLayout; 87 88 95 public class ObjRelationshipInfoDialog extends SPanel { 96 static final Logger logObj = Logger.getLogger(ObjRelationshipInfoDialog.class); 97 98 protected STable pathTable; 99 100 public ObjRelationshipInfoDialog() { 101 init(); 102 } 103 104 protected void init() { 105 SButton saveButton = 107 new SButton(new SAction(ObjRelationshipInfoController.SAVE_CONTROL)); 108 saveButton.setEnabled(true); 109 110 SButton cancelButton = 111 new SButton(new SAction(ObjRelationshipInfoController.CANCEL_CONTROL)); 112 cancelButton.setEnabled(true); 113 114 SButton newToOneButton = 115 new SButton(new SAction(ObjRelationshipInfoController.NEW_TOONE_CONTROL)); 116 newToOneButton.setEnabled(true); 117 SButton newToManyButton = 118 new SButton(new SAction(ObjRelationshipInfoController.NEW_TOMANY_CONTROL)); 119 newToManyButton.setEnabled(true); 120 121 STextField relationshipName = new STextField(25); 122 relationshipName.setSelector(ObjRelationshipInfoModel.RELATIONSHIP_NAME_SELECTOR); 123 124 SLabel sourceEntityLabel = new SLabel(); 125 sourceEntityLabel.setSelector( 126 ObjRelationshipInfoModel.SOURCE_ENTITY_NAME_SELECTOR); 127 128 SComboBox targetCombo = new SComboBox(); 129 targetCombo.setSelector(ObjRelationshipInfoModel.OBJECT_TARGETS_SELECTOR); 130 targetCombo.setSelectionSelector(ObjRelationshipInfoModel.OBJECT_TARGET_SELECTOR); 131 SListCellRenderer renderer = (SListCellRenderer) targetCombo.getRenderer(); 132 renderer.setTextSelector("name"); 133 134 pathTable = new ObjRelationshipPathTable(); 135 STableModel pathTableModel = new STableModel(pathTable); 136 pathTableModel.setSelector( 137 ObjRelationshipInfoModel.DB_RELATIONSHIP_PATH_SELECTOR); 138 pathTableModel.setColumnNames(new String [] { "DbRelationships" }); 139 pathTableModel.setColumnSelectors( 140 new Selector[] { 141 EntityRelationshipsModel.RELATIONSHIP_DISPLAY_NAME_SELECTOR }); 142 143 pathTable.setModel(pathTableModel); 144 pathTable.setSelectionSelector( 145 ObjRelationshipInfoModel.SELECTED_PATH_COMPONENT_SELECTOR); 146 pathTable.getColumn("DbRelationships").setCellEditor( 147 RelationshipPicker.createEditor(this)); 148 149 setDisplayMode(SwingView.MODAL_DIALOG); 151 setTitle("ObjRelationship Inspector"); 152 setLayout(new BorderLayout ()); 153 154 CellConstraints cc = new CellConstraints(); 155 PanelBuilder builder = 156 new PanelBuilder( 157 new FormLayout( 158 "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(150dlu;pref)", 159 "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, top:14dlu, 3dlu, top:p:grow")); 160 builder.setDefaultDialogBorder(); 161 162 builder.addSeparator("ObjRelationship Information", cc.xywh(1, 1, 5, 1)); 163 builder.addLabel("Relationship:", cc.xy(1, 3)); 164 builder.add(relationshipName, cc.xywh(3, 3, 1, 1)); 165 builder.addLabel("Source:", cc.xy(1, 5)); 166 builder.add(sourceEntityLabel, cc.xywh(3, 5, 1, 1)); 167 builder.addLabel("Target:", cc.xy(1, 7)); 168 builder.add(targetCombo, cc.xywh(3, 7, 1, 1)); 169 170 builder.addSeparator("Mapping to DbRelationships", cc.xywh(1, 9, 5, 1)); 171 builder.add(new JScrollPane (pathTable), cc.xywh(1, 11, 3, 3)); 172 builder.add(newToOneButton, cc.xywh(5, 11, 1, 1)); 173 builder.add(newToManyButton, cc.xywh(5, 13, 1, 1)); 174 175 add(builder.getPanel(), BorderLayout.CENTER); 176 add( 177 PanelFactory.createButtonPanel(new JButton [] { saveButton, cancelButton }), 178 BorderLayout.SOUTH); 179 } 180 181 184 public void cancelTableEditing() { 185 int row = pathTable.getEditingRow(); 186 if (row < 0) { 187 return; 188 } 189 190 int column = pathTable.getEditingColumn(); 191 if (column < 0) { 192 return; 193 } 194 195 TableCellEditor editor = pathTable.getCellEditor(row, column); 196 if (editor != null) { 197 editor.cancelCellEditing(); 198 } 199 } 200 201 class ObjRelationshipPathTable extends STable { 202 final Dimension preferredSize = new Dimension (203, 100); 203 204 ObjRelationshipPathTable() { 205 setRowHeight(25); 206 setRowMargin(3); 207 } 208 209 public Dimension getPreferredScrollableViewportSize() { 210 return preferredSize; 211 } 212 } 213 214 static final class RelationshipPicker extends DefaultCellEditor { 215 JComboBox comboBox; 216 SwingView view; 217 218 static TableCellEditor createEditor(SwingView view) { 219 JComboBox relationshipCombo = new JComboBox (); 220 relationshipCombo.setEditable(false); 221 return new RelationshipPicker(view, relationshipCombo); 222 } 223 224 RelationshipPicker(SwingView view, JComboBox comboBox) { 225 super(comboBox); 226 this.comboBox = comboBox; 227 this.view = view; 228 } 229 230 public Component getTableCellEditorComponent( 231 JTable table, 232 Object value, 233 boolean isSelected, 234 int row, 235 int column) { 236 237 ObjRelationshipInfoModel model = 239 (ObjRelationshipInfoModel) view.getBoundModel(); 240 241 EntityRelationshipsModel relationshipWrapper = 242 (EntityRelationshipsModel) model.getDbRelationshipPath().get(row); 243 244 DefaultComboBoxModel comboModel = 245 new DefaultComboBoxModel (relationshipWrapper.getRelationshipNames()); 246 comboModel.setSelectedItem(value); 247 comboBox.setModel(comboModel); 248 249 return super.getTableCellEditorComponent( 251 table, 252 value, 253 isSelected, 254 row, 255 column); 256 } 257 } 258 } 259 | Popular Tags |