1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*******************************************************************************
* Copyright (c) 2001, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.viewers;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.wst.common.ui.dialogs.SelectSingleFileDialog;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.Import;
import org.eclipse.wst.wsdl.WSDLElement;
import org.eclipse.wst.wsdl.ui.internal.WSDLEditor;
import org.eclipse.wst.wsdl.ui.internal.WSDLEditorPlugin;
import org.eclipse.wst.wsdl.ui.internal.model.ModelAdapterListener;
import org.eclipse.wst.wsdl.ui.internal.model.WSDLModelAdapterFactory;
import org.eclipse.wst.wsdl.ui.internal.util.ComponentReferenceUtil;
import org.eclipse.wst.wsdl.ui.internal.util.WSDLEditorUtil;
import org.eclipse.wst.wsdl.ui.internal.util.XMLQuickScan;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class ImportViewer extends BaseViewer implements ModelAdapterListener
{
protected Composite control;
protected IEditorPart editorPart;
protected Text namespaceText;
protected Text prefixText;
protected Text locationText;
Button selectButton;
/**
* Constructor for ImportViewer.
* @param parent
* @param editorPart
*/
public ImportViewer(Composite parent, IEditorPart editorPart)
{
super(getStatusLineManager(editorPart));
this.editorPart = editorPart;
createControl(parent);
}
/**
* @see org.eclipse.wst.wsdl.ui.internal.viewers.NamedComponentViewer#getHeadingText()
*/
protected String getHeadingText()
{
return WSDLEditorPlugin.getWSDLString("_UI_LABEL_IMPORT"); //$NON-NLS-1$
}
public void createControl(Composite parent)
{
control = flatViewUtility.createComposite(parent, 1, true);
GridLayout layout = (GridLayout)control.getLayout();
layout.verticalSpacing = 0;
flatViewUtility.createFlatPageHeader(control, getHeadingText());
Composite c = flatViewUtility.createComposite(control, 1, true);
Label bogus = flatViewUtility.createLabel(c, 0, "");
GridData gd= new GridData();
gd.horizontalAlignment= GridData.FILL;
gd.grabExcessHorizontalSpace= true;
bogus.setLayoutData(gd);
Composite composite = flatViewUtility.createComposite(c, 2, true);
GridLayout layout2 = new GridLayout();
layout2.makeColumnsEqualWidth = false;
layout2.numColumns = 3;
layout2.marginHeight = 3;
composite.setLayout(layout2);
Label namespaceLabel = flatViewUtility.createLabel(composite, 0, WSDLEditorPlugin.getWSDLString("_UI_LABEL_NAMESPACE")); //$NON-NLS-1$
namespaceText = flatViewUtility.createTextField(composite);
namespaceText.setEnabled(false);
// place holder
Control con= flatViewUtility.createLabel(composite, 0, "");
gd = new GridData();
gd.grabExcessHorizontalSpace = false;
con.setLayoutData(gd);
Label prefixLabel = flatViewUtility.createLabel(composite, 0, WSDLEditorPlugin.getWSDLString("_UI_LABEL_PREFIX")); //$NON-NLS-1$
prefixText = flatViewUtility.createTextField(composite);
prefixText.addListener(SWT.Modify,this);
prefixText.setEnabled(false);
// place holder
Control con2= flatViewUtility.createLabel(composite, 0, "");
gd = new GridData();
gd.grabExcessHorizontalSpace = false;
con2.setLayoutData(gd);
Label locationLabel = flatViewUtility.createLabel(composite, 0, WSDLEditorPlugin.getWSDLString("_UI_LABEL_LOCATION")); //$NON-NLS-1$
locationText = flatViewUtility.createTextField(composite);
locationText.setEnabled(false);
selectButton = flatViewUtility.createPushButton(composite, "...");
gd = new GridData();
gd.horizontalAlignment = GridData.BEGINNING;
gd.grabExcessHorizontalSpace = false;
selectButton.setLayoutData(gd);
selectButton.addSelectionListener(this);
}
public void doSetInput(Object input)
{
setListenerEnabled(false);
try
{
Node node = WSDLEditorUtil.getInstance().getNodeForObject(input);
update();
}
finally
{
setListenerEnabled(true);
}
if (oldInput != null)
{
WSDLModelAdapterFactory.getWSDLModelAdapterFactory().removeModelAdapterListener(oldInput, this);
}
WSDLModelAdapterFactory.getWSDLModelAdapterFactory().addModelAdapterListener(input, this);
oldInput = input;
}
protected void handleEventHelper(Element element, Event event)
{
if (event.type == SWT.Modify)
{
}
}
protected void update()
{
Element element = ((WSDLElement)input).getElement();
flatViewUtility.updateFlatPageHeaderTitle(getHeadingText());
prefixText.setText("");
locationText.setText("");
namespaceText.setText(" ");
namespaceText.getParent().layout();
namespaceText.setText("");
String locationValue = element.getAttribute("location");
locationText.setText(locationValue != null ? locationValue : "");
String namespaceValue = element.getAttribute("namespace");
namespaceText.setText(namespaceValue != null ? namespaceValue : "");
String prefix = ((Import)input).getEnclosingDefinition().getPrefix(namespaceValue);
prefixText.setText(prefix != null ? prefix : "");
}
public Control getControl()
{
return control;
}
/*
* @see BaseWindow#doHandleEvent(Event)
*/
public void doHandleEvent(Event event)
{
// if (event.widget == prefixText)
// {
// String newPrefix = prefixText.getText();
// String errorMessage = ValidateHelper.checkXMLPrefix(newPrefix);
//
// if (errorMessage == null || errorMessage.length() == 0)
// {
// Import importObj = (Import)input;
// org.w3c.dom.Element importElement = WSDLUtil.getInstance().getElementForObject(importObj);
//
// Definition definition = importObj.getEnclosingDefinition();
// org.w3c.dom.Element definitionElement = WSDLUtil.getInstance().getElementForObject(definition);
//
// String nsFromModel = importObj.getNamespaceURI();
// String nsFromDOM = importElement.getAttribute("namespace");
// System.out.println(nsFromModel);
// System.out.println(nsFromDOM);
//
// String oldPrefix = definition.getPrefix(nsFromModel);
// definitionElement.removeAttribute("xmlns:" + oldPrefix);
// definitionElement.setAttribute("xmlns:" + newPrefix, nsFromModel);
// }
// }
}
public void propertyChanged(Object object, String property)
{
if (isListenerEnabled())
{
setListenerEnabled(false);
update();
setListenerEnabled(true);
}
}
public void widgetDefaultSelected(SelectionEvent e)
{
}
public void widgetSelected(SelectionEvent e)
{
if (e.widget == selectButton)
{
ResourceSet resourceSet = null;
// TODO: port check
resourceSet = ((org.eclipse.emf.ecore.EObject)input).eResource().getResourceSet();
WSDLEditor editor = (WSDLEditor)editorPart;
IFile currentWSDLFile = ((IFileEditorInput)editor.getEditorInput()).getFile();
SelectSingleFileDialog dialog = new SelectSingleFileDialog(WSDLEditorPlugin.getShell(), null, true);
String [] filters = { "xsd", "wsdl" };
IFile [] excludedFiles = { currentWSDLFile };
dialog.addFilterExtensions(filters, excludedFiles);
dialog.create();
dialog.getShell().setText(WSDLEditorPlugin.getWSDLString("_UI_TITLE_SELECT")); //$NON-NLS-1$
dialog.setTitle(WSDLEditorPlugin.getWSDLString("_UI_TITLE_SELECT_FILE")); //$NON-NLS-1$
dialog.setMessage(WSDLEditorPlugin.getWSDLString("_UI_DESCRIPTION_SELECT_WSDL_OR_XSD")); //$NON-NLS-1$
int rc = dialog.open();
if (rc == IDialogConstants.OK_ID)
{
IFile selectedFile = dialog.getFile();
//if (selectedFile.getLocation().toOSString().equals(currentWSDLFile.getLocation().toOSString()))
//{
// System.out.println("SAME FILE:" + currentWSDLFile.getLocation());
//}
String location = ComponentReferenceUtil.computeRelativeURI(selectedFile, currentWSDLFile, true);
Import importObj = (Import)input;
org.w3c.dom.Element importElement = WSDLEditorUtil.getInstance().getElementForObject(importObj);
Definition definition = importObj.getEnclosingDefinition();
org.w3c.dom.Element definitionElement = WSDLEditorUtil.getInstance().getElementForObject(definition);
String importTargetNamespace = "";
String prefix = prefixText.getText();
String uniquePrefix = "";
URI uri = URI.createPlatformResourceURI(selectedFile.getFullPath().toString());
// note that the getTargetNamespaceURIForSchema works for both schema and wsdl files
// I should change the name of this convenience method
importTargetNamespace = XMLQuickScan.getTargetNamespaceURIForSchema(uri.toString());
if (prefix.trim().equals(""))
{
uniquePrefix = getUniquePrefix(definition, uri.fileExtension());
}
else
{
uniquePrefix = prefix;
}
if (importTargetNamespace == null ||
(importTargetNamespace != null && importTargetNamespace.trim().length() == 0))
{
return; // what to do with no namespace docs?
}
importElement.setAttribute("location", location);
importElement.setAttribute("namespace", importTargetNamespace);
definitionElement.setAttribute("xmlns:" + uniquePrefix, importTargetNamespace);
namespaceText.setText(importTargetNamespace);
locationText.setText(location);
prefixText.setText(uniquePrefix);
}
}
}
private String getUniquePrefix(Definition definition, String initPrefix)
{
String uniquePrefix;
Map map = definition.getNamespaces();
if (definition.getNamespace(initPrefix) == null)
{
uniquePrefix = initPrefix;
}
else // if used, then try to create a unique one
{
String tempPrefix = initPrefix;
int i = 1;
while(map.containsKey(tempPrefix + i))
{
i++;
}
uniquePrefix = tempPrefix + i;
}
return uniquePrefix;
}
}
|