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.ArrayList;
43 import java.util.Collections;
44 import java.util.Date;
45 import java.util.List;
48 import org.oic.simulator.AttributeValue.ValueType;
49 import org.oic.simulator.ILogger.Level;
50 import org.oic.simulator.SimulatorResourceAttribute;
51 import org.oic.simulator.client.SimulatorRemoteResource;
52 import org.oic.simulator.client.SimulatorRemoteResource.RequestType;
54 import oic.simulator.clientcontroller.Activator;
55 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
56 import oic.simulator.clientcontroller.listener.IGetUIListener;
57 import oic.simulator.clientcontroller.listener.IObserveUIListener;
58 import oic.simulator.clientcontroller.listener.IPostUIListener;
59 import oic.simulator.clientcontroller.listener.IPutUIListener;
60 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
61 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
62 import oic.simulator.clientcontroller.manager.ResourceManager;
63 import oic.simulator.clientcontroller.manager.UiListenerHandler;
64 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
65 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
66 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
67 import oic.simulator.clientcontroller.utils.Constants;
68 import oic.simulator.clientcontroller.utils.Utility;
69 import oic.simulator.clientcontroller.view.dialogs.GetRequestDialog;
70 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
71 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
72 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
75 * This class manages and shows the attribute view in the perspective.
77 public class AttributeView extends ViewPart {
79 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.attribute";
81 private TreeViewer attViewer;
83 private Button getButton;
84 private Button putButton;
85 private Button postButton;
86 private Button automateButton;
87 private Button observeResButton;
89 private final String[] attTblHeaders = {
90 "Attribute Name", "Attribute Value" };
91 private final Integer[] attTblColWidth = { 200, 200 };
93 private ResourceManager resourceManager;
95 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
96 private IGetUIListener getUIListener;
97 private IPutUIListener putUIListener;
98 private IPostUIListener postUIListener;
99 private IObserveUIListener observeUIListener;
100 private IVerificationUIListener verificationUIListener;
101 private IConfigurationUpload configUploadUIListener;
103 private RemoteResource resourceInSelection;
105 public AttributeView() {
106 resourceManager = Activator.getDefault().getResourceManager();
108 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
111 public void onResourceSelectionChange(final RemoteResource resource) {
112 Display.getDefault().asyncExec(new Runnable() {
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 != attViewer
123 && !attViewer.getControl().isDisposed()) {
124 updateViewer(resource);
127 // Update the observe status
128 updateObserve(resource);
134 getUIListener = new IGetUIListener() {
137 public void onGetCompleted(final RemoteResource resource) {
138 Display.getDefault().asyncExec(new Runnable() {
142 if (null == resource) {
145 // Update the attribute table
146 if (resourceInSelection != resource) {
149 updateViewer(resource);
151 // Update the observe status
152 updateObserve(resource);
158 putUIListener = new IPutUIListener() {
161 public void onPutCompleted(final RemoteResource resource) {
162 Display.getDefault().asyncExec(new Runnable() {
166 if (null == resource) {
169 // Update the attribute table
170 if (resourceInSelection != resource) {
173 updateViewer(resource);
175 // Update the observe status
176 updateObserve(resource);
182 postUIListener = new IPostUIListener() {
185 public void onPostCompleted(final RemoteResource resource) {
186 Display.getDefault().asyncExec(new Runnable() {
190 if (null == resource) {
193 // Update the attribute table
194 if (resourceInSelection != resource) {
197 updateViewer(resource);
199 // Update the observe status
200 updateObserve(resource);
206 observeUIListener = new IObserveUIListener() {
209 public void onObserveCompleted(final RemoteResource resource) {
210 Display.getDefault().asyncExec(new Runnable() {
214 if (null == resource) {
217 // Update the attribute table
218 if (resourceInSelection != resource) {
221 updateViewer(resource);
223 // Update the observe status
224 updateObserve(resource);
230 verificationUIListener = new IVerificationUIListener() {
233 public void onVerificationStarted(final RemoteResource resource,
234 final RequestType reqType) {
235 // Do Nothing. For Future Use.
239 public void onVerificationCompleted(final RemoteResource resource,
240 final RequestType reqType) {
242 if (null == resource) {
246 Display.getDefault().asyncExec(new Runnable() {
250 SimulatorRemoteResource remoteResource = resource
251 .getRemoteResourceRef();
252 if (null == remoteResource) {
258 .log(Level.INFO.ordinal(),
262 + "] Verification Completed for \""
263 + remoteResource.getURI()
270 public void onVerificationAborted(final RemoteResource resource,
271 final RequestType reqType) {
273 if (null == resource) {
277 Display.getDefault().asyncExec(new Runnable() {
281 SimulatorRemoteResource remoteResource = resource
282 .getRemoteResourceRef();
283 if (null == remoteResource) {
289 .log(Level.INFO.ordinal(),
293 + "] Verification Aborted for \""
294 + remoteResource.getURI()
301 configUploadUIListener = new IConfigurationUpload() {
304 public void onConfigurationUploaded(final RemoteResource resource) {
305 Display.getDefault().asyncExec(new Runnable() {
309 if (null == resource) {
312 if (resourceInSelection != resource) {
315 if (!automateButton.isDisposed()) {
316 automateButton.setEnabled(true);
324 private void updateViewer(RemoteResource resource) {
325 if (null == attViewer) {
328 Tree tree = attViewer.getTree();
329 if (null != resource) {
330 attViewer.setInput(resource.getResourceRepresentation());
331 attViewer.expandAll();
332 if (!tree.isDisposed()) {
333 tree.setLinesVisible(true);
336 if (!tree.isDisposed()) {
338 tree.setLinesVisible(false);
343 private void updateObserve(RemoteResource resource) {
344 if (null == resource || observeResButton.isDisposed()) {
347 boolean observed = resource.isObserved();
349 observeResButton.setText(Constants.STOP_OBSERVE);
351 observeResButton.setText(Constants.OBSERVE);
356 public void createPartControl(Composite parent) {
357 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
359 parent.setLayout(new GridLayout());
360 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
361 parent.setLayoutData(gd);
363 Group attGroup = new Group(parent, SWT.NONE);
364 attGroup.setLayout(new GridLayout());
366 gd.grabExcessHorizontalSpace = true;
367 gd.horizontalAlignment = SWT.FILL;
368 gd.grabExcessVerticalSpace = true;
369 gd.verticalAlignment = SWT.FILL;
370 gd.horizontalSpan = 2;
371 // gd.heightHint = 175;
372 attGroup.setLayoutData(gd);
373 attGroup.setText("Attributes");
374 attGroup.setBackground(color);
376 setupAttributeTable(attGroup);
378 setupRequestControls(parent);
382 addManagerListeners();
384 setVisibility(false);
386 // Updating the data in the UI as per the resource in selection.
387 if (null != attViewer && !attViewer.getControl().isDisposed()) {
388 updateViewer(resourceManager.getCurrentResourceInSelection());
392 private void setupRequestControls(Composite parent) {
394 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
395 Composite opsComp = new Composite(parent, SWT.NONE);
397 gd.grabExcessHorizontalSpace = true;
398 gd.horizontalAlignment = SWT.FILL;
399 opsComp.setLayoutData(gd);
400 GridLayout grid = new GridLayout(5, false);
401 opsComp.setLayout(grid);
402 opsComp.setBackground(color);
404 getButton = new Button(opsComp, SWT.PUSH);
405 getButton.setText("GET");
407 gd.grabExcessHorizontalSpace = true;
408 gd.horizontalAlignment = SWT.FILL;
410 getButton.setLayoutData(gd);
412 putButton = new Button(opsComp, SWT.PUSH);
413 putButton.setText("PUT");
415 gd.grabExcessHorizontalSpace = true;
416 gd.horizontalAlignment = SWT.FILL;
418 putButton.setLayoutData(gd);
420 postButton = new Button(opsComp, SWT.PUSH);
421 postButton.setText("POST");
423 gd.grabExcessHorizontalSpace = true;
424 gd.horizontalAlignment = SWT.FILL;
426 postButton.setLayoutData(gd);
428 observeResButton = new Button(opsComp, SWT.PUSH);
429 observeResButton.setText(Constants.OBSERVE);
431 gd.grabExcessHorizontalSpace = true;
432 gd.horizontalAlignment = SWT.FILL;
433 observeResButton.setLayoutData(gd);
435 automateButton = new Button(opsComp, SWT.PUSH);
436 automateButton.setText("Automation");
438 gd.grabExcessHorizontalSpace = true;
439 gd.horizontalAlignment = SWT.FILL;
440 automateButton.setLayoutData(gd);
443 private void setupAttributeTable(Group attGroup) {
444 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
445 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
446 addressTree.setHeaderVisible(true);
448 attViewer = new TreeViewer(addressTree);
450 createAttributeColumns(attViewer);
452 // make lines and header visible
453 Tree tree = attViewer.getTree();
454 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
455 tree.setHeaderVisible(true);
456 tree.setLinesVisible(true);
458 attViewer.setContentProvider(new AttributeContentProvider());
459 attViewer.setLabelProvider(new AttributeLabelProvider());
462 public void createAttributeColumns(TreeViewer viewer) {
463 Tree tree = viewer.getTree();
465 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
466 attName.setWidth(attTblColWidth[0]);
467 attName.setText(attTblHeaders[0]);
469 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
470 attValue.setWidth(attTblColWidth[1]);
471 attValue.setText(attTblHeaders[1]);
474 private void setUIListeners() {
476 getButton.addSelectionListener(new SelectionAdapter() {
478 public void widgetSelected(SelectionEvent e) {
479 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
482 GetRequestDialog getDialog = new GetRequestDialog(
483 Display.getDefault().getActiveShell());
484 if (getDialog.open() == Window.OK) {
485 // Call the native GET method
486 String query = getDialog.getOtherFilters();
487 String ifType = getDialog.getIfType();
488 resourceManager.sendGetRequest(ifType, query,
489 resourceInSelection);
496 putButton.addSelectionListener(new SelectionAdapter() {
498 public void widgetSelected(SelectionEvent e) {
499 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
502 boolean attributesExist = false;
503 ResourceRepresentation rep = resourceInSelection
504 .getResourceRepresentation();
506 Map<String, AttributeElement> attributes = rep
508 if (null != attributes && !attributes.isEmpty()) {
509 attributesExist = true;
512 if (!attributesExist) {
514 .openInformation(Display.getDefault()
515 .getActiveShell(), "PUT Request",
516 "No attributes exist in the resource model.");
519 PutRequestDialog putDialog = new PutRequestDialog(
520 Display.getDefault().getActiveShell());
521 if (putDialog.open() == Window.OK) {
522 // Call the native PUT method
523 String ifType = putDialog.getIfType();
524 resourceManager.sendPutRequest(ifType,
525 resourceInSelection, putDialog
526 .getUpdatedRepresentation()
534 postButton.addSelectionListener(new SelectionAdapter() {
536 public void widgetSelected(SelectionEvent e) {
537 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
540 boolean attributesExist = false;
541 ResourceRepresentation rep = resourceInSelection
542 .getResourceRepresentation();
544 Map<String, AttributeElement> attributes = rep
546 if (null != attributes && !attributes.isEmpty()) {
547 attributesExist = true;
550 if (!attributesExist) {
552 .openInformation(Display.getDefault()
553 .getActiveShell(), "POST Request",
554 "No attributes exist in the resource model.");
558 PostRequestDialog postDialog = new PostRequestDialog(
559 Display.getDefault().getActiveShell());
560 if (postDialog.open() == Window.OK) {
561 // Call the native POST method
562 String ifType = postDialog.getIfType();
563 ResourceRepresentation representation = postDialog
564 .getUpdatedRepresentation();
565 resourceManager.sendPostRequest(ifType,
567 representation.getSelectedModel());
574 observeResButton.addSelectionListener(new SelectionAdapter() {
576 public void widgetSelected(SelectionEvent e) {
578 if (observeResButton.getText().equals(Constants.OBSERVE)) {
579 result = resourceManager
580 .sendObserveRequest(resourceInSelection);
582 observeResButton.setText(Constants.STOP_OBSERVE);
584 MessageDialog.openError(Display.getDefault()
585 .getActiveShell(), "Observe failed",
586 "Failed to observe the resource. Try again.");
589 result = resourceManager.sendCancelObserveRequest(
590 resourceInSelection, true);
592 observeResButton.setText(Constants.OBSERVE);
595 .openError(Display.getDefault()
597 "Cancel Observe failed",
598 "Failed to stop observing the resource. Try again.");
604 automateButton.addSelectionListener(new SelectionAdapter() {
606 public void widgetSelected(SelectionEvent e) {
607 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
610 RemoteResource resource = resourceManager
611 .getCurrentResourceInSelection();
612 if (null == resource) {
615 Map<String, Boolean> autoStatus = resourceManager
616 .getAutomationStatus(resource);
617 if (null == autoStatus) {
623 boolean startGet, startPut, startPost;
624 boolean stopGet, stopPut, stopPost;
625 startGet = startPut = startPost = false;
626 stopGet = stopPut = stopPost = false;
627 String status = null;
628 String startMsg = "Verification will be started for: ";
629 String stopMsg = "Verification will be stopped for: ";
630 VerificationDialog ad = new VerificationDialog(Display
631 .getDefault().getActiveShell(), autoStatus);
632 if (ad.open() == Window.OK) {
633 Map<String, Boolean> oldStatus = resourceManager
634 .getAutomationStatus(resource);
635 if (null == oldStatus || oldStatus.size() < 1) {
636 status = "Failed to perform the requested operation.";
639 if (oldStatus.get(Constants.GET) != autoStatus
640 .get(Constants.GET)) {
641 if (autoStatus.get(Constants.GET)) {
642 startMsg += Constants.GET;
646 stopMsg += Constants.GET;
652 if (oldStatus.get(Constants.PUT) != autoStatus
653 .get(Constants.PUT)) {
654 if (autoStatus.get(Constants.PUT)) {
655 if (startCount == 1) {
658 startMsg += Constants.PUT;
662 if (stopCount == 1) {
665 stopMsg += Constants.PUT;
672 if (oldStatus.get(Constants.POST) != autoStatus
673 .get(Constants.POST)) {
674 if (autoStatus.get(Constants.POST)) {
675 if (startCount > 0) {
678 startMsg += Constants.POST;
685 stopMsg += Constants.POST;
690 if (startCount > 0) {
691 status = startMsg + ".";
694 if (startCount <= 0) {
697 status += "\n" + stopMsg + ".";
701 if (startCount == 0 && stopCount == 0) {
702 MessageDialog.openInformation(Display
703 .getDefault().getActiveShell(),
704 "Verification", "No New Changes.");
706 boolean answer = MessageDialog.openQuestion(
707 Display.getDefault().getActiveShell(),
708 "Verification", status
709 + "\nDo you want to proceed?");
711 if (startGet || stopGet)
712 automate(RequestType.GET,
713 autoStatus.get(Constants.GET));
714 if (startPut || stopPut)
715 automate(RequestType.PUT,
716 autoStatus.get(Constants.PUT));
717 if (startPost || stopPost)
718 automate(RequestType.POST,
719 autoStatus.get(Constants.POST));
729 private void automate(RequestType type, boolean start) {
731 resourceManager.startAutomationRequest(type, resourceInSelection);
733 resourceManager.stopAutomationRequest(type, resourceInSelection);
737 private void addManagerListeners() {
738 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
739 resourceSelectionChangedListener);
740 UiListenerHandler.getInstance().addGetUIListener(getUIListener);
741 UiListenerHandler.getInstance().addPutUIListener(putUIListener);
742 UiListenerHandler.getInstance().addPostUIListener(postUIListener);
743 UiListenerHandler.getInstance().addObserveUIListener(observeUIListener);
744 UiListenerHandler.getInstance().addVerificationUIListener(
745 verificationUIListener);
746 UiListenerHandler.getInstance().addConfigUploadUIListener(
747 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 ITreeContentProvider {
772 public void dispose() {
776 public void inputChanged(Viewer viewer, Object oldAttribute,
777 Object newAttribute) {
781 public Object[] getChildren(Object attribute) {
782 if (attribute instanceof AttributeElement) {
783 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
784 Map<String, AttributeElement> children = ((AttributeElement) attribute)
786 if (null != children) {
787 attElementList.addAll(children.values());
788 Collections.sort(attElementList,
789 Utility.attributeComparator);
790 return attElementList.toArray();
794 return new Object[0];
798 public Object getParent(Object attribute) {
799 if (attribute instanceof AttributeElement)
800 return ((AttributeElement) attribute).getParent();
805 public boolean hasChildren(Object attribute) {
806 if (attribute instanceof AttributeElement)
807 return ((AttributeElement) attribute).hasChildren();
812 public Object[] getElements(Object resourceModel) {
813 if (resourceModel instanceof ResourceRepresentation) {
814 return ((ResourceRepresentation) resourceModel).getAttributes()
818 return new Object[0];
822 class AttributeLabelProvider implements ITableLabelProvider {
825 public void addListener(ILabelProviderListener arg0) {
829 public void dispose() {
833 public boolean isLabelProperty(Object arg0, String arg1) {
838 public void removeListener(ILabelProviderListener arg0) {
843 public Image getColumnImage(Object element, int col) {
848 public String getColumnText(Object element, int column) {
849 if (element instanceof AttributeElement) {
850 AttributeElement attrElement = (AttributeElement) element;
852 case 0: // Attribute name column
854 SimulatorResourceAttribute attribute = attrElement
855 .getSimulatorResourceAttribute();
856 return attribute.name();
859 case 1: // Attribute value column
861 SimulatorResourceAttribute attribute = attrElement
862 .getSimulatorResourceAttribute();
864 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
865 String value = Utility
866 .getAttributeValueAsString(attribute
882 public void dispose() {
883 // Unregister the selection listener
884 if (null != resourceSelectionChangedListener) {
885 UiListenerHandler.getInstance()
886 .removeResourceSelectionChangedUIListener(
887 resourceSelectionChangedListener);
890 // Unregister the GET listener
891 if (null != getUIListener) {
892 UiListenerHandler.getInstance().removeGetUIListener(getUIListener);
899 public void setFocus() {