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;
21 import oic.simulator.clientcontroller.Activator;
22 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
23 import oic.simulator.clientcontroller.listener.IGetUIListener;
24 import oic.simulator.clientcontroller.listener.IObserveUIListener;
25 import oic.simulator.clientcontroller.listener.IPostUIListener;
26 import oic.simulator.clientcontroller.listener.IPutUIListener;
27 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
28 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
29 import oic.simulator.clientcontroller.manager.ResourceManager;
30 import oic.simulator.clientcontroller.manager.UiListenerHandler;
31 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
32 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
33 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
34 import oic.simulator.clientcontroller.utils.Constants;
35 import oic.simulator.clientcontroller.utils.Utility;
36 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
37 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
38 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.viewers.ILabelProviderListener;
42 import org.eclipse.jface.viewers.ITableLabelProvider;
43 import org.eclipse.jface.viewers.ITreeContentProvider;
44 import org.eclipse.jface.viewers.TreeViewer;
45 import org.eclipse.jface.viewers.Viewer;
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.graphics.Image;
52 import org.eclipse.swt.layout.GridData;
53 import org.eclipse.swt.layout.GridLayout;
54 import org.eclipse.swt.widgets.Button;
55 import org.eclipse.swt.widgets.Composite;
56 import org.eclipse.swt.widgets.Display;
57 import org.eclipse.swt.widgets.Group;
58 import org.eclipse.swt.widgets.Tree;
59 import org.eclipse.swt.widgets.TreeColumn;
60 import org.eclipse.ui.PlatformUI;
61 import org.eclipse.ui.part.ViewPart;
62 import org.oic.simulator.AttributeValue.ValueType;
63 import org.oic.simulator.SimulatorResourceAttribute;
64 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
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 TreeViewer attViewer;
75 private Button getButton;
76 private Button putButton;
77 private Button postButton;
78 private Button automateButton;
79 private Button observeResButton;
81 private final String[] attTblHeaders = {
82 "Attribute Name", "Attribute Value" };
83 private final Integer[] attTblColWidth = { 200, 200 };
85 private ResourceManager resourceManager;
87 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
88 private IGetUIListener getUIListener;
89 private IPutUIListener putUIListener;
90 private IPostUIListener postUIListener;
91 private IObserveUIListener observeUIListener;
92 private IVerificationUIListener verificationUIListener;
93 private IConfigurationUpload configUploadUIListener;
95 private RemoteResource resourceInSelection;
97 public AttributeView() {
98 resourceManager = Activator.getDefault().getResourceManager();
100 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
103 public void onResourceSelectionChange(final RemoteResource resource) {
104 Display.getDefault().asyncExec(new Runnable() {
108 resourceInSelection = resource;
110 // Set visibility of manual and automation controls
111 setVisibility((resource == null) ? false : true);
113 // Update the attribute table
114 if (null != attViewer
115 && !attViewer.getControl().isDisposed()) {
116 updateViewer(resource);
119 // Update the observe status
120 updateObserve(resource);
126 getUIListener = new IGetUIListener() {
129 public void onGetCompleted(final RemoteResource resource) {
130 Display.getDefault().asyncExec(new Runnable() {
134 if (null == resource) {
137 // Update the attribute table
138 if (resourceInSelection != resource) {
141 updateViewer(resource);
143 // Update the observe status
144 updateObserve(resource);
150 putUIListener = new IPutUIListener() {
153 public void onPutCompleted(final RemoteResource resource) {
154 Display.getDefault().asyncExec(new Runnable() {
158 if (null == resource) {
161 // Update the attribute table
162 if (resourceInSelection != resource) {
165 updateViewer(resource);
167 // Update the observe status
168 updateObserve(resource);
174 postUIListener = new IPostUIListener() {
177 public void onPostCompleted(final RemoteResource resource) {
178 Display.getDefault().asyncExec(new Runnable() {
182 if (null == resource) {
185 // Update the attribute table
186 if (resourceInSelection != resource) {
189 updateViewer(resource);
191 // Update the observe status
192 updateObserve(resource);
198 observeUIListener = new IObserveUIListener() {
201 public void onObserveCompleted(final RemoteResource resource) {
202 Display.getDefault().asyncExec(new Runnable() {
206 if (null == resource) {
209 // Update the attribute table
210 if (resourceInSelection != resource) {
213 updateViewer(resource);
215 // Update the observe status
216 updateObserve(resource);
222 verificationUIListener = new IVerificationUIListener() {
225 public void onVerificationStarted(final RemoteResource resource,
226 final int autoType) {
227 Display.getDefault().asyncExec(new Runnable() {
231 // changeReqBtnVisibility(autoType, false);
237 public void onVerificationCompleted(final RemoteResource resource,
238 final int autoType) {
239 Display.getDefault().asyncExec(new Runnable() {
243 // changeReqBtnVisibility(autoType, true);
249 public void onVerificationAborted(final RemoteResource resource,
250 final int autoType) {
251 Display.getDefault().asyncExec(new Runnable() {
255 // changeReqBtnVisibility(autoType, true);
261 configUploadUIListener = new IConfigurationUpload() {
264 public void onConfigurationUploaded(final RemoteResource resource) {
265 Display.getDefault().asyncExec(new Runnable() {
269 if (null == resource) {
272 if (resourceInSelection != resource) {
275 if (!automateButton.isDisposed()) {
276 automateButton.setEnabled(true);
284 private void changeReqBtnVisibility(int reqType, boolean visibility) {
285 if (reqType == Constants.GET_AUTOMATION_INDEX) {
286 if (!getButton.isDisposed()) {
287 getButton.setEnabled(visibility);
289 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
290 if (!putButton.isDisposed()) {
291 putButton.setEnabled(visibility);
293 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
294 if (!postButton.isDisposed()) {
295 postButton.setEnabled(visibility);
300 private void updateViewer(RemoteResource resource) {
301 if (null == attViewer) {
304 Tree tree = attViewer.getTree();
305 if (null != resource) {
306 attViewer.setInput(resource.getResourceRepresentation());
307 attViewer.expandAll();
308 if (!tree.isDisposed()) {
309 tree.setLinesVisible(true);
312 if (!tree.isDisposed()) {
314 tree.setLinesVisible(false);
319 private void updateObserve(RemoteResource resource) {
320 if (null == resource || observeResButton.isDisposed()) {
323 boolean observed = resource.isObserved();
325 observeResButton.setText(Constants.STOP_OBSERVE);
327 observeResButton.setText(Constants.OBSERVE);
332 public void createPartControl(Composite parent) {
333 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
335 parent.setLayout(new GridLayout());
336 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
337 parent.setLayoutData(gd);
339 Group attGroup = new Group(parent, SWT.NONE);
340 attGroup.setLayout(new GridLayout());
342 gd.grabExcessHorizontalSpace = true;
343 gd.horizontalAlignment = SWT.FILL;
344 gd.grabExcessVerticalSpace = true;
345 gd.verticalAlignment = SWT.FILL;
346 gd.horizontalSpan = 2;
347 // gd.heightHint = 175;
348 attGroup.setLayoutData(gd);
349 attGroup.setText("Attributes");
350 attGroup.setBackground(color);
352 setupAttributeTable(attGroup);
354 setupRequestControls(parent);
358 addManagerListeners();
360 setVisibility(false);
362 // Updating the data in the UI as per the resource in selection.
363 if (null != attViewer && !attViewer.getControl().isDisposed()) {
364 updateViewer(resourceManager.getCurrentResourceInSelection());
368 private void setupRequestControls(Composite parent) {
370 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
371 Composite opsComp = new Composite(parent, SWT.NONE);
373 gd.grabExcessHorizontalSpace = true;
374 gd.horizontalAlignment = SWT.FILL;
375 opsComp.setLayoutData(gd);
376 GridLayout grid = new GridLayout(5, false);
377 opsComp.setLayout(grid);
378 opsComp.setBackground(color);
380 getButton = new Button(opsComp, SWT.PUSH);
381 getButton.setText("GET");
383 gd.grabExcessHorizontalSpace = true;
384 gd.horizontalAlignment = SWT.FILL;
386 getButton.setLayoutData(gd);
388 putButton = new Button(opsComp, SWT.PUSH);
389 putButton.setText("PUT");
391 gd.grabExcessHorizontalSpace = true;
392 gd.horizontalAlignment = SWT.FILL;
394 putButton.setLayoutData(gd);
396 postButton = new Button(opsComp, SWT.PUSH);
397 postButton.setText("POST");
399 gd.grabExcessHorizontalSpace = true;
400 gd.horizontalAlignment = SWT.FILL;
402 postButton.setLayoutData(gd);
404 observeResButton = new Button(opsComp, SWT.PUSH);
405 observeResButton.setText(Constants.OBSERVE);
407 gd.grabExcessHorizontalSpace = true;
408 gd.horizontalAlignment = SWT.FILL;
409 observeResButton.setLayoutData(gd);
411 automateButton = new Button(opsComp, SWT.PUSH);
412 automateButton.setText("Automation");
414 gd.grabExcessHorizontalSpace = true;
415 gd.horizontalAlignment = SWT.FILL;
416 automateButton.setLayoutData(gd);
419 private void setupAttributeTable(Group attGroup) {
420 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
421 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
422 addressTree.setHeaderVisible(true);
424 attViewer = new TreeViewer(addressTree);
426 createAttributeColumns(attViewer);
428 // make lines and header visible
429 Tree tree = attViewer.getTree();
430 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
431 tree.setHeaderVisible(true);
432 tree.setLinesVisible(true);
434 attViewer.setContentProvider(new AttributeContentProvider());
435 attViewer.setLabelProvider(new AttributeLabelProvider());
438 public void createAttributeColumns(TreeViewer viewer) {
439 Tree tree = viewer.getTree();
441 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
442 attName.setWidth(attTblColWidth[0]);
443 attName.setText(attTblHeaders[0]);
445 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
446 attValue.setWidth(attTblColWidth[1]);
447 attValue.setText(attTblHeaders[1]);
450 private void setUIListeners() {
452 getButton.addSelectionListener(new SelectionAdapter() {
454 public void widgetSelected(SelectionEvent e) {
455 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
458 resourceManager.sendGetRequest(resourceInSelection);
464 putButton.addSelectionListener(new SelectionAdapter() {
466 public void widgetSelected(SelectionEvent e) {
467 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
470 boolean attributesExist = false;
471 ResourceRepresentation rep = resourceInSelection
472 .getResourceRepresentation();
474 Map<String, AttributeElement> attributes = rep
476 if (null != attributes && !attributes.isEmpty()) {
477 attributesExist = true;
480 if (!attributesExist) {
482 .openInformation(Display.getDefault()
483 .getActiveShell(), "PUT Request",
484 "No attributes exist in the resource model.");
487 PutRequestDialog putDialog = new PutRequestDialog(
488 Display.getDefault().getActiveShell());
489 if (putDialog.open() == Window.OK) {
490 resourceManager.sendPutRequest(resourceInSelection,
491 putDialog.getUpdatedRepresentation()
499 postButton.addSelectionListener(new SelectionAdapter() {
501 public void widgetSelected(SelectionEvent e) {
502 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
505 boolean attributesExist = false;
506 ResourceRepresentation rep = resourceInSelection
507 .getResourceRepresentation();
509 Map<String, AttributeElement> attributes = rep
511 if (null != attributes && !attributes.isEmpty()) {
512 attributesExist = true;
515 if (!attributesExist) {
517 .openInformation(Display.getDefault()
518 .getActiveShell(), "POST Request",
519 "No attributes exist in the resource model.");
523 PostRequestDialog postDialog = new PostRequestDialog(
524 Display.getDefault().getActiveShell());
525 if (postDialog.open() == Window.OK) {
526 ResourceRepresentation representation = postDialog
527 .getUpdatedRepresentation();
528 resourceManager.sendPostRequest(
530 representation.getSelectedModel());
537 observeResButton.addSelectionListener(new SelectionAdapter() {
539 public void widgetSelected(SelectionEvent e) {
541 if (observeResButton.getText().equals(Constants.OBSERVE)) {
542 result = resourceManager
543 .sendObserveRequest(resourceInSelection);
545 observeResButton.setText(Constants.STOP_OBSERVE);
547 MessageDialog.openError(Display.getDefault()
548 .getActiveShell(), "Observe failed",
549 "Failed to observe the resource. Try again.");
552 result = resourceManager.sendCancelObserveRequest(
553 resourceInSelection, true);
555 observeResButton.setText(Constants.OBSERVE);
558 .openError(Display.getDefault()
560 "Cancel Observe failed",
561 "Failed to stop observing the resource. Try again.");
567 automateButton.addSelectionListener(new SelectionAdapter() {
569 public void widgetSelected(SelectionEvent e) {
570 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
573 RemoteResource resource = resourceManager
574 .getCurrentResourceInSelection();
575 if (null == resource) {
578 Map<String, Boolean> autoStatus = resourceManager
579 .getAutomationStatus(resource);
580 if (null == autoStatus) {
586 boolean startGet, startPut, startPost;
587 boolean stopGet, stopPut, stopPost;
588 startGet = startPut = startPost = false;
589 stopGet = stopPut = stopPost = false;
590 String status = null;
591 String startMsg = "Verification will be started for: ";
592 String stopMsg = "Verification will be stopped for: ";
593 VerificationDialog ad = new VerificationDialog(Display
594 .getDefault().getActiveShell(), autoStatus);
595 if (ad.open() == Window.OK) {
596 Map<String, Boolean> oldStatus = resourceManager
597 .getAutomationStatus(resource);
598 if (null == oldStatus || oldStatus.size() < 1) {
599 status = "Failed to perform the requested operation.";
602 if (oldStatus.get(Constants.GET) != autoStatus
603 .get(Constants.GET)) {
604 if (autoStatus.get(Constants.GET)) {
605 startMsg += Constants.GET;
609 stopMsg += Constants.GET;
615 if (oldStatus.get(Constants.PUT) != autoStatus
616 .get(Constants.PUT)) {
617 if (autoStatus.get(Constants.PUT)) {
618 if (startCount == 1) {
621 startMsg += Constants.PUT;
625 if (stopCount == 1) {
628 stopMsg += Constants.PUT;
635 if (oldStatus.get(Constants.POST) != autoStatus
636 .get(Constants.POST)) {
637 if (autoStatus.get(Constants.POST)) {
638 if (startCount > 0) {
641 startMsg += Constants.POST;
648 stopMsg += Constants.POST;
653 if (startCount > 0) {
654 status = startMsg + ".";
657 if (startCount <= 0) {
660 status += "\n" + stopMsg + ".";
664 if (startCount == 0 && stopCount == 0) {
665 MessageDialog.openInformation(Display
666 .getDefault().getActiveShell(),
667 "Verification", "No New Changes.");
669 boolean answer = MessageDialog.openQuestion(
670 Display.getDefault().getActiveShell(),
671 "Verification", status
672 + "\nDo you want to proceed?");
674 if (startGet || stopGet)
675 automate(VerificationType.GET,
676 autoStatus.get(Constants.GET));
677 if (startPut || stopPut)
678 automate(VerificationType.PUT,
679 autoStatus.get(Constants.PUT));
680 if (startPost || stopPost)
681 automate(VerificationType.POST,
682 autoStatus.get(Constants.POST));
692 private void automate(VerificationType type, boolean start) {
694 resourceManager.startAutomationRequest(type, resourceInSelection);
696 resourceManager.stopAutomationRequest(type, resourceInSelection);
700 private void addManagerListeners() {
701 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
702 resourceSelectionChangedListener);
703 UiListenerHandler.getInstance().addGetUIListener(getUIListener);
704 UiListenerHandler.getInstance().addPutUIListener(putUIListener);
705 UiListenerHandler.getInstance().addPostUIListener(postUIListener);
706 UiListenerHandler.getInstance().addObserveUIListener(observeUIListener);
707 UiListenerHandler.getInstance().addVerificationUIListener(
708 verificationUIListener);
709 UiListenerHandler.getInstance().addConfigUploadUIListener(
710 configUploadUIListener);
713 private void setVisibility(boolean visibility) {
714 if (!getButton.isDisposed())
715 getButton.setEnabled(visibility);
716 if (!putButton.isDisposed())
717 putButton.setEnabled(visibility);
718 if (!postButton.isDisposed())
719 postButton.setEnabled(visibility);
720 if (!automateButton.isDisposed()) {
721 if (visibility && null != resourceInSelection
722 && resourceInSelection.isConfigUploaded()) {
723 automateButton.setEnabled(true);
725 automateButton.setEnabled(false);
728 if (!observeResButton.isDisposed())
729 observeResButton.setEnabled(visibility);
732 class AttributeContentProvider implements ITreeContentProvider {
735 public void dispose() {
739 public void inputChanged(Viewer viewer, Object oldAttribute,
740 Object newAttribute) {
744 public Object[] getChildren(Object attribute) {
745 if (attribute instanceof AttributeElement) {
746 return ((AttributeElement) attribute).getChildren().values()
750 return new Object[0];
754 public Object getParent(Object attribute) {
755 if (attribute instanceof AttributeElement)
756 return ((AttributeElement) attribute).getParent();
761 public boolean hasChildren(Object attribute) {
762 if (attribute instanceof AttributeElement)
763 return ((AttributeElement) attribute).hasChildren();
768 public Object[] getElements(Object resourceModel) {
769 if (resourceModel instanceof ResourceRepresentation) {
770 return ((ResourceRepresentation) resourceModel).getAttributes()
774 return new Object[0];
778 class AttributeLabelProvider implements ITableLabelProvider {
781 public void addListener(ILabelProviderListener arg0) {
785 public void dispose() {
789 public boolean isLabelProperty(Object arg0, String arg1) {
794 public void removeListener(ILabelProviderListener arg0) {
799 public Image getColumnImage(Object element, int col) {
804 public String getColumnText(Object element, int column) {
805 if (element instanceof AttributeElement) {
806 AttributeElement attrElement = (AttributeElement) element;
808 case 0: // Attribute name column
810 SimulatorResourceAttribute attribute = attrElement
811 .getSimulatorResourceAttribute();
812 return attribute.name();
815 case 1: // Attribute value column
817 SimulatorResourceAttribute attribute = attrElement
818 .getSimulatorResourceAttribute();
820 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
821 return Utility.getAttributeValueAsString(attribute
832 public void dispose() {
833 // Unregister the selection listener
834 if (null != resourceSelectionChangedListener) {
835 UiListenerHandler.getInstance()
836 .removeResourceSelectionChangedUIListener(
837 resourceSelectionChangedListener);
840 // Unregister the GET listener
841 if (null != getUIListener) {
842 UiListenerHandler.getInstance().removeGetUIListener(getUIListener);
849 public void setFocus() {