KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > PartAndElementOrTypeTableModel


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ElementOrTypeTableModel.java
22  *
23  * Created on August 30, 2006, 2:20 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.view;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import javax.swing.table.AbstractTableModel JavaDoc;
39 import javax.swing.table.DefaultTableModel JavaDoc;
40
41 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
42 import org.netbeans.modules.xml.schema.model.Schema;
43 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
44 import org.openide.DialogDisplayer;
45 import org.openide.NotifyDescriptor;
46 import org.openide.util.NbBundle;
47
48
49 /**
50  *
51  * @author radval
52  */

53 public class PartAndElementOrTypeTableModel extends AbstractTableModel JavaDoc {
54     
55     private List JavaDoc<PartAndElementOrType> mPartAndElementOrTypeList = new ArrayList JavaDoc();
56     private Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
57     
58     /** Creates a new instance of ElementOrTypeTableModel */
59     public PartAndElementOrTypeTableModel(Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap) {
60         this.namespaceToPrefixMap = namespaceToPrefixMap;
61     }
62     
63     public List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> getPartAndElementOrType() {
64         return this.mPartAndElementOrTypeList;
65     }
66     
67     public String JavaDoc getColumnName(int column) {
68         if(column == 0) {
69             return NbBundle.getMessage(PartAndElementOrTypeTableModel.class, "PartAndElementOrTypeTableMode.Column1.text");
70         } else {
71             return NbBundle.getMessage(PartAndElementOrTypeTableModel.class, "PartAndElementOrTypeTableMode.Column2.text");
72         }
73     }
74  
75     public int getColumnCount() {
76         return 2;
77     }
78
79     public int getRowCount() {
80         return mPartAndElementOrTypeList.size();
81     }
82
83     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
84         PartAndElementOrType partAndElementOrType = mPartAndElementOrTypeList.get(rowIndex);
85         if(columnIndex == 0) {
86             return partAndElementOrType.getPartName();
87         } else if(columnIndex == 1) {
88             return partAndElementOrType.getElementOrType();
89         }
90         
91         return "Missing Value";
92     }
93     
94     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
95         if (rowIndex < 0 || columnIndex < 0 || rowIndex >= getRowCount() || columnIndex >= getColumnCount())
96             return;
97         PartAndElementOrTypeTableModel.PartAndElementOrType item = this.mPartAndElementOrTypeList.get(rowIndex);
98         if(columnIndex == 0) {
99             String JavaDoc partName = (String JavaDoc) aValue;
100             String JavaDoc oldPartName = (String JavaDoc) this.getValueAt(rowIndex, 0);
101             if (oldPartName != null && oldPartName.equals(partName)) return;
102             if(isPartNameExists(partName)) {
103                 NotifyDescriptor.Message nd = new NotifyDescriptor.Message("Part \""+ partName + " is already exist ", NotifyDescriptor.WARNING_MESSAGE);
104                 DialogDisplayer.getDefault().notify(nd);
105             } else if(!isValidPartName(partName)) {
106                 NotifyDescriptor.Message nd = new NotifyDescriptor.Message("Name \"" + partName + "\" is not a valid NCName", NotifyDescriptor.WARNING_MESSAGE);
107                 DialogDisplayer.getDefault().notify(nd);
108                 
109             } else {
110             
111                 item.setPartName(partName);
112             }
113         } else if(columnIndex == 1) {
114             if(aValue instanceof ElementOrType) {
115                 ElementOrType eorT = (ElementOrType) aValue;
116                 item.setElementOrType(eorT);
117             }
118             
119             //TODO: if user types strings make sure it is valid type
120
//and convert it to ElementOrType and set it
121
}
122         fireTableCellUpdated(rowIndex, columnIndex);
123         
124     }
125     
126      public boolean isCellEditable(int row, int col) {
127             return true;
128      }
129
130      /*
131      * JTable uses this method to determine the default renderer/
132      * editor for each cell. If we didn't implement this method,
133      * then the last column would contain text ("true"/"false"),
134      * rather than a check box.
135      */

136     public Class JavaDoc getColumnClass(int c) {
137         return getValueAt(0, c).getClass();
138     }
139      
140     public void removeSelectedRow(int row) {
141         int size = this.mPartAndElementOrTypeList.size();
142         if(row >= size) {
143             throw new IllegalArgumentException JavaDoc("can not delete row "+ row+1 +"total rows are"+ size);
144         }
145         
146         this.mPartAndElementOrTypeList.remove(row);
147         super.fireTableRowsDeleted(row, row);
148     }
149     
150     public void addNewRow() {
151         //Get the schemacomponent representing the xsd:string
152
GlobalSimpleType stringSimpleType = null;
153         Schema schema = SchemaModelFactory.getDefault().getPrimitiveTypesModel().getSchema();
154         Collection JavaDoc<GlobalSimpleType> simpleTypes = schema.getSimpleTypes();
155         for (GlobalSimpleType st : simpleTypes) {
156             if (st.getName().equals("string")) {
157                 stringSimpleType = st;
158                 break;
159             }
160         }
161         
162         
163         PartAndElementOrType p = new PartAndElementOrType(generateUniquePartName(),
164                 new ElementOrType(stringSimpleType, namespaceToPrefixMap));
165         this.mPartAndElementOrTypeList.add(p);
166         int row = this.mPartAndElementOrTypeList.indexOf(p);
167         super.fireTableRowsInserted(row, row);
168     }
169     
170     private String JavaDoc generateUniquePartName() {
171         String JavaDoc newNamePrefix = "part";
172         int counter = 1;
173         String JavaDoc generatedName = newNamePrefix + counter++;
174         
175         while(isPartNameExists(generatedName)) {
176             generatedName = newNamePrefix + counter++;
177         }
178         
179         return generatedName;
180     }
181     
182     private boolean isPartNameExists(String JavaDoc newPartName) {
183         Iterator JavaDoc<PartAndElementOrType> it = this.mPartAndElementOrTypeList.iterator();
184         while(it.hasNext()) {
185             PartAndElementOrType row = it.next();
186             String JavaDoc partName = row.getPartName();
187             //if name exists then create another name
188
if(partName != null && partName.equals(newPartName)) {
189                 return true;
190             }
191         }
192         
193         return false;
194     }
195     
196     private boolean isValidPartName(String JavaDoc newPartName) {
197         return org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(newPartName);
198     }
199     
200     public class PartAndElementOrType {
201         
202         private String JavaDoc mPartName;
203         
204         private ElementOrType mElementOrType;
205         
206         public PartAndElementOrType(String JavaDoc partName, ElementOrType elementOrType) {
207             this.mPartName = partName;
208             this.mElementOrType = elementOrType;
209         }
210         
211         public String JavaDoc getPartName() {
212             return this.mPartName;
213         }
214         
215         public void setPartName(String JavaDoc partName) {
216             this.mPartName = partName;
217         }
218         
219         public ElementOrType getElementOrType() {
220             return this.mElementOrType;
221         }
222         
223         public void setElementOrType(ElementOrType elementOrType) {
224             this.mElementOrType = elementOrType;
225         }
226     }
227     
228 }
229
Popular Tags