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 || null == attViewer) {
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()) {
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 tree.setEnabled(true);
225 public void createPartControl(Composite parent) {
226 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
228 parent.setLayout(new GridLayout());
229 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
230 parent.setLayoutData(gd);
232 Group attGroup = new Group(parent, SWT.NONE);
233 attGroup.setLayout(new GridLayout());
234 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
235 attGroup.setLayoutData(gd);
236 attGroup.setText("Attributes");
237 attGroup.setBackground(color);
239 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
240 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
241 addressTree.setHeaderVisible(true);
243 attViewer = new TreeViewer(addressTree);
245 createAttributeColumns(attViewer);
247 // make lines and header visible
248 Tree tree = attViewer.getTree();
249 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
250 tree.setHeaderVisible(true);
251 tree.setLinesVisible(true);
253 attViewer.setContentProvider(new AttributeContentProvider());
254 attViewer.setLabelProvider(new AttributeLabelProvider());
256 addManagerListeners();
258 // Check whether there is any resource selected already
259 Resource resource = resourceManager.getCurrentResourceInSelection();
260 if (resource != null) {
261 attViewer.setInput(resource.getResourceRepresentation());
265 public void createAttributeColumns(TreeViewer viewer) {
266 Tree tree = viewer.getTree();
268 attributeEditor = new AttributeEditingSupport();
270 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
271 attName.setWidth(attTblColWidth[0]);
272 attName.setText(attTblHeaders[0]);
274 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
275 attValue.setWidth(attTblColWidth[1]);
276 attValue.setText(attTblHeaders[1]);
278 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
280 attValueVwrCol.setEditingSupport(attributeEditor
281 .createAttributeValueEditor(attViewer));
283 TreeColumn automation = new TreeColumn(tree, SWT.NONE);
284 automation.setWidth(attTblColWidth[2]);
285 automation.setText(attTblHeaders[2]);
286 TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
288 automationVwrCol.setEditingSupport(attributeEditor
289 .createAutomationEditor(attViewer));
291 addColumnListeners();
294 private void addColumnListeners() {
295 TreeColumn[] columns = attViewer.getTree().getColumns();
296 for (TreeColumn column : columns) {
297 column.addSelectionListener(new SelectionAdapter() {
299 public void widgetSelected(SelectionEvent e) {
300 // Refreshing the viewer. If combo list is open,
301 // then click events on other parts of the view or outside
302 // the combo should hide the editor.
303 // Refreshing the viewer hides the combo and other editors
311 private void addManagerListeners() {
312 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
313 resourceSelectionChangedListener);
314 UiListenerHandler.getInstance().addDataChangeListener(
316 UiListenerHandler.getInstance().addAutomationUIListener(
317 automationUIListener);
320 class AttributeContentProvider implements ITreeContentProvider {
323 public void dispose() {
327 public void inputChanged(Viewer viewer, Object oldAttribute,
328 Object newAttribute) {
332 public Object[] getChildren(Object attribute) {
333 if (attribute instanceof AttributeElement) {
334 return ((AttributeElement) attribute).getChildren().values()
338 return new Object[0];
342 public Object getParent(Object attribute) {
343 if (attribute instanceof AttributeElement)
344 return ((AttributeElement) attribute).getParent();
349 public boolean hasChildren(Object attribute) {
350 if (attribute instanceof AttributeElement)
351 return ((AttributeElement) attribute).hasChildren();
356 public Object[] getElements(Object resourceModel) {
357 if (resourceModel instanceof ResourceRepresentation) {
358 return ((ResourceRepresentation) resourceModel).getAttributes()
362 return new Object[0];
366 class AttributeLabelProvider implements ITableLabelProvider {
369 public void addListener(ILabelProviderListener arg0) {
373 public void dispose() {
377 public boolean isLabelProperty(Object arg0, String arg1) {
382 public void removeListener(ILabelProviderListener arg0) {
387 public Image getColumnImage(Object element, int col) {
389 if (element instanceof AttributeElement) {
390 // Ignore for non-single resource
391 Resource res = resourceManager
392 .getCurrentResourceInSelection();
393 if (res instanceof SingleResource) {
394 AttributeElement attrElement = (AttributeElement) element;
395 if (attrElement.isAutoUpdateSupport()
396 && !attrElement.isReadOnly()) {
397 if (attrElement.isAutoUpdateInProgress()) {
398 return Activator.getDefault()
400 .get(Constants.CHECKED);
402 return Activator.getDefault()
404 .get(Constants.UNCHECKED);
414 public String getColumnText(Object element, int column) {
415 if (element instanceof AttributeElement) {
416 AttributeElement attrElement = (AttributeElement) element;
418 case 0: // Attribute name column
420 SimulatorResourceAttribute attribute = attrElement
421 .getSimulatorResourceAttribute();
422 return attribute.name();
425 case 1: // Attribute value column
427 SimulatorResourceAttribute attribute = attrElement
428 .getSimulatorResourceAttribute();
430 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
431 return Utility.getAttributeValueAsString(attribute
437 // Ignore for non-single resource
438 Resource res = resourceManager
439 .getCurrentResourceInSelection();
440 if (res instanceof SingleResource) {
441 SimulatorResourceAttribute attribute = attrElement
442 .getSimulatorResourceAttribute();
443 TypeInfo type = attribute.value().typeInfo();
444 if (type.mType == AttributeValue.ValueType.ARRAY) {
445 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
448 } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
449 Object parent = attrElement.getParent();
451 && !(parent instanceof ResourceRepresentation)) {
453 } else if (attrElement.isReadOnly()) {
455 } else if (attrElement.isAutoUpdateSupport()) {
456 if (attrElement.isAutoUpdateInProgress())
457 return Constants.ENABLED;
459 return Constants.DISABLED;
475 public void dispose() {
476 // Unregister the selection listener
477 if (null != resourceSelectionChangedListener) {
478 UiListenerHandler.getInstance()
479 .removeResourceSelectionChangedUIListener(
480 resourceSelectionChangedListener);
483 // Unregister the data model change listener
484 if (null != dataChangeListener) {
485 UiListenerHandler.getInstance().removeDataChangeListener(
489 // Unregister the automation complete listener
490 if (null != automationUIListener) {
491 UiListenerHandler.getInstance().removeAutomationUIListener(
492 automationUIListener);
499 public void setFocus() {