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.Date;
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.manager.UiListenerHandler;
32 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
33 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
34 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
35 import oic.simulator.clientcontroller.utils.Constants;
36 import oic.simulator.clientcontroller.utils.Utility;
37 import oic.simulator.clientcontroller.view.dialogs.GetRequestDialog;
38 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
39 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
40 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
42 import org.eclipse.jface.dialogs.MessageDialog;
43 import org.eclipse.jface.viewers.ILabelProviderListener;
44 import org.eclipse.jface.viewers.ITableLabelProvider;
45 import org.eclipse.jface.viewers.ITreeContentProvider;
46 import org.eclipse.jface.viewers.TreeViewer;
47 import org.eclipse.jface.viewers.Viewer;
48 import org.eclipse.jface.window.Window;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.events.SelectionAdapter;
51 import org.eclipse.swt.events.SelectionEvent;
52 import org.eclipse.swt.graphics.Color;
53 import org.eclipse.swt.graphics.Image;
54 import org.eclipse.swt.layout.GridData;
55 import org.eclipse.swt.layout.GridLayout;
56 import org.eclipse.swt.widgets.Button;
57 import org.eclipse.swt.widgets.Composite;
58 import org.eclipse.swt.widgets.Display;
59 import org.eclipse.swt.widgets.Group;
60 import org.eclipse.swt.widgets.Tree;
61 import org.eclipse.swt.widgets.TreeColumn;
62 import org.eclipse.ui.PlatformUI;
63 import org.eclipse.ui.part.ViewPart;
64 import org.oic.simulator.AttributeValue.ValueType;
65 import org.oic.simulator.ILogger.Level;
66 import org.oic.simulator.SimulatorResourceAttribute;
67 import org.oic.simulator.client.SimulatorRemoteResource;
68 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
71 * This class manages and shows the attribute view in the perspective.
73 public class AttributeView extends ViewPart {
75 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.attribute";
77 private TreeViewer attViewer;
79 private Button getButton;
80 private Button putButton;
81 private Button postButton;
82 private Button automateButton;
83 private Button observeResButton;
85 private final String[] attTblHeaders = {
86 "Attribute Name", "Attribute Value" };
87 private final Integer[] attTblColWidth = { 200, 200 };
89 private ResourceManager resourceManager;
91 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
92 private IGetUIListener getUIListener;
93 private IPutUIListener putUIListener;
94 private IPostUIListener postUIListener;
95 private IObserveUIListener observeUIListener;
96 private IVerificationUIListener verificationUIListener;
97 private IConfigurationUpload configUploadUIListener;
99 private RemoteResource resourceInSelection;
101 public AttributeView() {
102 resourceManager = Activator.getDefault().getResourceManager();
104 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
107 public void onResourceSelectionChange(final RemoteResource resource) {
108 Display.getDefault().asyncExec(new Runnable() {
112 resourceInSelection = resource;
114 // Set visibility of manual and automation controls
115 setVisibility((resource == null) ? false : true);
117 // Update the attribute table
118 if (null != attViewer
119 && !attViewer.getControl().isDisposed()) {
120 updateViewer(resource);
123 // Update the observe status
124 updateObserve(resource);
130 getUIListener = new IGetUIListener() {
133 public void onGetCompleted(final RemoteResource resource) {
134 Display.getDefault().asyncExec(new Runnable() {
138 if (null == resource) {
141 // Update the attribute table
142 if (resourceInSelection != resource) {
145 updateViewer(resource);
147 // Update the observe status
148 updateObserve(resource);
154 putUIListener = new IPutUIListener() {
157 public void onPutCompleted(final RemoteResource resource) {
158 Display.getDefault().asyncExec(new Runnable() {
162 if (null == resource) {
165 // Update the attribute table
166 if (resourceInSelection != resource) {
169 updateViewer(resource);
171 // Update the observe status
172 updateObserve(resource);
178 postUIListener = new IPostUIListener() {
181 public void onPostCompleted(final RemoteResource resource) {
182 Display.getDefault().asyncExec(new Runnable() {
186 if (null == resource) {
189 // Update the attribute table
190 if (resourceInSelection != resource) {
193 updateViewer(resource);
195 // Update the observe status
196 updateObserve(resource);
202 observeUIListener = new IObserveUIListener() {
205 public void onObserveCompleted(final RemoteResource resource) {
206 Display.getDefault().asyncExec(new Runnable() {
210 if (null == resource) {
213 // Update the attribute table
214 if (resourceInSelection != resource) {
217 updateViewer(resource);
219 // Update the observe status
220 updateObserve(resource);
226 verificationUIListener = new IVerificationUIListener() {
229 public void onVerificationStarted(final RemoteResource resource,
230 final int autoType) {
231 // Do Nothing. For Future Use.
235 public void onVerificationCompleted(final RemoteResource resource,
236 final int autoType) {
238 if (null == resource) {
242 Display.getDefault().asyncExec(new Runnable() {
246 SimulatorRemoteResource remoteResource = resource
247 .getRemoteResourceRef();
248 if (null == remoteResource) {
254 .log(Level.INFO.ordinal(),
257 + VerificationType.values()[autoType]
259 + "] Verification Completed for \""
260 + remoteResource.getURI()
267 public void onVerificationAborted(final RemoteResource resource,
268 final int autoType) {
270 if (null == resource) {
274 Display.getDefault().asyncExec(new Runnable() {
278 SimulatorRemoteResource remoteResource = resource
279 .getRemoteResourceRef();
280 if (null == remoteResource) {
286 .log(Level.INFO.ordinal(),
289 + VerificationType.values()[autoType]
291 + "] Verification Aborted for \""
292 + remoteResource.getURI()
299 configUploadUIListener = new IConfigurationUpload() {
302 public void onConfigurationUploaded(final RemoteResource resource) {
303 Display.getDefault().asyncExec(new Runnable() {
307 if (null == resource) {
310 if (resourceInSelection != resource) {
313 if (!automateButton.isDisposed()) {
314 automateButton.setEnabled(true);
322 private void updateViewer(RemoteResource resource) {
323 if (null == attViewer) {
326 Tree tree = attViewer.getTree();
327 if (null != resource) {
328 attViewer.setInput(resource.getResourceRepresentation());
329 attViewer.expandAll();
330 if (!tree.isDisposed()) {
331 tree.setLinesVisible(true);
334 if (!tree.isDisposed()) {
336 tree.setLinesVisible(false);
341 private void updateObserve(RemoteResource resource) {
342 if (null == resource || observeResButton.isDisposed()) {
345 boolean observed = resource.isObserved();
347 observeResButton.setText(Constants.STOP_OBSERVE);
349 observeResButton.setText(Constants.OBSERVE);
354 public void createPartControl(Composite parent) {
355 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
357 parent.setLayout(new GridLayout());
358 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
359 parent.setLayoutData(gd);
361 Group attGroup = new Group(parent, SWT.NONE);
362 attGroup.setLayout(new GridLayout());
364 gd.grabExcessHorizontalSpace = true;
365 gd.horizontalAlignment = SWT.FILL;
366 gd.grabExcessVerticalSpace = true;
367 gd.verticalAlignment = SWT.FILL;
368 gd.horizontalSpan = 2;
369 // gd.heightHint = 175;
370 attGroup.setLayoutData(gd);
371 attGroup.setText("Attributes");
372 attGroup.setBackground(color);
374 setupAttributeTable(attGroup);
376 setupRequestControls(parent);
380 addManagerListeners();
382 setVisibility(false);
384 // Updating the data in the UI as per the resource in selection.
385 if (null != attViewer && !attViewer.getControl().isDisposed()) {
386 updateViewer(resourceManager.getCurrentResourceInSelection());
390 private void setupRequestControls(Composite parent) {
392 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
393 Composite opsComp = new Composite(parent, SWT.NONE);
395 gd.grabExcessHorizontalSpace = true;
396 gd.horizontalAlignment = SWT.FILL;
397 opsComp.setLayoutData(gd);
398 GridLayout grid = new GridLayout(5, false);
399 opsComp.setLayout(grid);
400 opsComp.setBackground(color);
402 getButton = new Button(opsComp, SWT.PUSH);
403 getButton.setText("GET");
405 gd.grabExcessHorizontalSpace = true;
406 gd.horizontalAlignment = SWT.FILL;
408 getButton.setLayoutData(gd);
410 putButton = new Button(opsComp, SWT.PUSH);
411 putButton.setText("PUT");
413 gd.grabExcessHorizontalSpace = true;
414 gd.horizontalAlignment = SWT.FILL;
416 putButton.setLayoutData(gd);
418 postButton = new Button(opsComp, SWT.PUSH);
419 postButton.setText("POST");
421 gd.grabExcessHorizontalSpace = true;
422 gd.horizontalAlignment = SWT.FILL;
424 postButton.setLayoutData(gd);
426 observeResButton = new Button(opsComp, SWT.PUSH);
427 observeResButton.setText(Constants.OBSERVE);
429 gd.grabExcessHorizontalSpace = true;
430 gd.horizontalAlignment = SWT.FILL;
431 observeResButton.setLayoutData(gd);
433 automateButton = new Button(opsComp, SWT.PUSH);
434 automateButton.setText("Automation");
436 gd.grabExcessHorizontalSpace = true;
437 gd.horizontalAlignment = SWT.FILL;
438 automateButton.setLayoutData(gd);
441 private void setupAttributeTable(Group attGroup) {
442 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
443 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
444 addressTree.setHeaderVisible(true);
446 attViewer = new TreeViewer(addressTree);
448 createAttributeColumns(attViewer);
450 // make lines and header visible
451 Tree tree = attViewer.getTree();
452 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
453 tree.setHeaderVisible(true);
454 tree.setLinesVisible(true);
456 attViewer.setContentProvider(new AttributeContentProvider());
457 attViewer.setLabelProvider(new AttributeLabelProvider());
460 public void createAttributeColumns(TreeViewer viewer) {
461 Tree tree = viewer.getTree();
463 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
464 attName.setWidth(attTblColWidth[0]);
465 attName.setText(attTblHeaders[0]);
467 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
468 attValue.setWidth(attTblColWidth[1]);
469 attValue.setText(attTblHeaders[1]);
472 private void setUIListeners() {
474 getButton.addSelectionListener(new SelectionAdapter() {
476 public void widgetSelected(SelectionEvent e) {
477 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
480 GetRequestDialog getDialog = new GetRequestDialog(
481 Display.getDefault().getActiveShell());
482 if (getDialog.open() == Window.OK) {
483 // Call the native GET method
484 String query = getDialog.getOtherFilters();
485 String ifType = getDialog.getIfType();
486 resourceManager.sendGetRequest(ifType, query,
487 resourceInSelection);
494 putButton.addSelectionListener(new SelectionAdapter() {
496 public void widgetSelected(SelectionEvent e) {
497 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
500 boolean attributesExist = false;
501 ResourceRepresentation rep = resourceInSelection
502 .getResourceRepresentation();
504 Map<String, AttributeElement> attributes = rep
506 if (null != attributes && !attributes.isEmpty()) {
507 attributesExist = true;
510 if (!attributesExist) {
512 .openInformation(Display.getDefault()
513 .getActiveShell(), "PUT Request",
514 "No attributes exist in the resource model.");
517 PutRequestDialog putDialog = new PutRequestDialog(
518 Display.getDefault().getActiveShell());
519 if (putDialog.open() == Window.OK) {
520 // Call the native PUT method
521 String ifType = putDialog.getIfType();
522 resourceManager.sendPutRequest(ifType,
523 resourceInSelection, putDialog
524 .getUpdatedRepresentation()
532 postButton.addSelectionListener(new SelectionAdapter() {
534 public void widgetSelected(SelectionEvent e) {
535 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
538 boolean attributesExist = false;
539 ResourceRepresentation rep = resourceInSelection
540 .getResourceRepresentation();
542 Map<String, AttributeElement> attributes = rep
544 if (null != attributes && !attributes.isEmpty()) {
545 attributesExist = true;
548 if (!attributesExist) {
550 .openInformation(Display.getDefault()
551 .getActiveShell(), "POST Request",
552 "No attributes exist in the resource model.");
556 PostRequestDialog postDialog = new PostRequestDialog(
557 Display.getDefault().getActiveShell());
558 if (postDialog.open() == Window.OK) {
559 // Call the native POST method
560 String ifType = postDialog.getIfType();
561 ResourceRepresentation representation = postDialog
562 .getUpdatedRepresentation();
563 resourceManager.sendPostRequest(ifType,
565 representation.getSelectedModel());
572 observeResButton.addSelectionListener(new SelectionAdapter() {
574 public void widgetSelected(SelectionEvent e) {
576 if (observeResButton.getText().equals(Constants.OBSERVE)) {
577 result = resourceManager
578 .sendObserveRequest(resourceInSelection);
580 observeResButton.setText(Constants.STOP_OBSERVE);
582 MessageDialog.openError(Display.getDefault()
583 .getActiveShell(), "Observe failed",
584 "Failed to observe the resource. Try again.");
587 result = resourceManager.sendCancelObserveRequest(
588 resourceInSelection, true);
590 observeResButton.setText(Constants.OBSERVE);
593 .openError(Display.getDefault()
595 "Cancel Observe failed",
596 "Failed to stop observing the resource. Try again.");
602 automateButton.addSelectionListener(new SelectionAdapter() {
604 public void widgetSelected(SelectionEvent e) {
605 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
608 RemoteResource resource = resourceManager
609 .getCurrentResourceInSelection();
610 if (null == resource) {
613 Map<String, Boolean> autoStatus = resourceManager
614 .getAutomationStatus(resource);
615 if (null == autoStatus) {
621 boolean startGet, startPut, startPost;
622 boolean stopGet, stopPut, stopPost;
623 startGet = startPut = startPost = false;
624 stopGet = stopPut = stopPost = false;
625 String status = null;
626 String startMsg = "Verification will be started for: ";
627 String stopMsg = "Verification will be stopped for: ";
628 VerificationDialog ad = new VerificationDialog(Display
629 .getDefault().getActiveShell(), autoStatus);
630 if (ad.open() == Window.OK) {
631 Map<String, Boolean> oldStatus = resourceManager
632 .getAutomationStatus(resource);
633 if (null == oldStatus || oldStatus.size() < 1) {
634 status = "Failed to perform the requested operation.";
637 if (oldStatus.get(Constants.GET) != autoStatus
638 .get(Constants.GET)) {
639 if (autoStatus.get(Constants.GET)) {
640 startMsg += Constants.GET;
644 stopMsg += Constants.GET;
650 if (oldStatus.get(Constants.PUT) != autoStatus
651 .get(Constants.PUT)) {
652 if (autoStatus.get(Constants.PUT)) {
653 if (startCount == 1) {
656 startMsg += Constants.PUT;
660 if (stopCount == 1) {
663 stopMsg += Constants.PUT;
670 if (oldStatus.get(Constants.POST) != autoStatus
671 .get(Constants.POST)) {
672 if (autoStatus.get(Constants.POST)) {
673 if (startCount > 0) {
676 startMsg += Constants.POST;
683 stopMsg += Constants.POST;
688 if (startCount > 0) {
689 status = startMsg + ".";
692 if (startCount <= 0) {
695 status += "\n" + stopMsg + ".";
699 if (startCount == 0 && stopCount == 0) {
700 MessageDialog.openInformation(Display
701 .getDefault().getActiveShell(),
702 "Verification", "No New Changes.");
704 boolean answer = MessageDialog.openQuestion(
705 Display.getDefault().getActiveShell(),
706 "Verification", status
707 + "\nDo you want to proceed?");
709 if (startGet || stopGet)
710 automate(VerificationType.GET,
711 autoStatus.get(Constants.GET));
712 if (startPut || stopPut)
713 automate(VerificationType.PUT,
714 autoStatus.get(Constants.PUT));
715 if (startPost || stopPost)
716 automate(VerificationType.POST,
717 autoStatus.get(Constants.POST));
727 private void automate(VerificationType type, boolean start) {
729 resourceManager.startAutomationRequest(type, resourceInSelection);
731 resourceManager.stopAutomationRequest(type, resourceInSelection);
735 private void addManagerListeners() {
736 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
737 resourceSelectionChangedListener);
738 UiListenerHandler.getInstance().addGetUIListener(getUIListener);
739 UiListenerHandler.getInstance().addPutUIListener(putUIListener);
740 UiListenerHandler.getInstance().addPostUIListener(postUIListener);
741 UiListenerHandler.getInstance().addObserveUIListener(observeUIListener);
742 UiListenerHandler.getInstance().addVerificationUIListener(
743 verificationUIListener);
744 UiListenerHandler.getInstance().addConfigUploadUIListener(
745 configUploadUIListener);
748 private void setVisibility(boolean visibility) {
749 if (!getButton.isDisposed())
750 getButton.setEnabled(visibility);
751 if (!putButton.isDisposed())
752 putButton.setEnabled(visibility);
753 if (!postButton.isDisposed())
754 postButton.setEnabled(visibility);
755 if (!automateButton.isDisposed()) {
756 if (visibility && null != resourceInSelection
757 && resourceInSelection.isConfigUploaded()) {
758 automateButton.setEnabled(true);
760 automateButton.setEnabled(false);
763 if (!observeResButton.isDisposed())
764 observeResButton.setEnabled(visibility);
767 class AttributeContentProvider implements ITreeContentProvider {
770 public void dispose() {
774 public void inputChanged(Viewer viewer, Object oldAttribute,
775 Object newAttribute) {
779 public Object[] getChildren(Object attribute) {
780 if (attribute instanceof AttributeElement) {
781 return ((AttributeElement) attribute).getChildren().values()
785 return new Object[0];
789 public Object getParent(Object attribute) {
790 if (attribute instanceof AttributeElement)
791 return ((AttributeElement) attribute).getParent();
796 public boolean hasChildren(Object attribute) {
797 if (attribute instanceof AttributeElement)
798 return ((AttributeElement) attribute).hasChildren();
803 public Object[] getElements(Object resourceModel) {
804 if (resourceModel instanceof ResourceRepresentation) {
805 return ((ResourceRepresentation) resourceModel).getAttributes()
809 return new Object[0];
813 class AttributeLabelProvider implements ITableLabelProvider {
816 public void addListener(ILabelProviderListener arg0) {
820 public void dispose() {
824 public boolean isLabelProperty(Object arg0, String arg1) {
829 public void removeListener(ILabelProviderListener arg0) {
834 public Image getColumnImage(Object element, int col) {
839 public String getColumnText(Object element, int column) {
840 if (element instanceof AttributeElement) {
841 AttributeElement attrElement = (AttributeElement) element;
843 case 0: // Attribute name column
845 SimulatorResourceAttribute attribute = attrElement
846 .getSimulatorResourceAttribute();
847 return attribute.name();
850 case 1: // Attribute value column
852 SimulatorResourceAttribute attribute = attrElement
853 .getSimulatorResourceAttribute();
855 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
856 return Utility.getAttributeValueAsString(attribute
867 public void dispose() {
868 // Unregister the selection listener
869 if (null != resourceSelectionChangedListener) {
870 UiListenerHandler.getInstance()
871 .removeResourceSelectionChangedUIListener(
872 resourceSelectionChangedListener);
875 // Unregister the GET listener
876 if (null != getUIListener) {
877 UiListenerHandler.getInstance().removeGetUIListener(getUIListener);
884 public void setFocus() {