7b1b0f600b25cd1cf24605387297d94c53b0cc74
[platform/upstream/iotivity.git] /
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.view;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Set;
22
23 import oic.simulator.serviceprovider.Activator;
24 import oic.simulator.serviceprovider.listener.IPropertiesChangedListener;
25 import oic.simulator.serviceprovider.listener.IResourceListChangedListener;
26 import oic.simulator.serviceprovider.manager.ResourceManager;
27 import oic.simulator.serviceprovider.manager.UiListenerHandler;
28 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
29 import oic.simulator.serviceprovider.model.CollectionResource;
30 import oic.simulator.serviceprovider.model.Device;
31 import oic.simulator.serviceprovider.model.Resource;
32 import oic.simulator.serviceprovider.model.ResourceType;
33 import oic.simulator.serviceprovider.model.SingleResource;
34 import oic.simulator.serviceprovider.utils.Constants;
35 import oic.simulator.serviceprovider.utils.Utility;
36 import oic.simulator.serviceprovider.view.dialogs.AddResourceToCollections;
37 import oic.simulator.serviceprovider.view.dialogs.AddResourceToDevices;
38 import oic.simulator.serviceprovider.view.dialogs.AddResources;
39 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
40 import oic.simulator.serviceprovider.view.dialogs.CreateResourceWizard;
41 import oic.simulator.serviceprovider.view.dialogs.DeleteResourceWizard;
42 import oic.simulator.serviceprovider.view.dialogs.MainPage.Option;
43 import oic.simulator.serviceprovider.view.dialogs.RemoveResourceFromCollections;
44 import oic.simulator.serviceprovider.view.dialogs.RemoveResourceFromDevices;
45 import oic.simulator.serviceprovider.view.dialogs.RemoveResources;
46 import oic.simulator.serviceprovider.view.dialogs.ResourceWizardDialog;
47
48 import org.eclipse.jface.dialogs.MessageDialog;
49 import org.eclipse.jface.viewers.ISelectionChangedListener;
50 import org.eclipse.jface.viewers.IStructuredSelection;
51 import org.eclipse.jface.viewers.ITreeContentProvider;
52 import org.eclipse.jface.viewers.LabelProvider;
53 import org.eclipse.jface.viewers.SelectionChangedEvent;
54 import org.eclipse.jface.viewers.StructuredSelection;
55 import org.eclipse.jface.viewers.TreeViewer;
56 import org.eclipse.jface.viewers.Viewer;
57 import org.eclipse.jface.window.Window;
58 import org.eclipse.jface.wizard.WizardDialog;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.custom.CTabFolder;
61 import org.eclipse.swt.custom.CTabItem;
62 import org.eclipse.swt.events.MenuAdapter;
63 import org.eclipse.swt.events.MenuEvent;
64 import org.eclipse.swt.events.SelectionAdapter;
65 import org.eclipse.swt.events.SelectionEvent;
66 import org.eclipse.swt.graphics.Color;
67 import org.eclipse.swt.graphics.Image;
68 import org.eclipse.swt.layout.GridData;
69 import org.eclipse.swt.layout.GridLayout;
70 import org.eclipse.swt.widgets.Button;
71 import org.eclipse.swt.widgets.Composite;
72 import org.eclipse.swt.widgets.Display;
73 import org.eclipse.swt.widgets.Group;
74 import org.eclipse.swt.widgets.Menu;
75 import org.eclipse.swt.widgets.MenuItem;
76 import org.eclipse.swt.widgets.Tree;
77 import org.eclipse.swt.widgets.TreeItem;
78 import org.eclipse.ui.PlatformUI;
79 import org.eclipse.ui.dialogs.FilteredTree;
80 import org.eclipse.ui.dialogs.PatternFilter;
81 import org.eclipse.ui.part.ViewPart;
82 import org.oic.simulator.SimulatorException;
83 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
84
85 /**
86  * This class manages and shows the resource manager view in the perspective.
87  */
88 public class ResourceManagerView extends ViewPart {
89
90     public static final String           VIEW_ID = "oic.simulator.serviceprovider.view.resourcemanager";
91
92     private Button                       createButton;
93     private Button                       deleteButton;
94
95     private TreeViewer                   singleResTreeViewer;
96     private TreeViewer                   collectionResTreeViewer;
97     private TreeViewer                   deviceTreeViewer;
98
99     private CTabFolder                   folder;
100     private CTabItem                     singleResTab;
101     private CTabItem                     collectionResTab;
102     private CTabItem                     deviceTab;
103
104     private IResourceListChangedListener resourceListChangedListener;
105
106     private IPropertiesChangedListener   resourcePropertiesChangedListener;
107
108     private ResourceManager              resourceManager;
109
110     public ResourceManagerView() {
111
112         resourceManager = Activator.getDefault().getResourceManager();
113
114         resourceListChangedListener = new IResourceListChangedListener() {
115
116             @Override
117             public void onResourceCreation(final ResourceType type) {
118                 Display.getDefault().asyncExec(new Runnable() {
119
120                     @Override
121                     public void run() {
122                         if (type == ResourceType.SINGLE) {
123                             if (null != singleResTreeViewer) {
124                                 singleResTreeViewer.refresh();
125                             }
126                         } else if (type == ResourceType.COLLECTION) {
127                             if (null != collectionResTreeViewer) {
128                                 collectionResTreeViewer.refresh();
129                             }
130                         } else {
131                             if (null != deviceTreeViewer) {
132                                 deviceTreeViewer.refresh();
133                             }
134                         }
135
136                         // Trigger the visibility of delete button
137                         changeDeleteVisibility();
138                     }
139                 });
140             }
141
142             @Override
143             public void onResourceDeletion(final ResourceType type) {
144                 Display.getDefault().asyncExec(new Runnable() {
145
146                     @Override
147                     public void run() {
148                         if (null != singleResTreeViewer
149                                 && null != collectionResTreeViewer
150                                 && null != deviceTreeViewer) {
151                             switch (type) {
152                                 case SINGLE:
153                                     singleResTreeViewer.refresh();
154                                 case COLLECTION:
155                                     collectionResTreeViewer.refresh();
156                                 default:
157                                     deviceTreeViewer.refresh();
158                             }
159                         }
160                         // Trigger the visibility of delete button
161                         changeDeleteVisibility();
162                     }
163                 });
164             }
165
166             @Override
167             public void onResourceListUpdate(final ResourceType type) {
168                 Display.getDefault().asyncExec(new Runnable() {
169
170                     @Override
171                     public void run() {
172                         if (null != singleResTreeViewer
173                                 && null != collectionResTreeViewer
174                                 && null != deviceTreeViewer) {
175                             switch (type) {
176                                 case SINGLE:
177                                     singleResTreeViewer.refresh();
178                                 case COLLECTION:
179                                     collectionResTreeViewer.refresh();
180                                 default:
181                                     deviceTreeViewer.refresh();
182                             }
183                         }
184                     }
185                 });
186             }
187         };
188
189         resourcePropertiesChangedListener = new IPropertiesChangedListener() {
190
191             @Override
192             public void onResourcePropertyChange() {
193                 Display.getDefault().asyncExec(new Runnable() {
194                     @Override
195                     public void run() {
196                         if (null != singleResTreeViewer
197                                 && null != collectionResTreeViewer
198                                 && null != deviceTreeViewer) {
199                             singleResTreeViewer.refresh();
200                             collectionResTreeViewer.refresh();
201                             deviceTreeViewer.refresh();
202                         }
203                     }
204                 });
205             }
206
207             @Override
208             public void onDevicePropertyChange() {
209                 Display.getDefault().asyncExec(new Runnable() {
210                     @Override
211                     public void run() {
212                         if (null != deviceTreeViewer) {
213                             deviceTreeViewer.refresh();
214                         }
215                     }
216                 });
217             }
218         };
219     }
220
221     public void changeDeleteVisibility() {
222         boolean visibility = resourceManager.isAnyResourceExist();
223         if (null != deleteButton && !deleteButton.isDisposed()) {
224             deleteButton.setEnabled(visibility);
225         }
226     }
227
228     @Override
229     public void createPartControl(Composite parent) {
230         Composite compContent = new Composite(parent, SWT.NONE);
231         GridLayout baseLayout = new GridLayout(1, false);
232         compContent.setLayout(baseLayout);
233
234         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
235         compContent.setLayoutData(gd);
236
237         Composite buttonComp = new Composite(compContent, SWT.NONE);
238         buttonComp.setLayout(new GridLayout(2, false));
239
240         gd = new GridData();
241         gd.horizontalAlignment = SWT.FILL;
242         gd.grabExcessHorizontalSpace = true;
243
244         buttonComp.setLayoutData(gd);
245
246         createButton = new Button(buttonComp, SWT.PUSH);
247         createButton.setText("Create");
248         createButton.setToolTipText("Create Simulator Resource(s)");
249
250         gd = new GridData();
251         gd.widthHint = 90;
252         createButton.setLayoutData(gd);
253
254         deleteButton = new Button(buttonComp, SWT.PUSH);
255         deleteButton.setText("Delete");
256         deleteButton.setToolTipText("Delete Simulator Resource(s)");
257
258         gd = new GridData();
259         gd.widthHint = 90;
260         deleteButton.setLayoutData(gd);
261
262         // Create a Tab Folder.
263         folder = new CTabFolder(compContent, SWT.BORDER);
264         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
265         folder.setLayoutData(gd);
266         folder.setSimple(false);
267         folder.setUnselectedCloseVisible(false);
268         folder.setUnselectedImageVisible(false);
269         folder.addSelectionListener(new SelectionAdapter() {
270             @Override
271             public void widgetSelected(SelectionEvent e) {
272                 // Tab is switched.
273                 singleResTreeViewer.setSelection(null);
274                 collectionResTreeViewer.setSelection(null);
275                 deviceTreeViewer.setSelection(null);
276                 resourceManager.resourceSelectionChanged(null);
277             }
278         });
279
280         createSimpleResourcesArea();
281
282         createCollectionResourcesArea();
283
284         createDeviceArea();
285
286         folder.setSelection(singleResTab);
287
288         addUIListeners();
289
290         addManagerListeners();
291
292         // If there is at least one resource exist, then enable the delete
293         // resource button
294         changeDeleteVisibility();
295     }
296
297     private void createSimpleResourcesArea() {
298         singleResTab = new CTabItem(folder, SWT.NULL);
299         singleResTab.setText("Simple Resources");
300
301         // Create a group to show all the discovered resources.
302         // Adding the group to the folder.
303         Group resourceGroup = new Group(folder, SWT.NONE);
304         // resourceGroup.setText("Discovered Resources");
305
306         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
307         resourceGroup.setBackground(color);
308
309         resourceGroup.setLayout(new GridLayout(1, false));
310         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
311         resourceGroup.setLayoutData(gd);
312
313         PatternFilter filter = new PatternFilter();
314         FilteredTree filteredTree = new FilteredTree(resourceGroup,
315                 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
316         singleResTreeViewer = filteredTree.getViewer();
317         singleResTreeViewer.getTree().setLayoutData(
318                 new GridData(SWT.FILL, SWT.FILL, true, true));
319         singleResTreeViewer
320                 .setContentProvider(new SingleResourceContentProvider());
321         singleResTreeViewer.setLabelProvider(new TreeLabelProvider());
322         singleResTreeViewer.setInput(new Object());
323
324         singleResTreeViewer
325                 .addSelectionChangedListener(new ISelectionChangedListener() {
326                     @Override
327                     public void selectionChanged(SelectionChangedEvent e) {
328                         IStructuredSelection selection = (IStructuredSelection) e
329                                 .getSelection();
330                         if (null == selection) {
331                             return;
332                         }
333
334                         Resource res = null;
335                         int size = selection.size();
336                         if (size == 1) {
337                             res = (Resource) selection.getFirstElement();
338                         }
339                         resourceManager.resourceSelectionChanged(res);
340                     }
341                 });
342
343         // TODO: Add menu items
344         addMenuToSimpleResources();
345
346         singleResTab.setControl(resourceGroup);
347     }
348
349     private void addMenuToSimpleResources() {
350         if (null != singleResTreeViewer) {
351             final Tree resourceTreeHead = singleResTreeViewer.getTree();
352             if (null != resourceTreeHead) {
353                 // Below code creates menu entries and shows them on right
354                 // clicking a resource
355                 final Menu menu = new Menu(resourceTreeHead);
356                 resourceTreeHead.setMenu(menu);
357                 menu.addMenuListener(new MenuAdapter() {
358                     @Override
359                     public void menuShown(MenuEvent e) {
360                         // Clear existing menu items
361                         MenuItem[] items = menu.getItems();
362                         for (int index = 0; index < items.length; index++) {
363                             items[index].dispose();
364                         }
365
366                         IStructuredSelection selection = ((IStructuredSelection) singleResTreeViewer
367                                 .getSelection());
368                         final SingleResource resource = (SingleResource) selection
369                                 .getFirstElement();
370                         if (null == resource) {
371                             return;
372                         }
373
374                         addAutomationMenu(menu, resource);
375
376                         // Menu to add resource to one or more collections.
377                         MenuItem addToCollection = new MenuItem(menu, SWT.NONE);
378                         addToCollection.setText("Add to Collection");
379                         addToCollection
380                                 .addSelectionListener(new SelectionAdapter() {
381                                     @Override
382                                     public void widgetSelected(SelectionEvent e) {
383                                         List<CollectionResource> possibleCollections;
384                                         possibleCollections = resourceManager
385                                                 .getCollectionsForAddingToSingleResource(resource);
386                                         if (null == possibleCollections
387                                                 || possibleCollections
388                                                         .isEmpty()) {
389                                             MessageDialog
390                                                     .openError(
391                                                             Display.getDefault()
392                                                                     .getActiveShell(),
393                                                             "No possible candidates",
394                                                             "There are no possible collections to which the resource can be added.");
395                                             return;
396                                         }
397
398                                         AddResourceToCollections addToCollectionsDlg = new AddResourceToCollections(
399                                                 Display.getDefault()
400                                                         .getActiveShell(),
401                                                 possibleCollections);
402                                         if (addToCollectionsDlg.open() != Window.OK) {
403                                             return;
404                                         }
405
406                                         Set<CollectionResource> resultSet = addToCollectionsDlg
407                                                 .getSelectedResourceList();
408
409                                         int addedCount = resourceManager
410                                                 .addResourceToCollection(
411                                                         resultSet, resource);
412                                         if (addedCount <= 0) {
413                                             MessageDialog
414                                                     .openInformation(Display
415                                                             .getDefault()
416                                                             .getActiveShell(),
417                                                             "Operation failed",
418                                                             "Failed to add resources to the collection.");
419                                             return;
420                                         }
421
422                                         collectionResTreeViewer.refresh();
423
424                                         deviceTreeViewer.refresh();
425
426                                         MessageDialog.openInformation(
427                                                 Display.getDefault()
428                                                         .getActiveShell(),
429                                                 "Added",
430                                                 "["
431                                                         + addedCount
432                                                         + "/"
433                                                         + resultSet.size()
434                                                         + "] added to selected collections");
435                                     }
436                                 });
437
438                         // Menu to remove resource from one or more collections
439                         // to which it is a member.
440                         MenuItem removeFromCollection = new MenuItem(menu,
441                                 SWT.NONE);
442                         removeFromCollection.setText("Remove from Collection");
443                         removeFromCollection
444                                 .addSelectionListener(new SelectionAdapter() {
445                                     @Override
446                                     public void widgetSelected(SelectionEvent e) {
447                                         List<CollectionResource> possibleCollectionstoRemoveFrom;
448                                         possibleCollectionstoRemoveFrom = resourceManager
449                                                 .getResourceReferences(resource);
450                                         if (null == possibleCollectionstoRemoveFrom
451                                                 || possibleCollectionstoRemoveFrom
452                                                         .isEmpty()) {
453                                             MessageDialog
454                                                     .openError(
455                                                             Display.getDefault()
456                                                                     .getActiveShell(),
457                                                             "No possible candidates",
458                                                             "Resource has not been added to any collections.");
459                                             return;
460                                         }
461
462                                         RemoveResourceFromCollections removeFromCollectionsDlg = new RemoveResourceFromCollections(
463                                                 Display.getDefault()
464                                                         .getActiveShell(),
465                                                 possibleCollectionstoRemoveFrom);
466                                         if (removeFromCollectionsDlg.open() != Window.OK) {
467                                             return;
468                                         }
469
470                                         Set<CollectionResource> resultSet = removeFromCollectionsDlg
471                                                 .getSelectedResourceList();
472
473                                         int removeCount = resourceManager
474                                                 .removeResourceFromCollection(
475                                                         resultSet, resource);
476                                         if (removeCount <= 0) {
477                                             MessageDialog
478                                                     .openInformation(Display
479                                                             .getDefault()
480                                                             .getActiveShell(),
481                                                             "Operation failed",
482                                                             "Failed to remove resources from the collection.");
483                                             return;
484                                         }
485
486                                         collectionResTreeViewer.refresh();
487
488                                         deviceTreeViewer.refresh();
489
490                                         MessageDialog.openInformation(
491                                                 Display.getDefault()
492                                                         .getActiveShell(),
493                                                 "Removed",
494                                                 "["
495                                                         + removeCount
496                                                         + "/"
497                                                         + resultSet.size()
498                                                         + "] resource removed from selected collections");
499                                     }
500                                 });
501
502                         // Menu to add resource to one or more devices.
503                         MenuItem addToDevice = new MenuItem(menu, SWT.NONE);
504                         addToDevice.setText("Add to Device");
505                         addToDevice
506                                 .addSelectionListener(new SelectionAdapter() {
507                                     @Override
508                                     public void widgetSelected(SelectionEvent e) {
509                                         List<Device> possibleDevices;
510                                         possibleDevices = resourceManager
511                                                 .getDevicesForAddingToResource(resource);
512                                         if (null == possibleDevices
513                                                 || possibleDevices.isEmpty()) {
514                                             MessageDialog
515                                                     .openError(
516                                                             Display.getDefault()
517                                                                     .getActiveShell(),
518                                                             "No possible candidates",
519                                                             "There are no possible devices to which the resource can be added.");
520                                             return;
521                                         }
522
523                                         AddResourceToDevices addToDevicesDlg = new AddResourceToDevices(
524                                                 Display.getDefault()
525                                                         .getActiveShell(),
526                                                 possibleDevices);
527                                         if (addToDevicesDlg.open() != Window.OK) {
528                                             return;
529                                         }
530
531                                         Set<Device> resultSet = addToDevicesDlg
532                                                 .getSelectedDeviceList();
533
534                                         resourceManager.addResourceToDevice(
535                                                 resultSet, resource);
536
537                                         deviceTreeViewer.refresh();
538
539                                         MessageDialog
540                                                 .openInformation(Display
541                                                         .getDefault()
542                                                         .getActiveShell(),
543                                                         "Added",
544                                                         "Resource added to selected devices.");
545                                     }
546                                 });
547
548                         // Menu to remove resource from one or more device to
549                         // which it is a member.
550                         MenuItem removeFromDevice = new MenuItem(menu, SWT.NONE);
551                         removeFromDevice.setText("Remove from Device");
552                         removeFromDevice
553                                 .addSelectionListener(new SelectionAdapter() {
554                                     @Override
555                                     public void widgetSelected(SelectionEvent e) {
556                                         List<Device> possibleDevices;
557                                         possibleDevices = resourceManager
558                                                 .getDeviceReferences(resource);
559                                         if (null == possibleDevices
560                                                 || possibleDevices.isEmpty()) {
561                                             MessageDialog
562                                                     .openError(
563                                                             Display.getDefault()
564                                                                     .getActiveShell(),
565                                                             "No possible candidates",
566                                                             "Resource has not been added to any devices.");
567                                             return;
568                                         }
569
570                                         RemoveResourceFromDevices removeFromDevicesDlg = new RemoveResourceFromDevices(
571                                                 Display.getDefault()
572                                                         .getActiveShell(),
573                                                 possibleDevices);
574                                         if (removeFromDevicesDlg.open() != Window.OK) {
575                                             return;
576                                         }
577
578                                         Set<Device> resultSet = removeFromDevicesDlg
579                                                 .getSelectedDeviceList();
580
581                                         resourceManager
582                                                 .removeResourceFromDevice(
583                                                         resultSet, resource);
584
585                                         deviceTreeViewer.refresh();
586
587                                         MessageDialog
588                                                 .openInformation(Display
589                                                         .getDefault()
590                                                         .getActiveShell(),
591                                                         "Removed",
592                                                         "Resource removed from selected devices");
593                                     }
594                                 });
595
596                         // Menu to remove resource from one or more device to
597                         // which it is a member.
598                         MenuItem deleteResource = new MenuItem(menu, SWT.NONE);
599                         deleteResource.setText("Delete");
600                         deleteResource
601                                 .addSelectionListener(new SelectionAdapter() {
602                                     @Override
603                                     public void widgetSelected(SelectionEvent e) {
604                                         try {
605                                             resourceManager
606                                                     .removeResource(resource);
607                                             singleResTreeViewer.refresh();
608                                             // if(refreshCollectionTree)
609                                             collectionResTreeViewer.refresh();
610                                             // if(refreshDeviceTree)
611                                             deviceTreeViewer.refresh();
612
613                                             resourceManager
614                                                     .resourceSelectionChanged(null);
615
616                                             MessageDialog.openInformation(
617                                                     Display.getDefault()
618                                                             .getActiveShell(),
619                                                     "Deleted",
620                                                     "Resource deleted.");
621                                         } catch (SimulatorException e1) {
622                                             MessageDialog
623                                                     .openInformation(Display
624                                                             .getDefault()
625                                                             .getActiveShell(),
626                                                             "Deletion Failed",
627                                                             "Failed to delete the resource.");
628                                         }
629                                         changeDeleteVisibility();
630                                     }
631                                 });
632                     }
633                 });
634             }
635         }
636     }
637
638     private void createCollectionResourcesArea() {
639         collectionResTab = new CTabItem(folder, SWT.NULL);
640         collectionResTab.setText("Collection Resources");
641
642         // Create a group to show all the discovered resources.
643         // Adding the group to the folder.
644         Group resourceGroup = new Group(folder, SWT.NONE);
645         // resourceGroup.setText("Discovered Resources");
646
647         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
648         resourceGroup.setBackground(color);
649
650         resourceGroup.setLayout(new GridLayout(1, false));
651         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
652         resourceGroup.setLayoutData(gd);
653
654         PatternFilter filter = new PatternFilter();
655         FilteredTree filteredTree = new FilteredTree(resourceGroup,
656                 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
657         collectionResTreeViewer = filteredTree.getViewer();
658         collectionResTreeViewer.getTree().setLayoutData(
659                 new GridData(SWT.FILL, SWT.FILL, true, true));
660         collectionResTreeViewer
661                 .setContentProvider(new CollectionResourceContentProvider());
662         collectionResTreeViewer.setLabelProvider(new TreeLabelProvider());
663         collectionResTreeViewer.setInput(new Object());
664
665         collectionResTreeViewer
666                 .addSelectionChangedListener(new ISelectionChangedListener() {
667                     @Override
668                     public void selectionChanged(SelectionChangedEvent e) {
669                         IStructuredSelection selection = (IStructuredSelection) e
670                                 .getSelection();
671                         if (null == selection) {
672                             return;
673                         }
674
675                         Resource res = null;
676                         int size = selection.size();
677                         if (size == 1) {
678                             res = (Resource) selection.getFirstElement();
679                         }
680                         resourceManager.resourceSelectionChanged(res);
681                     }
682                 });
683
684         // TODO: Add menu items
685         addMenuToCollectionResources();
686
687         collectionResTab.setControl(resourceGroup);
688     }
689
690     private void addMenuToCollectionResources() {
691         if (null != collectionResTreeViewer) {
692             final Tree resourceTreeHead = collectionResTreeViewer.getTree();
693             if (null != resourceTreeHead) {
694                 // Below code creates menu entries and shows them on right
695                 // clicking a resource
696                 final Menu menu = new Menu(resourceTreeHead);
697                 resourceTreeHead.setMenu(menu);
698                 menu.addMenuListener(new MenuAdapter() {
699                     @Override
700                     public void menuShown(MenuEvent e) {
701                         // Clear existing menu items
702                         MenuItem[] items = menu.getItems();
703                         for (int index = 0; index < items.length; index++) {
704                             items[index].dispose();
705                         }
706
707                         IStructuredSelection selection = ((IStructuredSelection) collectionResTreeViewer
708                                 .getSelection());
709                         final Resource resource = (Resource) selection
710                                 .getFirstElement();
711                         TreeItem[] treeItems = resourceTreeHead.getSelection();
712                         if (null == resource || null == treeItems) {
713                             return;
714                         }
715
716                         final TreeItem parent = treeItems[0].getParentItem();
717                         if (null == parent) {
718                             System.out.println("No parent exist");
719                             // Selected item is a top-level collection
720                             addMenuToTopLevelCollectionResources(menu,
721                                     (CollectionResource) resource);
722                         } else {
723                             if (resource instanceof SingleResource)
724                                 addAutomationMenu(menu,
725                                         (SingleResource) resource);
726
727                             MenuItem remFromParent = new MenuItem(menu,
728                                     SWT.NONE);
729                             remFromParent.setText("Remove from Parent");
730                             remFromParent
731                                     .addSelectionListener(new SelectionAdapter() {
732                                         @Override
733                                         public void widgetSelected(
734                                                 SelectionEvent e) {
735                                             CollectionResource parentCollection = (CollectionResource) parent
736                                                     .getData();
737                                             String status;
738                                             try {
739                                                 resourceManager
740                                                         .removeResourceFromCollection(
741                                                                 parentCollection,
742                                                                 resource);
743                                                 collectionResTreeViewer
744                                                         .refresh();
745                                                 deviceTreeViewer.refresh();
746
747                                                 resourceManager
748                                                         .resourceSelectionChanged(null);
749
750                                                 status = "Resource removed from its parent";
751                                             } catch (SimulatorException e1) {
752                                                 status = "Failed to remove the resource from its parent";
753                                             }
754                                             MessageDialog.openInformation(
755                                                     Display.getDefault()
756                                                             .getActiveShell(),
757                                                     "Status", status);
758                                         }
759                                     });
760
761                             if (resource instanceof SingleResource) {
762                                 // Menu Items for single resource will be added
763                                 // here.
764                             }
765                         }
766                     }
767                 });
768             }
769         }
770     }
771
772     private void addMenuToTopLevelCollectionResources(final Menu menu,
773             final CollectionResource colRes) {
774         // Menu to add one or more resources to the collection.
775         MenuItem addResources = new MenuItem(menu, SWT.CASCADE);
776         addResources.setText("Add Resources");
777
778         Menu addSubMenu = new Menu(menu);
779         addResources.setMenu(addSubMenu);
780
781         MenuItem simpleResources = new MenuItem(addSubMenu, SWT.NONE);
782         simpleResources.setText("Simple");
783         simpleResources.addSelectionListener(new SelectionAdapter() {
784             @Override
785             public void widgetSelected(SelectionEvent e) {
786                 List<SingleResource> possibleResources;
787                 possibleResources = resourceManager
788                         .getSingleTypeResourcesForAddingToCollectionResource(colRes);
789                 if (null == possibleResources || possibleResources.isEmpty()) {
790                     MessageDialog
791                             .openError(Display.getDefault().getActiveShell(),
792                                     "No possible candidates",
793                                     "There are no possible simple resources that can be added.");
794                     return;
795                 }
796
797                 List<Resource> resources = Utility
798                         .convertSingleTypeResourceListToBaseType(possibleResources);
799
800                 AddResources addToCollectionsDlg = new AddResources(Display
801                         .getDefault().getActiveShell(), resources);
802                 if (addToCollectionsDlg.open() != Window.OK) {
803                     return;
804                 }
805
806                 Set<Resource> resultSet = addToCollectionsDlg
807                         .getSelectedResourceList();
808
809                 int addedCount = resourceManager.addResourceToCollection(
810                         colRes, resultSet);
811                 if (addedCount <= 0) {
812                     MessageDialog.openInformation(Display.getDefault()
813                             .getActiveShell(), "Operation failed",
814                             "Failed to add resources to the collection.");
815                     return;
816                 }
817
818                 collectionResTreeViewer.refresh();
819
820                 deviceTreeViewer.refresh();
821
822                 MessageDialog.openInformation(Display.getDefault()
823                         .getActiveShell(), "Added", "[" + addedCount + "/"
824                         + resultSet.size()
825                         + "] resources added to the collection");
826             }
827         });
828
829         MenuItem collectionResources = new MenuItem(addSubMenu, SWT.NONE);
830         collectionResources.setText("Collection");
831         collectionResources.addSelectionListener(new SelectionAdapter() {
832             @Override
833             public void widgetSelected(SelectionEvent e) {
834                 List<CollectionResource> possibleResources;
835                 possibleResources = resourceManager
836                         .getCollectionTypeResourcesForAddingToCollectionResource(colRes);
837                 if (null == possibleResources || possibleResources.isEmpty()) {
838                     MessageDialog
839                             .openError(Display.getDefault().getActiveShell(),
840                                     "No possible candidates",
841                                     "There are no possible collections that can be added.");
842                     return;
843                 }
844
845                 List<Resource> resources = Utility
846                         .convertCollectionTypeResourceListToBaseType(possibleResources);
847
848                 AddResources addToCollectionsDlg = new AddResources(Display
849                         .getDefault().getActiveShell(), resources);
850                 if (addToCollectionsDlg.open() != Window.OK) {
851                     return;
852                 }
853
854                 Set<Resource> resultSet = addToCollectionsDlg
855                         .getSelectedResourceList();
856
857                 int addedCount = resourceManager.addResourceToCollection(
858                         colRes, resultSet);
859                 if (addedCount <= 0) {
860                     MessageDialog.openInformation(Display.getDefault()
861                             .getActiveShell(), "Operation failed",
862                             "Failed to add resources to the collection.");
863                     return;
864                 }
865
866                 collectionResTreeViewer.refresh();
867
868                 deviceTreeViewer.refresh();
869
870                 MessageDialog.openInformation(Display.getDefault()
871                         .getActiveShell(), "Added", "[" + addedCount + "/"
872                         + resultSet.size()
873                         + "] resources added to the collection");
874             }
875         });
876
877         // Menu to remove one or more resources from the collection.
878         MenuItem removeResources = new MenuItem(menu, SWT.CASCADE);
879         removeResources.setText("Remove Resources");
880
881         Menu removeSubMenu = new Menu(menu);
882         removeResources.setMenu(removeSubMenu);
883
884         simpleResources = new MenuItem(removeSubMenu, SWT.NONE);
885         simpleResources.setText("Simple");
886         simpleResources.addSelectionListener(new SelectionAdapter() {
887             @Override
888             public void widgetSelected(SelectionEvent e) {
889                 List<SingleResource> possibleResources;
890                 possibleResources = resourceManager.getSingleTypeChilds(colRes);
891                 if (null == possibleResources || possibleResources.isEmpty()) {
892                     MessageDialog
893                             .openError(Display.getDefault().getActiveShell(),
894                                     "No possible candidates",
895                                     "There are no possible simple resources which can be removed.");
896                     return;
897                 }
898
899                 List<Resource> resources = Utility
900                         .convertSingleTypeResourceListToBaseType(possibleResources);
901
902                 RemoveResources removeFromCollectionsDlg = new RemoveResources(
903                         Display.getDefault().getActiveShell(), resources);
904                 if (removeFromCollectionsDlg.open() != Window.OK) {
905                     return;
906                 }
907
908                 Set<Resource> resultSet = removeFromCollectionsDlg
909                         .getSelectedResourceList();
910
911                 int removeCount = resourceManager
912                         .removeResourcesFromCollection(colRes, resultSet);
913                 if (removeCount <= 0) {
914                     MessageDialog.openInformation(Display.getDefault()
915                             .getActiveShell(), "Operation failed",
916                             "Failed to remove resources from the collection.");
917                     return;
918                 }
919
920                 collectionResTreeViewer.refresh();
921
922                 deviceTreeViewer.refresh();
923
924                 MessageDialog.openInformation(Display.getDefault()
925                         .getActiveShell(), "Removed", "[" + removeCount + "/"
926                         + resultSet.size()
927                         + "] resources removed from the collection");
928             }
929         });
930
931         collectionResources = new MenuItem(removeSubMenu, SWT.NONE);
932         collectionResources.setText("Collection");
933         collectionResources.addSelectionListener(new SelectionAdapter() {
934             @Override
935             public void widgetSelected(SelectionEvent e) {
936                 List<CollectionResource> possibleResources;
937                 possibleResources = resourceManager
938                         .getCollectionTypeChilds(colRes);
939                 if (null == possibleResources || possibleResources.isEmpty()) {
940                     MessageDialog
941                             .openError(Display.getDefault().getActiveShell(),
942                                     "No possible candidates",
943                                     "There are no possible collections which can be removed.");
944                     return;
945                 }
946
947                 List<Resource> resources = Utility
948                         .convertCollectionTypeResourceListToBaseType(possibleResources);
949
950                 RemoveResources removeFromCollectionsDlg = new RemoveResources(
951                         Display.getDefault().getActiveShell(), resources);
952                 if (removeFromCollectionsDlg.open() != Window.OK) {
953                     return;
954                 }
955
956                 Set<Resource> resultSet = removeFromCollectionsDlg
957                         .getSelectedResourceList();
958
959                 int removeCount = resourceManager
960                         .removeResourcesFromCollection(colRes, resultSet);
961                 if (removeCount <= 0) {
962                     MessageDialog.openInformation(Display.getDefault()
963                             .getActiveShell(), "Operation failed",
964                             "Failed to remove resources from the collection.");
965                     return;
966                 }
967
968                 collectionResTreeViewer.refresh();
969
970                 deviceTreeViewer.refresh();
971
972                 MessageDialog.openInformation(Display.getDefault()
973                         .getActiveShell(), "Removed", "[" + removeCount + "/"
974                         + resultSet.size()
975                         + "] resources removed from the collection");
976             }
977         });
978
979         // Menu to add resource to one or more devices.
980         MenuItem addToDevice = new MenuItem(menu, SWT.NONE);
981         addToDevice.setText("Add to Device");
982         addToDevice.addSelectionListener(new SelectionAdapter() {
983             @Override
984             public void widgetSelected(SelectionEvent e) {
985                 List<Device> possibleDevices;
986                 possibleDevices = resourceManager
987                         .getDevicesForAddingToResource(colRes);
988                 if (null == possibleDevices || possibleDevices.isEmpty()) {
989                     MessageDialog
990                             .openError(Display.getDefault().getActiveShell(),
991                                     "No possible candidates",
992                                     "There are no possible devices to which the resource can be added.");
993                     return;
994                 }
995
996                 AddResourceToDevices addToDevicesDlg = new AddResourceToDevices(
997                         Display.getDefault().getActiveShell(), possibleDevices);
998                 if (addToDevicesDlg.open() != Window.OK) {
999                     return;
1000                 }
1001
1002                 Set<Device> resultSet = addToDevicesDlg.getSelectedDeviceList();
1003
1004                 resourceManager.addResourceToDevice(resultSet, colRes);
1005
1006                 deviceTreeViewer.refresh();
1007
1008                 MessageDialog.openInformation(Display.getDefault()
1009                         .getActiveShell(), "Added",
1010                         "Resource added to selected devices.");
1011             }
1012         });
1013
1014         // Menu to remove resource from one or more device to which it is a
1015         // member.
1016         MenuItem removeFromDevice = new MenuItem(menu, SWT.NONE);
1017         removeFromDevice.setText("Remove from Device");
1018         removeFromDevice.addSelectionListener(new SelectionAdapter() {
1019             @Override
1020             public void widgetSelected(SelectionEvent e) {
1021                 List<Device> possibleDevices;
1022                 possibleDevices = resourceManager.getDeviceReferences(colRes);
1023                 if (null == possibleDevices || possibleDevices.isEmpty()) {
1024                     MessageDialog.openError(Display.getDefault()
1025                             .getActiveShell(), "No possible candidates",
1026                             "Resource has not been added to any devices.");
1027                     return;
1028                 }
1029
1030                 RemoveResourceFromDevices removeFromDevicesDlg = new RemoveResourceFromDevices(
1031                         Display.getDefault().getActiveShell(), possibleDevices);
1032                 if (removeFromDevicesDlg.open() != Window.OK) {
1033                     return;
1034                 }
1035
1036                 Set<Device> resultSet = removeFromDevicesDlg
1037                         .getSelectedDeviceList();
1038
1039                 resourceManager.removeResourceFromDevice(resultSet, colRes);
1040
1041                 deviceTreeViewer.refresh();
1042
1043                 MessageDialog.openInformation(Display.getDefault()
1044                         .getActiveShell(), "Removed",
1045                         "Resource removed from selected devices");
1046             }
1047         });
1048
1049         // Menu to remove resource from one or more device to which it is a
1050         // member.
1051         MenuItem deleteResource = new MenuItem(menu, SWT.NONE);
1052         deleteResource.setText("Delete");
1053         deleteResource.addSelectionListener(new SelectionAdapter() {
1054             @Override
1055             public void widgetSelected(SelectionEvent e) {
1056                 /*
1057                  * boolean refreshCollectionTree = false; boolean
1058                  * refreshDeviceTree = false;
1059                  * if(resource.isMemberOfAnyCollection()) {
1060                  * refreshCollectionTree = true; }
1061                  * if(resource.isMemberOfAnyDevice()) { refreshDeviceTree =
1062                  * true; }
1063                  */
1064                 try {
1065                     resourceManager.removeResource(colRes);
1066                     // if(refreshCollectionTree)
1067                     collectionResTreeViewer.refresh();
1068                     // if(refreshDeviceTree)
1069                     deviceTreeViewer.refresh();
1070
1071                     resourceManager.resourceSelectionChanged(null);
1072
1073                     MessageDialog.openInformation(Display.getDefault()
1074                             .getActiveShell(), "Deleted", "Resource deleted.");
1075                 } catch (SimulatorException e1) {
1076                     MessageDialog.openInformation(Display.getDefault()
1077                             .getActiveShell(), "Deletion Failed",
1078                             "Failed to delete the resource.");
1079                 }
1080
1081                 changeDeleteVisibility();
1082             }
1083         });
1084
1085     }
1086
1087     private void createDeviceArea() {
1088         deviceTab = new CTabItem(folder, SWT.NULL);
1089         deviceTab.setText("Devices");
1090
1091         // Create a group to show all the discovered resources.
1092         // Adding the group to the folder.
1093         Group resourceGroup = new Group(folder, SWT.NONE);
1094         // resourceGroup.setText("Discovered Resources");
1095
1096         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
1097         resourceGroup.setBackground(color);
1098
1099         resourceGroup.setLayout(new GridLayout(1, false));
1100         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
1101         resourceGroup.setLayoutData(gd);
1102
1103         PatternFilter filter = new PatternFilter();
1104         FilteredTree filteredTree = new FilteredTree(resourceGroup,
1105                 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
1106         deviceTreeViewer = filteredTree.getViewer();
1107         deviceTreeViewer.getTree().setLayoutData(
1108                 new GridData(SWT.FILL, SWT.FILL, true, true));
1109         deviceTreeViewer.setContentProvider(new DeviceContentProvider());
1110         deviceTreeViewer.setLabelProvider(new TreeLabelProvider());
1111         deviceTreeViewer.setInput(new Object());
1112
1113         deviceTreeViewer
1114                 .addSelectionChangedListener(new ISelectionChangedListener() {
1115                     @Override
1116                     public void selectionChanged(SelectionChangedEvent e) {
1117                         IStructuredSelection selection = (IStructuredSelection) e
1118                                 .getSelection();
1119                         if (null == selection) {
1120                             return;
1121                         }
1122
1123                         int size = selection.size();
1124                         if (size == 1) {
1125                             Object obj = selection.getFirstElement();
1126                             if (obj instanceof Device) {
1127                                 resourceManager
1128                                         .deviceSelectionChanged((Device) obj);
1129                             } else {
1130                                 resourceManager
1131                                         .resourceSelectionChanged((Resource) obj);
1132                             }
1133                         } else {
1134                             resourceManager.deviceSelectionChanged(null);
1135                         }
1136                     }
1137                 });
1138
1139         // TODO: Add menu items
1140         addMenuToDevices();
1141
1142         deviceTab.setControl(resourceGroup);
1143     }
1144
1145     private void addMenuToDevices() {
1146         if (null != deviceTreeViewer) {
1147             final Tree resourceTreeHead = deviceTreeViewer.getTree();
1148             if (null != resourceTreeHead) {
1149                 // Below code creates menu entries and shows them on right
1150                 // clicking a resource
1151                 final Menu menu = new Menu(resourceTreeHead);
1152                 resourceTreeHead.setMenu(menu);
1153                 menu.addMenuListener(new MenuAdapter() {
1154                     @Override
1155                     public void menuShown(MenuEvent e) {
1156                         // Clear existing menu items
1157                         MenuItem[] items = menu.getItems();
1158                         for (int index = 0; index < items.length; index++) {
1159                             items[index].dispose();
1160                         }
1161
1162                         IStructuredSelection selection = ((IStructuredSelection) deviceTreeViewer
1163                                 .getSelection());
1164                         final Object element = selection.getFirstElement();
1165                         if (element instanceof Device) {
1166                             addMenuToDevices(menu, (Device) element);
1167                         } else {
1168                             final Resource resource = (Resource) element;
1169                             TreeItem[] treeItems = resourceTreeHead
1170                                     .getSelection();
1171                             if (null == resource || null == treeItems) {
1172                                 return;
1173                             }
1174
1175                             if (resource instanceof SingleResource)
1176                                 addAutomationMenu(menu,
1177                                         (SingleResource) resource);
1178
1179                             final TreeItem parent = treeItems[0]
1180                                     .getParentItem();
1181                             MenuItem remFromParent = new MenuItem(menu,
1182                                     SWT.NONE);
1183                             remFromParent.setText("Remove from Parent");
1184                             remFromParent
1185                                     .addSelectionListener(new SelectionAdapter() {
1186                                         @Override
1187                                         public void widgetSelected(
1188                                                 SelectionEvent e) {
1189                                             String status = "Resource removed from its parent.";
1190                                             Object obj = parent.getData();
1191                                             if (obj instanceof Device) {
1192                                                 resourceManager
1193                                                         .removeResourceFromDevice(
1194                                                                 (Device) obj,
1195                                                                 resource);
1196                                             } else {
1197                                                 try {
1198                                                     resourceManager
1199                                                             .removeResourceFromCollection(
1200                                                                     (CollectionResource) obj,
1201                                                                     resource);
1202                                                     collectionResTreeViewer
1203                                                             .refresh();
1204                                                     status = "Resource removed from its parent";
1205                                                 } catch (SimulatorException e1) {
1206                                                     status = "Failed to remove the resource from its parent";
1207                                                 }
1208                                             }
1209
1210                                             deviceTreeViewer.refresh();
1211
1212                                             MessageDialog.openInformation(
1213                                                     Display.getDefault()
1214                                                             .getActiveShell(),
1215                                                     "Status", status);
1216                                         }
1217                                     });
1218
1219                             if (resource instanceof SingleResource) {
1220                                 // Menu Items for single resource will be added
1221                                 // here.
1222                             }
1223                         }
1224                     }
1225                 });
1226             }
1227         }
1228     }
1229
1230     private void addMenuToDevices(Menu menu, final Device dev) {
1231         // Menu to add one or more resources to the collection.
1232         MenuItem addResources = new MenuItem(menu, SWT.CASCADE);
1233         addResources.setText("Add Resources");
1234
1235         Menu addSubMenu = new Menu(menu);
1236         addResources.setMenu(addSubMenu);
1237
1238         MenuItem simpleResources = new MenuItem(addSubMenu, SWT.NONE);
1239         simpleResources.setText("Simple");
1240         simpleResources.addSelectionListener(new SelectionAdapter() {
1241             @Override
1242             public void widgetSelected(SelectionEvent e) {
1243                 List<SingleResource> possibleResources;
1244                 possibleResources = resourceManager
1245                         .getSingleTypeResourcesForAddingToDevice(dev);
1246                 if (null == possibleResources || possibleResources.isEmpty()) {
1247                     MessageDialog
1248                             .openError(Display.getDefault().getActiveShell(),
1249                                     "No possible candidates",
1250                                     "There are no possible simple resources that can be added.");
1251                     return;
1252                 }
1253
1254                 List<Resource> resources = Utility
1255                         .convertSingleTypeResourceListToBaseType(possibleResources);
1256
1257                 AddResources addDlg = new AddResources(Display.getDefault()
1258                         .getActiveShell(), resources);
1259                 if (addDlg.open() != Window.OK) {
1260                     return;
1261                 }
1262
1263                 Set<Resource> resultSet = addDlg.getSelectedResourceList();
1264
1265                 resourceManager.addResourceToDevice(dev, resultSet);
1266
1267                 deviceTreeViewer.refresh();
1268
1269                 MessageDialog.openInformation(Display.getDefault()
1270                         .getActiveShell(), "Added",
1271                         "Single Resources added to the device");
1272             }
1273         });
1274
1275         MenuItem collectionResources = new MenuItem(addSubMenu, SWT.NONE);
1276         collectionResources.setText("Collection");
1277         collectionResources.addSelectionListener(new SelectionAdapter() {
1278             @Override
1279             public void widgetSelected(SelectionEvent e) {
1280                 List<CollectionResource> possibleResources;
1281                 possibleResources = resourceManager
1282                         .getCollectionTypeResourcesForAddingToDevice(dev);
1283                 if (null == possibleResources || possibleResources.isEmpty()) {
1284                     MessageDialog
1285                             .openError(Display.getDefault().getActiveShell(),
1286                                     "No possible candidates",
1287                                     "There are no possible collections that can be added.");
1288                     return;
1289                 }
1290
1291                 List<Resource> resources = Utility
1292                         .convertCollectionTypeResourceListToBaseType(possibleResources);
1293
1294                 AddResources addDlg = new AddResources(Display.getDefault()
1295                         .getActiveShell(), resources);
1296                 if (addDlg.open() != Window.OK) {
1297                     return;
1298                 }
1299
1300                 Set<Resource> resultSet = addDlg.getSelectedResourceList();
1301
1302                 resourceManager.addResourceToDevice(dev, resultSet);
1303
1304                 deviceTreeViewer.refresh();
1305
1306                 MessageDialog.openInformation(Display.getDefault()
1307                         .getActiveShell(), "Added",
1308                         "Collection Resources added to the device");
1309             }
1310         });
1311
1312         // Menu to remove one or more resources from the collection.
1313         MenuItem removeResources = new MenuItem(menu, SWT.CASCADE);
1314         removeResources.setText("Remove Resources");
1315
1316         Menu removeSubMenu = new Menu(menu);
1317         removeResources.setMenu(removeSubMenu);
1318
1319         simpleResources = new MenuItem(removeSubMenu, SWT.NONE);
1320         simpleResources.setText("Simple");
1321         simpleResources.addSelectionListener(new SelectionAdapter() {
1322             @Override
1323             public void widgetSelected(SelectionEvent e) {
1324                 List<SingleResource> possibleResources;
1325                 possibleResources = resourceManager.getSingleTypeChilds(dev);
1326                 if (null == possibleResources || possibleResources.isEmpty()) {
1327                     MessageDialog
1328                             .openError(Display.getDefault().getActiveShell(),
1329                                     "No possible candidates",
1330                                     "There are no possible simple resources which can be removed.");
1331                     return;
1332                 }
1333
1334                 List<Resource> resources = Utility
1335                         .convertSingleTypeResourceListToBaseType(possibleResources);
1336
1337                 RemoveResources removeDlg = new RemoveResources(Display
1338                         .getDefault().getActiveShell(), resources);
1339                 if (removeDlg.open() != Window.OK) {
1340                     return;
1341                 }
1342
1343                 Set<Resource> resultSet = removeDlg.getSelectedResourceList();
1344
1345                 resourceManager.removeResourcesFromDevice(dev, resultSet);
1346
1347                 collectionResTreeViewer.refresh();
1348
1349                 deviceTreeViewer.refresh();
1350
1351                 MessageDialog.openInformation(Display.getDefault()
1352                         .getActiveShell(), "Removed",
1353                         "Single Resources removed from the device");
1354             }
1355         });
1356
1357         collectionResources = new MenuItem(removeSubMenu, SWT.NONE);
1358         collectionResources.setText("Collection");
1359         collectionResources.addSelectionListener(new SelectionAdapter() {
1360             @Override
1361             public void widgetSelected(SelectionEvent e) {
1362                 List<CollectionResource> possibleResources;
1363                 possibleResources = resourceManager
1364                         .getCollectionTypeChilds(dev);
1365                 if (null == possibleResources || possibleResources.isEmpty()) {
1366                     MessageDialog
1367                             .openError(Display.getDefault().getActiveShell(),
1368                                     "No possible candidates",
1369                                     "There are no possible collections which can be removed.");
1370                     return;
1371                 }
1372
1373                 List<Resource> resources = Utility
1374                         .convertCollectionTypeResourceListToBaseType(possibleResources);
1375
1376                 RemoveResources removeDlg = new RemoveResources(Display
1377                         .getDefault().getActiveShell(), resources);
1378                 if (removeDlg.open() != Window.OK) {
1379                     return;
1380                 }
1381
1382                 Set<Resource> resultSet = removeDlg.getSelectedResourceList();
1383
1384                 resourceManager.removeResourcesFromDevice(dev, resultSet);
1385
1386                 collectionResTreeViewer.refresh();
1387
1388                 deviceTreeViewer.refresh();
1389
1390                 MessageDialog.openInformation(Display.getDefault()
1391                         .getActiveShell(), "Removed",
1392                         "Collection Resources removed from the device");
1393             }
1394         });
1395
1396         // Menu to remove the device.
1397         MenuItem deleteDevice = new MenuItem(menu, SWT.NONE);
1398         deleteDevice.setText("Delete");
1399         deleteDevice.addSelectionListener(new SelectionAdapter() {
1400             @Override
1401             public void widgetSelected(SelectionEvent e) {
1402                 resourceManager.removeDevice(dev);
1403                 deviceTreeViewer.refresh();
1404             }
1405         });
1406     }
1407
1408     private void addAutomationMenu(final Menu menu,
1409             final SingleResource selectedResource) {
1410         MenuItem startItem = new MenuItem(menu, SWT.NONE);
1411         startItem.setText(Constants.START_RESOURCE_AUTOMATION);
1412         startItem.addSelectionListener(new SelectionAdapter() {
1413             @Override
1414             public void widgetSelected(SelectionEvent e) {
1415                 // Block starting resource level
1416                 // automation if any attribute level
1417                 // automation is in progress for the
1418                 // selected resource
1419                 boolean started = resourceManager
1420                         .isAttributeAutomationStarted(selectedResource);
1421                 if (started) {
1422                     MessageDialog
1423                             .openInformation(
1424                                     Display.getDefault().getActiveShell(),
1425                                     "Attribute automation is in progress",
1426                                     "Attribute level automation for this resource is already in progress!!!\nPlease stop all "
1427                                             + "running attribute level automations to start resource level automation.");
1428                 } else {
1429
1430                     // Start the automation
1431                     // Fetch the settings data
1432                     List<AutomationSettingHelper> automationSettings;
1433                     automationSettings = AutomationSettingHelper
1434                             .getAutomationSettings(null);
1435
1436                     // Open the settings dialog
1437                     AutomationSettingDialog dialog = new AutomationSettingDialog(
1438                             Activator.getDefault().getWorkbench()
1439                                     .getActiveWorkbenchWindow().getShell(),
1440                             automationSettings);
1441                     dialog.create();
1442                     if (dialog.open() == Window.OK) {
1443                         String automationType = dialog.getAutomationType();
1444                         String updateFreq = dialog.getUpdateFrequency();
1445
1446                         AutoUpdateType autoType = AutoUpdateType
1447                                 .valueOf(automationType);
1448                         int updFreq = Utility
1449                                 .getUpdateIntervalFromString(updateFreq);
1450                         boolean status = resourceManager
1451                                 .startResourceAutomationUIRequest(autoType,
1452                                         updFreq, selectedResource);
1453                         String statusMsg = status ? "Automation started successfully!!!"
1454                                 : "Automation request failed!!!";
1455                         MessageDialog.openInformation(Display.getDefault()
1456                                 .getActiveShell(), "Automation Status",
1457                                 statusMsg);
1458                     }
1459                 }
1460             }
1461         });
1462
1463         MenuItem stopItem = new MenuItem(menu, SWT.NONE);
1464         stopItem.setText(Constants.STOP_RESOURCE_AUTOMATION);
1465         stopItem.addSelectionListener(new SelectionAdapter() {
1466             @Override
1467             public void widgetSelected(SelectionEvent e) {
1468                 boolean status = resourceManager
1469                         .stopResourceAutomationUIRequest(selectedResource);
1470                 String statusMsg = status ? "Automation stopped!!!"
1471                         : "Automation stop failed.";
1472                 MessageDialog.openInformation(Display.getDefault()
1473                         .getActiveShell(), "Automation Status", statusMsg);
1474             }
1475         });
1476
1477         // Set the initial visibility of menu items
1478         boolean status = resourceManager
1479                 .isResourceAutomationStarted(selectedResource);
1480         startItem.setEnabled(!status);
1481         stopItem.setEnabled(status);
1482     }
1483
1484     private void addUIListeners() {
1485
1486         createButton.addSelectionListener(new SelectionAdapter() {
1487             @Override
1488             public void widgetSelected(SelectionEvent e) {
1489                 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
1490
1491                     @Override
1492                     public void run() {
1493                         CreateResourceWizard createWizard = new CreateResourceWizard();
1494                         ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
1495                                 PlatformUI.getWorkbench().getDisplay()
1496                                         .getActiveShell(), createWizard);
1497                         createWizard.setWizardDialog(wizardDialog);
1498                         int open = wizardDialog.open();
1499                         if (open == WizardDialog.OK
1500                                 || createWizard.isDlgForceClosed()) {
1501
1502                             Option option = createWizard.getMainPage()
1503                                     .getOption();
1504                             if (option == Option.DEVICE) {
1505                                 Device dev = createWizard.getCreatedDevice();
1506                                 if (null != dev) {
1507                                     folder.setSelection(deviceTab);
1508                                     deviceTreeViewer.setSelection(
1509                                             new StructuredSelection(dev), true);
1510                                     resourceManager.deviceSelectionChanged(dev);
1511                                     deviceTreeViewer.expandToLevel(dev,
1512                                             Constants.TREE_EXPANSION_LEVEL);
1513                                 }
1514                             } else if (option == Option.SIMPLE_FROM_RAML
1515                                     || option == Option.SIMPLE) {
1516                                 SingleResource res = (SingleResource) createWizard
1517                                         .getCreatedResource();
1518                                 if (null != res) {
1519                                     folder.setSelection(singleResTab);
1520                                     boolean canSelect = true;
1521                                     if (option == Option.SIMPLE_FROM_RAML
1522                                             && createWizard.getResourceCount() > 1) {
1523                                         canSelect = false;
1524                                     }
1525                                     if (canSelect) {
1526                                         singleResTreeViewer.setSelection(
1527                                                 new StructuredSelection(res),
1528                                                 true);
1529                                         resourceManager
1530                                                 .resourceSelectionChanged(res);
1531                                     }
1532                                 }
1533                             } else if (option == Option.COLLECTION_FROM_RAML
1534                                     || option == Option.COLLECTION) {
1535                                 CollectionResource res = (CollectionResource) createWizard
1536                                         .getCreatedResource();
1537                                 if (null != res) {
1538                                     folder.setSelection(collectionResTab);
1539                                     collectionResTreeViewer.setSelection(
1540                                             new StructuredSelection(res), true);
1541                                     resourceManager
1542                                             .resourceSelectionChanged(res);
1543                                     collectionResTreeViewer.expandToLevel(res,
1544                                             Constants.TREE_EXPANSION_LEVEL);
1545                                 }
1546                             }
1547
1548                             MessageDialog.openInformation(Display.getDefault()
1549                                     .getActiveShell(),
1550                                     "Resource Creation Status", createWizard
1551                                             .getStatus());
1552                         }
1553                     }
1554                 });
1555             }
1556         });
1557
1558         deleteButton.addSelectionListener(new SelectionAdapter() {
1559             @Override
1560             public void widgetSelected(SelectionEvent e) {
1561                 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
1562
1563                     @Override
1564                     public void run() {
1565                         boolean exist = resourceManager.isAnyResourceExist();
1566                         if (!exist) {
1567                             return;
1568                         }
1569                         DeleteResourceWizard deleteWizard = new DeleteResourceWizard();
1570                         ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
1571                                 PlatformUI.getWorkbench().getDisplay()
1572                                         .getActiveShell(), deleteWizard);
1573                         int open = wizardDialog.open();
1574                         if (open == Window.OK) {
1575                             singleResTreeViewer.refresh();
1576                             collectionResTreeViewer.refresh();
1577                             deviceTreeViewer.refresh();
1578                             MessageDialog.openInformation(Display.getDefault()
1579                                     .getActiveShell(),
1580                                     "Resource Deletion Status", deleteWizard
1581                                             .getStatus());
1582
1583                             changeDeleteVisibility();
1584                         }
1585                     }
1586                 });
1587             }
1588         });
1589     }
1590
1591     public void addManagerListeners() {
1592         UiListenerHandler.getInstance().addResourceListChangedUIListener(
1593                 resourceListChangedListener);
1594         UiListenerHandler.getInstance().addResourcePropertiesChangedUIListener(
1595                 resourcePropertiesChangedListener);
1596     }
1597
1598     @Override
1599     public void setFocus() {
1600     }
1601
1602     @Override
1603     public void dispose() {
1604         // Unregister the listener
1605         if (null != resourceListChangedListener) {
1606             UiListenerHandler.getInstance()
1607                     .removeResourceListChangedUIListener(
1608                             resourceListChangedListener);
1609             resourceManager.resourceSelectionChanged(null);
1610         }
1611         if (null != resourcePropertiesChangedListener) {
1612             UiListenerHandler.getInstance()
1613                     .removeResourcePropertiesChangedUIListener(
1614                             resourcePropertiesChangedListener);
1615         }
1616         super.dispose();
1617     }
1618 }
1619
1620 class SingleResourceContentProvider implements ITreeContentProvider {
1621
1622     @Override
1623     public void dispose() {
1624     }
1625
1626     @Override
1627     public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
1628     }
1629
1630     @Override
1631     public Object[] getChildren(Object parent) {
1632         return null;
1633     }
1634
1635     @Override
1636     public Object[] getElements(Object parent) {
1637         List<SingleResource> resList;
1638         resList = Activator.getDefault().getResourceManager()
1639                 .getSingleResourceList();
1640         if (null == resList) {
1641             resList = new ArrayList<SingleResource>();
1642         }
1643         return resList.toArray();
1644     }
1645
1646     @Override
1647     public Object getParent(Object child) {
1648         return null;
1649     }
1650
1651     @Override
1652     public boolean hasChildren(Object parent) {
1653         return false;
1654     }
1655 }
1656
1657 class CollectionResourceContentProvider implements ITreeContentProvider {
1658
1659     @Override
1660     public void dispose() {
1661     }
1662
1663     @Override
1664     public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
1665     }
1666
1667     @Override
1668     public Object[] getChildren(Object parent) {
1669         if (parent instanceof CollectionResource) {
1670             CollectionResource cRes = (CollectionResource) parent;
1671             Set<Resource> childs = cRes.getChildResources();
1672             if (null == childs) {
1673                 return null;
1674             }
1675             return childs.toArray();
1676         }
1677         return null;
1678     }
1679
1680     @Override
1681     public Object[] getElements(Object parent) {
1682         List<CollectionResource> resList;
1683         resList = Activator.getDefault().getResourceManager()
1684                 .getCollectionResourceList();
1685         if (null == resList) {
1686             resList = new ArrayList<CollectionResource>();
1687         }
1688         return resList.toArray();
1689     }
1690
1691     @Override
1692     public Object getParent(Object child) {
1693         return null;
1694     }
1695
1696     @Override
1697     public boolean hasChildren(Object parent) {
1698         if (parent instanceof CollectionResource) {
1699             CollectionResource cRes = (CollectionResource) parent;
1700             Set<Resource> children = cRes.getChildResources();
1701             if (null != children && children.size() > 0) {
1702                 return true;
1703             }
1704         }
1705         return false;
1706     }
1707 }
1708
1709 class DeviceContentProvider implements ITreeContentProvider {
1710
1711     @Override
1712     public void dispose() {
1713     }
1714
1715     @Override
1716     public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
1717     }
1718
1719     @Override
1720     public Object[] getChildren(Object parent) {
1721         if (parent instanceof Device) {
1722             Device dev = (Device) parent;
1723             Set<Resource> children = dev.getChildResources();
1724             if (null != children && children.size() > 0) {
1725                 return children.toArray();
1726             }
1727         } else if (parent instanceof CollectionResource) {
1728             CollectionResource res = (CollectionResource) parent;
1729             Set<Resource> children = res.getChildResources();
1730             if (null != children && children.size() > 0) {
1731                 return children.toArray();
1732             }
1733         }
1734         return null;
1735     }
1736
1737     @Override
1738     public Object[] getElements(Object parent) {
1739         List<Device> deviceList;
1740         deviceList = Activator.getDefault().getResourceManager()
1741                 .getDeviceList();
1742         if (null == deviceList) {
1743             deviceList = new ArrayList<Device>();
1744         }
1745         return deviceList.toArray();
1746     }
1747
1748     @Override
1749     public Object getParent(Object child) {
1750         return null;
1751     }
1752
1753     @Override
1754     public boolean hasChildren(Object parent) {
1755         if (parent instanceof Device) {
1756             Device dev = (Device) parent;
1757             Set<Resource> children = dev.getChildResources();
1758             if (null != children && children.size() > 0) {
1759                 return true;
1760             }
1761         } else if (parent instanceof CollectionResource) {
1762             CollectionResource res = (CollectionResource) parent;
1763             Set<Resource> children = res.getChildResources();
1764             if (null != children && children.size() > 0) {
1765                 return true;
1766             }
1767         }
1768         return false;
1769     }
1770 }
1771
1772 class TreeLabelProvider extends LabelProvider {
1773     @Override
1774     public String getText(Object element) {
1775         if (element instanceof Resource) {
1776             Resource res = (Resource) element;
1777             return res.getResourceName();
1778         } else {
1779             Device dev = (Device) element;
1780             return dev.getDeviceName();
1781         }
1782     }
1783
1784     @Override
1785     public Image getImage(Object element) {
1786         if (element instanceof SingleResource) {
1787             return Activator.getDefault().getImageRegistry()
1788                     .get(Constants.SINGLE_RESOURCE);
1789         } else if (element instanceof CollectionResource) {
1790             return Activator.getDefault().getImageRegistry()
1791                     .get(Constants.COLLECTION_RESOURCE);
1792         } else {
1793             return Activator.getDefault().getImageRegistry()
1794                     .get(Constants.DEVICE);
1795         }
1796     }
1797 }