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.repository.ui;
26
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.eclipse.jface.action.*;
31 import org.eclipse.jface.dialogs.InputDialog;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.resource.JFaceResources;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.jface.wizard.IWizard;
36 import org.eclipse.jface.wizard.WizardDialog;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.dnd.*;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.*;
43 import org.eclipse.ui.*;
44 import org.eclipse.ui.internal.Workbench;
45 import org.omwg.ontology.Ontology;
46 import org.wsmo.common.*;
47 import org.wsmo.datastore.WsmoRepository;
48 import org.wsmo.factory.WsmoFactory;
49 import org.wsmo.mediator.Mediator;
50 import org.wsmo.service.Goal;
51 import org.wsmo.service.WebService;
52 import org.wsmostudio.repository.ExtensionManager;
53 import org.wsmostudio.repository.Registry;
54 import org.wsmostudio.repository.ui.wizards.ExportEntitiesWizard;
55 import org.wsmostudio.repository.ui.wizards.SaveEntityWizard;
56 import org.wsmostudio.runtime.*;
57 import org.wsmostudio.ui.GUIHelper;
58 import org.wsmostudio.ui.WsmoUIPlugin;
59 import org.wsmostudio.ui.editors.WSMOEditorInput;
60 import org.wsmostudio.ui.editors.model.ObservableModel;
61
62 /***
63 * A helper UI component which appears as a part of the <i>RepositoryEditor</i>
64 * component. Its task is to visualize a list of all available entities of certain
65 * type within a repository instance. The basic operations are addition, retrieval
66 * and removal of entities.
67 *
68 * @author not attributable
69 * @version $Revision: 1224 $ $Date: 2007-07-19 15:54:57 +0300 $
70 */
71
72 public class WSMOEntityContainer {
73
74 public static final byte ONTOLOGY_CONTENT = 1;
75 public static final byte MEDIATOR_CONTENT = 2;
76 public static final byte GOAL_CONTENT = 3;
77 public static final byte SERVICE_CONTENT = 4;
78
79 private Table entitiesList;
80 private WsmoRepository repository;
81 private byte entityType;
82
83 public WSMOEntityContainer(TabFolder parentFolder,
84 RepositoryEditor editor,
85 WsmoRepository repo,
86 String panelTitle,
87 byte contentType) {
88
89 this.entityType = contentType;
90 this.repository = repo;
91
92 TabItem mainItem = new TabItem(parentFolder, SWT.BORDER);
93 parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
94 mainItem.setText(panelTitle);
95 parentFolder.setLayout(new GridLayout(1, false));
96
97 createTable(parentFolder);
98 createContextMenu();
99
100 initDNDTarget(editor);
101
102 mainItem.setControl(entitiesList);
103 }
104
105 public void reloadContent() {
106 List<IRI> newIRIs = getContent();
107 TableItem[] allItems = entitiesList.getItems();
108
109
110 for(int i = 0; i < allItems.length; i++) {
111 Identifier id = (Identifier)allItems[i].getData();
112 if (newIRIs.contains(id)) {
113 newIRIs.remove(id);
114 }
115 else {
116 allItems[i].dispose();
117 }
118 }
119 for(Identifier newID : newIRIs) {
120 addEntry(newID);
121 }
122 entitiesList.redraw();
123 }
124
125 private void createTable(Composite parent) {
126 entitiesList = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
127 entitiesList.setLayoutData(new GridData(GridData.FILL_BOTH));
128 entitiesList.setLinesVisible(false);
129 for(Iterator it = getContent().iterator(); it.hasNext();) {
130 Identifier entityID = (Identifier)it.next();
131 addEntry(entityID);
132 }
133 }
134
135 private void initDNDTarget(final RepositoryEditor editor) {
136 DropTarget target = new DropTarget(entitiesList,
137 DND.DROP_COPY | DND.DROP_DEFAULT);
138
139 final FileTransfer fileTransfer = FileTransfer.getInstance();
140 target.setTransfer(new Transfer[] {fileTransfer});
141
142 target.addDropListener(new DropTargetAdapter() {
143 public void dragEnter(DropTargetEvent event) {
144 if (event.detail == DND.DROP_DEFAULT) {
145 if (fileTransfer.isSupportedType(event.currentDataType)) {
146
147 String[] fileNames = (String[])
148 fileTransfer.nativeToJava(event.currentDataType);
149 for(int i = 0; i < fileNames.length; i++) {
150 if (false == fileNames[i].toLowerCase().endsWith(".wsml")) {
151 event.detail = DND.DROP_NONE;
152 return;
153 }
154 }
155 }
156 event.detail = DND.DROP_COPY;
157 }
158 }
159 public void drop(DropTargetEvent event) {
160 if (fileTransfer.isSupportedType(event.currentDataType)) {
161 String[] names = (String[])event.data;
162 for (int i = 0; i < names.length; i++) {
163 editor.addResourceFromFile(names[i]);
164 }
165 }
166 }
167 });
168 }
169
170 public boolean containsEntry(Identifier entityRef) {
171 if (getItemForIdentifier(entityRef) != null) {
172 boolean confirm = MessageDialog.openConfirm(
173 PlatformUI.getWorkbench().getDisplay().getActiveShell(),
174 "Overwrite "+getTypeAsText(),
175 getTypeAsText() + " '"+ entityRef.toString()
176 +"' already exists in the repository!"
177 +"\nDo you want to overwrite it?");
178 if (false == confirm) {
179 return true;
180 }
181 else {
182 removeEntityFromRepository((IRI)entityRef);
183 getItemForIdentifier(entityRef).dispose();
184 }
185 }
186 return false;
187 }
188
189 public void addEntry(Identifier entityRef) {
190 TableItem item = new TableItem(entitiesList, SWT.None);
191 item.setData(entityRef);
192 item.setImage(getRelevantImage());
193 item.setText(entityRef.toString());
194 }
195
196 private void createContextMenu() {
197 final MenuManager menuMgr = new MenuManager();
198 menuMgr.setRemoveAllWhenShown(true);
199 menuMgr.addMenuListener(new IMenuListener() {
200 public void menuAboutToShow(IMenuManager mgr) {
201 fillContextMenu(menuMgr);
202 }
203 });
204 entitiesList.setMenu(menuMgr.createContextMenu(entitiesList));
205 }
206
207 private void fillContextMenu(MenuManager menuMgr) {
208
209 if (false == GUIHelper.containsCursor(entitiesList)) {
210 return;
211 }
212 if (entityType != MEDIATOR_CONTENT) {
213 menuMgr.add(new Action("Create " + getTypeAsText()) {
214 public void run() {
215 doCreateEntity(getText());
216 }
217 });
218 }
219 else {
220 menuMgr.add(new Action("Create OO Mediator") {
221 public void run() {
222 doCreateEntity(getText());
223 }
224 });
225 menuMgr.add(new Action("Create WW Mediator") {
226 public void run() {
227 doCreateEntity(getText());
228 }
229 });
230 menuMgr.add(new Action("Create WG Mediator") {
231 public void run() {
232 doCreateEntity(getText());
233 }
234 });
235 menuMgr.add(new Action("Create GG Mediator") {
236 public void run() {
237 doCreateEntity(getText());
238 }
239 });
240 }
241 menuMgr.add(new Separator());
242 menuMgr.add(new Action("Import from Workspace") {
243 public void run() {
244 doExportEntities();
245 }
246 });
247 TableItem[] sel = entitiesList.getSelection();
248 if (sel != null
249 && sel.length > 0
250 && GUIHelper.containsCursor(sel[0].getBounds(), entitiesList)) {
251 menuMgr.add(new Separator());
252 menuMgr.add(new Action("View " + getTypeAsText()) {
253 public void run() {
254 doOpenEntity();
255 }
256 });
257 menuMgr.add(new Separator());
258 menuMgr.add(new Action("Save in Workspace") {
259 public void run() {
260 doSaveEntity();
261 }
262 });
263 menuMgr.add(new Separator());
264 menuMgr.add(new Action("Remove " + getTypeAsText()) {
265 public void run() {
266 doRemoveEntity();
267 }
268 });
269
270 Registry.getInstance().getContentActionsManager().initActions(
271 menuMgr,
272 ExtensionManager.getRepositoryType(repository),
273 this.entityType,
274 repository,
275 (IRI)sel[0].getData());
276
277 }
278 }
279 public void dispose() {
280 entitiesList.dispose();
281 }
282
283 private List<IRI> getContent() {
284 switch (entityType) {
285 case ONTOLOGY_CONTENT:
286 return repository.listOntologies();
287 case MEDIATOR_CONTENT:
288 return repository.listMediators();
289 case GOAL_CONTENT:
290 return repository.listGoals();
291 case SERVICE_CONTENT:
292 return repository.listWebServices();
293 }
294 return null;
295 }
296
297 private String getTypeAsText() {
298 switch (entityType) {
299 case ONTOLOGY_CONTENT:
300 return "Ontology";
301 case MEDIATOR_CONTENT:
302 return "Mediator";
303 case GOAL_CONTENT:
304 return "Goal";
305 case SERVICE_CONTENT:
306 return "WebService";
307 }
308 return "";
309 }
310
311 private Image getRelevantImage() {
312 String iconType;
313 switch (entityType) {
314 case ONTOLOGY_CONTENT:
315 iconType = WsmoImageRegistry.ONTOLOGY_ICON;
316 break;
317 case MEDIATOR_CONTENT:
318 iconType = WsmoImageRegistry.OOMEDIATOR_ICON;
319 break;
320 case GOAL_CONTENT:
321 iconType = WsmoImageRegistry.GOAL_ICON;
322 break;
323 case SERVICE_CONTENT:
324 iconType = WsmoImageRegistry.WEBSERVICE_ICON;
325 break;
326 default:
327 return null;
328 }
329 return JFaceResources.getImageRegistry().get(iconType);
330 }
331
332 private TopEntity makeAndPersistEntity(WsmoFactory factory,
333 IRI id,
334 String actionName) {
335 switch (entityType) {
336 case ONTOLOGY_CONTENT:
337 Ontology onto = factory.createOntology(id);
338 repository.addOntology(onto);
339 return onto;
340 case MEDIATOR_CONTENT:
341 Mediator medi = null;
342 if (actionName.indexOf(" OO ") != -1) {
343 medi = factory.createOOMediator(id);
344 }
345 else if (actionName.indexOf(" WW ") != -1) {
346 medi = factory.createWWMediator(id);
347 }
348 else if (actionName.indexOf(" WG ") != -1) {
349 medi = factory.createWGMediator(id);
350 }
351 else {
352 medi = factory.createGGMediator(id);
353 }
354 repository.addMediator(medi);
355 return medi;
356 case GOAL_CONTENT:
357 Goal goal = factory.createGoal(id);
358 repository.addGoal(goal);
359 return goal;
360 case SERVICE_CONTENT:
361 WebService ws = factory.createWebService(id);
362 repository.addWebService(ws);
363 return ws;
364 }
365 return null;
366 }
367
368 private void removeEntityFromRepository(IRI id) {
369 switch (entityType) {
370 case ONTOLOGY_CONTENT:
371 repository.deleteOntology(id);
372 break;
373 case MEDIATOR_CONTENT:
374 repository.deleteMediator(id);
375 break;
376 case GOAL_CONTENT:
377 repository.deleteGoal(id);
378 break;
379 case SERVICE_CONTENT:
380 repository.deleteWebService(id);
381 break;
382 }
383
384 }
385
386 private void doOpenEntity() {
387 TableItem[] sel = entitiesList.getSelection();
388 TopEntity topEntity = null;
389 try {
390 topEntity = retrieveTopEntity(repository, (IRI)sel[0].getData(), entityType);
391 }
392 catch(Throwable anyError) {
393 MessageDialog.openError(entitiesList.getShell(),
394 "Repository Error",
395 anyError.getMessage());
396 LogManager.logError(anyError);
397 return;
398 }
399 if (topEntity == null) {
400 return;
401 }
402
403 try {
404 String targetEditorID = WsmoUIPlugin.getDefault()
405 .getExtensionManager()
406 .locateEditorForEntity(topEntity);
407 if (targetEditorID == null) {
408 return;
409 }
410
411 ObservableModel model = GUIHelper.createEditorModel(topEntity, targetEditorID);
412 WSMOEditorInput input = new WSMOEditorInput(topEntity, model);
413 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
414 .openEditor(input, targetEditorID);
415 model.setChanged();
416 }
417 catch(PartInitException e) {
418 LogManager.logError(e);
419 }
420
421 }
422
423 private void doSaveEntity() {
424 TableItem[] sel = entitiesList.getSelection();
425 IRI entityID = (IRI)sel[0].getData();
426 IWorkbenchWizard wizard = new SaveEntityWizard(
427 "Save "+getTypeAsText(),
428 repository,
429 entityID,
430 entityType);
431 wizard.init(Workbench.getInstance(), new StructuredSelection());
432 WizardDialog dialog = new WizardDialog(entitiesList.getShell(), wizard);
433 dialog.open();
434 }
435
436 private void doCreateEntity(String action) {
437 InputDialog iDialog = new InputDialog(
438 entitiesList.getShell(),
439 "New " + getTypeAsText(),
440 "Name:",
441 null, null);
442 iDialog.open();
443 String newEntityName = iDialog.getValue();
444 if (newEntityName == null
445 || newEntityName.trim().length() == 0) {
446 return;
447 }
448 WsmoFactory wsmoFactory = WSMORuntime.getRuntime().getWsmoFactory();
449 IRI entityRef = null;
450 try {
451 entityRef = wsmoFactory.createIRI(newEntityName);
452 }
453 catch(Exception iae) {
454 MessageDialog.openError(entitiesList.getShell(),
455 "Invalid Identifier",
456 iae.getMessage());
457 return;
458 }
459
460 if (getItemForIdentifier(entityRef) != null) {
461 MessageDialog.openError(entitiesList.getShell(),
462 "Name Clash",
463 "Entity name '"+newEntityName+"' already in use!");
464 return;
465 }
466
467 makeAndPersistEntity(wsmoFactory, entityRef, action);
468
469 addEntry(entityRef);
470 entitiesList.redraw();
471 }
472
473 private void doRemoveEntity() {
474 TableItem[] sel = entitiesList.getSelection();
475 IRI entityID = (IRI)sel[0].getData();
476 removeEntityFromRepository(entityID);
477 sel[0].dispose();
478 entitiesList.redraw();
479 }
480
481 private void doExportEntities() {
482 IWizard wizard = new ExportEntitiesWizard();
483 WizardDialog dialog = new WizardDialog(entitiesList.getShell(), wizard);
484 dialog.open();
485 }
486
487 private TableItem getItemForIdentifier(Identifier id) {
488 TableItem[] items = entitiesList.getItems();
489 for(int i = 0; i < items.length; i++) {
490 if (id.equals(items[i].getData())) {
491 return items[i];
492 }
493 }
494 return null;
495 }
496
497 public static TopEntity retrieveTopEntity(WsmoRepository repo, IRI id, byte eType) {
498 TopEntity result = null;
499 switch (eType) {
500 case WSMOEntityContainer.ONTOLOGY_CONTENT:
501 result = repo.getOntology(id);
502 break;
503 case WSMOEntityContainer.MEDIATOR_CONTENT:
504 result = repo.getMediator(id);
505 break;
506 case WSMOEntityContainer.GOAL_CONTENT:
507 result = repo.getGoal(id);
508 break;
509 case WSMOEntityContainer.SERVICE_CONTENT:
510 result = repo.getWebService(id);
511 }
512 return result;
513 }
514 }
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572