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 is successful 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 is failed 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) {
513 PutRequestDialog putDialog = new PutRequestDialog(
514 Display.getDefault().getActiveShell());
515 if (putDialog.open() == Window.OK) {
516 // Call the native PUT method
517 String ifType = putDialog.getIfType();
518 ResourceRepresentation updatedRepresentation = putDialog
519 .getUpdatedRepresentation();
520 if (null != updatedRepresentation) {
521 resourceManager.sendPutRequest(ifType,
523 updatedRepresentation.getModel());
528 .openInformation(Display.getDefault()
531 "No attributes exist in the resource model.");
539 postButton.addSelectionListener(new SelectionAdapter() {
541 public void widgetSelected(SelectionEvent e) {
542 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
545 boolean attributesExist = false;
546 ResourceRepresentation rep = resourceInSelection
547 .getResourceRepresentation();
549 Map<String, AttributeElement> attributes = rep
551 if (null != attributes && !attributes.isEmpty()) {
552 attributesExist = true;
555 if (attributesExist) {
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 updatedRepresentation = postDialog
562 .getUpdatedRepresentation();
563 if (null != updatedRepresentation) {
564 resourceManager.sendPostRequest(ifType,
566 updatedRepresentation
567 .getSelectedModel());
572 .openInformation(Display.getDefault()
575 "No attributes exist in the resource model.");
583 observeResButton.addSelectionListener(new SelectionAdapter() {
585 public void widgetSelected(SelectionEvent e) {
587 if (observeResButton.getText().equals(Constants.OBSERVE)) {
588 result = resourceManager
589 .sendObserveRequest(resourceInSelection);
591 observeResButton.setText(Constants.STOP_OBSERVE);
593 MessageDialog.openError(Display.getDefault()
594 .getActiveShell(), "Observe failed",
595 "Failed to observe the resource. Try again.");
598 result = resourceManager.sendCancelObserveRequest(
599 resourceInSelection, true);
601 observeResButton.setText(Constants.OBSERVE);
604 .openError(Display.getDefault()
606 "Cancel Observe failed",
607 "Failed to stop observing the resource. Try again.");
613 automateButton.addSelectionListener(new SelectionAdapter() {
615 public void widgetSelected(SelectionEvent e) {
616 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
619 RemoteResource resource = resourceManager
620 .getCurrentResourceInSelection();
621 if (null == resource) {
624 Map<String, Boolean> autoStatus = resourceManager
625 .getAutomationStatus(resource);
626 if (null == autoStatus) {
632 boolean startGet, startPut, startPost;
633 boolean stopGet, stopPut, stopPost;
634 startGet = startPut = startPost = false;
635 stopGet = stopPut = stopPost = false;
636 String status = null;
637 String startMsg = "Verification will be started for: ";
638 String stopMsg = "Verification will be stopped for: ";
639 VerificationDialog ad = new VerificationDialog(Display
640 .getDefault().getActiveShell(), autoStatus);
641 if (ad.open() == Window.OK) {
642 Map<String, Boolean> oldStatus = resourceManager
643 .getAutomationStatus(resource);
644 if (null == oldStatus || oldStatus.size() < 1) {
645 status = "Failed to perform the requested operation.";
648 if (!oldStatus.get(Constants.GET).equals(
649 autoStatus.get(Constants.GET))) {
650 if (autoStatus.get(Constants.GET)) {
651 startMsg += Constants.GET;
655 stopMsg += Constants.GET;
661 if (!oldStatus.get(Constants.PUT).equals(
662 autoStatus.get(Constants.PUT))) {
663 if (autoStatus.get(Constants.PUT)) {
664 if (startCount == 1) {
667 startMsg += Constants.PUT;
671 if (stopCount == 1) {
674 stopMsg += Constants.PUT;
681 if (!oldStatus.get(Constants.POST).equals(
682 autoStatus.get(Constants.POST))) {
683 if (autoStatus.get(Constants.POST)) {
684 if (startCount > 0) {
687 startMsg += Constants.POST;
694 stopMsg += Constants.POST;
699 if (startCount > 0) {
700 status = startMsg + ".";
703 if (startCount <= 0) {
706 status += "\n" + stopMsg + ".";
710 if (!(startCount == 0 && stopCount == 0)) {
711 boolean answer = MessageDialog.openQuestion(
712 Display.getDefault().getActiveShell(),
713 "Verification", status
714 + "\nDo you want to proceed?");
716 if (startGet || stopGet)
717 automate(RequestType.GET,
718 autoStatus.get(Constants.GET));
719 if (startPut || stopPut)
720 automate(RequestType.PUT,
721 autoStatus.get(Constants.PUT));
722 if (startPost || stopPost)
723 automate(RequestType.POST,
724 autoStatus.get(Constants.POST));
734 private void automate(RequestType type, boolean start) {
736 resourceManager.startAutomationRequest(type, resourceInSelection);
738 resourceManager.stopAutomationRequest(type, resourceInSelection);
742 private void addManagerListeners() {
743 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
744 resourceSelectionChangedListener);
745 UiListenerHandler.getInstance().addGetUIListener(getUIListener);
746 UiListenerHandler.getInstance().addPutUIListener(putUIListener);
747 UiListenerHandler.getInstance().addPostUIListener(postUIListener);
748 UiListenerHandler.getInstance().addObserveUIListener(observeUIListener);
749 UiListenerHandler.getInstance().addVerificationUIListener(
750 verificationUIListener);
751 UiListenerHandler.getInstance().addConfigUploadUIListener(
752 configUploadUIListener);
755 private void setVisibility(boolean visibility) {
756 if (!getButton.isDisposed())
757 getButton.setEnabled(visibility);
758 if (!putButton.isDisposed())
759 putButton.setEnabled(visibility);
760 if (!postButton.isDisposed())
761 postButton.setEnabled(visibility);
762 if (!automateButton.isDisposed()) {
763 if (visibility && null != resourceInSelection
764 && resourceInSelection.isConfigUploaded()) {
765 automateButton.setEnabled(true);
767 automateButton.setEnabled(false);
770 if (!observeResButton.isDisposed())
771 observeResButton.setEnabled(visibility);
774 private static class AttributeContentProvider implements
775 ITreeContentProvider {
778 public void dispose() {
782 public void inputChanged(Viewer viewer, Object oldAttribute,
783 Object newAttribute) {
787 public Object[] getChildren(Object attribute) {
788 if (attribute instanceof AttributeElement) {
789 List<AttributeElement> attElementList = new ArrayList<AttributeElement>();
790 Map<String, AttributeElement> children = ((AttributeElement) attribute)
792 if (null != children) {
793 attElementList.addAll(children.values());
794 Collections.sort(attElementList,
795 Utility.attributeComparator);
796 return attElementList.toArray();
800 return new Object[0];
804 public Object getParent(Object attribute) {
805 if (attribute instanceof AttributeElement)
806 return ((AttributeElement) attribute).getParent();
811 public boolean hasChildren(Object attribute) {
812 if (attribute instanceof AttributeElement)
813 return ((AttributeElement) attribute).hasChildren();
818 public Object[] getElements(Object resourceModel) {
819 if (resourceModel instanceof ResourceRepresentation) {
820 return ((ResourceRepresentation) resourceModel).getAttributes()
824 return new Object[0];
828 private static class AttributeLabelProvider implements ITableLabelProvider {
831 public void addListener(ILabelProviderListener arg0) {
835 public void dispose() {
839 public boolean isLabelProperty(Object arg0, String arg1) {
844 public void removeListener(ILabelProviderListener arg0) {
849 public Image getColumnImage(Object element, int col) {
854 public String getColumnText(Object element, int column) {
855 if (element instanceof AttributeElement) {
856 AttributeElement attrElement = (AttributeElement) element;
858 case 0: // Attribute name column
860 SimulatorResourceAttribute attribute = attrElement
861 .getSimulatorResourceAttribute();
862 return attribute.name();
865 case 1: // Attribute value column
867 SimulatorResourceAttribute attribute = attrElement
868 .getSimulatorResourceAttribute();
870 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
871 String value = Utility
872 .getAttributeValueAsString(attribute
888 public void dispose() {
889 // Unregister the selection listener
890 if (null != resourceSelectionChangedListener) {
891 UiListenerHandler.getInstance()
892 .removeResourceSelectionChangedUIListener(
893 resourceSelectionChangedListener);
896 // Unregister the GET listener
897 if (null != getUIListener) {
898 UiListenerHandler.getInstance().removeGetUIListener(getUIListener);
905 public void setFocus() {