View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, Ontotext Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2006</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.grounding.sawsdl.ui.editor;
26  
27  import java.util.*;
28  
29  import javax.xml.xpath.*;
30  
31  import org.eclipse.jface.viewers.*;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.events.*;
34  import org.eclipse.swt.layout.*;
35  import org.eclipse.swt.widgets.*;
36  import org.w3c.dom.*;
37  import org.wsmostudio.runtime.LogManager;
38  
39  import com.ontotext.wsmo4j.grounding.sawsdl.SimpleNamespaceContext;
40  import com.ontotext.wsmo4j.grounding.sawsdl.WSDLUtils;
41  
42  public class MappingPropertiesView {
43  
44          private Document doc = null;
45          private Table table;
46          private XPathExpression queryExpr;
47          private WSDLAnnotationManager manager;
48          private boolean ascOrder = true;
49          
50          public MappingPropertiesView(Document doc, 
51                                       WSDLAnnotationManager manager,
52                                       Composite parent,
53                                       final TreeViewer mainViewer) {
54              this.doc = doc;
55              this.manager = manager;
56              
57              Group panel = new Group(parent, SWT.NONE);
58              panel.setText("Mapping Info");
59              panel.setLayout(new GridLayout(1, false));
60              this.table = new Table(panel, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
61              this.table.setLayoutData(new GridData(GridData.FILL_BOTH));
62              this.table.setLinesVisible(true);
63              this.table.setHeaderVisible(true);
64  
65              TableColumn column1 = new TableColumn(this.table, SWT.NONE);
66              column1.setWidth(230);
67              column1.setText("WSDL Element");
68              column1.setAlignment(SWT.CENTER);
69              column1.addSelectionListener(new SelectionAdapter() {
70                  public void widgetSelected(SelectionEvent event) {
71                      ascOrder = !ascOrder;
72                      setMappingData();
73                  }
74              });
75  
76              TableColumn column2 = new TableColumn(this.table, SWT.NONE);
77              column2.setText("Model Reference");
78              column2.setWidth(400);
79              
80              this.table.addMouseListener(new MouseAdapter() {
81                  public void mouseDoubleClick(MouseEvent e) {
82                      TableItem[] sel = table.getSelection();
83                      if (sel == null 
84                              || sel.length == 0) {
85                          return;
86                      }
87                      mainViewer.setSelection(new StructuredSelection(sel[0].getData()), true);
88                  }
89              });
90              this.table.addKeyListener(new KeyAdapter() {
91                  public void keyReleased(KeyEvent e) {
92                      if (e.keyCode == SWT.F5) {
93                          mainViewer.refresh();
94                          setMappingData();
95                      }
96                  }
97              });
98  
99              XPathFactory factory = XPathFactory.newInstance();
100             XPath xpath = factory.newXPath();
101             SimpleNamespaceContext nsContextProvider = new SimpleNamespaceContext(doc);
102             xpath.setNamespaceContext(nsContextProvider);
103             try {
104                 String xpathExpr = (manager.getSAWSDLPrefix().length() == 0) ?
105                         "//attribute::" + Utils.REF_ATTRIBUTE
106                         : "//attribute::"+ manager.getSAWSDLPrefix() + ":" + Utils.REF_ATTRIBUTE;
107                 queryExpr = xpath.compile(xpathExpr);
108             }
109             catch(XPathException xpe) {
110                 LogManager.logError(xpe.getMessage());
111             }
112             setMappingData();
113         }
114 
115         public void setMappingData() {
116 
117             if (queryExpr == null) {
118                 return;
119             }
120             NodeList all = null;
121             try {
122                 all = (NodeList)queryExpr.evaluate(doc, XPathConstants.NODESET);  
123             }
124             catch(XPathException xpe) {
125                 LogManager.logError(xpe);
126             }
127             if (all == null) {
128                 return;
129             }
130             String[] labels = new String[all.getLength()];
131             Map<String, Attr> label2Node = new HashMap<String, Attr>();
132             for(int i = 0; i < all.getLength(); i++) {
133                 labels[i] = generateWSDL_ID(((Attr)all.item(i)).getOwnerElement());
134                 label2Node.put(labels[i], (Attr)all.item(i));
135             }
136             Arrays.sort(labels);
137             
138             this.table.removeAll();
139             for(int i = 0; i < labels.length; i++) {
140                 String label = labels[(ascOrder) ? i : (labels.length - i - 1)];
141                 Attr attr = label2Node.get(label);
142                 if (false == Utils.isSupportedElement(attr.getOwnerElement(), manager)) {
143                 	continue;
144                 }
145                 TableItem item = new TableItem(this.table, SWT.NONE);
146                 item.setData(attr.getOwnerElement());
147                 item.setText(0, label);
148                 item.setText(1, "=>  " + attr.getValue());
149             }
150         }
151         
152         public void selectItem(Object data) {
153             if (data == null) {
154                 this.table.deselectAll();
155                 return;
156             }
157             TableItem[] items = this.table.getItems();
158             int selPos = -1;
159             for (int i = 0; i < items.length; i++) {
160                 if (data.equals(items[i].getData())) {
161                     selPos = i;
162                     break;
163                 }
164             }
165             if (selPos == -1) {
166                 this.table.deselectAll();
167             }
168             else {
169                 this.table.select(selPos);
170                 this.table.showSelection();
171             }
172         }
173 
174         public void dispose() {
175             if (table != null) {
176                 table.dispose();
177             }
178             doc = null;
179             queryExpr = null;
180         }
181         
182         private static String generateWSDL_ID(Element el) {
183             String localName = Utils.getLocalName(el);
184             if (localName.equals("element")) {
185                 return WSDLUtils.createElementIdentifier(el.getAttribute("name"));
186             }
187             if (localName.equals("complexType") 
188                     || localName.equals("simpleType")) {
189                 return WSDLUtils.createTypeIdentifier(el.getAttribute("name"));
190             }
191             if (localName.equals("interface") 
192             		|| localName.equals("portType")) {
193                 return WSDLUtils.createInterfaceIdentifier(el.getAttribute("name"));
194             }
195             if (localName.equals("fault")) {
196                 return WSDLUtils.createInterfaceFaultIdentifier(
197                         ((Element)el.getParentNode()).getAttribute("name"), 
198                         el.getAttribute("name"));
199             }
200             if (localName.equals("operation")) {
201                 return WSDLUtils.createOperationIdentifier(
202                         ((Element)el.getParentNode()).getAttribute("name"), 
203                         el.getAttribute("name"));
204             }
205 
206             if (localName.equals("attribute")) {
207                 return "wsdl.attributeDeclaration("+ el.getAttribute("name") + ")";
208             }
209             if (localName.equals("part")) {
210                 return "wsdl.messagePart("
211                         + ((Element)el.getParentNode()).getAttribute("name")
212                         + '/'
213                         + el.getAttribute("name")
214                         + ')';
215             }
216 
217             return "<" + el.getNodeName() + ">";
218         }
219 }
220 
221 /*
222  * $Log$
223  * Revision 1.6  2007/05/02 14:06:00  alex_simov
224  * invalid import fix
225  *
226  * Revision 1.5  2007/04/25 16:53:38  alex_simov
227  * no message
228  *
229  * Revision 1.4  2006/10/26 14:39:55  alex_simov
230  * bugfix[1584391]: the table now can be sorted on the items of its first column
231  *
232  * Revision 1.3  2006/10/04 14:27:39  alex_simov
233  * interface fault support added
234  *
235  * Revision 1.2  2006/07/19 11:27:40  alex_simov
236  * WSDL 1.1 annotation support added (this option is controlled by preference
237  *  pages)
238  *
239  * Revision 1.1  2006/07/07 13:00:41  alex_simov
240  * Mapping information panel added in the bottom of the main editor component
241  *
242  */