001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.actions.iface.tools.support;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Dimension;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.List;
020:
021: import javax.swing.JPanel;
022: import javax.swing.JScrollPane;
023: import javax.swing.JTable;
024: import javax.swing.table.AbstractTableModel;
025:
026: import org.jdesktop.swingx.JXTable;
027:
028: import com.eviware.soapui.SoapUI;
029: import com.eviware.soapui.model.iface.Interface;
030: import com.eviware.soapui.support.UISupport;
031: import com.eviware.soapui.support.types.StringToStringMap;
032: import com.eviware.x.form.XForm.ToolkitType;
033: import com.eviware.x.impl.swing.AbstractSwingXFormField;
034:
035: /**
036: * Swing JTable for holding Namespace/Package mappings
037: * @author ole.matzura
038: *
039: */
040:
041: public class NamespaceTable extends AbstractSwingXFormField<JPanel> {
042: private JXTable table;
043: private JScrollPane scrollPane;
044: private final Interface iface;
045: private NamespaceTableModel namespaceTableModel;
046:
047: public NamespaceTable(Interface iface) {
048: super (new JPanel(new BorderLayout()));
049:
050: this .iface = iface;
051:
052: namespaceTableModel = new NamespaceTableModel();
053: table = new JXTable(namespaceTableModel);
054: table.setHorizontalScrollEnabled(true);
055: scrollPane = new JScrollPane(table);
056: scrollPane.setPreferredSize(new Dimension(300, 150));
057: getComponent().add(scrollPane, BorderLayout.CENTER);
058: }
059:
060: public JPanel getComponent(ToolkitType toolkitType) {
061: if (toolkitType == ToolkitType.SWT) {
062: UISupport
063: .showErrorMessage("SWT not supported by namespace table");
064: return null;
065: }
066:
067: return getComponent();
068: }
069:
070: public void setValue(String value) {
071: namespaceTableModel.setMappings(StringToStringMap
072: .fromXml(value));
073: }
074:
075: public String getValue() {
076: return namespaceTableModel.getMappings().toXml();
077: }
078:
079: private class NamespaceTableModel extends AbstractTableModel {
080: private List<String> namespaces = new ArrayList<String>();
081: private List<String> packages;
082:
083: public NamespaceTableModel() {
084: try {
085: if (iface != null)
086: namespaces.addAll(iface.getWsdlContext()
087: .getDefinedNamespaces());
088: } catch (Exception e) {
089: SoapUI.logError(e);
090: }
091:
092: packages = new ArrayList<String>(Arrays
093: .asList(new String[namespaces.size()]));
094: }
095:
096: public void setMappings(StringToStringMap mapping) {
097: for (int c = 0; c < namespaces.size(); c++) {
098: if (mapping.containsKey(namespaces.get(c))) {
099: packages.set(c, mapping.get(namespaces.get(c)));
100: } else {
101: packages.set(c, "");
102: }
103: }
104:
105: fireTableDataChanged();
106: }
107:
108: public int getRowCount() {
109: return namespaces.size();
110: }
111:
112: public int getColumnCount() {
113: return 2;
114: }
115:
116: public Class<?> getColumnClass(int columnIndex) {
117: return String.class;
118: }
119:
120: public String getColumnName(int column) {
121: return column == 0 ? "Namespace" : "Package";
122: }
123:
124: public boolean isCellEditable(int rowIndex, int columnIndex) {
125: return columnIndex == 1;
126: }
127:
128: public void setValueAt(Object aValue, int rowIndex,
129: int columnIndex) {
130: if (columnIndex == 1)
131: packages.set(rowIndex, aValue.toString());
132: }
133:
134: public Object getValueAt(int rowIndex, int columnIndex) {
135: if (columnIndex == 0)
136: return namespaces.get(rowIndex);
137: else
138: return packages.get(rowIndex);
139: }
140:
141: public StringToStringMap getMappings() {
142: StringToStringMap result = new StringToStringMap();
143: for (int c = 0; c < namespaces.size(); c++) {
144: String pkg = packages.get(c);
145: if (pkg != null && pkg.trim().length() > 0) {
146: result.put(namespaces.get(c), pkg.trim());
147: }
148: }
149:
150: return result;
151: }
152: }
153:
154: @Override
155: public boolean isMultiRow() {
156: return true;
157: }
158: }
|