1 56 57 package org.objectstyle.cayenne.dataview.dvmodeler; 58 59 import java.awt.CardLayout ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.io.File ; 63 import java.util.Iterator ; 64 65 import javax.swing.DefaultComboBoxModel ; 66 import javax.swing.JComboBox ; 67 import javax.swing.JPanel ; 68 import javax.swing.JScrollPane ; 69 import javax.swing.JTable ; 70 import javax.swing.JTextField ; 71 import javax.swing.ListSelectionModel ; 72 73 import com.jgoodies.forms.builder.DefaultFormBuilder; 74 import com.jgoodies.forms.factories.Borders; 75 import com.jgoodies.forms.layout.FormLayout; 76 77 83 84 public class CardPanel extends JPanel { 85 private FieldEditor fieldPanel; 86 private JTable attributesTable; 87 private JPanel objEntityViewPanel; 88 89 private JPanel dataMapPanel; 90 private JTextField dataMapNameField; 91 private JTextField dataMapFileField; 92 93 private JPanel dataViewPanel; 94 private DataView dataView; 95 private JTextField dataViewNameField; 96 private JTextField dataViewFileField; 97 private DataMapTreeModel dataMapTreeModel; 98 99 private JTextField dataViewField; 100 private JTextField viewNameField; 101 private ObjEntityView objEntityView; 102 private JComboBox objEntityCombo; 103 104 private JPanel emptyPanel; 105 private CardLayout cardLayout; 106 107 108 private DVObject selectedObject; 109 110 public CardPanel() { 111 super(); 112 cardLayout = new CardLayout (); 113 this.setLayout( cardLayout ); 114 115 116 emptyPanel = new JPanel (); 117 add(emptyPanel, "emptyPanel"); 118 119 120 fieldPanel = new FieldEditor(); 121 add(fieldPanel, "objEntityViewFieldPanel"); 122 123 124 attributesTable = new JTable (); 125 attributesTable.setModel(new AttributesTableModel()); 126 attributesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 127 JScrollPane scrollPane = new JScrollPane (attributesTable); 128 scrollPane.setBorder(Borders.EMPTY_BORDER); 129 add(scrollPane, "attributesTable"); 130 131 132 FormLayout layout = new FormLayout( 133 "right:55dlu, 3dlu, 200dlu", 134 ""); 135 DefaultFormBuilder builderView = new DefaultFormBuilder(layout); 136 builderView.setDefaultDialogBorder(); 137 138 viewNameField = new JTextField (); 139 viewNameField.getDocument().addDocumentListener( 140 new NameChangeListener()); 141 builderView.append("Name:", viewNameField); 142 143 objEntityCombo = new JComboBox (); 144 objEntityCombo.addActionListener(new ActionListener (){ 145 public void actionPerformed(ActionEvent e){ 146 objEntityComboAction(e); 147 } 148 }); 149 builderView.append("ObjEntity:", objEntityCombo); 150 151 dataViewField = new JTextField (); 152 153 dataViewField.setEditable(false); 154 dataViewField.setBorder(Borders.EMPTY_BORDER); 155 builderView.append("Data View:", dataViewField); 156 157 objEntityViewPanel = builderView.getPanel(); 158 scrollPane = new JScrollPane (objEntityViewPanel); 159 scrollPane.setBorder(Borders.EMPTY_BORDER); 160 add(scrollPane, "objEntityViewPanel"); 161 162 163 FormLayout layout1 = new FormLayout( 164 "right:55dlu, 3dlu, 200dlu", 165 ""); 166 167 DefaultFormBuilder builderMap = new DefaultFormBuilder(layout1); 168 builderMap.setDefaultDialogBorder(); 169 170 dataMapNameField = new JTextField (); 171 dataMapNameField.setEditable(false); 172 dataMapNameField.setBorder(Borders.EMPTY_BORDER); 173 builderMap.append("Name:", dataMapNameField); 174 175 dataMapFileField = new JTextField (); 176 dataMapFileField.setEditable(false); 177 dataMapFileField.setBorder(Borders.EMPTY_BORDER); 178 builderMap.append("Location:", dataMapFileField); 179 180 dataMapPanel = builderMap.getPanel(); 181 scrollPane = new JScrollPane (dataMapPanel); 182 scrollPane.setBorder(Borders.EMPTY_BORDER); 183 add(scrollPane, "dataMapPanel"); 184 185 186 FormLayout layout2 = new FormLayout( 187 "right:55dlu, 3dlu, 200dlu", 188 ""); 189 190 DefaultFormBuilder builderDataView = new DefaultFormBuilder(layout2); 191 builderDataView.setDefaultDialogBorder(); 192 193 dataViewNameField = new JTextField (); 194 dataViewNameField.getDocument().addDocumentListener( 195 new NameChangeListener()); 196 197 builderDataView.append("Name:", dataViewNameField); 198 199 dataViewFileField = new JTextField (); 200 dataViewFileField.setEditable(false); 201 dataViewFileField.setBorder(Borders.EMPTY_BORDER); 202 builderDataView.append("Location:", dataViewFileField); 203 204 dataViewPanel = builderDataView.getPanel(); 205 scrollPane = new JScrollPane (dataViewPanel); 206 scrollPane.setBorder(Borders.EMPTY_BORDER); 207 add(scrollPane, "dataViewPanel"); 208 209 cardLayout.show(this, "emptyPanel"); 210 } 211 212 213 public void showPanel(DVObject selectedObject){ 214 this.selectedObject = null; 215 if (selectedObject instanceof DataMap){ 216 DataMap dataMap = (DataMap)selectedObject; 217 dataMapNameField.setText(dataMap.getName()); 218 dataMapFileField.setText(dataMap.getFile().getName()); 219 cardLayout.show(this, "dataMapPanel"); 220 } else if (selectedObject instanceof DataView){ 221 dataView = (DataView)selectedObject; 222 dataViewNameField.setText(dataView.getName()); 223 dataViewFileField.setText( 224 (dataView.getFile() != null ? 225 dataView.getFile().getName() : 226 "New Data View")); 227 228 cardLayout.show(this, "dataViewPanel"); 229 } else if (selectedObject instanceof ObjEntity){ 230 ObjEntity objEntity = (ObjEntity)selectedObject; 231 AttributesTableModel attributesTableModel = 232 (AttributesTableModel)attributesTable.getModel(); 233 attributesTableModel.setObjEntity(objEntity); 234 cardLayout.show(this, "attributesTable"); 235 } else if (selectedObject instanceof ObjEntityView){ 236 objEntityView = (ObjEntityView)selectedObject; 237 dataViewField.setText(objEntityView.getDataView().getName()); 238 viewNameField.setText(objEntityView.getName()); 239 240 ObjEntity objEntity = objEntityView.getObjEntity(); 241 if (objEntity == null){ 242 objEntityCombo.setSelectedIndex(0); 243 } else { 244 objEntityCombo.setSelectedItem(objEntity); 245 } 246 247 cardLayout.show(this, "objEntityViewPanel"); 248 } else if (selectedObject instanceof ObjEntityViewField){ 249 ObjEntityViewField objEntityViewField = (ObjEntityViewField)selectedObject; 250 fieldPanel.setFieldProperties(objEntityViewField); 251 cardLayout.show(this, "objEntityViewFieldPanel"); 252 } else if (selectedObject == null ){ 253 cardLayout.show(this, "emptyPanel"); 254 } 255 this.selectedObject = selectedObject; 256 } 257 258 259 public void setModels(DataMapTreeModel mapModel, 260 DataViewTreeModel viewModel, 261 FieldsTableModel tableModel){ 262 dataMapTreeModel = mapModel; 263 fieldPanel.setModels(mapModel, viewModel, tableModel); 264 } 265 266 267 public void setProject(CayenneProject project) { 268 if (project == null) { 269 objEntityCombo.setModel(new DefaultComboBoxModel ()); 270 return; 271 } 272 273 ObjEntity[] projectEntities = project.getObjEntities(); 274 ObjEntity[] entities = new ObjEntity[projectEntities.length + 1]; 275 ObjEntity nullEntity = null; 276 entities[0] = nullEntity; 277 278 for (int j = 0; j < projectEntities.length; j++){ 279 entities[j+1] = projectEntities[j]; 280 } 281 DefaultComboBoxModel objEntitiesDefaultModel = 282 new DefaultComboBoxModel (entities); 283 objEntityCombo.setModel(objEntitiesDefaultModel); 284 } 285 286 private class NameChangeListener extends BasicDocumentListener { 287 public void documentUpdated(String text) { 288 if (selectedObject != null){ 289 selectedObject.setName(text.trim()); 290 if (selectedObject instanceof DataView){ 291 File file = dataView.getFile(); 292 File newFile = new File (file.getParentFile(), dataView.getName() + ".view.xml"); 293 dataView.setFile(newFile); 294 dataViewFileField.setText(dataView.getFile().getName()); 295 } 296 } 297 } 298 } 299 300 private void objEntityComboAction(ActionEvent e){ 301 ObjEntity selectedObjEntity = (ObjEntity)objEntityCombo.getSelectedItem(); 302 ObjEntity objEntity = objEntityView.getObjEntity(); 303 if ((objEntity != selectedObjEntity)){ 304 if ((objEntity != null) && (selectedObjEntity != null)){ 305 java.util.List relationships = selectedObjEntity.getDataMap().getObjRelationshipsBySourceToOne(selectedObjEntity); 306 int oldIndex = objEntity.getIndexOfObjEntityView(objEntityView); 307 objEntityView.setObjEntity(selectedObjEntity); 308 309 311 dataMapTreeModel.replaceObjEntityView(objEntity, oldIndex, selectedObjEntity, objEntityView); 312 313 314 java.util.List fields = objEntityView.getObjEntityViewFields(); 315 for (Iterator itr = fields.iterator();itr.hasNext();){ 316 ObjEntityViewField field = (ObjEntityViewField)itr.next(); 317 if(field.getCalcType().equals("nocalc")){ 318 if (field.getObjAttribute() != null){ 319 String fieldAttributeName = field.getObjAttribute().getName(); 320 if (selectedObjEntity.getObjAttribute(fieldAttributeName) != null){ 321 field.setObjAttribute(selectedObjEntity.getObjAttribute(fieldAttributeName)); 322 }else{ 323 field.setObjAttribute(null); 324 } 325 } 326 } 327 if(field.getCalcType().equals("lookup")){ 328 ObjRelationship fieldRelationship = field.getObjRelationship(); 329 if (fieldRelationship != null){ 330 String relationshipName = fieldRelationship.getName(); 331 ObjEntity targetObjEntity = fieldRelationship.getTargetObjEntity(); 332 Lookup lookup = field.getLookup(); 333 Lookup nullLookup = new Lookup(field); 334 nullLookup.setLookupField(null); 335 nullLookup.setLookupObjEntityView(null); 336 337 field.setObjRelationship(null); 338 field.setLookup(nullLookup); 339 for (Iterator j = relationships.iterator(); j.hasNext();){ 340 ObjRelationship relationship = (ObjRelationship)j.next(); 341 if ((relationship.getName().equals(relationshipName))&& 342 (relationship.getTargetObjEntity() == targetObjEntity)){ 343 field.setObjRelationship(relationship); 344 field.setLookup(lookup); 345 } 346 } 347 } 348 } 349 } 350 } else if ((objEntity == null) && (selectedObjEntity != null)){ 351 objEntityView.setObjEntity(selectedObjEntity); 352 353 354 dataMapTreeModel.objEntityViewAdded(objEntityView); 355 } else if ((objEntity != null) && (selectedObjEntity == null)){ 356 int index = objEntity.getIndexOfObjEntityView(objEntityView); 357 objEntityView.setObjEntity(null); 358 359 360 dataMapTreeModel.objEntityViewRemoved(objEntity, objEntityView, index); 361 362 363 java.util.List fields = objEntityView.getObjEntityViewFields(); 364 365 for (Iterator itr = fields.iterator();itr.hasNext();){ 366 ObjEntityViewField field = (ObjEntityViewField)itr.next(); 367 if(field.getCalcType().equals("nocalc")){ 368 field.setObjAttribute(null); 369 } 370 if(field.getCalcType().equals("lookup")){ 371 Lookup nullLookup = new Lookup(field); 372 nullLookup.setLookupField(null); 373 nullLookup.setLookupObjEntityView(null); 374 375 field.setObjRelationship(null); 376 field.setLookup(nullLookup); 377 378 } 379 } 380 } 381 } 382 } 383 } 384 | Popular Tags |