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;
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
24 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
25 import oic.simulator.serviceprovider.listener.ISelectionChangedUIListener;
26 import oic.simulator.serviceprovider.manager.ResourceManager;
27 import oic.simulator.serviceprovider.manager.UiListenerHandler;
28 import oic.simulator.serviceprovider.model.CollectionResource;
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.SRMItem;
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;
58 import org.oic.simulator.SimulatorResourceModel;
61 * This class manages and shows the attribute view in the perspective.
63 public class AttributeView extends ViewPart {
65 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
67 private TreeViewer attViewer;
69 private AttributeEditingSupport attributeEditor;
71 private ISelectionChangedUIListener resourceSelectionChangedListener;
72 private IResourceModelChangedUIListener resourceModelChangedUIListener;
73 private IAutomationUIListener automationUIListener;
75 private final String[] attTblHeaders = { "Name", "Value",
77 private final Integer[] attTblColWidth = { 150, 190, 150 };
79 private ResourceManager resourceManager;
81 public AttributeView() {
83 resourceManager = Activator.getDefault().getResourceManager();
85 resourceSelectionChangedListener = new ISelectionChangedUIListener() {
88 public void onResourceSelectionChange(final Resource resource) {
89 Display.getDefault().asyncExec(new Runnable() {
92 if (null != attViewer) {
93 updateViewer(getData(resource));
94 Tree tree = attViewer.getTree();
95 if (!tree.isDisposed()) {
97 && (resource instanceof SingleResource && ((SingleResource) resource)
98 .isResourceAutomationInProgress())) {
99 tree.setEnabled(false);
101 tree.setEnabled(true);
110 public void onDeviceSelectionChange(Device dev) {
111 Display.getDefault().asyncExec(new Runnable() {
120 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
123 public void onResourceModelChange(final Resource resource) {
124 Display.getDefault().asyncExec(new Runnable() {
127 // Handle the notification only if it is for the current
128 // resource in selection
129 Resource resourceInSelection = resourceManager
130 .getCurrentResourceInSelection();
131 if (null == resourceInSelection) {
134 if (resource != resourceInSelection) {
135 // This notification is for a different resource
136 // whose attributes are not
137 // currently not being shown in UI. So ignoring this
141 // Refresh the table viewers which will display
142 // the updated values
143 if (null != attViewer) {
144 if (resource instanceof CollectionResource) {
145 updateViewer(getData(resource));
147 updateViewer(getData(resource));
155 automationUIListener = new IAutomationUIListener() {
158 public void onResourceAutomationStart(final SingleResource resource) {
159 Display.getDefault().asyncExec(new Runnable() {
163 if (null == resource) {
166 Resource resourceInSelection = resourceManager
167 .getCurrentResourceInSelection();
168 if (null == resourceInSelection) {
171 // Checking whether attributes view is currently
172 // displaying the attributes of the
173 // resource whose automation has just started
174 if (resource == resourceInSelection) {
176 tree = attViewer.getTree();
177 if (!tree.isDisposed()) {
180 // Disabling the table to prevent interactions
181 // during the automation
182 tree.setEnabled(false);
191 public void onAutomationComplete(final SingleResource resource,
192 final String attName) {
193 // This method notifies the completion of attribute level
195 Display.getDefault().asyncExec(new Runnable() {
199 if (null == resource) {
202 // Check if the given resourceURI is the uri of the
203 // resource whose attributes are currently being
204 // displayed by this view.
205 Resource resourceInSelection = resourceManager
206 .getCurrentResourceInSelection();
207 if (null == resourceInSelection) {
210 if (resource != resourceInSelection) {
214 tree = attViewer.getTree();
215 if (!tree.isDisposed()) {
216 if (null != attName) {
217 // Attribute level automation has stopped
218 LocalResourceAttribute att = resourceManager
219 .getAttributeByResourceURI(resource,
224 attViewer.update(att, null);
227 // Resource level automation has stopped
228 // Enabling the table which was disabled at the
229 // beginning of automation
230 tree.setEnabled(true);
241 public void createPartControl(Composite parent) {
242 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
244 parent.setLayout(new GridLayout());
245 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
246 parent.setLayoutData(gd);
248 Group attGroup = new Group(parent, SWT.NONE);
249 attGroup.setLayout(new GridLayout());
250 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
251 attGroup.setLayoutData(gd);
252 attGroup.setText("Attributes");
253 attGroup.setBackground(color);
255 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
256 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
257 addressTree.setHeaderVisible(true);
259 attViewer = new TreeViewer(addressTree);
261 createAttributeColumns(attViewer);
263 // make lines and header visible
264 Tree tree = attViewer.getTree();
265 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
266 tree.setHeaderVisible(true);
267 tree.setLinesVisible(true);
269 attViewer.setContentProvider(new AttributeContentProvider());
270 attViewer.setLabelProvider(new AttributeLabelProvider());
272 addManagerListeners();
274 // Check whether there is any resource selected already
275 List<LocalResourceAttribute> propertyList = getData(resourceManager
276 .getCurrentResourceInSelection());
277 if (null != propertyList) {
278 updateViewer(propertyList);
282 public void createAttributeColumns(TreeViewer viewer) {
283 Tree tree = viewer.getTree();
285 attributeEditor = new AttributeEditingSupport();
287 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
288 attName.setWidth(attTblColWidth[0]);
289 attName.setText(attTblHeaders[0]);
291 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
292 attValue.setWidth(attTblColWidth[1]);
293 attValue.setText(attTblHeaders[1]);
294 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
296 attValueVwrCol.setEditingSupport(attributeEditor
297 .createAttributeValueEditor(attViewer));
299 TreeColumn automation = new TreeColumn(tree, SWT.NONE);
300 automation.setWidth(attTblColWidth[2]);
301 automation.setText(attTblHeaders[2]);
302 TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
304 automationVwrCol.setEditingSupport(attributeEditor
305 .createAutomationEditor(attViewer));
308 private void addManagerListeners() {
309 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
310 resourceSelectionChangedListener);
311 UiListenerHandler.getInstance().addResourceModelChangedUIListener(
312 resourceModelChangedUIListener);
313 UiListenerHandler.getInstance().addAutomationUIListener(
314 automationUIListener);
317 private List<LocalResourceAttribute> getData(Resource resource) {
318 if (null != resource) {
319 List<LocalResourceAttribute> attList = resourceManager
320 .getAttributes((Resource) resource);
321 // List<LocalResourceAttribute> attList =
322 // Utility.getDummyAttributes();
329 private void updateViewer(List<LocalResourceAttribute> attList) {
330 Tree tree = attViewer.getTree();;
331 if (null != attList) {
332 if (null != tree && !tree.isDisposed()) {
333 tree.setLinesVisible(true);
334 attViewer.setInput(attList.toArray());
337 // Clear the attributes table viewer
338 if (null != attViewer) {
339 if (null != tree && !tree.isDisposed()) {
340 // tbl.deselectAll();
342 tree.setLinesVisible(false);
348 class AttributeContentProvider implements ITreeContentProvider {
351 public void dispose() {
355 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
359 public Object[] getChildren(Object element) {
360 if (element instanceof SimulatorResourceAttribute
361 || element instanceof LocalResourceAttribute) {
362 SimulatorResourceAttribute att;
363 if (element instanceof LocalResourceAttribute) {
364 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
365 att = localAtt.getResourceAttributeRef();
367 return new Object[1];
370 att = (SimulatorResourceAttribute) element;
372 AttributeValue val = att.value();
374 return new Object[1];
376 TypeInfo type = val.typeInfo();
377 if (type.mType == ValueType.RESOURCEMODEL) {
378 SimulatorResourceModel model = (SimulatorResourceModel) val
381 return new Object[1];
383 return resourceManager.getAttributes(model).toArray();
384 } else if (type.mType == ValueType.ARRAY
385 && type.mBaseType == ValueType.RESOURCEMODEL
386 && type.mDepth == 1) {
387 SimulatorResourceModel[] model = (SimulatorResourceModel[]) val
389 if (null == model || model.length < 1) {
390 return new Object[1];
392 return resourceManager.getIndexedAttributes(model)
395 } else if (element instanceof SRMItem) {
396 SRMItem item = (SRMItem) element;
397 SimulatorResourceModel model = (SimulatorResourceModel) item
400 return new Object[1];
402 return resourceManager.getAttributes(model).toArray();
404 return new Object[1];
408 public Object[] getElements(Object element) {
409 Object[] elements = (Object[]) element;
414 public Object getParent(Object element) {
419 public boolean hasChildren(Object element) {
420 if (element instanceof SimulatorResourceAttribute
421 || element instanceof LocalResourceAttribute) {
422 SimulatorResourceAttribute att;
423 if (element instanceof LocalResourceAttribute) {
424 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
425 att = localAtt.getResourceAttributeRef();
430 att = (SimulatorResourceAttribute) element;
432 AttributeValue val = att.value();
436 TypeInfo type = val.typeInfo();
437 if (type.mType == ValueType.RESOURCEMODEL) {
438 SimulatorResourceModel model = (SimulatorResourceModel) val
443 Map<String, SimulatorResourceAttribute> attributes = model
445 if (null != attributes && attributes.size() > 0) {
448 } else if (type.mType == ValueType.ARRAY
449 && type.mBaseType == ValueType.RESOURCEMODEL
450 && type.mDepth == 1) {
451 SimulatorResourceModel[] model = (SimulatorResourceModel[]) val
453 if (null != model && model.length > 0) {
457 } else if (element instanceof SRMItem) {
458 SRMItem srmItem = (SRMItem) element;
459 SimulatorResourceModel model = srmItem.getModel();
463 Map<String, SimulatorResourceAttribute> attributes = model
465 if (null != attributes && attributes.size() > 0) {
473 class AttributeLabelProvider implements ITableLabelProvider {
476 public void addListener(ILabelProviderListener arg0) {
480 public void dispose() {
484 public boolean isLabelProperty(Object arg0, String arg1) {
489 public void removeListener(ILabelProviderListener arg0) {
494 public Image getColumnImage(Object element, int col) {
496 if (element instanceof SimulatorResourceAttribute
497 || element instanceof LocalResourceAttribute) {
498 SimulatorResourceAttribute att;
499 if (element instanceof LocalResourceAttribute) {
500 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
501 att = localAtt.getResourceAttributeRef();
503 att = (SimulatorResourceAttribute) element;
505 AttributeValue val = att.value();
509 TypeInfo type = val.typeInfo();
510 if (type.mType == ValueType.RESOURCEMODEL
511 || type.mType == ValueType.ARRAY) {
514 if (element instanceof LocalResourceAttribute) {
515 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
516 System.out.println("No range or allowed values");
519 if (((LocalResourceAttribute) element)
520 .isAutomationInProgress()) {
521 return Activator.getDefault().getImageRegistry()
522 .get(Constants.CHECKED);
524 return Activator.getDefault().getImageRegistry()
525 .get(Constants.UNCHECKED);
534 public String getColumnText(Object element, int col) {
535 if (element instanceof SimulatorResourceAttribute
536 || element instanceof LocalResourceAttribute) {
537 SimulatorResourceAttribute att;
538 if (element instanceof LocalResourceAttribute) {
539 LocalResourceAttribute localAtt = (LocalResourceAttribute) element;
540 att = localAtt.getResourceAttributeRef();
542 att = (SimulatorResourceAttribute) element;
544 AttributeValue val = att.value();
548 TypeInfo type = val.typeInfo();
553 if (!(type.mType == ValueType.RESOURCEMODEL || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL))) {
554 String value = Utility
555 .getAttributeValueAsString(val);
564 Resource res = resourceManager
565 .getCurrentResourceInSelection();
566 if (null != res && res instanceof CollectionResource) {
570 if (type.mType == ValueType.RESOURCEMODEL
571 || type.mType == ValueType.ARRAY) {
574 if (element instanceof LocalResourceAttribute) {
576 .isAttHasRangeOrAllowedValues(att)) {
578 .println("No range or allowed values");
581 if (((LocalResourceAttribute) element)
582 .isAutomationInProgress()) {
583 return Constants.ENABLED;
585 return Constants.DISABLED;
589 } else if (element instanceof SRMItem) {
590 SRMItem item = (SRMItem) element;
593 return "[" + item.getIndex() + "]";
606 public void dispose() {
607 // Unregister the selection listener
608 if (null != resourceSelectionChangedListener) {
609 UiListenerHandler.getInstance()
610 .removeResourceSelectionChangedUIListener(
611 resourceSelectionChangedListener);
614 // Unregister the model change listener
615 if (null != resourceModelChangedUIListener) {
616 UiListenerHandler.getInstance()
617 .removeResourceModelChangedUIListener(
618 resourceModelChangedUIListener);
621 // Unregister the automation complete listener
622 if (null != automationUIListener) {
623 UiListenerHandler.getInstance().removeAutomationUIListener(
624 automationUIListener);
631 public void setFocus() {