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.Resource;
27 import oic.simulator.serviceprovider.model.ResourceRepresentation;
28 import oic.simulator.serviceprovider.model.SingleResource;
29 import oic.simulator.serviceprovider.utils.Constants;
30 import oic.simulator.serviceprovider.utils.Utility;
32 import org.eclipse.jface.viewers.ILabelProviderListener;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.jface.viewers.TreeViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.graphics.Color;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.Group;
48 import org.eclipse.swt.widgets.Tree;
49 import org.eclipse.swt.widgets.TreeColumn;
50 import org.eclipse.ui.part.ViewPart;
51 import org.oic.simulator.AttributeValue;
52 import org.oic.simulator.AttributeValue.TypeInfo;
53 import org.oic.simulator.AttributeValue.ValueType;
54 import org.oic.simulator.SimulatorResourceAttribute;
57 * This class manages and shows the attribute view in the perspective.
59 public class AttributeView extends ViewPart {
61 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
63 private TreeViewer attViewer;
65 private AttributeEditingSupport attributeEditor;
67 private ISelectionChangedListener resourceSelectionChangedListener;
68 private IAutomationListener automationUIListener;
69 private IDataChangeListener dataChangeListener;
71 private final String[] attTblHeaders = { "Name", "Value",
73 private final Integer[] attTblColWidth = { 150, 190, 150 };
75 private ResourceManager resourceManager;
77 public AttributeView() {
79 resourceManager = Activator.getDefault().getResourceManager();
81 resourceSelectionChangedListener = new ISelectionChangedListener() {
84 public void onResourceSelectionChange(final Resource resource) {
85 Display.getDefault().asyncExec(new Runnable() {
88 if (null != attViewer) {
89 Tree tree = attViewer.getTree();
90 if (null == tree || tree.isDisposed()) {
94 // Enabling/disabling the tree based on the resource
95 // and automation status.
96 if (resource instanceof SingleResource)
97 if (((SingleResource) resource)
98 .isResourceAutomationInProgress())
99 tree.setEnabled(false);
101 tree.setEnabled(true);
105 .getResourceRepresentation()) {
106 attViewer.setInput(resource
107 .getResourceRepresentation());
108 attViewer.expandAll();
109 tree.setLinesVisible(true);
112 attViewer.setInput(null);
113 tree.setLinesVisible(false);
121 dataChangeListener = new IDataChangeListener() {
124 public void add(final AttributeElement attribute) {
125 Display.getDefault().asyncExec(new Runnable() {
128 attViewer.refresh(attribute.getParent());
129 attViewer.expandAll();
135 public void remove(final AttributeElement attribute) {
136 Display.getDefault().asyncExec(new Runnable() {
139 attViewer.refresh(attribute.getParent());
140 attViewer.expandAll();
146 public void update(final AttributeElement attribute) {
147 Display.getDefault().asyncExec(new Runnable() {
150 attViewer.update(attribute, null);
151 attViewer.expandAll();
157 automationUIListener = new IAutomationListener() {
160 public void onResourceAutomationStart(final SingleResource resource) {
161 Display.getDefault().asyncExec(new Runnable() {
165 if (null == resource) {
168 Resource resourceInSelection = resourceManager
169 .getCurrentResourceInSelection();
170 if (null == resourceInSelection) {
173 // Checking whether attributes view is currently
174 // displaying the attributes of the
175 // resource whose automation has just started
176 if (resource == resourceInSelection) {
178 tree = attViewer.getTree();
179 if (!tree.isDisposed()) {
182 // Disabling the table to prevent interactions
183 // during the automation
184 tree.setEnabled(false);
193 public void onAutomationComplete(final SingleResource resource,
194 final String attName) {
195 // This method notifies the completion of attribute level
197 Display.getDefault().asyncExec(new Runnable() {
201 if (null == resource) {
204 // Check if the given resourceURI is the uri of the
205 // resource whose attributes are currently being
206 // displayed by this view.
207 Resource resourceInSelection = resourceManager
208 .getCurrentResourceInSelection();
209 if (null == resourceInSelection) {
212 if (resource != resourceInSelection) {
216 tree = attViewer.getTree();
217 if (!tree.isDisposed()) {
218 tree.setEnabled(true);
228 public void createPartControl(Composite parent) {
229 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
231 parent.setLayout(new GridLayout());
232 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
233 parent.setLayoutData(gd);
235 Group attGroup = new Group(parent, SWT.NONE);
236 attGroup.setLayout(new GridLayout());
237 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
238 attGroup.setLayoutData(gd);
239 attGroup.setText("Attributes");
240 attGroup.setBackground(color);
242 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
243 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
244 addressTree.setHeaderVisible(true);
246 attViewer = new TreeViewer(addressTree);
248 createAttributeColumns(attViewer);
250 // make lines and header visible
251 Tree tree = attViewer.getTree();
252 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
253 tree.setHeaderVisible(true);
254 tree.setLinesVisible(true);
256 attViewer.setContentProvider(new AttributeContentProvider());
257 attViewer.setLabelProvider(new AttributeLabelProvider());
259 addManagerListeners();
261 // Check whether there is any resource selected already
262 Resource resource = resourceManager.getCurrentResourceInSelection();
263 if (resource != null) {
264 attViewer.setInput(resource.getResourceRepresentation());
268 public void createAttributeColumns(TreeViewer viewer) {
269 Tree tree = viewer.getTree();
271 attributeEditor = new AttributeEditingSupport();
273 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
274 attName.setWidth(attTblColWidth[0]);
275 attName.setText(attTblHeaders[0]);
277 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
278 attValue.setWidth(attTblColWidth[1]);
279 attValue.setText(attTblHeaders[1]);
281 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
283 attValueVwrCol.setEditingSupport(attributeEditor
284 .createAttributeValueEditor(attViewer));
286 TreeColumn automation = new TreeColumn(tree, SWT.NONE);
287 automation.setWidth(attTblColWidth[2]);
288 automation.setText(attTblHeaders[2]);
289 TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
291 automationVwrCol.setEditingSupport(attributeEditor
292 .createAutomationEditor(attViewer));
294 addColumnListeners();
297 private void addColumnListeners() {
298 TreeColumn[] columns = attViewer.getTree().getColumns();
299 for (TreeColumn column : columns) {
300 column.addSelectionListener(new SelectionAdapter() {
302 public void widgetSelected(SelectionEvent e) {
303 // Refreshing the viewer. If combo list is open,
304 // then click events on other parts of the view or outside
305 // the combo should hide the editor.
306 // Refreshing the viewer hides the combo and other editors
314 private void addManagerListeners() {
315 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
316 resourceSelectionChangedListener);
317 UiListenerHandler.getInstance().addDataChangeListener(
319 UiListenerHandler.getInstance().addAutomationUIListener(
320 automationUIListener);
323 class AttributeContentProvider implements ITreeContentProvider {
326 public void dispose() {
330 public void inputChanged(Viewer viewer, Object oldAttribute,
331 Object newAttribute) {
335 public Object[] getChildren(Object attribute) {
336 if (attribute instanceof AttributeElement) {
337 return ((AttributeElement) attribute).getChildren().values()
341 return new Object[0];
345 public Object getParent(Object attribute) {
346 if (attribute instanceof AttributeElement)
347 return ((AttributeElement) attribute).getParent();
352 public boolean hasChildren(Object attribute) {
353 if (attribute instanceof AttributeElement)
354 return ((AttributeElement) attribute).hasChildren();
359 public Object[] getElements(Object resourceModel) {
360 if (resourceModel instanceof ResourceRepresentation) {
361 return ((ResourceRepresentation) resourceModel).getAttributes()
365 return new Object[0];
369 class AttributeLabelProvider implements ITableLabelProvider {
372 public void addListener(ILabelProviderListener arg0) {
376 public void dispose() {
380 public boolean isLabelProperty(Object arg0, String arg1) {
385 public void removeListener(ILabelProviderListener arg0) {
390 public Image getColumnImage(Object element, int col) {
392 if (element instanceof AttributeElement) {
393 // Ignore for non-single resource
394 Resource res = resourceManager
395 .getCurrentResourceInSelection();
396 if (res instanceof SingleResource) {
397 AttributeElement attrElement = (AttributeElement) element;
398 if (attrElement.isAutoUpdateSupport()
399 && !attrElement.isReadOnly()) {
400 if (attrElement.isAutoUpdateInProgress()) {
401 return Activator.getDefault()
403 .get(Constants.CHECKED);
405 return Activator.getDefault()
407 .get(Constants.UNCHECKED);
417 public String getColumnText(Object element, int column) {
418 if (element instanceof AttributeElement) {
419 AttributeElement attrElement = (AttributeElement) element;
421 case 0: // Attribute name column
423 SimulatorResourceAttribute attribute = attrElement
424 .getSimulatorResourceAttribute();
425 return attribute.name();
428 case 1: // Attribute value column
430 SimulatorResourceAttribute attribute = attrElement
431 .getSimulatorResourceAttribute();
433 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
434 return Utility.getAttributeValueAsString(attribute
440 // Ignore for non-single resource
441 Resource res = resourceManager
442 .getCurrentResourceInSelection();
443 if (res instanceof SingleResource) {
444 SimulatorResourceAttribute attribute = attrElement
445 .getSimulatorResourceAttribute();
446 TypeInfo type = attribute.value().typeInfo();
447 if (type.mType == AttributeValue.ValueType.ARRAY) {
448 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
451 } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
452 Object parent = attrElement.getParent();
454 && !(parent instanceof ResourceRepresentation)) {
456 } else if (attrElement.isReadOnly()) {
458 } else if (attrElement.isAutoUpdateSupport()) {
459 if (attrElement.isAutoUpdateInProgress())
460 return Constants.ENABLED;
462 return Constants.DISABLED;
478 public void dispose() {
479 // Unregister the selection listener
480 if (null != resourceSelectionChangedListener) {
481 UiListenerHandler.getInstance()
482 .removeResourceSelectionChangedUIListener(
483 resourceSelectionChangedListener);
486 // Unregister the data model change listener
487 if (null != dataChangeListener) {
488 UiListenerHandler.getInstance().removeDataChangeListener(
492 // Unregister the automation complete listener
493 if (null != automationUIListener) {
494 UiListenerHandler.getInstance().removeAutomationUIListener(
495 automationUIListener);
502 public void setFocus() {