1 19 20 package org.netbeans.modules.j2ee.ddloaders.multiview; 21 22 import org.netbeans.modules.j2ee.dd.api.ejb.CmrField; 23 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 24 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelation; 25 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelationshipRole; 26 import org.netbeans.modules.j2ee.dd.api.ejb.RelationshipRoleSource; 27 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships; 28 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 34 37 public class CmpRelationshipsTableModel extends InnerTableModel { 38 39 private EjbJar ejbJar; 40 private final Map relationshipsHelperMap = new HashMap (); 41 private static final String [] COLUMN_NAMES = {Utils.getBundleMessage("LBL_RelationshipName"), 42 Utils.getBundleMessage("LBL_Cardinality"), 43 Utils.getBundleMessage("LBL_EntityBean"), 44 Utils.getBundleMessage("LBL_Role"), 45 Utils.getBundleMessage("LBL_Field"), 46 Utils.getBundleMessage("LBL_EntityBean"), 47 Utils.getBundleMessage("LBL_Role"), 48 Utils.getBundleMessage("LBL_Field")}; 49 private static final int[] COLUMN_WIDTHS = new int[]{140, 70, 100, 100, 100, 100, 100, 100}; 50 private EjbJarMultiViewDataObject dataObject; 51 52 public CmpRelationshipsTableModel(EjbJarMultiViewDataObject dataObject) { 53 super(dataObject.getModelSynchronizer(), COLUMN_NAMES, COLUMN_WIDTHS); 54 this.dataObject = dataObject; 55 this.ejbJar = dataObject.getEjbJar(); 56 ejbJar.addPropertyChangeListener(new PropertyChangeListener () { 57 public void propertyChange(PropertyChangeEvent evt) { 58 Object source = evt.getSource(); 59 if (source instanceof Relationships || source instanceof EjbRelation || source instanceof CmrField || 60 source instanceof EjbRelationshipRole || source instanceof RelationshipRoleSource) { 61 tableChanged(); 62 } 63 } 64 }); 65 } 66 67 public int addRow() { 68 CmpRelationshipsDialogHelper dialogHelper = new CmpRelationshipsDialogHelper(dataObject, ejbJar); 69 if (dialogHelper.showCmpRelationshipsDialog(Utils.getBundleMessage("LBL_AddCMPRelationship"), null)) { 70 modelUpdatedFromUI(); 71 } 72 return getRowCount() - 1; 73 } 74 75 public void removeRow(int row) { 76 final Relationships relationships = ejbJar.getSingleRelationships(); 77 relationships.removeEjbRelation(relationships.getEjbRelation(row)); 78 if (relationships.getEjbRelation().length == 0) { 79 ejbJar.setRelationships(null); 80 } 81 modelUpdatedFromUI(); 82 } 83 84 public void editRow(int row) { 85 EjbRelation ejbRelation = ejbJar.getSingleRelationships().getEjbRelation(row); 86 CmpRelationshipsDialogHelper dialogHelper = new CmpRelationshipsDialogHelper(dataObject, ejbJar); 87 if (dialogHelper.showCmpRelationshipsDialog(Utils.getBundleMessage("LBL_Edit_CMP_Relationship"), 88 ejbRelation)) { 89 modelUpdatedFromUI(); 90 } 91 92 } 93 94 public void refreshView() { 95 relationshipsHelperMap.clear(); 96 super.refreshView(); 97 } 98 99 public int getRowCount() { 100 Relationships relationships = ejbJar.getSingleRelationships(); 101 return relationships == null ? 0 : relationships.sizeEjbRelation(); 102 } 103 104 public Object getValueAt(int rowIndex, int columnIndex) { 105 EjbRelation relation = ejbJar.getSingleRelationships().getEjbRelation(rowIndex); 106 RelationshipHelper helper = getRelationshipHelper(relation); 107 RelationshipHelper.RelationshipRoleHelper roleA = helper.roleA; 108 RelationshipHelper.RelationshipRoleHelper roleB = helper.roleB; 109 switch (columnIndex) { 110 case 0: 111 return helper.getRelationName(); 112 case 1: 113 if (roleA.isMultiple()) { 114 return roleB.isMultiple() ? "M:N" : "N:1"; 115 } else { 116 return roleB.isMultiple() ? "1:N" : "1:1"; 117 } 118 case 2: 119 return roleA.getEjbName(); 120 case 3: 121 return roleA.getRoleName(); 122 case 4: 123 return roleA.getFieldName(); 124 case 5: 125 return roleB.getEjbName(); 126 case 6: 127 return roleB.getRoleName(); 128 case 7: 129 return roleB.getFieldName(); 130 } 131 return null; 132 } 133 134 public RelationshipHelper getRelationshipHelper(EjbRelation relation) { 135 RelationshipHelper helper = (RelationshipHelper) relationshipsHelperMap.get(relation); 136 if (helper == null) { 137 helper = new RelationshipHelper(relation); 138 } 139 return helper; 140 } 141 142 public boolean isCellEditable(int rowIndex, int columnIndex) { 143 return false; 144 } 145 146 public Class getColumnClass(int columnIndex) { 147 switch (columnIndex) { 148 case 0: 149 return String .class; 150 } 151 return super.getColumnClass(columnIndex); 152 } 153 154 } 155 | Popular Tags |