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.PostRequestDialog;
36 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
37 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
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 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
63 * This class manages and shows the attribute view in the perspective.
65 public class AttributeView extends ViewPart {
67 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.attribute";
69 private TableViewer attTblViewer;
71 private Button getButton;
72 private Button putButton;
73 private Button postButton;
74 private Button automateButton;
75 private Button observeResButton;
77 private final String[] attTblHeaders = {
78 "Attribute Name", "Attribute Value" };
79 private final Integer[] attTblColWidth = { 200, 200 };
81 private ResourceManager resourceManager;
83 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
84 private IGetUIListener getUIListener;
85 private IPutUIListener putUIListener;
86 private IPostUIListener postUIListener;
87 private IObserveUIListener observeUIListener;
88 private IVerificationUIListener verificationUIListener;
89 private IConfigurationUpload configUploadUIListener;
91 private RemoteResource resourceInSelection;
93 public AttributeView() {
94 resourceManager = Activator.getDefault().getResourceManager();
96 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
99 public void onResourceSelectionChange(final RemoteResource resource) {
100 Display.getDefault().asyncExec(new Runnable() {
104 resourceInSelection = resource;
106 // Set visibility of manual and automation controls
107 setVisibility((resource == null) ? false : true);
109 // Update the attribute table
110 if (null != attTblViewer
111 && !attTblViewer.getControl().isDisposed()) {
112 updateViewer(getData(resource));
115 // Update the observe status
116 updateObserve(resource);
122 getUIListener = new IGetUIListener() {
125 public void onGetCompleted(final RemoteResource resource) {
126 Display.getDefault().asyncExec(new Runnable() {
130 if (null == resource) {
133 // Update the attribute table
134 if (resourceInSelection != resource) {
137 updateViewer(getData(resource));
139 // Update the observe status
140 updateObserve(resource);
146 putUIListener = new IPutUIListener() {
149 public void onPutCompleted(final RemoteResource resource) {
150 Display.getDefault().asyncExec(new Runnable() {
154 if (null == resource) {
157 // Update the attribute table
158 if (resourceInSelection != resource) {
161 updateViewer(getData(resource));
163 // Update the observe status
164 updateObserve(resource);
170 postUIListener = new IPostUIListener() {
173 public void onPostCompleted(final RemoteResource resource) {
174 Display.getDefault().asyncExec(new Runnable() {
178 if (null == resource) {
181 // Update the attribute table
182 if (resourceInSelection != resource) {
185 updateViewer(getData(resource));
187 // Update the observe status
188 updateObserve(resource);
194 observeUIListener = new IObserveUIListener() {
197 public void onObserveCompleted(final RemoteResource resource) {
198 Display.getDefault().asyncExec(new Runnable() {
202 if (null == resource) {
205 // Update the attribute table
206 if (resourceInSelection != resource) {
209 updateViewer(getData(resource));
211 // Update the observe status
212 updateObserve(resource);
218 verificationUIListener = new IVerificationUIListener() {
221 public void onVerificationStarted(final RemoteResource resource,
222 final int autoType) {
223 Display.getDefault().asyncExec(new Runnable() {
227 // changeReqBtnVisibility(autoType, false);
233 public void onVerificationCompleted(final RemoteResource resource,
234 final int autoType) {
235 Display.getDefault().asyncExec(new Runnable() {
239 // changeReqBtnVisibility(autoType, true);
245 public void onVerificationAborted(final RemoteResource resource,
246 final int autoType) {
247 Display.getDefault().asyncExec(new Runnable() {
251 // changeReqBtnVisibility(autoType, true);
257 configUploadUIListener = new IConfigurationUpload() {
260 public void onConfigurationUploaded(final RemoteResource resource) {
261 Display.getDefault().asyncExec(new Runnable() {
265 if (null == resource) {
268 if (resourceInSelection != resource) {
271 if (!automateButton.isDisposed()) {
272 automateButton.setEnabled(true);
280 private void changeReqBtnVisibility(int reqType, boolean visibility) {
281 if (reqType == Constants.GET_AUTOMATION_INDEX) {
282 if (!getButton.isDisposed()) {
283 getButton.setEnabled(visibility);
285 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
286 if (!putButton.isDisposed()) {
287 putButton.setEnabled(visibility);
289 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
290 if (!postButton.isDisposed()) {
291 postButton.setEnabled(visibility);
296 private Map<String, RemoteResourceAttribute> getData(RemoteResource resource) {
297 if (null == resource) {
300 Map<String, RemoteResourceAttribute> attMap = resource
301 .getResourceAttributesMap();
305 private void updateViewer(Map<String, RemoteResourceAttribute> attMap) {
306 if (null != attTblViewer) {
307 Table tbl = attTblViewer.getTable();
308 if (null != attMap) {
309 attTblViewer.setInput(attMap.entrySet().toArray());
310 if (!tbl.isDisposed()) {
311 tbl.setLinesVisible(true);
314 if (!tbl.isDisposed()) {
316 tbl.setLinesVisible(false);
322 private void updateObserve(RemoteResource resource) {
323 if (null == resource || observeResButton.isDisposed()) {
326 boolean observed = resource.isObserved();
328 observeResButton.setText(Constants.STOP_OBSERVE);
330 observeResButton.setText(Constants.OBSERVE);
335 public void createPartControl(Composite parent) {
336 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
338 parent.setLayout(new GridLayout());
339 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
340 parent.setLayoutData(gd);
342 Group attGroup = new Group(parent, SWT.NONE);
343 attGroup.setLayout(new GridLayout());
345 gd.grabExcessHorizontalSpace = true;
346 gd.horizontalAlignment = SWT.FILL;
347 gd.grabExcessVerticalSpace = true;
348 gd.verticalAlignment = SWT.FILL;
349 gd.horizontalSpan = 2;
350 // gd.heightHint = 175;
351 attGroup.setLayoutData(gd);
352 attGroup.setText("Attributes");
353 attGroup.setBackground(color);
355 setupAttributeTable(attGroup);
357 setupRequestControls(parent);
361 addManagerListeners();
363 setVisibility(false);
365 // Updating the data in the UI as per the resource in selection.
366 if (null != attTblViewer && !attTblViewer.getControl().isDisposed()) {
367 updateViewer(getData(resourceManager
368 .getCurrentResourceInSelection()));
372 private void setupRequestControls(Composite parent) {
374 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
375 Composite opsComp = new Composite(parent, SWT.NONE);
377 gd.grabExcessHorizontalSpace = true;
378 gd.horizontalAlignment = SWT.FILL;
379 opsComp.setLayoutData(gd);
380 GridLayout grid = new GridLayout(5, false);
381 opsComp.setLayout(grid);
382 opsComp.setBackground(color);
384 getButton = new Button(opsComp, SWT.PUSH);
385 getButton.setText("GET");
387 gd.grabExcessHorizontalSpace = true;
388 gd.horizontalAlignment = SWT.FILL;
390 getButton.setLayoutData(gd);
392 putButton = new Button(opsComp, SWT.PUSH);
393 putButton.setText("PUT");
395 gd.grabExcessHorizontalSpace = true;
396 gd.horizontalAlignment = SWT.FILL;
398 putButton.setLayoutData(gd);
400 postButton = new Button(opsComp, SWT.PUSH);
401 postButton.setText("POST");
403 gd.grabExcessHorizontalSpace = true;
404 gd.horizontalAlignment = SWT.FILL;
406 postButton.setLayoutData(gd);
408 observeResButton = new Button(opsComp, SWT.PUSH);
409 observeResButton.setText(Constants.OBSERVE);
411 gd.grabExcessHorizontalSpace = true;
412 gd.horizontalAlignment = SWT.FILL;
413 observeResButton.setLayoutData(gd);
415 automateButton = new Button(opsComp, SWT.PUSH);
416 automateButton.setText("Automation");
418 gd.grabExcessHorizontalSpace = true;
419 gd.horizontalAlignment = SWT.FILL;
420 automateButton.setLayoutData(gd);
423 private void setupAttributeTable(Group attGroup) {
424 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
425 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
427 createAttributeColumns(attTblViewer);
429 // make lines and header visible
430 Table table = attTblViewer.getTable();
431 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
432 table.setHeaderVisible(true);
433 table.setLinesVisible(true);
435 attTblViewer.setContentProvider(new AttributeContentProvider());
438 public void createAttributeColumns(TableViewer tableViewer) {
440 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
441 attName.getColumn().setWidth(attTblColWidth[0]);
442 attName.getColumn().setText(attTblHeaders[0]);
443 attName.setLabelProvider(new StyledCellLabelProvider() {
445 public void update(ViewerCell cell) {
446 Object element = cell.getElement();
447 if (element instanceof Map.Entry) {
448 @SuppressWarnings("unchecked")
449 Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
450 cell.setText(entry.getKey());
455 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
457 attValue.getColumn().setWidth(attTblColWidth[1]);
458 attValue.getColumn().setText(attTblHeaders[1]);
459 attValue.setLabelProvider(new StyledCellLabelProvider() {
461 public void update(ViewerCell cell) {
462 Object element = cell.getElement();
463 if (element instanceof Map.Entry) {
464 @SuppressWarnings("unchecked")
465 Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
466 Object value = entry.getValue().getAttributeValue();
470 cell.setText(String.valueOf(value));
477 private void setUIListeners() {
479 getButton.addSelectionListener(new SelectionAdapter() {
481 public void widgetSelected(SelectionEvent e) {
482 if (resourceInSelection.isGetAutomtnInProgress()) {
484 .openInformation(Display.getDefault()
485 .getActiveShell(), "GET Request",
486 "GET Automation is in progress. Please wait or stop the automation.");
488 resourceManager.sendGetRequest(resourceInSelection);
493 putButton.addSelectionListener(new SelectionAdapter() {
495 public void widgetSelected(SelectionEvent e) {
496 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
499 if (resourceInSelection.isPutAutomtnInProgress()) {
501 .openInformation(Display.getDefault()
502 .getActiveShell(), "PUT Request",
503 "PUT Automation is in progress. Please wait or stop the automation.");
506 List<PutPostAttributeModel> putPostModelList;
507 putPostModelList = resourceInSelection
509 if (null == putPostModelList) {
511 .openInformation(Display.getDefault()
512 .getActiveShell(), "PUT Request",
513 "No attributes exist in the resource model.");
516 PutRequestDialog putDialog = new PutRequestDialog(
517 Display.getDefault().getActiveShell(),
519 if (putDialog.open() == Window.OK) {
520 // Call the native PUT method
521 resourceManager.sendPutRequest(resourceInSelection,
529 postButton.addSelectionListener(new SelectionAdapter() {
531 public void widgetSelected(SelectionEvent e) {
532 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
535 if (resourceInSelection.isPostAutomtnInProgress()) {
537 .openInformation(Display.getDefault()
538 .getActiveShell(), "POST Request",
539 "POST Automation is in progress. Please wait or stop the automation.");
542 List<PutPostAttributeModel> putPostModelList;
543 putPostModelList = resourceInSelection
545 if (null == putPostModelList) {
547 .openInformation(Display.getDefault()
548 .getActiveShell(), "PUT Request",
549 "No attributes exist in the resource model.");
553 PostRequestDialog postDialog = new PostRequestDialog(
554 Display.getDefault().getActiveShell(),
556 if (postDialog.open() == Window.OK) {
557 // Call the native POST method
558 resourceManager.sendPostRequest(
559 resourceInSelection, putPostModelList);
566 observeResButton.addSelectionListener(new SelectionAdapter() {
568 public void widgetSelected(SelectionEvent e) {
570 if (observeResButton.getText().equals(Constants.OBSERVE)) {
571 result = resourceManager
572 .sendObserveRequest(resourceInSelection);
574 observeResButton.setText(Constants.STOP_OBSERVE);
576 MessageDialog.openError(Display.getDefault()
577 .getActiveShell(), "Observe failed",
578 "Failed to observe the resource. Try again.");
581 result = resourceManager.sendCancelObserveRequest(
582 resourceInSelection, true);
584 observeResButton.setText(Constants.OBSERVE);
587 .openError(Display.getDefault()
589 "Cancel Observe failed",
590 "Failed to stop observing the resource. Try again.");
596 automateButton.addSelectionListener(new SelectionAdapter() {
598 public void widgetSelected(SelectionEvent e) {
599 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
602 RemoteResource resource = resourceManager
603 .getCurrentResourceInSelection();
604 if (null == resource) {
607 Map<String, Boolean> autoStatus = resourceManager
608 .getAutomationStatus(resource);
609 if (null == autoStatus) {
615 boolean startGet, startPut, startPost;
616 boolean stopGet, stopPut, stopPost;
617 startGet = startPut = startPost = false;
618 stopGet = stopPut = stopPost = false;
619 String status = null;
620 String startMsg = "Verification will be started for: ";
621 String stopMsg = "Verification will be stopped for: ";
622 VerificationDialog ad = new VerificationDialog(Display
623 .getDefault().getActiveShell(), autoStatus);
624 if (ad.open() == Window.OK) {
625 Map<String, Boolean> oldStatus = resourceManager
626 .getAutomationStatus(resource);
627 if (null == oldStatus || oldStatus.size() < 1) {
628 status = "Failed to perform the requested operation.";
631 if (oldStatus.get(Constants.GET) != autoStatus
632 .get(Constants.GET)) {
633 if (autoStatus.get(Constants.GET)) {
634 startMsg += Constants.GET;
638 stopMsg += Constants.GET;
644 if (oldStatus.get(Constants.PUT) != autoStatus
645 .get(Constants.PUT)) {
646 if (autoStatus.get(Constants.PUT)) {
647 if (startCount == 1) {
650 startMsg += Constants.PUT;
654 if (stopCount == 1) {
657 stopMsg += Constants.PUT;
664 if (oldStatus.get(Constants.POST) != autoStatus
665 .get(Constants.POST)) {
666 if (autoStatus.get(Constants.POST)) {
667 if (startCount > 0) {
670 startMsg += Constants.POST;
677 stopMsg += Constants.POST;
682 if (startCount > 0) {
683 status = startMsg + ".";
686 if (startCount <= 0) {
689 status += "\n" + stopMsg + ".";
693 if (startCount == 0 && stopCount == 0) {
694 MessageDialog.openInformation(Display
695 .getDefault().getActiveShell(),
696 "Verification", "No New Changes.");
698 boolean answer = MessageDialog.openQuestion(
699 Display.getDefault().getActiveShell(),
700 "Verification", status
701 + "\nDo you want to proceed?");
703 if (startGet || stopGet)
704 automate(VerificationType.GET,
705 autoStatus.get(Constants.GET));
706 if (startPut || stopPut)
707 automate(VerificationType.PUT,
708 autoStatus.get(Constants.PUT));
709 if (startPost || stopPost)
710 automate(VerificationType.POST,
711 autoStatus.get(Constants.POST));
721 private void automate(VerificationType type, boolean start) {
723 resourceManager.startAutomationRequest(type, resourceInSelection);
725 resourceManager.stopAutomationRequest(type, resourceInSelection);
729 private void addManagerListeners() {
731 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
732 resourceManager.addGetUIListener(getUIListener);
733 resourceManager.addPutUIListener(putUIListener);
734 resourceManager.addPostUIListener(postUIListener);
735 resourceManager.addObserveUIListener(observeUIListener);
736 resourceManager.addVerificationUIListener(verificationUIListener);
737 resourceManager.addConfigUploadUIListener(configUploadUIListener);
740 private void setVisibility(boolean visibility) {
741 if (!getButton.isDisposed())
742 getButton.setEnabled(visibility);
743 if (!putButton.isDisposed())
744 putButton.setEnabled(visibility);
745 if (!postButton.isDisposed())
746 postButton.setEnabled(visibility);
747 if (!automateButton.isDisposed()) {
748 if (visibility && null != resourceInSelection
749 && resourceInSelection.isConfigUploaded()) {
750 automateButton.setEnabled(true);
752 automateButton.setEnabled(false);
755 if (!observeResButton.isDisposed())
756 observeResButton.setEnabled(visibility);
759 class AttributeContentProvider implements IStructuredContentProvider {
762 public void dispose() {
766 public void inputChanged(Viewer viewer, Object arg1, Object arg2) {
770 public Object[] getElements(Object element) {
771 return (Object[]) element;
777 public void dispose() {
778 // Unregister the selection listener
779 if (null != resourceSelectionChangedListener) {
781 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
784 // Unregister the GET listener
785 if (null != getUIListener) {
786 resourceManager.removeGetUIListener(getUIListener);
793 public void setFocus() {