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) {
92 Tree tree = attViewer.getTree();
93 if (null == tree || tree.isDisposed()) {
98 .getResourceRepresentation()) {
99 attViewer.setInput(resource
100 .getResourceRepresentation());
101 attViewer.expandAll();
103 tree.setLinesVisible(true);
106 attViewer.setInput(null);
108 tree.setLinesVisible(false);
117 public void onDeviceSelectionChange(Device dev) {
118 Display.getDefault().asyncExec(new Runnable() {
121 if (null != attViewer) {
122 Tree tree = attViewer.getTree();
123 if (null != tree && !tree.isDisposed()) {
124 // tbl.deselectAll();
126 tree.setLinesVisible(false);
134 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
137 public void onResourceModelChange(final Resource resource) {
138 Display.getDefault().asyncExec(new Runnable() {
141 // Handle the notification only if it is for the current
142 // resource in selection
143 Resource resourceInSelection = resourceManager
144 .getCurrentResourceInSelection();
145 if (null == resourceInSelection) {
153 automationUIListener = new IAutomationUIListener() {
156 public void onResourceAutomationStart(final SingleResource resource) {
157 Display.getDefault().asyncExec(new Runnable() {
161 if (null == resource) {
164 Resource resourceInSelection = resourceManager
165 .getCurrentResourceInSelection();
166 if (null == resourceInSelection) {
169 // Checking whether attributes view is currently
170 // displaying the attributes of the
171 // resource whose automation has just started
172 if (resource == resourceInSelection) {
174 tree = attViewer.getTree();
175 if (!tree.isDisposed()) {
178 // Disabling the table to prevent interactions
179 // during the automation
180 tree.setEnabled(false);
189 public void onAutomationComplete(final SingleResource resource,
190 final String attName) {
191 // This method notifies the completion of attribute level
193 Display.getDefault().asyncExec(new Runnable() {
197 if (null == resource) {
200 // Check if the given resourceURI is the uri of the
201 // resource whose attributes are currently being
202 // displayed by this view.
203 Resource resourceInSelection = resourceManager
204 .getCurrentResourceInSelection();
205 if (null == resourceInSelection) {
208 if (resource != resourceInSelection) {
212 tree = attViewer.getTree();
213 if (!tree.isDisposed()) {
214 if (null != attName) {
215 // Attribute level automation has stopped
216 LocalResourceAttribute att = resourceManager
217 .getAttributeByResourceURI(resource,
222 attViewer.update(att, null);
225 // Resource level automation has stopped
226 // Enabling the table which was disabled at the
227 // beginning of automation
228 tree.setEnabled(true);
239 public void createPartControl(Composite parent) {
240 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
242 parent.setLayout(new GridLayout());
243 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
244 parent.setLayoutData(gd);
246 Group attGroup = new Group(parent, SWT.NONE);
247 attGroup.setLayout(new GridLayout());
248 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
249 attGroup.setLayoutData(gd);
250 attGroup.setText("Attributes");
251 attGroup.setBackground(color);
253 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
254 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
255 addressTree.setHeaderVisible(true);
257 attViewer = new TreeViewer(addressTree);
259 createAttributeColumns(attViewer);
261 // make lines and header visible
262 Tree tree = attViewer.getTree();
263 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
264 tree.setHeaderVisible(true);
265 tree.setLinesVisible(true);
267 attViewer.setContentProvider(new AttributeContentProvider());
268 attViewer.setLabelProvider(new AttributeLabelProvider());
270 addManagerListeners();
272 // Check whether there is any resource selected already
273 Resource resource = resourceManager.getCurrentResourceInSelection();
274 if (resource != null) {
275 attViewer.setInput(resource.getResourceRepresentation());
279 public void createAttributeColumns(TreeViewer viewer) {
280 Tree tree = viewer.getTree();
282 attributeEditor = new AttributeEditingSupport();
284 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
285 attName.setWidth(attTblColWidth[0]);
286 attName.setText(attTblHeaders[0]);
288 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
289 attValue.setWidth(attTblColWidth[1]);
290 attValue.setText(attTblHeaders[1]);
291 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
293 attValueVwrCol.setEditingSupport(attributeEditor
294 .createAttributeValueEditor(attViewer));
296 TreeColumn automation = new TreeColumn(tree, SWT.NONE);
297 automation.setWidth(attTblColWidth[2]);
298 automation.setText(attTblHeaders[2]);
299 TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
301 automationVwrCol.setEditingSupport(attributeEditor
302 .createAutomationEditor(attViewer));
305 private void addManagerListeners() {
306 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
307 resourceSelectionChangedListener);
308 UiListenerHandler.getInstance().addResourceModelChangedUIListener(
309 resourceModelChangedUIListener);
310 UiListenerHandler.getInstance().addAutomationUIListener(
311 automationUIListener);
314 class AttributeContentProvider implements ITreeContentProvider,
317 private TreeViewer mTreeViewer;
320 public void dispose() {
324 public void inputChanged(Viewer viewer, Object oldAttribute,
325 Object newAttribute) {
326 mTreeViewer = (TreeViewer) viewer;
330 public Object[] getChildren(Object attribute) {
331 if (attribute instanceof AttributeElement) {
332 return ((AttributeElement) attribute).getChildren().values()
336 return new Object[0];
340 public Object getParent(Object attribute) {
341 if (attribute instanceof AttributeElement)
342 return ((AttributeElement) attribute).getParent();
347 public boolean hasChildren(Object attribute) {
348 if (attribute instanceof AttributeElement)
349 return ((AttributeElement) attribute).hasChildren();
354 public Object[] getElements(Object resourceModel) {
355 if (resourceModel instanceof ResourceRepresentation) {
356 ((ResourceRepresentation) resourceModel).setListener(this);
357 return ((ResourceRepresentation) resourceModel).getAttributes()
361 return new Object[0];
365 public void add(final AttributeElement attribute) {
366 Display.getDefault().asyncExec(new Runnable() {
369 mTreeViewer.refresh(attribute.getParent());
370 mTreeViewer.expandAll();
376 public void remove(final AttributeElement attribute) {
377 Display.getDefault().asyncExec(new Runnable() {
380 mTreeViewer.refresh(attribute.getParent());
381 mTreeViewer.expandAll();
387 public void update(final AttributeElement attribute) {
388 Display.getDefault().asyncExec(new Runnable() {
391 mTreeViewer.update(attribute, null);
392 mTreeViewer.expandAll();
398 class AttributeLabelProvider implements ITableLabelProvider {
401 public void addListener(ILabelProviderListener arg0) {
405 public void dispose() {
409 public boolean isLabelProperty(Object arg0, String arg1) {
414 public void removeListener(ILabelProviderListener arg0) {
419 public Image getColumnImage(Object element, int col) {
421 if (element instanceof AttributeElement) {
422 // Ignore for collection resource
423 Resource res = resourceManager
424 .getCurrentResourceInSelection();
425 if (res instanceof SingleResource) {
426 AttributeElement attrElement = (AttributeElement) element;
427 SimulatorResourceAttribute attribute = attrElement
428 .getSimulatorResourceAttribute();
429 TypeInfo type = attribute.value().typeInfo();
430 if (attrElement.isAutoUpdateSupport()
431 && !attrElement.isReadOnly()) {
432 if (attrElement.isAutoUpdateInProgress()) {
433 return Activator.getDefault()
435 .get(Constants.CHECKED);
437 return Activator.getDefault()
439 .get(Constants.UNCHECKED);
449 public String getColumnText(Object element, int column) {
450 if (element instanceof AttributeElement) {
451 AttributeElement attrElement = (AttributeElement) element;
453 case 0: // Attribute name column
455 SimulatorResourceAttribute attribute = attrElement
456 .getSimulatorResourceAttribute();
457 return attribute.name();
460 case 1: // Attribute value column
462 SimulatorResourceAttribute attribute = attrElement
463 .getSimulatorResourceAttribute();
465 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
466 return Utility.getAttributeValueAsString(attribute
472 // Ignore for collection resource
473 Resource res = resourceManager
474 .getCurrentResourceInSelection();
475 if (res instanceof SingleResource) {
476 SimulatorResourceAttribute attribute = attrElement
477 .getSimulatorResourceAttribute();
478 TypeInfo type = attribute.value().typeInfo();
479 if (type.mType == AttributeValue.ValueType.ARRAY) {
480 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
483 } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
484 Object parent = attrElement.getParent();
486 && !(parent instanceof ResourceRepresentation)) {
488 } else if (attrElement.isReadOnly()) {
490 } else if (attrElement.isAutoUpdateSupport()) {
491 if (attrElement.isAutoUpdateInProgress())
492 return Constants.ENABLED;
494 return Constants.DISABLED;
510 public void dispose() {
511 // Unregister the selection listener
512 if (null != resourceSelectionChangedListener) {
513 UiListenerHandler.getInstance()
514 .removeResourceSelectionChangedUIListener(
515 resourceSelectionChangedListener);
518 // Unregister the model change listener
519 if (null != resourceModelChangedUIListener) {
520 UiListenerHandler.getInstance()
521 .removeResourceModelChangedUIListener(
522 resourceModelChangedUIListener);
525 // Unregister the automation complete listener
526 if (null != automationUIListener) {
527 UiListenerHandler.getInstance().removeAutomationUIListener(
528 automationUIListener);
535 public void setFocus() {