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.List;
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.remoteresource.PutPostAttributeModel;
32 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
33 import oic.simulator.clientcontroller.remoteresource.RemoteResourceAttribute;
34 import oic.simulator.clientcontroller.utils.Constants;
35 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
36 import oic.simulator.clientcontroller.view.dialogs.PutRequestDialog;
37 import oic.simulator.clientcontroller.view.dialogs.VerificationDialog;
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.viewers.IStructuredContentProvider;
41 import org.eclipse.jface.viewers.StyledCellLabelProvider;
42 import org.eclipse.jface.viewers.TableViewer;
43 import org.eclipse.jface.viewers.TableViewerColumn;
44 import org.eclipse.jface.viewers.Viewer;
45 import org.eclipse.jface.viewers.ViewerCell;
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.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Display;
56 import org.eclipse.swt.widgets.Group;
57 import org.eclipse.swt.widgets.Table;
58 import org.eclipse.ui.PlatformUI;
59 import org.eclipse.ui.part.ViewPart;
62 * This class manages and shows the attribute view in the perspective.
64 public class AttributeView extends ViewPart {
66 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.attribute";
68 private TableViewer attTblViewer;
70 private Button getButton;
71 private Button putButton;
72 private Button postButton;
73 private Button automateButton;
74 private Button observeResButton;
76 private final String[] attTblHeaders = {
77 "Attribute Name", "Attribute Value" };
78 private final Integer[] attTblColWidth = { 200, 200 };
80 private ResourceManager resourceManager;
82 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
83 private IGetUIListener getUIListener;
84 private IPutUIListener putUIListener;
85 private IPostUIListener postUIListener;
86 private IObserveUIListener observeUIListener;
87 private IVerificationUIListener verificationUIListener;
88 private IConfigurationUpload configUploadUIListener;
90 private RemoteResource resourceInSelection;
92 public AttributeView() {
93 resourceManager = Activator.getDefault().getResourceManager();
95 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
98 public void onResourceSelectionChange(final RemoteResource resource) {
99 Display.getDefault().asyncExec(new Runnable() {
104 .println("AttributeView: onResourceSelectionChange");
105 resourceInSelection = resource;
107 // Set visibility of manual and automation controls
108 setVisibility((resource == null) ? false : true);
110 // Update the attribute table
111 if (null != attTblViewer
112 && !attTblViewer.getControl().isDisposed()) {
113 System.out.println("viewer is alive");
114 updateViewer(getData(resource));
117 // Update the observe status
118 updateObserve(resource);
120 // Update the pay-load details if any
126 getUIListener = new IGetUIListener() {
129 public void onGetCompleted(final RemoteResource resource) {
130 Display.getDefault().asyncExec(new Runnable() {
135 System.out.println("AttributeView: onGetCompleted");
136 if (null == resource) {
139 // Update the attribute table
140 if (resourceInSelection != resource) {
142 .println("AttributeView: get response arrived for a different resource");
145 updateViewer(getData(resource));
147 // Update the observe status
148 updateObserve(resource);
150 // Update the pay-load details if any
156 public void onGetFailed(RemoteResource resource) {
160 putUIListener = new IPutUIListener() {
163 public void onPutCompleted(final RemoteResource resource) {
164 Display.getDefault().asyncExec(new Runnable() {
169 System.out.println("AttributeView: onPutCompleted");
170 if (null == resource) {
173 // Update the attribute table
174 if (resourceInSelection != resource) {
176 .println("AttributeView: put response arrived for a different resource");
179 updateViewer(getData(resource));
181 // Update the observe status
182 updateObserve(resource);
184 // Update the pay-load details if any
190 public void onPutFailed(RemoteResource resource) {
194 postUIListener = new IPostUIListener() {
197 public void onPostCompleted(final RemoteResource resource) {
198 Display.getDefault().asyncExec(new Runnable() {
203 System.out.println("AttributeView: onPostCompleted");
204 if (null == resource) {
207 // Update the attribute table
208 if (resourceInSelection != resource) {
210 .println("AttributeView: post response arrived for a different resource");
213 updateViewer(getData(resource));
215 // Update the observe status
216 updateObserve(resource);
218 // Update the pay-load details if any
224 public void onPostFailed(RemoteResource resource) {
228 observeUIListener = new IObserveUIListener() {
231 public void onObserveCompleted(final RemoteResource resource) {
232 Display.getDefault().asyncExec(new Runnable() {
237 System.out.println("AttributeView: onObserveCompleted");
238 if (null == resource) {
241 // Update the attribute table
242 if (resourceInSelection != resource) {
244 .println("AttributeView: observe response arrived for a different resource");
247 updateViewer(getData(resource));
249 // Update the observe status
250 updateObserve(resource);
252 // Update the pay-load details if any
258 public void onObserveFailed(RemoteResource resource) {
262 verificationUIListener = new IVerificationUIListener() {
265 public void onVerificationStarted(final RemoteResource resource,
266 final int autoType) {
267 Display.getDefault().asyncExec(new Runnable() {
271 // changeReqBtnVisibility(autoType, false);
277 public void onVerificationCompleted(final RemoteResource resource,
278 final int autoType) {
279 Display.getDefault().asyncExec(new Runnable() {
283 // changeReqBtnVisibility(autoType, true);
289 public void onVerificationAborted(final RemoteResource resource,
290 final int autoType) {
291 Display.getDefault().asyncExec(new Runnable() {
295 // changeReqBtnVisibility(autoType, true);
301 configUploadUIListener = new IConfigurationUpload() {
304 public void onConfigurationUploaded(final RemoteResource resource) {
305 Display.getDefault().asyncExec(new Runnable() {
311 .println("AttributeView: onConfigurationUploaded");
312 if (null == resource) {
315 if (resourceInSelection != resource) {
317 .println("AttributeView: config upload response arrived for a different resource");
320 if (!automateButton.isDisposed()) {
321 automateButton.setEnabled(true);
329 private void changeReqBtnVisibility(int reqType, boolean visibility) {
330 if (reqType == Constants.GET_AUTOMATION_INDEX) {
331 if (!getButton.isDisposed()) {
332 getButton.setEnabled(visibility);
334 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
335 if (!putButton.isDisposed()) {
336 putButton.setEnabled(visibility);
338 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
339 if (!postButton.isDisposed()) {
340 postButton.setEnabled(visibility);
345 private Map<String, RemoteResourceAttribute> getData(RemoteResource resource) {
346 if (null == resource) {
349 Map<String, RemoteResourceAttribute> attMap = resource
350 .getResourceAttributesMap();
351 System.out.println("AttributeView: \n" + attMap);
355 private void updateViewer(Map<String, RemoteResourceAttribute> attMap) {
356 if (null != attTblViewer) {
357 Table tbl = attTblViewer.getTable();
358 if (null != attMap) {
359 attTblViewer.setInput(attMap.entrySet().toArray());
360 if (!tbl.isDisposed()) {
361 tbl.setLinesVisible(true);
364 if (!tbl.isDisposed()) {
366 tbl.setLinesVisible(false);
372 private void updateObserve(RemoteResource resource) {
373 if (null == resource || observeResButton.isDisposed()) {
376 boolean observed = resource.isObserved();
378 observeResButton.setText(Constants.STOP_OBSERVE);
380 observeResButton.setText(Constants.OBSERVE);
385 public void createPartControl(Composite parent) {
386 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
388 parent.setLayout(new GridLayout());
389 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
390 parent.setLayoutData(gd);
392 Group attGroup = new Group(parent, SWT.NONE);
393 attGroup.setLayout(new GridLayout());
395 gd.grabExcessHorizontalSpace = true;
396 gd.horizontalAlignment = SWT.FILL;
397 gd.grabExcessVerticalSpace = true;
398 gd.verticalAlignment = SWT.FILL;
399 gd.horizontalSpan = 2;
400 // gd.heightHint = 175;
401 attGroup.setLayoutData(gd);
402 attGroup.setText("Attributes");
403 attGroup.setBackground(color);
405 setupAttributeTable(attGroup);
407 setupRequestControls(parent);
411 addManagerListeners();
413 setVisibility(false);
415 // Updating the data in the UI as per the resource in selection.
416 if (null != attTblViewer && !attTblViewer.getControl().isDisposed()) {
417 updateViewer(getData(resourceManager
418 .getCurrentResourceInSelection()));
422 private void setupRequestControls(Composite parent) {
424 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
425 Composite opsComp = new Composite(parent, SWT.NONE);
427 gd.grabExcessHorizontalSpace = true;
428 gd.horizontalAlignment = SWT.FILL;
429 opsComp.setLayoutData(gd);
430 GridLayout grid = new GridLayout(5, false);
431 opsComp.setLayout(grid);
432 opsComp.setBackground(color);
434 getButton = new Button(opsComp, SWT.PUSH);
435 getButton.setText("GET");
437 gd.grabExcessHorizontalSpace = true;
438 gd.horizontalAlignment = SWT.FILL;
440 getButton.setLayoutData(gd);
442 putButton = new Button(opsComp, SWT.PUSH);
443 putButton.setText("PUT");
445 gd.grabExcessHorizontalSpace = true;
446 gd.horizontalAlignment = SWT.FILL;
448 putButton.setLayoutData(gd);
450 postButton = new Button(opsComp, SWT.PUSH);
451 postButton.setText("POST");
453 gd.grabExcessHorizontalSpace = true;
454 gd.horizontalAlignment = SWT.FILL;
456 postButton.setLayoutData(gd);
458 observeResButton = new Button(opsComp, SWT.PUSH);
459 observeResButton.setText(Constants.OBSERVE);
461 gd.grabExcessHorizontalSpace = true;
462 gd.horizontalAlignment = SWT.FILL;
463 observeResButton.setLayoutData(gd);
465 automateButton = new Button(opsComp, SWT.PUSH);
466 automateButton.setText("Automation");
468 gd.grabExcessHorizontalSpace = true;
469 gd.horizontalAlignment = SWT.FILL;
470 automateButton.setLayoutData(gd);
473 private void setupAttributeTable(Group attGroup) {
474 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
475 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
477 createAttributeColumns(attTblViewer);
479 // make lines and header visible
480 Table table = attTblViewer.getTable();
481 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
482 table.setHeaderVisible(true);
483 table.setLinesVisible(true);
485 attTblViewer.setContentProvider(new AttributeContentProvider());
488 public void createAttributeColumns(TableViewer tableViewer) {
490 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
491 attName.getColumn().setWidth(attTblColWidth[0]);
492 attName.getColumn().setText(attTblHeaders[0]);
493 attName.setLabelProvider(new StyledCellLabelProvider() {
495 public void update(ViewerCell cell) {
496 Object element = cell.getElement();
497 if (element instanceof Map.Entry) {
498 @SuppressWarnings("unchecked")
499 Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
500 cell.setText(entry.getKey());
505 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
507 attValue.getColumn().setWidth(attTblColWidth[1]);
508 attValue.getColumn().setText(attTblHeaders[1]);
509 attValue.setLabelProvider(new StyledCellLabelProvider() {
511 public void update(ViewerCell cell) {
512 Object element = cell.getElement();
513 if (element instanceof Map.Entry) {
514 @SuppressWarnings("unchecked")
515 Map.Entry<String, RemoteResourceAttribute> entry = (Map.Entry<String, RemoteResourceAttribute>) element;
516 Object value = entry.getValue().getAttributeValue();
520 cell.setText(String.valueOf(value));
527 private void setUIListeners() {
529 getButton.addSelectionListener(new SelectionAdapter() {
531 public void widgetSelected(SelectionEvent e) {
532 if (resourceInSelection.isGetAutomtnInProgress()) {
534 .openInformation(Display.getDefault()
535 .getActiveShell(), "GET Request",
536 "GET Automation is in progress. Please wait or stop the automation.");
538 resourceManager.sendGetRequest(resourceInSelection);
543 putButton.addSelectionListener(new SelectionAdapter() {
545 public void widgetSelected(SelectionEvent e) {
546 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
549 if (resourceInSelection.isPutAutomtnInProgress()) {
551 .openInformation(Display.getDefault()
552 .getActiveShell(), "PUT Request",
553 "PUT Automation is in progress. Please wait or stop the automation.");
556 List<PutPostAttributeModel> putPostModelList;
557 putPostModelList = resourceInSelection
559 if (null == putPostModelList) {
561 .openInformation(Display.getDefault()
562 .getActiveShell(), "PUT Request",
563 "No attributes exist in the resource model.");
566 PutRequestDialog putDialog = new PutRequestDialog(
567 Display.getDefault().getActiveShell(),
569 if (putDialog.open() == Window.OK) {
570 // Call the native PUT method
571 resourceManager.sendPutRequest(resourceInSelection,
579 postButton.addSelectionListener(new SelectionAdapter() {
581 public void widgetSelected(SelectionEvent e) {
582 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
585 if (resourceInSelection.isPostAutomtnInProgress()) {
587 .openInformation(Display.getDefault()
588 .getActiveShell(), "POST Request",
589 "POST Automation is in progress. Please wait or stop the automation.");
592 List<PutPostAttributeModel> putPostModelList;
593 putPostModelList = resourceInSelection
595 if (null == putPostModelList) {
597 .openInformation(Display.getDefault()
598 .getActiveShell(), "PUT Request",
599 "No attributes exist in the resource model.");
603 PostRequestDialog postDialog = new PostRequestDialog(
604 Display.getDefault().getActiveShell(),
606 if (postDialog.open() == Window.OK) {
607 // Call the native POST method
608 resourceManager.sendPostRequest(
609 resourceInSelection, putPostModelList);
616 observeResButton.addSelectionListener(new SelectionAdapter() {
618 public void widgetSelected(SelectionEvent e) {
619 if (observeResButton.getText().equals(Constants.OBSERVE)) {
620 resourceManager.sendObserveRequest(resourceInSelection);
621 observeResButton.setText(Constants.STOP_OBSERVE);
624 .sendCancelObserveRequest(resourceInSelection);
625 observeResButton.setText(Constants.OBSERVE);
630 automateButton.addSelectionListener(new SelectionAdapter() {
632 public void widgetSelected(SelectionEvent e) {
633 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
636 RemoteResource resource = resourceManager
637 .getCurrentResourceInSelection();
638 if (null == resource) {
641 Map<String, Boolean> autoStatus = resourceManager
642 .getAutomationStatus(resource);
643 if (null == autoStatus) {
646 VerificationDialog ad = new VerificationDialog(Display
647 .getDefault().getActiveShell(), autoStatus);
655 private void addManagerListeners() {
657 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
658 resourceManager.addGetUIListener(getUIListener);
659 resourceManager.addPutUIListener(putUIListener);
660 resourceManager.addPostUIListener(postUIListener);
661 resourceManager.addObserveUIListener(observeUIListener);
662 resourceManager.addVerificationUIListener(verificationUIListener);
663 resourceManager.addConfigUploadUIListener(configUploadUIListener);
666 private void setVisibility(boolean visibility) {
667 if (!getButton.isDisposed())
668 getButton.setEnabled(visibility);
669 if (!putButton.isDisposed())
670 putButton.setEnabled(visibility);
671 if (!postButton.isDisposed())
672 postButton.setEnabled(visibility);
673 if (!automateButton.isDisposed()) {
674 if (visibility && null != resourceInSelection
675 && resourceInSelection.isConfigUploaded()) {
676 automateButton.setEnabled(true);
678 automateButton.setEnabled(false);
681 if (!observeResButton.isDisposed())
682 observeResButton.setEnabled(visibility);
685 class AttributeContentProvider implements IStructuredContentProvider {
688 public void dispose() {
692 public void inputChanged(Viewer viewer, Object arg1, Object arg2) {
696 public Object[] getElements(Object element) {
697 return (Object[]) element;
703 public void dispose() {
704 // Unregister the selection listener
705 if (null != resourceSelectionChangedListener) {
707 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
710 // Unregister the GET listener
711 if (null != getUIListener) {
712 resourceManager.removeGetUIListener(getUIListener);
719 public void setFocus() {