KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > CmpRelationshipsTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * @author pfiala
36  */

37 public class CmpRelationshipsTableModel extends InnerTableModel {
38
39     private EjbJar ejbJar;
40     private final Map JavaDoc relationshipsHelperMap = new HashMap JavaDoc();
41     private static final String JavaDoc[] 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 JavaDoc() {
57             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
58                 Object JavaDoc 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 JavaDoc 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 JavaDoc getColumnClass(int columnIndex) {
147         switch (columnIndex) {
148             case 0:
149                 return String JavaDoc.class;
150         }
151         return super.getColumnClass(columnIndex);
152     }
153
154 }
155
Popular Tags