Simulator Client controller plug-in changes:
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / AttributeView.java
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.clientcontroller.view;
18
19 import java.util.List;
20 import java.util.Map;
21
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
24 import oic.simulator.clientcontroller.listener.IGetUIListener;
25 import oic.simulator.clientcontroller.listener.IObserveUIListener;
26 import oic.simulator.clientcontroller.listener.IPostUIListener;
27 import oic.simulator.clientcontroller.listener.IPutUIListener;
28 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
29 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
30 import oic.simulator.clientcontroller.manager.ResourceManager;
31 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
32 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
33 import oic.simulator.clientcontroller.remoteresource.RemoteResourceAttribute;
34 import oic.simulator.clientcontroller.utils.Constants;
35 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
36 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
37 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
38
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.viewers.IStructuredContentProvider;
41 import org.eclipse.jface.viewers.StyledCellLabelProvider;
42 import org.eclipse.jface.viewers.TableViewer;
43 import org.eclipse.jface.viewers.TableViewerColumn;
44 import org.eclipse.jface.viewers.Viewer;
45 import org.eclipse.jface.viewers.ViewerCell;
46 import org.eclipse.jface.window.Window;
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.events.SelectionAdapter;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.graphics.Color;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Display;
56 import org.eclipse.swt.widgets.Group;
57 import org.eclipse.swt.widgets.Table;
58 import org.eclipse.ui.PlatformUI;
59 import org.eclipse.ui.part.ViewPart;
60
61 /**
62  * This class manages and shows the attribute view in the perspective.
63  */
64 public class AttributeView extends ViewPart {
65
66     public static final String                  VIEW_ID        = "oic.simulator.clientcontroller.view.attribute";
67
68     private TableViewer                         attTblViewer;
69
70     private Button                              getButton;
71     private Button                              putButton;
72     private Button                              postButton;
73     private Button                              automateButton;
74     private Button                              observeResButton;
75
76     private final String[]                      attTblHeaders  = {
77             "Attribute Name", "Attribute Value"               };
78     private final Integer[]                     attTblColWidth = { 200, 200 };
79
80     private ResourceManager                     resourceManager;
81
82     private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
83     private IGetUIListener                      getUIListener;
84     private IPutUIListener                      putUIListener;
85     private IPostUIListener                     postUIListener;
86     private IObserveUIListener                  observeUIListener;
87     private IVerificationUIListener             verificationUIListener;
88     private IConfigurationUpload                configUploadUIListener;
89
90     private RemoteResource                      resourceInSelection;
91
92     public AttributeView() {
93         resourceManager = Activator.getDefault().getResourceManager();
94
95         resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
96
97             @Override
98             public void onResourceSelectionChange(final RemoteResource resource) {
99                 Display.getDefault().asyncExec(new Runnable() {
100
101                     @Override
102                     public void run() {
103                         System.out
104                                 .println("AttributeView: onResourceSelectionChange");
105                         resourceInSelection = resource;
106
107                         // Set visibility of manual and automation controls
108                         setVisibility((resource == null) ? false : true);
109
110                         // Update the attribute table
111                         if (null != attTblViewer
112                                 && !attTblViewer.getControl().isDisposed()) {
113                             System.out.println("viewer is alive");
114                             updateViewer(getData(resource));
115                         }
116
117                         // Update the observe status
118                         updateObserve(resource);
119
120                         // Update the pay-load details if any
121                     }
122                 });
123             }
124         };
125
126         getUIListener = new IGetUIListener() {
127
128             @Override
129             public void onGetCompleted(final RemoteResource resource) {
130                 Display.getDefault().asyncExec(new Runnable() {
131
132                     @Override
133                     public void run() {
134
135                         System.out.println("AttributeView: onGetCompleted");
136                         if (null == resource) {
137                             return;
138                         }
139                         // Update the attribute table
140                         if (resourceInSelection != resource) {
141                             System.out
142                                     .println("AttributeView: get response arrived for a different resource");
143                             return;
144                         }
145                         updateViewer(getData(resource));
146
147                         // Update the observe status
148                         updateObserve(resource);
149
150                         // Update the pay-load details if any
151                     }
152                 });
153             }
154
155             @Override
156             public void onGetFailed(RemoteResource resource) {
157             }
158         };
159
160         putUIListener = new IPutUIListener() {
161
162             @Override
163             public void onPutCompleted(final RemoteResource resource) {
164                 Display.getDefault().asyncExec(new Runnable() {
165
166                     @Override
167                     public void run() {
168
169                         System.out.println("AttributeView: onPutCompleted");
170                         if (null == resource) {
171                             return;
172                         }
173                         // Update the attribute table
174                         if (resourceInSelection != resource) {
175                             System.out
176                                     .println("AttributeView: put response arrived for a different resource");
177                             return;
178                         }
179                         updateViewer(getData(resource));
180
181                         // Update the observe status
182                         updateObserve(resource);
183
184                         // Update the pay-load details if any
185                     }
186                 });
187             }
188
189             @Override
190             public void onPutFailed(RemoteResource resource) {
191             }
192         };
193
194         postUIListener = new IPostUIListener() {
195
196             @Override
197             public void onPostCompleted(final RemoteResource resource) {
198                 Display.getDefault().asyncExec(new Runnable() {
199
200                     @Override
201                     public void run() {
202
203                         System.out.println("AttributeView: onPostCompleted");
204                         if (null == resource) {
205                             return;
206                         }
207                         // Update the attribute table
208                         if (resourceInSelection != resource) {
209                             System.out
210                                     .println("AttributeView: post response arrived for a different resource");
211                             return;
212                         }
213                         updateViewer(getData(resource));
214
215                         // Update the observe status
216                         updateObserve(resource);
217
218                         // Update the pay-load details if any
219                     }
220                 });
221             }
222
223             @Override
224             public void onPostFailed(RemoteResource resource) {
225             }
226         };
227
228         observeUIListener = new IObserveUIListener() {
229
230             @Override
231             public void onObserveCompleted(final RemoteResource resource) {
232                 Display.getDefault().asyncExec(new Runnable() {
233
234                     @Override
235                     public void run() {
236
237                         System.out.println("AttributeView: onObserveCompleted");
238                         if (null == resource) {
239                             return;
240                         }
241                         // Update the attribute table
242                         if (resourceInSelection != resource) {
243                             System.out
244                                     .println("AttributeView: observe response arrived for a different resource");
245                             return;
246                         }
247                         updateViewer(getData(resource));
248
249                         // Update the observe status
250                         updateObserve(resource);
251
252                         // Update the pay-load details if any
253                     }
254                 });
255             }
256
257             @Override
258             public void onObserveFailed(RemoteResource resource) {
259             }
260         };
261
262         verificationUIListener = new IVerificationUIListener() {
263
264             @Override
265             public void onVerificationStarted(final RemoteResource resource,
266                     final int autoType) {
267                 Display.getDefault().asyncExec(new Runnable() {
268
269                     @Override
270                     public void run() {
271                         // changeReqBtnVisibility(autoType, false);
272                     }
273                 });
274             }
275
276             @Override
277             public void onVerificationCompleted(final RemoteResource resource,
278                     final int autoType) {
279                 Display.getDefault().asyncExec(new Runnable() {
280
281                     @Override
282                     public void run() {
283                         // changeReqBtnVisibility(autoType, true);
284                     }
285                 });
286             }
287
288             @Override
289             public void onVerificationAborted(final RemoteResource resource,
290                     final int autoType) {
291                 Display.getDefault().asyncExec(new Runnable() {
292
293                     @Override
294                     public void run() {
295                         // changeReqBtnVisibility(autoType, true);
296                     }
297                 });
298             }
299         };
300
301         configUploadUIListener = new IConfigurationUpload() {
302
303             @Override
304             public void onConfigurationUploaded(final RemoteResource resource) {
305                 Display.getDefault().asyncExec(new Runnable() {
306
307                     @Override
308                     public void run() {
309
310                         System.out
311                                 .println("AttributeView: onConfigurationUploaded");
312                         if (null == resource) {
313                             return;
314                         }
315                         if (resourceInSelection != resource) {
316                             System.out
317                                     .println("AttributeView: config upload response arrived for a different resource");
318                             return;
319                         }
320                         if (!automateButton.isDisposed()) {
321                             automateButton.setEnabled(true);
322                         }
323                     }
324                 });
325             }
326         };
327     }
328
329     private void changeReqBtnVisibility(int reqType, boolean visibility) {
330         if (reqType == Constants.GET_AUTOMATION_INDEX) {
331             if (!getButton.isDisposed()) {
332                 getButton.setEnabled(visibility);
333             }
334         } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
335             if (!putButton.isDisposed()) {
336                 putButton.setEnabled(visibility);
337             }
338         } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
339             if (!postButton.isDisposed()) {
340                 postButton.setEnabled(visibility);
341             }
342         }
343     }
344
345     private Map<String, RemoteResourceAttribute> getData(RemoteResource resource) {
346         if (null == resource) {
347             return null;
348         }
349         Map<String, RemoteResourceAttribute> attMap = resource
350                 .getResourceAttributesMap();
351         System.out.println("AttributeView: \n" + attMap);
352         return attMap;
353     }
354
355     private void updateViewer(Map<String, RemoteResourceAttribute> attMap) {
356         if (null != attTblViewer) {
357             Table tbl = attTblViewer.getTable();
358             if (null != attMap) {
359                 attTblViewer.setInput(attMap.entrySet().toArray());
360                 if (!tbl.isDisposed()) {
361                     tbl.setLinesVisible(true);
362                 }
363             } else {
364                 if (!tbl.isDisposed()) {
365                     tbl.removeAll();
366                     tbl.setLinesVisible(false);
367                 }
368             }
369         }
370     }
371
372     private void updateObserve(RemoteResource resource) {
373         if (null == resource || observeResButton.isDisposed()) {
374             return;
375         }
376         boolean observed = resource.isObserved();
377         if (observed) {
378             observeResButton.setText(Constants.STOP_OBSERVE);
379         } else {
380             observeResButton.setText(Constants.OBSERVE);
381         }
382     }
383
384     @Override
385     public void createPartControl(Composite parent) {
386         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
387
388         parent.setLayout(new GridLayout());
389         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
390         parent.setLayoutData(gd);
391
392         Group attGroup = new Group(parent, SWT.NONE);
393         attGroup.setLayout(new GridLayout());
394         gd = new GridData();
395         gd.grabExcessHorizontalSpace = true;
396         gd.horizontalAlignment = SWT.FILL;
397         gd.grabExcessVerticalSpace = true;
398         gd.verticalAlignment = SWT.FILL;
399         gd.horizontalSpan = 2;
400         // gd.heightHint = 175;
401         attGroup.setLayoutData(gd);
402         attGroup.setText("Attributes");
403         attGroup.setBackground(color);
404
405         setupAttributeTable(attGroup);
406
407         setupRequestControls(parent);
408
409         setUIListeners();
410
411         addManagerListeners();
412
413         setVisibility(false);
414
415         // Updating the data in the UI as per the resource in selection.
416         if (null != attTblViewer && !attTblViewer.getControl().isDisposed()) {
417             updateViewer(getData(resourceManager
418                     .getCurrentResourceInSelection()));
419         }
420     }
421
422     private void setupRequestControls(Composite parent) {
423         GridData gd;
424         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
425         Composite opsComp = new Composite(parent, SWT.NONE);
426         gd = new GridData();
427         gd.grabExcessHorizontalSpace = true;
428         gd.horizontalAlignment = SWT.FILL;
429         opsComp.setLayoutData(gd);
430         GridLayout grid = new GridLayout(5, false);
431         opsComp.setLayout(grid);
432         opsComp.setBackground(color);
433
434         getButton = new Button(opsComp, SWT.PUSH);
435         getButton.setText("GET");
436         gd = new GridData();
437         gd.grabExcessHorizontalSpace = true;
438         gd.horizontalAlignment = SWT.FILL;
439         gd.widthHint = 50;
440         getButton.setLayoutData(gd);
441
442         putButton = new Button(opsComp, SWT.PUSH);
443         putButton.setText("PUT");
444         gd = new GridData();
445         gd.grabExcessHorizontalSpace = true;
446         gd.horizontalAlignment = SWT.FILL;
447         gd.widthHint = 50;
448         putButton.setLayoutData(gd);
449
450         postButton = new Button(opsComp, SWT.PUSH);
451         postButton.setText("POST");
452         gd = new GridData();
453         gd.grabExcessHorizontalSpace = true;
454         gd.horizontalAlignment = SWT.FILL;
455         gd.widthHint = 50;
456         postButton.setLayoutData(gd);
457
458         observeResButton = new Button(opsComp, SWT.PUSH);
459         observeResButton.setText(Constants.OBSERVE);
460         gd = new GridData();
461         gd.grabExcessHorizontalSpace = true;
462         gd.horizontalAlignment = SWT.FILL;
463         observeResButton.setLayoutData(gd);
464
465         automateButton = new Button(opsComp, SWT.PUSH);
466         automateButton.setText("Automation");
467         gd = new GridData();
468         gd.grabExcessHorizontalSpace = true;
469         gd.horizontalAlignment = SWT.FILL;
470         automateButton.setLayoutData(gd);
471     }
472
473     private void setupAttributeTable(Group attGroup) {
474         attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
475                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
476
477         createAttributeColumns(attTblViewer);
478
479         // make lines and header visible
480         Table table = attTblViewer.getTable();
481         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
482         table.setHeaderVisible(true);
483         table.setLinesVisible(true);
484
485         attTblViewer.setContentProvider(new AttributeContentProvider());
486     }
487
488     public void createAttributeColumns(TableViewer tableViewer) {
489
490         TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
491         attName.getColumn().setWidth(attTblColWidth[0]);
492         attName.getColumn().setText(attTblHeaders[0]);
493         attName.setLabelProvider(new StyledCellLabelProvider() {
494             @Override
495             public void update(ViewerCell cell) {
496                 Object element = cell.getElement();
497                 if (element instanceof Map.Entry) {
498                     @SuppressWarnings("unchecked")
499                     Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
500                     cell.setText(entry.getKey());
501                 }
502             }
503         });
504
505         TableViewerColumn attValue = new TableViewerColumn(tableViewer,
506                 SWT.NONE);
507         attValue.getColumn().setWidth(attTblColWidth[1]);
508         attValue.getColumn().setText(attTblHeaders[1]);
509         attValue.setLabelProvider(new StyledCellLabelProvider() {
510             @Override
511             public void update(ViewerCell cell) {
512                 Object element = cell.getElement();
513                 if (element instanceof Map.Entry) {
514                     @SuppressWarnings("unchecked")
515                     Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
516                     Object value = entry.getValue().getAttributeValue();
517                     if (null == value) {
518                         cell.setText("");
519                     } else {
520                         cell.setText(String.valueOf(value));
521                     }
522                 }
523             }
524         });
525     }
526
527     private void setUIListeners() {
528
529         getButton.addSelectionListener(new SelectionAdapter() {
530             @Override
531             public void widgetSelected(SelectionEvent e) {
532                 if (resourceInSelection.isGetAutomtnInProgress()) {
533                     MessageDialog
534                             .openInformation(Display.getDefault()
535                                     .getActiveShell(), "GET Request",
536                                     "GET Automation is in progress. Please wait or stop the automation.");
537                 } else {
538                     resourceManager.sendGetRequest(resourceInSelection);
539                 }
540             }
541         });
542
543         putButton.addSelectionListener(new SelectionAdapter() {
544             @Override
545             public void widgetSelected(SelectionEvent e) {
546                 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
547                     @Override
548                     public void run() {
549                         if (resourceInSelection.isPutAutomtnInProgress()) {
550                             MessageDialog
551                                     .openInformation(Display.getDefault()
552                                             .getActiveShell(), "PUT Request",
553                                             "PUT Automation is in progress. Please wait or stop the automation.");
554                             return;
555                         }
556                         List<PutPostAttributeModel> putPostModelList;
557                         putPostModelList = resourceInSelection
558                                 .getPutPostModel();
559                         if (null == putPostModelList) {
560                             MessageDialog
561                                     .openInformation(Display.getDefault()
562                                             .getActiveShell(), "PUT Request",
563                                             "No attributes exist in the resource model.");
564                             return;
565                         }
566                         PutRequestDialog putDialog = new PutRequestDialog(
567                                 Display.getDefault().getActiveShell(),
568                                 putPostModelList);
569                         if (putDialog.open() == Window.OK) {
570                             // Call the native PUT method
571                             resourceManager.sendPutRequest(resourceInSelection,
572                                     putPostModelList);
573                         }
574                     }
575                 });
576             }
577         });
578
579         postButton.addSelectionListener(new SelectionAdapter() {
580             @Override
581             public void widgetSelected(SelectionEvent e) {
582                 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
583                     @Override
584                     public void run() {
585                         if (resourceInSelection.isPostAutomtnInProgress()) {
586                             MessageDialog
587                                     .openInformation(Display.getDefault()
588                                             .getActiveShell(), "POST Request",
589                                             "POST Automation is in progress. Please wait or stop the automation.");
590                             return;
591                         }
592                         List<PutPostAttributeModel> putPostModelList;
593                         putPostModelList = resourceInSelection
594                                 .getPutPostModel();
595                         if (null == putPostModelList) {
596                             MessageDialog
597                                     .openInformation(Display.getDefault()
598                                             .getActiveShell(), "PUT Request",
599                                             "No attributes exist in the resource model.");
600                             return;
601                         }
602
603                         PostRequestDialog postDialog = new PostRequestDialog(
604                                 Display.getDefault().getActiveShell(),
605                                 putPostModelList);
606                         if (postDialog.open() == Window.OK) {
607                             // Call the native POST method
608                             resourceManager.sendPostRequest(
609                                     resourceInSelection, putPostModelList);
610                         }
611                     }
612                 });
613             }
614         });
615
616         observeResButton.addSelectionListener(new SelectionAdapter() {
617             @Override
618             public void widgetSelected(SelectionEvent e) {
619                 if (observeResButton.getText().equals(Constants.OBSERVE)) {
620                     resourceManager.sendObserveRequest(resourceInSelection);
621                     observeResButton.setText(Constants.STOP_OBSERVE);
622                 } else {
623                     resourceManager
624                             .sendCancelObserveRequest(resourceInSelection);
625                     observeResButton.setText(Constants.OBSERVE);
626                 }
627             }
628         });
629
630         automateButton.addSelectionListener(new SelectionAdapter() {
631             @Override
632             public void widgetSelected(SelectionEvent e) {
633                 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
634                     @Override
635                     public void run() {
636                         RemoteResource resource = resourceManager
637                                 .getCurrentResourceInSelection();
638                         if (null == resource) {
639                             return;
640                         }
641                         Map<String, Boolean> autoStatus = resourceManager
642                                 .getAutomationStatus(resource);
643                         if (null == autoStatus) {
644                             return;
645                         }
646                         VerificationDialog ad = new VerificationDialog(Display
647                                 .getDefault().getActiveShell(), autoStatus);
648                         ad.open();
649                     }
650                 });
651             }
652         });
653     }
654
655     private void addManagerListeners() {
656         resourceManager
657                 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
658         resourceManager.addGetUIListener(getUIListener);
659         resourceManager.addPutUIListener(putUIListener);
660         resourceManager.addPostUIListener(postUIListener);
661         resourceManager.addObserveUIListener(observeUIListener);
662         resourceManager.addVerificationUIListener(verificationUIListener);
663         resourceManager.addConfigUploadUIListener(configUploadUIListener);
664     }
665
666     private void setVisibility(boolean visibility) {
667         if (!getButton.isDisposed())
668             getButton.setEnabled(visibility);
669         if (!putButton.isDisposed())
670             putButton.setEnabled(visibility);
671         if (!postButton.isDisposed())
672             postButton.setEnabled(visibility);
673         if (!automateButton.isDisposed()) {
674             if (visibility && null != resourceInSelection
675                     && resourceInSelection.isConfigUploaded()) {
676                 automateButton.setEnabled(true);
677             } else {
678                 automateButton.setEnabled(false);
679             }
680         }
681         if (!observeResButton.isDisposed())
682             observeResButton.setEnabled(visibility);
683     }
684
685     class AttributeContentProvider implements IStructuredContentProvider {
686
687         @Override
688         public void dispose() {
689         }
690
691         @Override
692         public void inputChanged(Viewer viewer, Object arg1, Object arg2) {
693         }
694
695         @Override
696         public Object[] getElements(Object element) {
697             return (Object[]) element;
698         }
699
700     }
701
702     @Override
703     public void dispose() {
704         // Unregister the selection listener
705         if (null != resourceSelectionChangedListener) {
706             resourceManager
707                     .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
708         }
709
710         // Unregister the GET listener
711         if (null != getUIListener) {
712             resourceManager.removeGetUIListener(getUIListener);
713         }
714
715         super.dispose();
716     }
717
718     @Override
719     public void setFocus() {
720     }
721
722 }