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 org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.jface.window.Window;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Group;
37 import org.eclipse.swt.widgets.Tree;
38 import org.eclipse.swt.widgets.TreeColumn;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.part.ViewPart;
42 import java.util.Date;
45 import org.oic.simulator.AttributeValue.ValueType;
46 import org.oic.simulator.ILogger.Level;
47 import org.oic.simulator.SimulatorResourceAttribute;
48 import org.oic.simulator.client.SimulatorRemoteResource;
49 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
51 import oic.simulator.clientcontroller.Activator;
52 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
53 import oic.simulator.clientcontroller.listener.IGetUIListener;
54 import oic.simulator.clientcontroller.listener.IObserveUIListener;
55 import oic.simulator.clientcontroller.listener.IPostUIListener;
56 import oic.simulator.clientcontroller.listener.IPutUIListener;
57 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
58 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
59 import oic.simulator.clientcontroller.manager.ResourceManager;
60 import oic.simulator.clientcontroller.manager.UiListenerHandler;
61 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
62 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
63 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
64 import oic.simulator.clientcontroller.utils.Constants;
65 import oic.simulator.clientcontroller.utils.Utility;
66 import oic.simulator.clientcontroller.view.dialogs.GetRequestDialog;
67 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
68 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
69 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
72 * This class manages and shows the attribute view in the perspective.
74 public class AttributeView extends ViewPart {
76 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.attribute";
78 private TreeViewer attViewer;
80 private Button getButton;
81 private Button putButton;
82 private Button postButton;
83 private Button automateButton;
84 private Button observeResButton;
86 private final String[] attTblHeaders = {
87 "Attribute Name", "Attribute Value" };
88 private final Integer[] attTblColWidth = { 200, 200 };
90 private ResourceManager resourceManager;
92 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
93 private IGetUIListener getUIListener;
94 private IPutUIListener putUIListener;
95 private IPostUIListener postUIListener;
96 private IObserveUIListener observeUIListener;
97 private IVerificationUIListener verificationUIListener;
98 private IConfigurationUpload configUploadUIListener;
100 private RemoteResource resourceInSelection;
102 public AttributeView() {
103 resourceManager = Activator.getDefault().getResourceManager();
105 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
108 public void onResourceSelectionChange(final RemoteResource resource) {
109 Display.getDefault().asyncExec(new Runnable() {
113 resourceInSelection = resource;
115 // Set visibility of manual and automation controls
116 setVisibility((resource == null) ? false : true);
118 // Update the attribute table
119 if (null != attViewer
120 && !attViewer.getControl().isDisposed()) {
121 updateViewer(resource);
124 // Update the observe status
125 updateObserve(resource);
131 getUIListener = new IGetUIListener() {
134 public void onGetCompleted(final RemoteResource resource) {
135 Display.getDefault().asyncExec(new Runnable() {
139 if (null == resource) {
142 // Update the attribute table
143 if (resourceInSelection != resource) {
146 updateViewer(resource);
148 // Update the observe status
149 updateObserve(resource);
155 putUIListener = new IPutUIListener() {
158 public void onPutCompleted(final RemoteResource resource) {
159 Display.getDefault().asyncExec(new Runnable() {
163 if (null == resource) {
166 // Update the attribute table
167 if (resourceInSelection != resource) {
170 updateViewer(resource);
172 // Update the observe status
173 updateObserve(resource);
179 postUIListener = new IPostUIListener() {
182 public void onPostCompleted(final RemoteResource resource) {
183 Display.getDefault().asyncExec(new Runnable() {
187 if (null == resource) {
190 // Update the attribute table
191 if (resourceInSelection != resource) {
194 updateViewer(resource);
196 // Update the observe status
197 updateObserve(resource);
203 observeUIListener = new IObserveUIListener() {
206 public void onObserveCompleted(final RemoteResource resource) {
207 Display.getDefault().asyncExec(new Runnable() {
211 if (null == resource) {
214 // Update the attribute table
215 if (resourceInSelection != resource) {
218 updateViewer(resource);
220 // Update the observe status
221 updateObserve(resource);
227 verificationUIListener = new IVerificationUIListener() {
230 public void onVerificationStarted(final RemoteResource resource,
231 final int autoType) {
232 // Do Nothing. For Future Use.
236 public void onVerificationCompleted(final RemoteResource resource,
237 final int autoType) {
239 if (null == resource) {
243 Display.getDefault().asyncExec(new Runnable() {
247 SimulatorRemoteResource remoteResource = resource
248 .getRemoteResourceRef();
249 if (null == remoteResource) {
255 .log(Level.INFO.ordinal(),
258 + VerificationType.values()[autoType]
260 + "] Verification Completed for \""
261 + remoteResource.getURI()
268 public void onVerificationAborted(final RemoteResource resource,
269 final int autoType) {
271 if (null == resource) {
275 Display.getDefault().asyncExec(new Runnable() {
279 SimulatorRemoteResource remoteResource = resource
280 .getRemoteResourceRef();
281 if (null == remoteResource) {
287 .log(Level.INFO.ordinal(),
290 + VerificationType.values()[autoType]
292 + "] Verification Aborted for \""
293 + remoteResource.getURI()
300 configUploadUIListener = new IConfigurationUpload() {
303 public void onConfigurationUploaded(final RemoteResource resource) {
304 Display.getDefault().asyncExec(new Runnable() {
308 if (null == resource) {
311 if (resourceInSelection != resource) {
314 if (!automateButton.isDisposed()) {
315 automateButton.setEnabled(true);
323 private void updateViewer(RemoteResource resource) {
324 if (null == attViewer) {
327 Tree tree = attViewer.getTree();
328 if (null != resource) {
329 attViewer.setInput(resource.getResourceRepresentation());
330 attViewer.expandAll();
331 if (!tree.isDisposed()) {
332 tree.setLinesVisible(true);
335 if (!tree.isDisposed()) {
337 tree.setLinesVisible(false);
342 private void updateObserve(RemoteResource resource) {
343 if (null == resource || observeResButton.isDisposed()) {
346 boolean observed = resource.isObserved();
348 observeResButton.setText(Constants.STOP_OBSERVE);
350 observeResButton.setText(Constants.OBSERVE);
355 public void createPartControl(Composite parent) {
356 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
358 parent.setLayout(new GridLayout());
359 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
360 parent.setLayoutData(gd);
362 Group attGroup = new Group(parent, SWT.NONE);
363 attGroup.setLayout(new GridLayout());
365 gd.grabExcessHorizontalSpace = true;
366 gd.horizontalAlignment = SWT.FILL;
367 gd.grabExcessVerticalSpace = true;
368 gd.verticalAlignment = SWT.FILL;
369 gd.horizontalSpan = 2;
370 // gd.heightHint = 175;
371 attGroup.setLayoutData(gd);
372 attGroup.setText("Attributes");
373 attGroup.setBackground(color);
375 setupAttributeTable(attGroup);
377 setupRequestControls(parent);
381 addManagerListeners();
383 setVisibility(false);
385 // Updating the data in the UI as per the resource in selection.
386 if (null != attViewer && !attViewer.getControl().isDisposed()) {
387 updateViewer(resourceManager.getCurrentResourceInSelection());
391 private void setupRequestControls(Composite parent) {
393 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
394 Composite opsComp = new Composite(parent, SWT.NONE);
396 gd.grabExcessHorizontalSpace = true;
397 gd.horizontalAlignment = SWT.FILL;
398 opsComp.setLayoutData(gd);
399 GridLayout grid = new GridLayout(5, false);
400 opsComp.setLayout(grid);
401 opsComp.setBackground(color);
403 getButton = new Button(opsComp, SWT.PUSH);
404 getButton.setText("GET");
406 gd.grabExcessHorizontalSpace = true;
407 gd.horizontalAlignment = SWT.FILL;
409 getButton.setLayoutData(gd);
411 putButton = new Button(opsComp, SWT.PUSH);
412 putButton.setText("PUT");
414 gd.grabExcessHorizontalSpace = true;
415 gd.horizontalAlignment = SWT.FILL;
417 putButton.setLayoutData(gd);
419 postButton = new Button(opsComp, SWT.PUSH);
420 postButton.setText("POST");
422 gd.grabExcessHorizontalSpace = true;
423 gd.horizontalAlignment = SWT.FILL;
425 postButton.setLayoutData(gd);
427 observeResButton = new Button(opsComp, SWT.PUSH);
428 observeResButton.setText(Constants.OBSERVE);
430 gd.grabExcessHorizontalSpace = true;
431 gd.horizontalAlignment = SWT.FILL;
432 observeResButton.setLayoutData(gd);
434 automateButton = new Button(opsComp, SWT.PUSH);
435 automateButton.setText("Automation");
437 gd.grabExcessHorizontalSpace = true;
438 gd.horizontalAlignment = SWT.FILL;
439 automateButton.setLayoutData(gd);
442 private void setupAttributeTable(Group attGroup) {
443 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
444 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
445 addressTree.setHeaderVisible(true);
447 attViewer = new TreeViewer(addressTree);
449 createAttributeColumns(attViewer);
451 // make lines and header visible
452 Tree tree = attViewer.getTree();
453 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
454 tree.setHeaderVisible(true);
455 tree.setLinesVisible(true);
457 attViewer.setContentProvider(new AttributeContentProvider());
458 attViewer.setLabelProvider(new AttributeLabelProvider());
461 public void createAttributeColumns(TreeViewer viewer) {
462 Tree tree = viewer.getTree();
464 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
465 attName.setWidth(attTblColWidth[0]);
466 attName.setText(attTblHeaders[0]);
468 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
469 attValue.setWidth(attTblColWidth[1]);
470 attValue.setText(attTblHeaders[1]);
473 private void setUIListeners() {
475 getButton.addSelectionListener(new SelectionAdapter() {
477 public void widgetSelected(SelectionEvent e) {
478 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
481 GetRequestDialog getDialog = new GetRequestDialog(
482 Display.getDefault().getActiveShell());
483 if (getDialog.open() == Window.OK) {
484 // Call the native GET method
485 String query = getDialog.getOtherFilters();
486 String ifType = getDialog.getIfType();
487 resourceManager.sendGetRequest(ifType, query,
488 resourceInSelection);
495 putButton.addSelectionListener(new SelectionAdapter() {
497 public void widgetSelected(SelectionEvent e) {
498 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
501 boolean attributesExist = false;
502 ResourceRepresentation rep = resourceInSelection
503 .getResourceRepresentation();
505 Map<String, AttributeElement> attributes = rep
507 if (null != attributes && !attributes.isEmpty()) {
508 attributesExist = true;
511 if (!attributesExist) {
513 .openInformation(Display.getDefault()
514 .getActiveShell(), "PUT Request",
515 "No attributes exist in the resource model.");
518 PutRequestDialog putDialog = new PutRequestDialog(
519 Display.getDefault().getActiveShell());
520 if (putDialog.open() == Window.OK) {
521 // Call the native PUT method
522 String ifType = putDialog.getIfType();
523 resourceManager.sendPutRequest(ifType,
524 resourceInSelection, putDialog
525 .getUpdatedRepresentation()
533 postButton.addSelectionListener(new SelectionAdapter() {
535 public void widgetSelected(SelectionEvent e) {
536 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
539 boolean attributesExist = false;
540 ResourceRepresentation rep = resourceInSelection
541 .getResourceRepresentation();
543 Map<String, AttributeElement> attributes = rep
545 if (null != attributes && !attributes.isEmpty()) {
546 attributesExist = true;
549 if (!attributesExist) {
551 .openInformation(Display.getDefault()
552 .getActiveShell(), "POST Request",
553 "No attributes exist in the resource model.");
557 PostRequestDialog postDialog = new PostRequestDialog(
558 Display.getDefault().getActiveShell());
559 if (postDialog.open() == Window.OK) {
560 // Call the native POST method
561 String ifType = postDialog.getIfType();
562 ResourceRepresentation representation = postDialog
563 .getUpdatedRepresentation();
564 resourceManager.sendPostRequest(ifType,
566 representation.getSelectedModel());
573 observeResButton.addSelectionListener(new SelectionAdapter() {
575 public void widgetSelected(SelectionEvent e) {
577 if (observeResButton.getText().equals(Constants.OBSERVE)) {
578 result = resourceManager
579 .sendObserveRequest(resourceInSelection);
581 observeResButton.setText(Constants.STOP_OBSERVE);
583 MessageDialog.openError(Display.getDefault()
584 .getActiveShell(), "Observe failed",
585 "Failed to observe the resource. Try again.");
588 result = resourceManager.sendCancelObserveRequest(
589 resourceInSelection, true);
591 observeResButton.setText(Constants.OBSERVE);
594 .openError(Display.getDefault()
596 "Cancel Observe failed",
597 "Failed to stop observing the resource. Try again.");
603 automateButton.addSelectionListener(new SelectionAdapter() {
605 public void widgetSelected(SelectionEvent e) {
606 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
609 RemoteResource resource = resourceManager
610 .getCurrentResourceInSelection();
611 if (null == resource) {
614 Map<String, Boolean> autoStatus = resourceManager
615 .getAutomationStatus(resource);
616 if (null == autoStatus) {
622 boolean startGet, startPut, startPost;
623 boolean stopGet, stopPut, stopPost;
624 startGet = startPut = startPost = false;
625 stopGet = stopPut = stopPost = false;
626 String status = null;
627 String startMsg = "Verification will be started for: ";
628 String stopMsg = "Verification will be stopped for: ";
629 VerificationDialog ad = new VerificationDialog(Display
630 .getDefault().getActiveShell(), autoStatus);
631 if (ad.open() == Window.OK) {
632 Map<String, Boolean> oldStatus = resourceManager
633 .getAutomationStatus(resource);
634 if (null == oldStatus || oldStatus.size() < 1) {
635 status = "Failed to perform the requested operation.";
638 if (oldStatus.get(Constants.GET) != autoStatus
639 .get(Constants.GET)) {
640 if (autoStatus.get(Constants.GET)) {
641 startMsg += Constants.GET;
645 stopMsg += Constants.GET;
651 if (oldStatus.get(Constants.PUT) != autoStatus
652 .get(Constants.PUT)) {
653 if (autoStatus.get(Constants.PUT)) {
654 if (startCount == 1) {
657 startMsg += Constants.PUT;
661 if (stopCount == 1) {
664 stopMsg += Constants.PUT;
671 if (oldStatus.get(Constants.POST) != autoStatus
672 .get(Constants.POST)) {
673 if (autoStatus.get(Constants.POST)) {
674 if (startCount > 0) {
677 startMsg += Constants.POST;
684 stopMsg += Constants.POST;
689 if (startCount > 0) {
690 status = startMsg + ".";
693 if (startCount <= 0) {
696 status += "\n" + stopMsg + ".";
700 if (startCount == 0 && stopCount == 0) {
701 MessageDialog.openInformation(Display
702 .getDefault().getActiveShell(),
703 "Verification", "No New Changes.");
705 boolean answer = MessageDialog.openQuestion(
706 Display.getDefault().getActiveShell(),
707 "Verification", status
708 + "\nDo you want to proceed?");
710 if (startGet || stopGet)
711 automate(VerificationType.GET,
712 autoStatus.get(Constants.GET));
713 if (startPut || stopPut)
714 automate(VerificationType.PUT,
715 autoStatus.get(Constants.PUT));
716 if (startPost || stopPost)
717 automate(VerificationType.POST,
718 autoStatus.get(Constants.POST));
728 private void automate(VerificationType type, boolean start) {
730 resourceManager.startAutomationRequest(type, resourceInSelection);
732 resourceManager.stopAutomationRequest(type, resourceInSelection);
736 private void addManagerListeners() {
737 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
738 resourceSelectionChangedListener);
739 UiListenerHandler.getInstance().addGetUIListener(getUIListener);
740 UiListenerHandler.getInstance().addPutUIListener(putUIListener);
741 UiListenerHandler.getInstance().addPostUIListener(postUIListener);
742 UiListenerHandler.getInstance().addObserveUIListener(observeUIListener);
743 UiListenerHandler.getInstance().addVerificationUIListener(
744 verificationUIListener);
745 UiListenerHandler.getInstance().addConfigUploadUIListener(
746 configUploadUIListener);
749 private void setVisibility(boolean visibility) {
750 if (!getButton.isDisposed())
751 getButton.setEnabled(visibility);
752 if (!putButton.isDisposed())
753 putButton.setEnabled(visibility);
754 if (!postButton.isDisposed())
755 postButton.setEnabled(visibility);
756 if (!automateButton.isDisposed()) {
757 if (visibility && null != resourceInSelection
758 && resourceInSelection.isConfigUploaded()) {
759 automateButton.setEnabled(true);
761 automateButton.setEnabled(false);
764 if (!observeResButton.isDisposed())
765 observeResButton.setEnabled(visibility);
768 class AttributeContentProvider implements ITreeContentProvider {
771 public void dispose() {
775 public void inputChanged(Viewer viewer, Object oldAttribute,
776 Object newAttribute) {
780 public Object[] getChildren(Object attribute) {
781 if (attribute instanceof AttributeElement) {
782 return ((AttributeElement) attribute).getChildren().values()
786 return new Object[0];
790 public Object getParent(Object attribute) {
791 if (attribute instanceof AttributeElement)
792 return ((AttributeElement) attribute).getParent();
797 public boolean hasChildren(Object attribute) {
798 if (attribute instanceof AttributeElement)
799 return ((AttributeElement) attribute).hasChildren();
804 public Object[] getElements(Object resourceModel) {
805 if (resourceModel instanceof ResourceRepresentation) {
806 return ((ResourceRepresentation) resourceModel).getAttributes()
810 return new Object[0];
814 class AttributeLabelProvider implements ITableLabelProvider {
817 public void addListener(ILabelProviderListener arg0) {
821 public void dispose() {
825 public boolean isLabelProperty(Object arg0, String arg1) {
830 public void removeListener(ILabelProviderListener arg0) {
835 public Image getColumnImage(Object element, int col) {
840 public String getColumnText(Object element, int column) {
841 if (element instanceof AttributeElement) {
842 AttributeElement attrElement = (AttributeElement) element;
844 case 0: // Attribute name column
846 SimulatorResourceAttribute attribute = attrElement
847 .getSimulatorResourceAttribute();
848 return attribute.name();
851 case 1: // Attribute value column
853 SimulatorResourceAttribute attribute = attrElement
854 .getSimulatorResourceAttribute();
856 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
857 return Utility.getAttributeValueAsString(attribute
868 public void dispose() {
869 // Unregister the selection listener
870 if (null != resourceSelectionChangedListener) {
871 UiListenerHandler.getInstance()
872 .removeResourceSelectionChangedUIListener(
873 resourceSelectionChangedListener);
876 // Unregister the GET listener
877 if (null != getUIListener) {
878 UiListenerHandler.getInstance().removeGetUIListener(getUIListener);
885 public void setFocus() {