b15c3e371ac32322054058e44e12cd6af2e22f38
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / ResourceManagerView.java
1 package oic.simulator.serviceprovider.view;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
8 import oic.simulator.serviceprovider.manager.ResourceManager;
9 import oic.simulator.serviceprovider.resource.DeleteCategory;
10 import oic.simulator.serviceprovider.utils.Constants;
11 import oic.simulator.serviceprovider.utils.Utility;
12 import oic.simulator.serviceprovider.view.dialogs.CreateResourceWizard;
13 import oic.simulator.serviceprovider.view.dialogs.DeleteResourceWizard;
14 import oic.simulator.serviceprovider.view.dialogs.ResourceWizardDialog;
15
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.ITreeContentProvider;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.wizard.WizardDialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.MenuAdapter;
24 import org.eclipse.swt.events.MenuEvent;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Group;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.swt.widgets.MenuItem;
37 import org.eclipse.swt.widgets.Tree;
38 import org.eclipse.swt.widgets.TreeItem;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.dialogs.FilteredTree;
41 import org.eclipse.ui.dialogs.PatternFilter;
42 import org.eclipse.ui.part.ViewPart;
43
44 public class ResourceManagerView extends ViewPart {
45
46     public static final String             VIEW_ID = "oic.simulator.serviceprovider.view.resourcemanager";
47
48     private Button                         createButton;
49     private Button                         deleteButton;
50
51     private TreeViewer                     treeViewer;
52
53     private IResourceListChangedUIListener resourceListChangedListener;
54
55     private ResourceManager                resourceManager;
56
57     public ResourceManagerView() {
58
59         resourceManager = Activator.getDefault().getResourceManager();
60
61         resourceListChangedListener = new IResourceListChangedUIListener() {
62
63             @Override
64             public void onResourceCreation() {
65                 Display.getDefault().asyncExec(new Runnable() {
66
67                     @Override
68                     public void run() {
69                         if (null != treeViewer) {
70                             treeViewer.refresh();
71                         }
72
73                         // Trigger the visibility of delete button
74                         changeDeleteVisibility();
75                     }
76                 });
77             }
78
79             @Override
80             public void onResourceDeletion() {
81                 Display.getDefault().asyncExec(new Runnable() {
82
83                     @Override
84                     public void run() {
85                         if (null != treeViewer) {
86                             treeViewer.refresh();
87                         }
88
89                         // Trigger the visibility of delete button
90                         changeDeleteVisibility();
91                     }
92                 });
93             }
94         };
95     }
96
97     public void changeDeleteVisibility() {
98         if (null == treeViewer) {
99             return;
100         }
101         boolean visibility;
102         Tree tree = treeViewer.getTree();
103         if (null != tree && !tree.isDisposed() && tree.getItemCount() > 0) {
104             visibility = true;
105         } else {
106             visibility = false;
107         }
108         if (null != deleteButton && !deleteButton.isDisposed()) {
109             deleteButton.setEnabled(visibility);
110         }
111     }
112
113     @Override
114     public void createPartControl(Composite parent) {
115         Composite compContent = new Composite(parent, SWT.NONE);
116         GridLayout baseLayout = new GridLayout(1, false);
117         compContent.setLayout(baseLayout);
118
119         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
120         compContent.setLayoutData(gd);
121
122         Composite buttonComp = new Composite(compContent, SWT.NONE);
123         buttonComp.setLayout(new GridLayout(2, false));
124
125         gd = new GridData();
126         gd.horizontalAlignment = SWT.FILL;
127         gd.grabExcessHorizontalSpace = true;
128
129         buttonComp.setLayoutData(gd);
130
131         createButton = new Button(buttonComp, SWT.PUSH);
132         createButton.setText("Create");
133         createButton.setToolTipText("Create Simulator Resource(s)");
134
135         gd = new GridData();
136         gd.widthHint = 90;
137         createButton.setLayoutData(gd);
138
139         deleteButton = new Button(buttonComp, SWT.PUSH);
140         deleteButton.setText("Delete");
141         deleteButton.setToolTipText("Delete Simulator Resource(s)");
142
143         gd = new GridData();
144         gd.widthHint = 90;
145         deleteButton.setLayoutData(gd);
146
147         Group resourceGroup = new Group(compContent, SWT.NONE);
148         resourceGroup.setText("Created Resources");
149
150         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
151         resourceGroup.setBackground(color);
152
153         resourceGroup.setLayout(new GridLayout(1, false));
154         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
155         resourceGroup.setLayoutData(gd);
156
157         PatternFilter filter = new PatternFilter();
158         FilteredTree filteredTree = new FilteredTree(resourceGroup,
159                 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
160         treeViewer = filteredTree.getViewer();
161         treeViewer.getTree().setLayoutData(
162                 new GridData(SWT.FILL, SWT.FILL, true, true));
163         treeViewer.setContentProvider(new TreeContentProvider());
164         treeViewer.setLabelProvider(new TreeLabelProvider());
165         treeViewer.setInput(new Object());
166
167         addUIListeners();
168
169         addManagerListeners();
170
171         // If there is at least one resource exist, then enable the delete
172         // resource button
173         changeDeleteVisibility();
174     }
175
176     private void addUIListeners() {
177
178         createButton.addSelectionListener(new SelectionAdapter() {
179             @Override
180             public void widgetSelected(SelectionEvent e) {
181                 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
182
183                     @Override
184                     public void run() {
185                         CreateResourceWizard createWizard = new CreateResourceWizard();
186                         ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
187                                 PlatformUI.getWorkbench().getDisplay()
188                                         .getActiveShell(), createWizard);
189                         int open = wizardDialog.open();
190                         if (open == WizardDialog.OK) {
191                             String configFilePath;
192                             int count;
193                             configFilePath = createWizard.getConfigFilePath();
194                             System.out.println("Resultant config file path is "
195                                     + configFilePath);
196                             count = createWizard.getResourceCount();
197                             if (count <= 1) {
198                                 // Single resource creation
199                                 resourceManager.createResource(configFilePath);
200                             } else {
201                                 // Multi-resource creation
202                                 resourceManager.createResource(configFilePath,
203                                         count);
204                             }
205                         }
206                     }
207                 });
208             }
209         });
210
211         deleteButton.addSelectionListener(new SelectionAdapter() {
212             @Override
213             public void widgetSelected(SelectionEvent e) {
214                 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
215
216                     @Override
217                     public void run() {
218                         DeleteResourceWizard deleteWizard = new DeleteResourceWizard();
219                         ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
220                                 PlatformUI.getWorkbench().getDisplay()
221                                         .getActiveShell(), deleteWizard);
222                         int open = wizardDialog.open();
223                         if (open == WizardDialog.OK) {
224                             DeleteCategory deleteCategory = deleteWizard
225                                     .getDeleteCategory();
226                             if (deleteCategory == DeleteCategory.BY_URI) {
227                                 String uri = deleteWizard.getDeleteCandidate();
228                                 boolean completeURI = Utility
229                                         .isUriComplete(uri);
230                                 if (!completeURI) {
231                                     uri = Utility.displayNameToUri(uri);
232                                 }
233                                 resourceManager.deleteResourceByURI(uri);
234                             } else if (deleteCategory == DeleteCategory.BY_TYPE) {
235                                 resourceManager
236                                         .deleteResourceByType(deleteWizard
237                                                 .getDeleteCandidate());
238                             } else if (deleteCategory == DeleteCategory.ALL) {
239                                 resourceManager.deleteAllResources();
240                             }
241                         }
242                     }
243                 });
244             }
245         });
246
247         if (null != treeViewer) {
248             final Tree resourceTreeHead = treeViewer.getTree();
249             if (null != resourceTreeHead) {
250                 // Below code adds a listener to the tree for selection changes
251                 // and notifies the resource manager
252                 resourceTreeHead.addSelectionListener(new SelectionAdapter() {
253                     @Override
254                     public void widgetSelected(SelectionEvent e) {
255                         TreeItem selectedItem = (TreeItem) e.item;
256                         if (null != selectedItem) {
257                             String selectedItemText = selectedItem.getText();
258                             selectedItemText = Utility
259                                     .displayNameToUri(selectedItemText);
260                             // Propagate this selection change event to manager
261                             resourceManager
262                                     .resourceSelectionChanged(selectedItemText);
263                         }
264                     }
265                 });
266                 // Below code creates menu entries and shows them on right
267                 // clicking a resource
268                 final Menu menu = new Menu(resourceTreeHead);
269                 resourceTreeHead.setMenu(menu);
270                 menu.addMenuListener(new MenuAdapter() {
271                     @Override
272                     public void menuShown(MenuEvent e) {
273                         // Clear existing menu items
274                         MenuItem[] items = menu.getItems();
275                         for (int index = 0; index < items.length; index++) {
276                             items[index].dispose();
277                         }
278                         final String selectedItem = resourceTreeHead
279                                 .getSelection()[0].getText();
280                         MenuItem startItem = new MenuItem(menu, SWT.NONE);
281                         startItem.setText(Constants.START_RESOURCE_AUTOMATION);
282                         startItem.addSelectionListener(new SelectionAdapter() {
283                             @Override
284                             public void widgetSelected(SelectionEvent e) {
285                                 // Block starting resource level
286                                 // automation if any attribute level
287                                 // automation is in progress for the
288                                 // selected resource
289                                 boolean started = resourceManager.isAttributeAutomationStarted(Utility
290                                         .displayNameToUri(selectedItem));
291                                 if (started) {
292                                     MessageDialog
293                                             .openInformation(
294                                                     Display.getDefault()
295                                                             .getActiveShell(),
296                                                     "Attribute automation is in progress",
297                                                     "Attribute level automation for this resource is already in progress!!!\nPlease stop all "
298                                                             + "running attribute level automations to start resource level automation.");
299                                 } else {
300                                     boolean status = resourceManager
301                                             .startResourceAutomationUIRequest(Utility
302                                                     .displayNameToUri(selectedItem));
303                                     String statusMsg = status ? "Automation started successfully!!!"
304                                             : "Automation request failed!!!";
305                                     MessageDialog.openInformation(Display
306                                             .getDefault().getActiveShell(),
307                                             "Automation Status", statusMsg);
308                                 }
309                             }
310                         });
311
312                         MenuItem stopItem = new MenuItem(menu, SWT.NONE);
313                         stopItem.setText(Constants.STOP_RESOURCE_AUTOMATION);
314                         stopItem.addSelectionListener(new SelectionAdapter() {
315                             @Override
316                             public void widgetSelected(SelectionEvent e) {
317                                 boolean status = resourceManager
318                                         .stopResourceAutomationUIRequest(Utility
319                                                 .displayNameToUri(selectedItem));
320                                 String statusMsg = status ? "Automation stop requested!!!"
321                                         : "Automation stop failed.";
322                                 MessageDialog.openInformation(Display
323                                         .getDefault().getActiveShell(),
324                                         "Automation Status", statusMsg);
325                             }
326                         });
327
328                         // Set the initial visibility of menu items
329                         boolean status = resourceManager
330                                 .isResourceAutomationStarted(Utility
331                                         .displayNameToUri(selectedItem));
332                         startItem.setEnabled(!status);
333                         stopItem.setEnabled(status);
334                     }
335                 });
336             }
337         }
338     }
339
340     public void addManagerListeners() {
341         resourceManager
342                 .addResourceListChangedUIListener(resourceListChangedListener);
343     }
344
345     @Override
346     public void setFocus() {
347     }
348
349     @Override
350     public void dispose() {
351         // Unregister the listener
352         if (null != resourceListChangedListener) {
353             resourceManager
354                     .removeResourceListChangedUIListener(resourceListChangedListener);
355             resourceManager.resourceSelectionChanged(null);
356         }
357         super.dispose();
358     }
359 }
360
361 class TreeContentProvider implements ITreeContentProvider {
362
363     @Override
364     public void dispose() {
365     }
366
367     @Override
368     public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
369     }
370
371     @Override
372     public Object[] getChildren(Object parent) {
373         return null;
374     }
375
376     @Override
377     public Object[] getElements(Object parent) {
378         List<String> uriList;
379         uriList = Activator.getDefault().getResourceManager().getURIList();
380         if (null == uriList) {
381             uriList = new ArrayList<String>();
382         }
383         return uriList.toArray();
384     }
385
386     @Override
387     public Object getParent(Object child) {
388         return null;
389     }
390
391     @Override
392     public boolean hasChildren(Object parent) {
393         return false;
394     }
395 }
396
397 class TreeLabelProvider extends LabelProvider {
398     @Override
399     public String getText(Object element) {
400         String value = (String) element;
401         value = Utility.uriToDisplayName(value);
402         return value;
403     }
404
405     @Override
406     public Image getImage(Object element) {
407         ResourceManager resourceManager = Activator.getDefault()
408                 .getResourceManager();
409         return resourceManager.getImage((String) element);
410     }
411 }