2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.clientcontroller.view;
19 import java.util.List;
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.VerificationDialog;
36 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
37 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
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.custom.CTabFolder;
49 import org.eclipse.swt.custom.CTabFolder2Adapter;
50 import org.eclipse.swt.custom.CTabFolderEvent;
51 import org.eclipse.swt.custom.CTabItem;
52 import org.eclipse.swt.events.SelectionAdapter;
53 import org.eclipse.swt.events.SelectionEvent;
54 import org.eclipse.swt.graphics.Color;
55 import org.eclipse.swt.layout.GridData;
56 import org.eclipse.swt.layout.GridLayout;
57 import org.eclipse.swt.widgets.Button;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.Display;
60 import org.eclipse.swt.widgets.Group;
61 import org.eclipse.swt.widgets.Table;
62 import org.eclipse.swt.widgets.Text;
63 import org.eclipse.ui.PlatformUI;
64 import org.eclipse.ui.part.ViewPart;
67 * This class manages and shows the attribute view in the perspective.
69 public class AttributeView extends ViewPart {
71 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.attribute";
73 private TableViewer attTblViewer;
75 private Button getButton;
76 private Button putButton;
77 private Button postButton;
78 private Button automateButton;
79 private Button observeResButton;
81 private CTabFolder payloadFolder;
82 private CTabItem requestPayloadTab;
83 private CTabItem responsePayloadTab;
84 private Text requestPayloadTxt;
85 private Text responsePayloadTxt;
87 private final String[] attTblHeaders = {
88 "Attribute Name", "Attribute Value" };
89 private final Integer[] attTblColWidth = { 200, 200 };
91 private ResourceManager resourceManager;
93 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
94 private IGetUIListener getUIListener;
95 private IPutUIListener putUIListener;
96 private IPostUIListener postUIListener;
97 private IObserveUIListener observeUIListener;
98 private IVerificationUIListener verificationUIListener;
99 private IConfigurationUpload configUploadUIListener;
101 private RemoteResource resourceInSelection;
103 public AttributeView() {
104 resourceManager = Activator.getDefault().getResourceManager();
106 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
109 public void onResourceSelectionChange(final RemoteResource resource) {
110 Display.getDefault().asyncExec(new Runnable() {
115 .println("AttributeView: onResourceSelectionChange");
116 resourceInSelection = resource;
118 // Set visibility of manual and automation controls
119 setVisibility((resource == null) ? false : true);
121 // Update the attribute table
122 if (null != attTblViewer
123 && !attTblViewer.getControl().isDisposed()) {
124 System.out.println("viewer is alive");
125 updateViewer(getData(resource));
128 // Update the observe status
129 updateObserve(resource);
131 // Update the pay-load details if any
137 getUIListener = new IGetUIListener() {
140 public void onGetCompleted(final RemoteResource resource) {
141 Display.getDefault().asyncExec(new Runnable() {
146 System.out.println("AttributeView: onGetCompleted");
147 if (null == resource) {
150 // Update the attribute table
151 if (resourceInSelection != resource) {
153 .println("AttributeView: get response arrived for a different resource");
156 updateViewer(getData(resource));
158 // Update the observe status
159 updateObserve(resource);
161 // Update the pay-load details if any
167 public void onGetFailed(RemoteResource resource) {
171 putUIListener = new IPutUIListener() {
174 public void onPutCompleted(final RemoteResource resource) {
175 Display.getDefault().asyncExec(new Runnable() {
180 System.out.println("AttributeView: onPutCompleted");
181 if (null == resource) {
184 // Update the attribute table
185 if (resourceInSelection != resource) {
187 .println("AttributeView: put response arrived for a different resource");
190 updateViewer(getData(resource));
192 // Update the observe status
193 updateObserve(resource);
195 // Update the pay-load details if any
201 public void onPutFailed(RemoteResource resource) {
205 postUIListener = new IPostUIListener() {
208 public void onPostCompleted(final RemoteResource resource) {
209 Display.getDefault().asyncExec(new Runnable() {
214 System.out.println("AttributeView: onPostCompleted");
215 if (null == resource) {
218 // Update the attribute table
219 if (resourceInSelection != resource) {
221 .println("AttributeView: post response arrived for a different resource");
224 updateViewer(getData(resource));
226 // Update the observe status
227 updateObserve(resource);
229 // Update the pay-load details if any
235 public void onPostFailed(RemoteResource resource) {
239 observeUIListener = new IObserveUIListener() {
242 public void onObserveCompleted(final RemoteResource resource) {
243 Display.getDefault().asyncExec(new Runnable() {
248 System.out.println("AttributeView: onObserveCompleted");
249 if (null == resource) {
252 // Update the attribute table
253 if (resourceInSelection != resource) {
255 .println("AttributeView: observe response arrived for a different resource");
258 updateViewer(getData(resource));
260 // Update the observe status
261 updateObserve(resource);
263 // Update the pay-load details if any
269 public void onObserveFailed(RemoteResource resource) {
273 verificationUIListener = new IVerificationUIListener() {
276 public void onVerificationStarted(final RemoteResource resource,
277 final int autoType) {
278 Display.getDefault().asyncExec(new Runnable() {
282 changeReqBtnVisibility(autoType, false);
288 public void onVerificationCompleted(final RemoteResource resource,
289 final int autoType) {
290 Display.getDefault().asyncExec(new Runnable() {
294 changeReqBtnVisibility(autoType, true);
300 public void onVerificationAborted(final RemoteResource resource,
301 final int autoType) {
302 Display.getDefault().asyncExec(new Runnable() {
306 changeReqBtnVisibility(autoType, true);
312 configUploadUIListener = new IConfigurationUpload() {
315 public void onConfigurationUploaded(final RemoteResource resource) {
316 Display.getDefault().asyncExec(new Runnable() {
322 .println("AttributeView: onConfigurationUploaded");
323 if (null == resource) {
326 if (resourceInSelection != resource) {
328 .println("AttributeView: config upload response arrived for a different resource");
331 if (!automateButton.isDisposed()) {
332 automateButton.setEnabled(true);
340 private void changeReqBtnVisibility(int reqType, boolean visibility) {
341 if (reqType == Constants.GET_AUTOMATION_INDEX) {
342 if (!getButton.isDisposed()) {
343 getButton.setEnabled(visibility);
345 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
346 if (!putButton.isDisposed()) {
347 putButton.setEnabled(visibility);
349 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
350 if (!postButton.isDisposed()) {
351 postButton.setEnabled(visibility);
356 private Map<String, RemoteResourceAttribute> getData(RemoteResource resource) {
357 if (null == resource) {
360 Map<String, RemoteResourceAttribute> attMap = resource
361 .getResourceAttributesMap();
362 System.out.println("AttributeView: \n" + attMap);
366 private void updateViewer(Map<String, RemoteResourceAttribute> attMap) {
367 if (null != attTblViewer) {
368 Table tbl = attTblViewer.getTable();
369 if (null != attMap) {
370 attTblViewer.setInput(attMap.entrySet().toArray());
371 if (!tbl.isDisposed()) {
372 tbl.setLinesVisible(true);
375 if (!tbl.isDisposed()) {
377 tbl.setLinesVisible(false);
383 private void updateObserve(RemoteResource resource) {
384 if (null == resource) {
387 boolean observed = resource.isObserved();
388 if (!observeResButton.isDisposed()) {
389 observeResButton.setSelection(observed);
394 public void createPartControl(Composite parent) {
395 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
397 parent.setLayout(new GridLayout(2, false));
398 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
399 parent.setLayoutData(gd);
401 Group attGroup = new Group(parent, SWT.NONE);
402 attGroup.setLayout(new GridLayout());
404 gd.grabExcessHorizontalSpace = true;
405 gd.horizontalAlignment = SWT.FILL;
406 gd.horizontalSpan = 2;
408 attGroup.setLayoutData(gd);
409 attGroup.setText("Attributes");
410 attGroup.setBackground(color);
412 setupAttributeTable(attGroup);
414 setupMessageArea(parent);
416 setupResourceLevelOpsArea(parent);
418 setupTabArea(parent);
422 addManagerListeners();
424 setVisibility(false);
426 // Updating the data in the UI as per the resource in selection.
427 if (null != attTblViewer && !attTblViewer.getControl().isDisposed()) {
428 updateViewer(getData(resourceManager
429 .getCurrentResourceInSelection()));
433 private void setupMessageArea(Composite parent) {
435 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
436 Group msgTypesGrp = new Group(parent, SWT.NONE);
438 gd.grabExcessHorizontalSpace = true;
439 gd.horizontalAlignment = SWT.FILL;
440 msgTypesGrp.setLayoutData(gd);
441 GridLayout grid = new GridLayout(3, false);
442 msgTypesGrp.setLayout(grid);
443 msgTypesGrp.setText("Request Types");
444 msgTypesGrp.setBackground(color);
446 getButton = new Button(msgTypesGrp, SWT.PUSH);
447 getButton.setText("GET");
449 gd.grabExcessHorizontalSpace = true;
450 gd.horizontalAlignment = SWT.FILL;
452 getButton.setLayoutData(gd);
454 putButton = new Button(msgTypesGrp, SWT.PUSH);
455 putButton.setText("PUT");
457 gd.grabExcessHorizontalSpace = true;
458 gd.horizontalAlignment = SWT.FILL;
460 putButton.setLayoutData(gd);
462 postButton = new Button(msgTypesGrp, SWT.PUSH);
463 postButton.setText("POST");
465 gd.grabExcessHorizontalSpace = true;
466 gd.horizontalAlignment = SWT.FILL;
468 postButton.setLayoutData(gd);
471 private void setupResourceLevelOpsArea(Composite parent) {
473 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
474 Group resOpsGrp = new Group(parent, SWT.NONE);
476 gd.grabExcessHorizontalSpace = true;
477 gd.horizontalAlignment = SWT.FILL;
478 resOpsGrp.setLayoutData(gd);
479 GridLayout grid = new GridLayout(2, false);
480 resOpsGrp.setLayout(grid);
481 resOpsGrp.setText("Resource-Level Operations");
482 resOpsGrp.setBackground(color);
484 automateButton = new Button(resOpsGrp, SWT.PUSH);
485 automateButton.setText("Automation");
487 gd.grabExcessHorizontalSpace = true;
488 gd.horizontalAlignment = SWT.FILL;
489 automateButton.setLayoutData(gd);
491 observeResButton = new Button(resOpsGrp, SWT.CHECK);
492 observeResButton.setText("Observe");
494 gd.grabExcessHorizontalSpace = true;
495 gd.horizontalAlignment = SWT.FILL;
496 observeResButton.setLayoutData(gd);
499 private void setupTabArea(final Composite parent) {
501 payloadFolder = new CTabFolder(parent, SWT.BORDER);
502 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
503 gd.horizontalSpan = 2;
504 payloadFolder.setLayoutData(gd);
505 payloadFolder.setSimple(false);
506 payloadFolder.setUnselectedCloseVisible(false);
507 payloadFolder.setUnselectedImageVisible(false);
508 // payloadFolder.setMaximizeVisible(true);
509 // payloadFolder.setMinimizeVisible(true);
511 requestPayloadTab = new CTabItem(payloadFolder, SWT.NULL);
512 requestPayloadTab.setText("Request Payload");
514 requestPayloadTxt = new Text(payloadFolder, SWT.MULTI | SWT.V_SCROLL
515 | SWT.H_SCROLL | SWT.READ_ONLY | SWT.WRAP);
516 requestPayloadTab.setControl(requestPayloadTxt);
518 responsePayloadTab = new CTabItem(payloadFolder, SWT.NULL);
519 responsePayloadTab.setText("Response Payload");
521 responsePayloadTxt = new Text(payloadFolder, SWT.MULTI | SWT.V_SCROLL
522 | SWT.H_SCROLL | SWT.READ_ONLY | SWT.WRAP);
523 responsePayloadTab.setControl(responsePayloadTxt);
527 payloadFolder.addCTabFolder2Listener(new CTabFolder2Adapter() {
529 public void minimize(CTabFolderEvent event) {
530 payloadFolder.setMinimized(true);
531 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
532 gd.horizontalSpan = 2;
533 payloadFolder.setLayoutData(gd);
538 public void maximize(CTabFolderEvent event) {
539 payloadFolder.setMaximized(true);
540 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
541 gd.horizontalSpan = 2;
542 payloadFolder.setLayoutData(gd);
547 public void restore(CTabFolderEvent event) {
548 payloadFolder.setMaximized(false);
549 payloadFolder.setMinimized(false);
550 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
551 gd.horizontalSpan = 2;
552 payloadFolder.setLayoutData(gd);
558 private void setupAttributeTable(Group attGroup) {
559 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
560 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
562 createAttributeColumns(attTblViewer);
564 // make lines and header visible
565 Table table = attTblViewer.getTable();
566 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
567 table.setHeaderVisible(true);
568 table.setLinesVisible(true);
570 attTblViewer.setContentProvider(new AttributeContentProvider());
573 public void createAttributeColumns(TableViewer tableViewer) {
575 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
576 attName.getColumn().setWidth(attTblColWidth[0]);
577 attName.getColumn().setText(attTblHeaders[0]);
578 attName.setLabelProvider(new StyledCellLabelProvider() {
580 public void update(ViewerCell cell) {
581 Object element = cell.getElement();
582 if (element instanceof Map.Entry) {
583 @SuppressWarnings("unchecked")
584 Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
585 cell.setText(entry.getKey());
590 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
592 attValue.getColumn().setWidth(attTblColWidth[1]);
593 attValue.getColumn().setText(attTblHeaders[1]);
594 attValue.setLabelProvider(new StyledCellLabelProvider() {
596 public void update(ViewerCell cell) {
597 Object element = cell.getElement();
598 if (element instanceof Map.Entry) {
599 @SuppressWarnings("unchecked")
600 Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
601 Object value = entry.getValue().getAttributeValue();
605 cell.setText(String.valueOf(value));
612 private void setUIListeners() {
614 getButton.addSelectionListener(new SelectionAdapter() {
616 public void widgetSelected(SelectionEvent e) {
617 if (resourceInSelection.isGetAutomtnInProgress()) {
619 .openInformation(Display.getDefault()
620 .getActiveShell(), "GET Request",
621 "GET Automation is in progress. Please wait or stop the automation.");
623 resourceManager.sendGetRequest(resourceInSelection);
628 putButton.addSelectionListener(new SelectionAdapter() {
630 public void widgetSelected(SelectionEvent e) {
631 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
634 if (resourceInSelection.isPutAutomtnInProgress()) {
636 .openInformation(Display.getDefault()
637 .getActiveShell(), "PUT Request",
638 "PUT Automation is in progress. Please wait or stop the automation.");
641 List<PutPostAttributeModel> putPostModelList;
642 putPostModelList = resourceInSelection
644 if (null == putPostModelList) {
646 .openInformation(Display.getDefault()
647 .getActiveShell(), "PUT Request",
648 "No attributes exist in the resource model.");
651 PutRequestDialog putDialog = new PutRequestDialog(
652 Display.getDefault().getActiveShell(),
654 if (putDialog.open() == Window.OK) {
655 // Call the native PUT method
656 resourceManager.sendPutRequest(resourceInSelection,
664 postButton.addSelectionListener(new SelectionAdapter() {
666 public void widgetSelected(SelectionEvent e) {
667 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
670 if (resourceInSelection.isPostAutomtnInProgress()) {
672 .openInformation(Display.getDefault()
673 .getActiveShell(), "POST Request",
674 "POST Automation is in progress. Please wait or stop the automation.");
677 List<PutPostAttributeModel> putPostModelList;
678 putPostModelList = resourceInSelection
680 if (null == putPostModelList) {
682 .openInformation(Display.getDefault()
683 .getActiveShell(), "PUT Request",
684 "No attributes exist in the resource model.");
688 PostRequestDialog postDialog = new PostRequestDialog(
689 Display.getDefault().getActiveShell(),
691 if (postDialog.open() == Window.OK) {
692 // Call the native POST method
693 resourceManager.sendPostRequest(
694 resourceInSelection, putPostModelList);
701 observeResButton.addSelectionListener(new SelectionAdapter() {
703 public void widgetSelected(SelectionEvent e) {
704 boolean checked = observeResButton.getSelection();
706 resourceManager.sendObserveRequest(resourceInSelection);
709 .sendCancelObserveRequest(resourceInSelection);
714 automateButton.addSelectionListener(new SelectionAdapter() {
716 public void widgetSelected(SelectionEvent e) {
717 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
720 RemoteResource resource = resourceManager
721 .getCurrentResourceInSelection();
722 if (null == resource) {
725 Map<String, Boolean> autoStatus = resourceManager
726 .getAutomationStatus(resource);
727 if (null == autoStatus) {
730 VerificationDialog ad = new VerificationDialog(Display
731 .getDefault().getActiveShell(), autoStatus);
739 private void addManagerListeners() {
741 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
742 resourceManager.addGetUIListener(getUIListener);
743 resourceManager.addPutUIListener(putUIListener);
744 resourceManager.addPostUIListener(postUIListener);
745 resourceManager.addObserveUIListener(observeUIListener);
746 resourceManager.addVerificationUIListener(verificationUIListener);
747 resourceManager.addConfigUploadUIListener(configUploadUIListener);
750 private void setVisibility(boolean visibility) {
751 if (!getButton.isDisposed())
752 getButton.setEnabled(visibility);
753 if (!putButton.isDisposed())
754 putButton.setEnabled(visibility);
755 if (!postButton.isDisposed())
756 postButton.setEnabled(visibility);
757 if (!automateButton.isDisposed()) {
758 if (visibility && null != resourceInSelection
759 && resourceInSelection.isConfigUploaded()) {
760 automateButton.setEnabled(true);
762 automateButton.setEnabled(false);
765 if (!observeResButton.isDisposed())
766 observeResButton.setEnabled(visibility);
769 class AttributeContentProvider implements IStructuredContentProvider {
772 public void dispose() {
776 public void inputChanged(Viewer viewer, Object arg1, Object arg2) {
780 public Object[] getElements(Object element) {
781 return (Object[]) element;
787 public void dispose() {
788 // Unregister the selection listener
789 if (null != resourceSelectionChangedListener) {
791 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
794 // Unregister the GET listener
795 if (null != getUIListener) {
796 resourceManager.removeGetUIListener(getUIListener);
803 public void setFocus() {