001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * ElementOrTypeTableModel.java
044: *
045: * Created on August 30, 2006, 2:20 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.wsdl.ui.view;
052:
053: import java.util.ArrayList;
054: import java.util.Collection;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.List;
058: import java.util.Map;
059:
060: import javax.swing.table.AbstractTableModel;
061:
062: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
063: import org.netbeans.modules.xml.schema.model.Schema;
064: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
065: import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.OperationPanel;
066: import org.openide.DialogDisplayer;
067: import org.openide.NotifyDescriptor;
068: import org.openide.util.NbBundle;
069:
070: /**
071: *
072: * @author radval
073: */
074: public class PartAndElementOrTypeTableModel extends AbstractTableModel {
075:
076: private List<PartAndElementOrType> mPartAndElementOrTypeList = new ArrayList();
077: private Map<String, String> namespaceToPrefixMap = new HashMap<String, String>();
078:
079: /** Creates a new instance of ElementOrTypeTableModel */
080: public PartAndElementOrTypeTableModel(
081: Map<String, String> namespaceToPrefixMap) {
082: this .namespaceToPrefixMap = namespaceToPrefixMap;
083: }
084:
085: public List<PartAndElementOrTypeTableModel.PartAndElementOrType> getPartAndElementOrType() {
086: return this .mPartAndElementOrTypeList;
087: }
088:
089: public String getColumnName(int column) {
090: if (column == 0) {
091: return NbBundle.getMessage(
092: PartAndElementOrTypeTableModel.class,
093: "PartAndElementOrTypeTableMode.Column1.text");
094: } else {
095: return NbBundle.getMessage(
096: PartAndElementOrTypeTableModel.class,
097: "PartAndElementOrTypeTableMode.Column2.text");
098: }
099: }
100:
101: public int getColumnCount() {
102: return 2;
103: }
104:
105: public int getRowCount() {
106: return mPartAndElementOrTypeList.size();
107: }
108:
109: public Object getValueAt(int rowIndex, int columnIndex) {
110: PartAndElementOrType partAndElementOrType = mPartAndElementOrTypeList
111: .get(rowIndex);
112: if (columnIndex == 0) {
113: return partAndElementOrType.getPartName();
114: } else if (columnIndex == 1) {
115: return partAndElementOrType.getElementOrType();
116: }
117:
118: return "Missing Value";
119: }
120:
121: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
122: if (rowIndex < 0 || columnIndex < 0
123: || rowIndex >= getRowCount()
124: || columnIndex >= getColumnCount())
125: return;
126: PartAndElementOrTypeTableModel.PartAndElementOrType item = this .mPartAndElementOrTypeList
127: .get(rowIndex);
128: if (columnIndex == 0) {
129: String partName = (String) aValue;
130: String oldPartName = (String) this .getValueAt(rowIndex, 0);
131: if (oldPartName != null && oldPartName.equals(partName))
132: return;
133: if (isPartNameExists(partName)) {
134: NotifyDescriptor.Message nd = new NotifyDescriptor.Message(
135: NbBundle.getMessage(
136: PartAndElementOrTypeTableModel.class,
137: "ERR_MSG_PART_EXISTS", partName),
138: NotifyDescriptor.WARNING_MESSAGE);
139: DialogDisplayer.getDefault().notify(nd);
140: } else if (!isValidPartName(partName)) {
141: NotifyDescriptor.Message nd = new NotifyDescriptor.Message(
142: NbBundle.getMessage(OperationPanel.class,
143: "ERR_MSG_INVALID_NAME", partName),
144: NotifyDescriptor.WARNING_MESSAGE);
145: DialogDisplayer.getDefault().notify(nd);
146:
147: } else {
148:
149: item.setPartName(partName);
150: }
151: } else if (columnIndex == 1) {
152: if (aValue instanceof ElementOrType) {
153: ElementOrType eorT = (ElementOrType) aValue;
154: item.setElementOrType(eorT);
155: }
156:
157: //TODO: if user types strings make sure it is valid type
158: //and convert it to ElementOrType and set it
159: }
160: fireTableCellUpdated(rowIndex, columnIndex);
161:
162: }
163:
164: public boolean isCellEditable(int row, int col) {
165: return true;
166: }
167:
168: /*
169: * JTable uses this method to determine the default renderer/
170: * editor for each cell. If we didn't implement this method,
171: * then the last column would contain text ("true"/"false"),
172: * rather than a check box.
173: */
174: public Class getColumnClass(int c) {
175: return getValueAt(0, c).getClass();
176: }
177:
178: public void removeSelectedRow(int row) {
179: int size = this .mPartAndElementOrTypeList.size();
180: if (row >= size) {
181: throw new IllegalArgumentException("can not delete row "
182: + row + 1 + "total rows are" + size);
183: }
184:
185: this .mPartAndElementOrTypeList.remove(row);
186: super .fireTableRowsDeleted(row, row);
187: }
188:
189: public void addNewRow() {
190: //Get the schemacomponent representing the xsd:string
191: GlobalSimpleType stringSimpleType = null;
192: Schema schema = SchemaModelFactory.getDefault()
193: .getPrimitiveTypesModel().getSchema();
194: Collection<GlobalSimpleType> simpleTypes = schema
195: .getSimpleTypes();
196: for (GlobalSimpleType st : simpleTypes) {
197: if (st.getName().equals("string")) {
198: stringSimpleType = st;
199: break;
200: }
201: }
202:
203: PartAndElementOrType p = new PartAndElementOrType(
204: generateUniquePartName(), new ElementOrType(
205: stringSimpleType, namespaceToPrefixMap));
206: this .mPartAndElementOrTypeList.add(p);
207: int row = this .mPartAndElementOrTypeList.indexOf(p);
208: super .fireTableRowsInserted(row, row);
209: }
210:
211: private String generateUniquePartName() {
212: String newNamePrefix = "part";
213: int counter = 1;
214: String generatedName = newNamePrefix + counter++;
215:
216: while (isPartNameExists(generatedName)) {
217: generatedName = newNamePrefix + counter++;
218: }
219:
220: return generatedName;
221: }
222:
223: private boolean isPartNameExists(String newPartName) {
224: Iterator<PartAndElementOrType> it = this .mPartAndElementOrTypeList
225: .iterator();
226: while (it.hasNext()) {
227: PartAndElementOrType row = it.next();
228: String partName = row.getPartName();
229: //if name exists then create another name
230: if (partName != null && partName.equals(newPartName)) {
231: return true;
232: }
233: }
234:
235: return false;
236: }
237:
238: private boolean isValidPartName(String newPartName) {
239: return org.netbeans.modules.xml.xam.dom.Utils
240: .isValidNCName(newPartName);
241: }
242:
243: public class PartAndElementOrType {
244:
245: private String mPartName;
246:
247: private ElementOrType mElementOrType;
248:
249: public PartAndElementOrType(String partName,
250: ElementOrType elementOrType) {
251: this .mPartName = partName;
252: this .mElementOrType = elementOrType;
253: }
254:
255: public String getPartName() {
256: return this .mPartName;
257: }
258:
259: public void setPartName(String partName) {
260: this .mPartName = partName;
261: }
262:
263: public ElementOrType getElementOrType() {
264: return this .mElementOrType;
265: }
266:
267: public void setElementOrType(ElementOrType elementOrType) {
268: this.mElementOrType = elementOrType;
269: }
270: }
271:
272: }
|