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.serviceprovider.view;
19 import java.util.List;
21 import oic.simulator.serviceprovider.Activator;
22 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
23 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
24 import oic.simulator.serviceprovider.listener.ISelectionChangedUIListener;
25 import oic.simulator.serviceprovider.manager.ResourceManager;
26 import oic.simulator.serviceprovider.manager.UiListenerHandler;
27 import oic.simulator.serviceprovider.model.AttributeElement;
28 import oic.simulator.serviceprovider.model.DataChangeListener;
29 import oic.simulator.serviceprovider.model.Device;
30 import oic.simulator.serviceprovider.model.LocalResourceAttribute;
31 import oic.simulator.serviceprovider.model.Resource;
32 import oic.simulator.serviceprovider.model.ResourceRepresentation;
33 import oic.simulator.serviceprovider.model.SingleResource;
34 import oic.simulator.serviceprovider.utils.Constants;
35 import oic.simulator.serviceprovider.utils.Utility;
37 import org.eclipse.jface.viewers.ILabelProviderListener;
38 import org.eclipse.jface.viewers.ITableLabelProvider;
39 import org.eclipse.jface.viewers.ITreeContentProvider;
40 import org.eclipse.jface.viewers.TreeViewer;
41 import org.eclipse.jface.viewers.TreeViewerColumn;
42 import org.eclipse.jface.viewers.Viewer;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.graphics.Color;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Display;
50 import org.eclipse.swt.widgets.Group;
51 import org.eclipse.swt.widgets.Tree;
52 import org.eclipse.swt.widgets.TreeColumn;
53 import org.eclipse.ui.part.ViewPart;
54 import org.oic.simulator.AttributeValue;
55 import org.oic.simulator.AttributeValue.TypeInfo;
56 import org.oic.simulator.AttributeValue.ValueType;
57 import org.oic.simulator.SimulatorResourceAttribute;
60 * This class manages and shows the attribute view in the perspective.
62 public class AttributeView extends ViewPart {
64 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
66 private TreeViewer attViewer;
68 private AttributeEditingSupport attributeEditor;
70 private ISelectionChangedUIListener resourceSelectionChangedListener;
71 private IResourceModelChangedUIListener resourceModelChangedUIListener;
72 private IAutomationUIListener automationUIListener;
74 private final String[] attTblHeaders = { "Name", "Value",
76 private final Integer[] attTblColWidth = { 150, 190, 150 };
78 private ResourceManager resourceManager;
80 public AttributeView() {
82 resourceManager = Activator.getDefault().getResourceManager();
84 resourceSelectionChangedListener = new ISelectionChangedUIListener() {
87 public void onResourceSelectionChange(final Resource resource) {
88 Display.getDefault().asyncExec(new Runnable() {
91 if (null != attViewer) {
94 .getResourceRepresentation())
95 attViewer.setInput(resource
96 .getResourceRepresentation());
98 * updateViewer(getData(resource)); Tree tree =
99 * attViewer.getTree(); if (!tree.isDisposed()) { if
100 * (null != resource && (resource instanceof
101 * SingleResource && ((SingleResource) resource)
102 * .isResourceAutomationInProgress())) {
103 * tree.setEnabled(false); } else {
104 * tree.setEnabled(true); } }
112 public void onDeviceSelectionChange(Device dev) {
113 Display.getDefault().asyncExec(new Runnable() {
122 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
125 public void onResourceModelChange(final Resource resource) {
126 Display.getDefault().asyncExec(new Runnable() {
129 // Handle the notification only if it is for the current
130 // resource in selection
131 Resource resourceInSelection = resourceManager
132 .getCurrentResourceInSelection();
133 if (null == resourceInSelection) {
137 * if (resource != resourceInSelection) { // This
138 * notification is for a different resource // whose
139 * attributes are not // currently not being shown in
140 * UI. So ignoring this // notification. return; } //
141 * Refresh the table viewers which will display // the
142 * updated values if (null != attViewer) { if (resource
143 * instanceof CollectionResource) {
144 * updateViewer(getData(resource)); } else {
145 * updateViewer(getData(resource)); } }
152 automationUIListener = new IAutomationUIListener() {
155 public void onResourceAutomationStart(final SingleResource resource) {
156 Display.getDefault().asyncExec(new Runnable() {
160 if (null == resource) {
163 Resource resourceInSelection = resourceManager
164 .getCurrentResourceInSelection();
165 if (null == resourceInSelection) {
168 // Checking whether attributes view is currently
169 // displaying the attributes of the
170 // resource whose automation has just started
171 if (resource == resourceInSelection) {
173 tree = attViewer.getTree();
174 if (!tree.isDisposed()) {
177 // Disabling the table to prevent interactions
178 // during the automation
179 tree.setEnabled(false);
188 public void onAutomationComplete(final SingleResource resource,
189 final String attName) {
190 // This method notifies the completion of attribute level
192 Display.getDefault().asyncExec(new Runnable() {
196 if (null == resource) {
199 // Check if the given resourceURI is the uri of the
200 // resource whose attributes are currently being
201 // displayed by this view.
202 Resource resourceInSelection = resourceManager
203 .getCurrentResourceInSelection();
204 if (null == resourceInSelection) {
207 if (resource != resourceInSelection) {
211 tree = attViewer.getTree();
212 if (!tree.isDisposed()) {
213 if (null != attName) {
214 // Attribute level automation has stopped
215 LocalResourceAttribute att = resourceManager
216 .getAttributeByResourceURI(resource,
221 attViewer.update(att, null);
224 // Resource level automation has stopped
225 // Enabling the table which was disabled at the
226 // beginning of automation
227 tree.setEnabled(true);
238 public void createPartControl(Composite parent) {
239 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
241 parent.setLayout(new GridLayout());
242 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
243 parent.setLayoutData(gd);
245 Group attGroup = new Group(parent, SWT.NONE);
246 attGroup.setLayout(new GridLayout());
247 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
248 attGroup.setLayoutData(gd);
249 attGroup.setText("Attributes");
250 attGroup.setBackground(color);
252 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
253 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
254 addressTree.setHeaderVisible(true);
256 attViewer = new TreeViewer(addressTree);
258 createAttributeColumns(attViewer);
260 // make lines and header visible
261 Tree tree = attViewer.getTree();
262 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
263 tree.setHeaderVisible(true);
264 tree.setLinesVisible(true);
266 attViewer.setContentProvider(new AttributeContentProvider());
267 attViewer.setLabelProvider(new AttributeLabelProvider());
269 addManagerListeners();
271 // Check whether there is any resource selected already
272 Resource resource = resourceManager.getCurrentResourceInSelection();
273 if (resource != null) {
274 attViewer.setInput(resource.getResourceRepresentation());
278 public void createAttributeColumns(TreeViewer viewer) {
279 Tree tree = viewer.getTree();
281 attributeEditor = new AttributeEditingSupport();
283 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
284 attName.setWidth(attTblColWidth[0]);
285 attName.setText(attTblHeaders[0]);
287 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
288 attValue.setWidth(attTblColWidth[1]);
289 attValue.setText(attTblHeaders[1]);
290 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
292 attValueVwrCol.setEditingSupport(attributeEditor
293 .createAttributeValueEditor(attViewer));
295 TreeColumn automation = new TreeColumn(tree, SWT.NONE);
296 automation.setWidth(attTblColWidth[2]);
297 automation.setText(attTblHeaders[2]);
298 TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
300 automationVwrCol.setEditingSupport(attributeEditor
301 .createAutomationEditor(attViewer));
304 private void addManagerListeners() {
305 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
306 resourceSelectionChangedListener);
307 UiListenerHandler.getInstance().addResourceModelChangedUIListener(
308 resourceModelChangedUIListener);
309 UiListenerHandler.getInstance().addAutomationUIListener(
310 automationUIListener);
313 private List<LocalResourceAttribute> getData(Resource resource) {
314 if (null != resource) {
315 List<LocalResourceAttribute> attList = resourceManager
316 .getAttributes((Resource) resource);
317 // List<LocalResourceAttribute> attList =
318 // Utility.getDummyAttributes();
325 private void updateViewer(List<LocalResourceAttribute> attList) {
326 Tree tree = attViewer.getTree();;
327 if (null != attList) {
328 if (null != tree && !tree.isDisposed()) {
329 tree.setLinesVisible(true);
330 attViewer.setInput(attList.toArray());
333 // Clear the attributes table viewer
334 if (null != attViewer) {
335 if (null != tree && !tree.isDisposed()) {
336 // tbl.deselectAll();
338 tree.setLinesVisible(false);
344 class AttributeContentProvider implements ITreeContentProvider,
347 private TreeViewer mTreeViewer;
350 public void dispose() {
354 public void inputChanged(Viewer viewer, Object oldAttribute,
355 Object newAttribute) {
356 mTreeViewer = (TreeViewer) viewer;
360 public Object[] getChildren(Object attribute) {
361 if (attribute instanceof AttributeElement) {
362 return ((AttributeElement) attribute).getChildren().values()
366 return new Object[0];
370 public Object getParent(Object attribute) {
371 if (attribute instanceof AttributeElement)
372 return ((AttributeElement) attribute).getParent();
377 public boolean hasChildren(Object attribute) {
378 if (attribute instanceof AttributeElement)
379 return ((AttributeElement) attribute).hasChildren();
384 public Object[] getElements(Object resourceModel) {
385 if (resourceModel instanceof ResourceRepresentation) {
386 ((ResourceRepresentation) resourceModel).setListener(this);
387 return ((ResourceRepresentation) resourceModel).getAttributes()
391 return new Object[0];
395 public void add(final AttributeElement attribute) {
396 Display.getDefault().asyncExec(new Runnable() {
399 mTreeViewer.refresh(attribute.getParent());
405 public void remove(final AttributeElement attribute) {
406 Display.getDefault().asyncExec(new Runnable() {
409 mTreeViewer.refresh(attribute.getParent());
415 public void update(final AttributeElement attribute) {
416 Display.getDefault().asyncExec(new Runnable() {
419 mTreeViewer.update(attribute, null);
425 class AttributeLabelProvider implements ITableLabelProvider {
428 public void addListener(ILabelProviderListener arg0) {
432 public void dispose() {
436 public boolean isLabelProperty(Object arg0, String arg1) {
441 public void removeListener(ILabelProviderListener arg0) {
446 public Image getColumnImage(Object element, int col) {
448 if (element instanceof AttributeElement) {
449 // Ignore for collection resource
450 Resource res = resourceManager
451 .getCurrentResourceInSelection();
452 if (res instanceof SingleResource) {
453 AttributeElement attrElement = (AttributeElement) element;
454 SimulatorResourceAttribute attribute = attrElement
455 .getSimulatorResourceAttribute();
456 TypeInfo type = attribute.value().typeInfo();
457 if (attrElement.isAutoUpdateSupport()
458 && !attrElement.isReadOnly()) {
459 if (attrElement.isAutoUpdateInProgress()) {
460 return Activator.getDefault()
462 .get(Constants.CHECKED);
464 return Activator.getDefault()
466 .get(Constants.UNCHECKED);
476 public String getColumnText(Object element, int column) {
477 if (element instanceof AttributeElement) {
478 AttributeElement attrElement = (AttributeElement) element;
480 case 0: // Attribute name column
482 SimulatorResourceAttribute attribute = attrElement
483 .getSimulatorResourceAttribute();
484 return attribute.name();
487 case 1: // Attribute value column
489 SimulatorResourceAttribute attribute = attrElement
490 .getSimulatorResourceAttribute();
492 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
493 return Utility.getAttributeValueAsString(attribute
499 // Ignore for collection resource
500 Resource res = resourceManager
501 .getCurrentResourceInSelection();
502 if (res instanceof SingleResource) {
503 SimulatorResourceAttribute attribute = attrElement
504 .getSimulatorResourceAttribute();
505 TypeInfo type = attribute.value().typeInfo();
506 if (type.mType == AttributeValue.ValueType.ARRAY) {
507 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
510 } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
511 Object parent = attrElement.getParent();
513 && !(parent instanceof ResourceRepresentation)) {
515 } else if (attrElement.isReadOnly()) {
517 } else if (attrElement.isAutoUpdateSupport()) {
518 if (attrElement.isAutoUpdateInProgress())
519 return Constants.ENABLED;
521 return Constants.DISABLED;
537 public void dispose() {
538 // Unregister the selection listener
539 if (null != resourceSelectionChangedListener) {
540 UiListenerHandler.getInstance()
541 .removeResourceSelectionChangedUIListener(
542 resourceSelectionChangedListener);
545 // Unregister the model change listener
546 if (null != resourceModelChangedUIListener) {
547 UiListenerHandler.getInstance()
548 .removeResourceModelChangedUIListener(
549 resourceModelChangedUIListener);
552 // Unregister the automation complete listener
553 if (null != automationUIListener) {
554 UiListenerHandler.getInstance().removeAutomationUIListener(
555 automationUIListener);
562 public void setFocus() {