1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
26
27 import org.eclipse.jface.resource.JFaceResources;
28 import org.eclipse.jface.viewers.*;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.*;
31 import org.eclipse.swt.graphics.*;
32 import org.eclipse.swt.layout.*;
33 import org.eclipse.swt.widgets.*;
34 import org.w3c.dom.Element;
35 import org.wsmostudio.grounding.sawsdl.ui.editor.*;
36 import org.wsmostudio.runtime.WsmoImageRegistry;
37 import org.wsmostudio.ui.editors.common.IWSMOSelectionValidator;
38
39 import com.ontotext.wsmo4j.grounding.sawsdl.WSDLUtils;
40
41 public class WSDLElementChooser {
42
43 private TreeViewer treeViewer;
44 private Shell shell;
45 private String result = null;
46 private IWSMOSelectionValidator filter;
47
48 public static WSDLElementChooser createInputsChooser(Shell parentShell,
49 Object rootElement) {
50 WSDLElementChooser chooser = new WSDLElementChooser(parentShell,
51 rootElement,
52 WSDLTreeContentProvider.INPUT_MSG_MASK);
53 chooser.setFilter(new IWSMOSelectionValidator() {
54 public String isValid(Object selection) {
55 if (selection instanceof Element
56 && false == Utils.getLocalName((Element)selection).equals("input")) {
57 return "Please select an Input message";
58 }
59 return null;
60 }
61 });
62 return chooser;
63 }
64 public static WSDLElementChooser createOutputsChooser(Shell parentShell,
65 Object rootElement) {
66 WSDLElementChooser chooser = new WSDLElementChooser(parentShell,
67 rootElement,
68 WSDLTreeContentProvider.OUTPUT_MSG_MASK);
69 chooser.setFilter(new IWSMOSelectionValidator() {
70 public String isValid(Object selection) {
71 if (selection instanceof Element
72 && false == Utils.getLocalName((Element)selection).equals("output")) {
73 return "Please select an Output message";
74 }
75 return null;
76 }
77 });
78 return chooser;
79 }
80
81 public WSDLElementChooser(Shell parentShell, Object rootElement, int contentMask) {
82 this(parentShell, SWT.SINGLE, rootElement, new WSDLTreeContentProvider(contentMask));
83 }
84
85 public WSDLElementChooser(Shell parentShell,
86 int treeStyle,
87 Object rootElement,
88 ITreeContentProvider contentProvider) {
89
90 shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
91 Image icon = JFaceResources.getImageRegistry().get(WsmoImageRegistry.DEFAULT_WINDOW_ICON);
92 shell.setImage(icon);
93 shell.setText("Choose WSDL Element");
94 shell.setSize(450, 450);
95 shell.setLayout(new GridLayout(1, false));
96
97 treeViewer = new TreeViewer(shell, treeStyle);
98
99 treeViewer.setContentProvider(contentProvider);
100 WSDLTreeLabelProvider labProvider = new WSDLTreeLabelProvider(null);
101 labProvider.setShowExtensionInfo(false);
102 treeViewer.setLabelProvider(labProvider);
103 treeViewer.setInput(rootElement);
104 treeViewer.getTree().addMouseListener(new MouseAdapter() {
105 public void mouseDoubleClick(MouseEvent e) {
106 TreeItem[] sel = treeViewer.getTree().getSelection();
107 if (!checkSelectionData()) {
108 if (sel[0].getItems().length > 0) {
109 sel[0].setExpanded(!sel[0].getExpanded());
110 treeViewer.refresh(sel[0].getData());
111 }
112 return;
113 }
114 generateResult();
115 shell.dispose();
116 }
117
118 public void mouseUp(MouseEvent e) {
119 if (e.button != 3) {
120 return;
121 }
122
123 }
124 });
125 treeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
126
127 createControlArea(shell);
128
129 treeViewer.getTree().addSelectionListener(new SelectionAdapter() {
130 public void widgetSelected(SelectionEvent e) {
131 checkSelectionData();
132 }
133 });
134 Point pLocation = parentShell.getLocation();
135 Point pSize = parentShell.getSize();
136 shell.setLocation(pLocation.x + pSize.x / 2 - shell.getSize().x / 2,
137 pLocation.y + pSize.y / 2 - shell.getSize().y / 2);
138 }
139
140 public void setDialogTitle(String title) {
141 shell.setText(title);
142 }
143
144 private void createControlArea(Composite mainContainer) {
145 Composite buttons = new Composite(mainContainer, SWT.NONE);
146 buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
147 FillLayout layout = new FillLayout(SWT.HORIZONTAL);
148 layout.spacing = 3;
149 buttons.setLayout(layout);
150
151 Button okButton = new Button(buttons, SWT.PUSH);
152 okButton.setText("Select");
153 okButton.setEnabled(false);
154 okButton.addSelectionListener(new SelectionAdapter() {
155 public void widgetSelected(SelectionEvent e) {
156 generateResult();
157 shell.dispose();
158 }
159 });
160 shell.setDefaultButton(okButton);
161
162 Button noButton = new Button(buttons, SWT.PUSH);
163 noButton.setText("Cancel");
164 noButton.addSelectionListener(new SelectionAdapter() {
165 public void widgetSelected(SelectionEvent e) {
166 shell.dispose();
167 }
168 });
169 }
170
171 private void generateResult() {
172 Element selection = (Element)
173 ((StructuredSelection)treeViewer.getSelection()).getFirstElement();
174 String localName = Utils.getLocalName(selection);
175 if (localName.equals("element")) {
176 result = WSDLUtils.ELEMENT_DECLARATION_NS + '(' + selection.getAttribute("name") + ')';
177 }
178 else if (localName.equals("complexType")) {
179 result = WSDLUtils.TYPE_DEFINITION_NS + '(' + selection.getAttribute("name") + ')';
180 }
181 else if (localName.equals("interface")) {
182 result = WSDLUtils.INTERFACE_NS + '(' + selection.getAttribute("name") + ')';
183 }
184 else if (localName.equals("operation")) {
185 result = WSDLUtils.OPERATION_NS + '('
186 + ((Element)selection.getParentNode()).getAttribute("name")
187 + '/' + selection.getAttribute("name") + ')';
188 }
189 else if (localName.equals("input")
190 || localName.equals("output")) {
191 result = WSDLUtils.MESSAGE_NS + '('
192 + ((Element)selection.getParentNode()).getAttribute("name")
193 + '/' + ((Element)selection.getParentNode()).getAttribute("name")
194 + '/' + selection.getAttribute("messageLabel") + ')';
195 }
196 }
197
198 public String open() {
199 shell.open();
200 treeViewer.expandAll();
201 Display display = shell.getDisplay();
202 while (!shell.isDisposed()) {
203 if (!display.readAndDispatch()) {
204 display.sleep();
205 }
206 }
207 return result;
208 }
209
210 public void dispose() {
211 treeViewer.getTree().dispose();
212 shell.dispose();
213 result = null;
214 }
215
216 public void setFilter(IWSMOSelectionValidator filter) {
217 this.filter = filter;
218 }
219
220 private boolean checkSelectionData() {
221 if (filter == null) {
222 shell.getDefaultButton().setEnabled(true);
223 return true;
224 }
225 TreeItem[] sel = treeViewer.getTree().getSelection();
226 String error = null;
227 if (sel.length > 0) {
228 for (int i = 0; i < sel.length; i++) {
229 error = filter.isValid(sel[i].getData());
230 if (error != null && error.trim().length() > 0) {
231 break;
232 }
233 }
234 }
235 shell.getDefaultButton().setEnabled(error == null
236 || error.trim().length() == 0);
237 return shell.getDefaultButton().isEnabled();
238 }
239
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269