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 oic.simulator.serviceprovider.Activator;
20 import oic.simulator.serviceprovider.listener.IAutomationListener;
21 import oic.simulator.serviceprovider.listener.IDataChangeListener;
22 import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
23 import oic.simulator.serviceprovider.manager.ResourceManager;
24 import oic.simulator.serviceprovider.manager.UiListenerHandler;
25 import oic.simulator.serviceprovider.model.AttributeElement;
26 import oic.simulator.serviceprovider.model.Device;
27 import oic.simulator.serviceprovider.model.Resource;
28 import oic.simulator.serviceprovider.model.ResourceRepresentation;
29 import oic.simulator.serviceprovider.model.SingleResource;
30 import oic.simulator.serviceprovider.utils.Constants;
31 import oic.simulator.serviceprovider.utils.Utility;
33 import org.eclipse.jface.viewers.ILabelProviderListener;
34 import org.eclipse.jface.viewers.ITableLabelProvider;
35 import org.eclipse.jface.viewers.ITreeContentProvider;
36 import org.eclipse.jface.viewers.TreeViewer;
37 import org.eclipse.jface.viewers.TreeViewerColumn;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.graphics.Color;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Tree;
48 import org.eclipse.swt.widgets.TreeColumn;
49 import org.eclipse.ui.part.ViewPart;
50 import org.oic.simulator.AttributeValue;
51 import org.oic.simulator.AttributeValue.TypeInfo;
52 import org.oic.simulator.AttributeValue.ValueType;
53 import org.oic.simulator.SimulatorResourceAttribute;
56 * This class manages and shows the attribute view in the perspective.
58 public class AttributeView extends ViewPart {
60 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
62 private TreeViewer attViewer;
64 private AttributeEditingSupport attributeEditor;
66 private ISelectionChangedListener resourceSelectionChangedListener;
67 private IAutomationListener automationUIListener;
68 private IDataChangeListener dataChangeListener;
70 private final String[] attTblHeaders = { "Name", "Value",
72 private final Integer[] attTblColWidth = { 150, 190, 150 };
74 private ResourceManager resourceManager;
76 public AttributeView() {
78 resourceManager = Activator.getDefault().getResourceManager();
80 resourceSelectionChangedListener = new ISelectionChangedListener() {
83 public void onResourceSelectionChange(final Resource resource) {
84 Display.getDefault().asyncExec(new Runnable() {
87 if (null != attViewer) {
88 Tree tree = attViewer.getTree();
89 if (null == tree || tree.isDisposed()) {
93 // Enabling/disabling the tree based on the resource
94 // and automation status.
95 if (resource instanceof SingleResource)
96 if (((SingleResource) resource)
97 .isResourceAutomationInProgress())
98 tree.setEnabled(false);
100 tree.setEnabled(true);
104 .getResourceRepresentation()) {
105 attViewer.setInput(resource
106 .getResourceRepresentation());
107 attViewer.expandAll();
108 tree.setLinesVisible(true);
111 attViewer.setInput(null);
112 tree.setLinesVisible(false);
120 public void onDeviceSelectionChange(Device dev) {
121 Display.getDefault().asyncExec(new Runnable() {
124 if (null != attViewer) {
125 Tree tree = attViewer.getTree();
126 if (null != tree && !tree.isDisposed()) {
127 // tbl.deselectAll();
129 tree.setLinesVisible(false);
137 dataChangeListener = new IDataChangeListener() {
140 public void add(final AttributeElement attribute) {
141 Display.getDefault().asyncExec(new Runnable() {
144 attViewer.refresh(attribute.getParent());
145 attViewer.expandAll();
151 public void remove(final AttributeElement attribute) {
152 Display.getDefault().asyncExec(new Runnable() {
155 attViewer.refresh(attribute.getParent());
156 attViewer.expandAll();
162 public void update(final AttributeElement attribute) {
163 Display.getDefault().asyncExec(new Runnable() {
166 attViewer.update(attribute, null);
167 attViewer.expandAll();
173 automationUIListener = new IAutomationListener() {
176 public void onResourceAutomationStart(final SingleResource resource) {
177 Display.getDefault().asyncExec(new Runnable() {
181 if (null == resource) {
184 Resource resourceInSelection = resourceManager
185 .getCurrentResourceInSelection();
186 if (null == resourceInSelection) {
189 // Checking whether attributes view is currently
190 // displaying the attributes of the
191 // resource whose automation has just started
192 if (resource == resourceInSelection) {
194 tree = attViewer.getTree();
195 if (!tree.isDisposed()) {
198 // Disabling the table to prevent interactions
199 // during the automation
200 tree.setEnabled(false);
209 public void onAutomationComplete(final SingleResource resource,
210 final String attName) {
211 // This method notifies the completion of attribute level
213 Display.getDefault().asyncExec(new Runnable() {
217 if (null == resource) {
220 // Check if the given resourceURI is the uri of the
221 // resource whose attributes are currently being
222 // displayed by this view.
223 Resource resourceInSelection = resourceManager
224 .getCurrentResourceInSelection();
225 if (null == resourceInSelection) {
228 if (resource != resourceInSelection) {
232 tree = attViewer.getTree();
233 if (!tree.isDisposed()) {
234 tree.setEnabled(true);
244 public void createPartControl(Composite parent) {
245 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
247 parent.setLayout(new GridLayout());
248 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
249 parent.setLayoutData(gd);
251 Group attGroup = new Group(parent, SWT.NONE);
252 attGroup.setLayout(new GridLayout());
253 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
254 attGroup.setLayoutData(gd);
255 attGroup.setText("Attributes");
256 attGroup.setBackground(color);
258 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
259 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
260 addressTree.setHeaderVisible(true);
262 attViewer = new TreeViewer(addressTree);
264 createAttributeColumns(attViewer);
266 // make lines and header visible
267 Tree tree = attViewer.getTree();
268 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
269 tree.setHeaderVisible(true);
270 tree.setLinesVisible(true);
272 attViewer.setContentProvider(new AttributeContentProvider());
273 attViewer.setLabelProvider(new AttributeLabelProvider());
275 addManagerListeners();
277 // Check whether there is any resource selected already
278 Resource resource = resourceManager.getCurrentResourceInSelection();
279 if (resource != null) {
280 attViewer.setInput(resource.getResourceRepresentation());
284 public void createAttributeColumns(TreeViewer viewer) {
285 Tree tree = viewer.getTree();
287 attributeEditor = new AttributeEditingSupport();
289 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
290 attName.setWidth(attTblColWidth[0]);
291 attName.setText(attTblHeaders[0]);
293 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
294 attValue.setWidth(attTblColWidth[1]);
295 attValue.setText(attTblHeaders[1]);
296 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
298 attValueVwrCol.setEditingSupport(attributeEditor
299 .createAttributeValueEditor(attViewer));
301 TreeColumn automation = new TreeColumn(tree, SWT.NONE);
302 automation.setWidth(attTblColWidth[2]);
303 automation.setText(attTblHeaders[2]);
304 TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
306 automationVwrCol.setEditingSupport(attributeEditor
307 .createAutomationEditor(attViewer));
310 private void addManagerListeners() {
311 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
312 resourceSelectionChangedListener);
313 UiListenerHandler.getInstance().addDataChangeListener(
315 UiListenerHandler.getInstance().addAutomationUIListener(
316 automationUIListener);
319 class AttributeContentProvider implements ITreeContentProvider {
322 public void dispose() {
326 public void inputChanged(Viewer viewer, Object oldAttribute,
327 Object newAttribute) {
331 public Object[] getChildren(Object attribute) {
332 if (attribute instanceof AttributeElement) {
333 return ((AttributeElement) attribute).getChildren().values()
337 return new Object[0];
341 public Object getParent(Object attribute) {
342 if (attribute instanceof AttributeElement)
343 return ((AttributeElement) attribute).getParent();
348 public boolean hasChildren(Object attribute) {
349 if (attribute instanceof AttributeElement)
350 return ((AttributeElement) attribute).hasChildren();
355 public Object[] getElements(Object resourceModel) {
356 if (resourceModel instanceof ResourceRepresentation) {
357 return ((ResourceRepresentation) resourceModel).getAttributes()
361 return new Object[0];
365 class AttributeLabelProvider implements ITableLabelProvider {
368 public void addListener(ILabelProviderListener arg0) {
372 public void dispose() {
376 public boolean isLabelProperty(Object arg0, String arg1) {
381 public void removeListener(ILabelProviderListener arg0) {
386 public Image getColumnImage(Object element, int col) {
388 if (element instanceof AttributeElement) {
389 // Ignore for collection resource
390 Resource res = resourceManager
391 .getCurrentResourceInSelection();
392 if (res instanceof SingleResource) {
393 AttributeElement attrElement = (AttributeElement) element;
394 if (attrElement.isAutoUpdateSupport()
395 && !attrElement.isReadOnly()) {
396 if (attrElement.isAutoUpdateInProgress()) {
397 return Activator.getDefault()
399 .get(Constants.CHECKED);
401 return Activator.getDefault()
403 .get(Constants.UNCHECKED);
413 public String getColumnText(Object element, int column) {
414 if (element instanceof AttributeElement) {
415 AttributeElement attrElement = (AttributeElement) element;
417 case 0: // Attribute name column
419 SimulatorResourceAttribute attribute = attrElement
420 .getSimulatorResourceAttribute();
421 return attribute.name();
424 case 1: // Attribute value column
426 SimulatorResourceAttribute attribute = attrElement
427 .getSimulatorResourceAttribute();
429 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
430 return Utility.getAttributeValueAsString(attribute
436 // Ignore for collection resource
437 Resource res = resourceManager
438 .getCurrentResourceInSelection();
439 if (res instanceof SingleResource) {
440 SimulatorResourceAttribute attribute = attrElement
441 .getSimulatorResourceAttribute();
442 TypeInfo type = attribute.value().typeInfo();
443 if (type.mType == AttributeValue.ValueType.ARRAY) {
444 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
447 } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
448 Object parent = attrElement.getParent();
450 && !(parent instanceof ResourceRepresentation)) {
452 } else if (attrElement.isReadOnly()) {
454 } else if (attrElement.isAutoUpdateSupport()) {
455 if (attrElement.isAutoUpdateInProgress())
456 return Constants.ENABLED;
458 return Constants.DISABLED;
474 public void dispose() {
475 // Unregister the selection listener
476 if (null != resourceSelectionChangedListener) {
477 UiListenerHandler.getInstance()
478 .removeResourceSelectionChangedUIListener(
479 resourceSelectionChangedListener);
482 // Unregister the data model change listener
483 if (null != dataChangeListener) {
484 UiListenerHandler.getInstance().removeDataChangeListener(
488 // Unregister the automation complete listener
489 if (null != automationUIListener) {
490 UiListenerHandler.getInstance().removeAutomationUIListener(
491 automationUIListener);
498 public void setFocus() {