IoTivity Simulator System testing bug fixes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / AttributeView.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.view;
18
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;
32
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;
54
55 /**
56  * This class manages and shows the attribute view in the perspective.
57  */
58 public class AttributeView extends ViewPart {
59
60     public static final String        VIEW_ID        = "oic.simulator.serviceprovider.view.attribute";
61
62     private TreeViewer                attViewer;
63
64     private AttributeEditingSupport   attributeEditor;
65
66     private ISelectionChangedListener resourceSelectionChangedListener;
67     private IAutomationListener       automationUIListener;
68     private IDataChangeListener       dataChangeListener;
69
70     private final String[]            attTblHeaders  = { "Name", "Value",
71             "Automation"                            };
72     private final Integer[]           attTblColWidth = { 150, 190, 150 };
73
74     private ResourceManager           resourceManager;
75
76     public AttributeView() {
77
78         resourceManager = Activator.getDefault().getResourceManager();
79
80         resourceSelectionChangedListener = new ISelectionChangedListener() {
81
82             @Override
83             public void onResourceSelectionChange(final Resource resource) {
84                 Display.getDefault().asyncExec(new Runnable() {
85                     @Override
86                     public void run() {
87                         if (null != attViewer) {
88                             Tree tree = attViewer.getTree();
89                             if (null == tree || tree.isDisposed()) {
90                                 return;
91                             }
92
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);
99                                 else
100                                     tree.setEnabled(true);
101
102                             if (null != resource
103                                     && null != resource
104                                             .getResourceRepresentation()) {
105                                 attViewer.setInput(resource
106                                         .getResourceRepresentation());
107                                 attViewer.expandAll();
108                                 tree.setLinesVisible(true);
109
110                             } else {
111                                 attViewer.setInput(null);
112                                 tree.setLinesVisible(false);
113                             }
114                         }
115                     }
116                 });
117             }
118
119             @Override
120             public void onDeviceSelectionChange(Device dev) {
121                 Display.getDefault().asyncExec(new Runnable() {
122                     @Override
123                     public void run() {
124                         if (null != attViewer) {
125                             Tree tree = attViewer.getTree();
126                             if (null != tree && !tree.isDisposed()) {
127                                 // tbl.deselectAll();
128                                 tree.removeAll();
129                                 tree.setLinesVisible(false);
130                             }
131                         }
132                     }
133                 });
134             }
135         };
136
137         dataChangeListener = new IDataChangeListener() {
138
139             @Override
140             public void add(final AttributeElement attribute) {
141                 Display.getDefault().asyncExec(new Runnable() {
142                     @Override
143                     public void run() {
144                         attViewer.refresh(attribute.getParent());
145                         attViewer.expandAll();
146                     }
147                 });
148             }
149
150             @Override
151             public void remove(final AttributeElement attribute) {
152                 Display.getDefault().asyncExec(new Runnable() {
153                     @Override
154                     public void run() {
155                         attViewer.refresh(attribute.getParent());
156                         attViewer.expandAll();
157                     }
158                 });
159             }
160
161             @Override
162             public void update(final AttributeElement attribute) {
163                 Display.getDefault().asyncExec(new Runnable() {
164                     @Override
165                     public void run() {
166                         attViewer.update(attribute, null);
167                         attViewer.expandAll();
168                     }
169                 });
170             }
171         };
172
173         automationUIListener = new IAutomationListener() {
174
175             @Override
176             public void onResourceAutomationStart(final SingleResource resource) {
177                 Display.getDefault().asyncExec(new Runnable() {
178
179                     @Override
180                     public void run() {
181                         if (null == resource) {
182                             return;
183                         }
184                         Resource resourceInSelection = resourceManager
185                                 .getCurrentResourceInSelection();
186                         if (null == resourceInSelection) {
187                             return;
188                         }
189                         // Checking whether attributes view is currently
190                         // displaying the attributes of the
191                         // resource whose automation has just started
192                         if (resource == resourceInSelection) {
193                             Tree tree;
194                             tree = attViewer.getTree();
195                             if (!tree.isDisposed()) {
196                                 attViewer.refresh();
197
198                                 // Disabling the table to prevent interactions
199                                 // during the automation
200                                 tree.setEnabled(false);
201                                 tree.deselectAll();
202                             }
203                         }
204                     }
205                 });
206             }
207
208             @Override
209             public void onAutomationComplete(final SingleResource resource,
210                     final String attName) {
211                 // This method notifies the completion of attribute level
212                 // automation.
213                 Display.getDefault().asyncExec(new Runnable() {
214
215                     @Override
216                     public void run() {
217                         if (null == resource) {
218                             return;
219                         }
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) {
226                             return;
227                         }
228                         if (resource != resourceInSelection) {
229                             return;
230                         }
231                         Tree tree;
232                         tree = attViewer.getTree();
233                         if (!tree.isDisposed()) {
234                             tree.setEnabled(true);
235                             attViewer.refresh();
236                         }
237                     }
238                 });
239             }
240         };
241     }
242
243     @Override
244     public void createPartControl(Composite parent) {
245         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
246
247         parent.setLayout(new GridLayout());
248         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
249         parent.setLayoutData(gd);
250
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);
257
258         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
259                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
260         addressTree.setHeaderVisible(true);
261
262         attViewer = new TreeViewer(addressTree);
263
264         createAttributeColumns(attViewer);
265
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);
271
272         attViewer.setContentProvider(new AttributeContentProvider());
273         attViewer.setLabelProvider(new AttributeLabelProvider());
274
275         addManagerListeners();
276
277         // Check whether there is any resource selected already
278         Resource resource = resourceManager.getCurrentResourceInSelection();
279         if (resource != null) {
280             attViewer.setInput(resource.getResourceRepresentation());
281         }
282     }
283
284     public void createAttributeColumns(TreeViewer viewer) {
285         Tree tree = viewer.getTree();
286
287         attributeEditor = new AttributeEditingSupport();
288
289         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
290         attName.setWidth(attTblColWidth[0]);
291         attName.setText(attTblHeaders[0]);
292
293         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
294         attValue.setWidth(attTblColWidth[1]);
295         attValue.setText(attTblHeaders[1]);
296         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
297                 attValue);
298         attValueVwrCol.setEditingSupport(attributeEditor
299                 .createAttributeValueEditor(attViewer));
300
301         TreeColumn automation = new TreeColumn(tree, SWT.NONE);
302         automation.setWidth(attTblColWidth[2]);
303         automation.setText(attTblHeaders[2]);
304         TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
305                 automation);
306         automationVwrCol.setEditingSupport(attributeEditor
307                 .createAutomationEditor(attViewer));
308     }
309
310     private void addManagerListeners() {
311         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
312                 resourceSelectionChangedListener);
313         UiListenerHandler.getInstance().addDataChangeListener(
314                 dataChangeListener);
315         UiListenerHandler.getInstance().addAutomationUIListener(
316                 automationUIListener);
317     }
318
319     class AttributeContentProvider implements ITreeContentProvider {
320
321         @Override
322         public void dispose() {
323         }
324
325         @Override
326         public void inputChanged(Viewer viewer, Object oldAttribute,
327                 Object newAttribute) {
328         }
329
330         @Override
331         public Object[] getChildren(Object attribute) {
332             if (attribute instanceof AttributeElement) {
333                 return ((AttributeElement) attribute).getChildren().values()
334                         .toArray();
335             }
336
337             return new Object[0];
338         }
339
340         @Override
341         public Object getParent(Object attribute) {
342             if (attribute instanceof AttributeElement)
343                 return ((AttributeElement) attribute).getParent();
344             return null;
345         }
346
347         @Override
348         public boolean hasChildren(Object attribute) {
349             if (attribute instanceof AttributeElement)
350                 return ((AttributeElement) attribute).hasChildren();
351             return false;
352         }
353
354         @Override
355         public Object[] getElements(Object resourceModel) {
356             if (resourceModel instanceof ResourceRepresentation) {
357                 return ((ResourceRepresentation) resourceModel).getAttributes()
358                         .values().toArray();
359             }
360
361             return new Object[0];
362         }
363     }
364
365     class AttributeLabelProvider implements ITableLabelProvider {
366
367         @Override
368         public void addListener(ILabelProviderListener arg0) {
369         }
370
371         @Override
372         public void dispose() {
373         }
374
375         @Override
376         public boolean isLabelProperty(Object arg0, String arg1) {
377             return false;
378         }
379
380         @Override
381         public void removeListener(ILabelProviderListener arg0) {
382
383         }
384
385         @Override
386         public Image getColumnImage(Object element, int col) {
387             if (col == 2) {
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()
398                                         .getImageRegistry()
399                                         .get(Constants.CHECKED);
400                             } else {
401                                 return Activator.getDefault()
402                                         .getImageRegistry()
403                                         .get(Constants.UNCHECKED);
404                             }
405                         }
406                     }
407                 }
408             }
409             return null;
410         }
411
412         @Override
413         public String getColumnText(Object element, int column) {
414             if (element instanceof AttributeElement) {
415                 AttributeElement attrElement = (AttributeElement) element;
416                 switch (column) {
417                     case 0: // Attribute name column
418                     {
419                         SimulatorResourceAttribute attribute = attrElement
420                                 .getSimulatorResourceAttribute();
421                         return attribute.name();
422                     }
423
424                     case 1: // Attribute value column
425                     {
426                         SimulatorResourceAttribute attribute = attrElement
427                                 .getSimulatorResourceAttribute();
428
429                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
430                             return Utility.getAttributeValueAsString(attribute
431                                     .value());
432                         return null;
433                     }
434
435                     case 2: {
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) {
445                                     return "NA";
446                                 }
447                             } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
448                                 Object parent = attrElement.getParent();
449                                 if (null != parent
450                                         && !(parent instanceof ResourceRepresentation)) {
451                                     return "NA";
452                                 } else if (attrElement.isReadOnly()) {
453                                     return "Read-only";
454                                 } else if (attrElement.isAutoUpdateSupport()) {
455                                     if (attrElement.isAutoUpdateInProgress())
456                                         return Constants.ENABLED;
457                                     else
458                                         return Constants.DISABLED;
459                                 }
460                             }
461                         }
462
463                         return "";
464                     }
465                 }
466             }
467
468             return null;
469         }
470
471     }
472
473     @Override
474     public void dispose() {
475         // Unregister the selection listener
476         if (null != resourceSelectionChangedListener) {
477             UiListenerHandler.getInstance()
478                     .removeResourceSelectionChangedUIListener(
479                             resourceSelectionChangedListener);
480         }
481
482         // Unregister the data model change listener
483         if (null != dataChangeListener) {
484             UiListenerHandler.getInstance().removeDataChangeListener(
485                     dataChangeListener);
486         }
487
488         // Unregister the automation complete listener
489         if (null != automationUIListener) {
490             UiListenerHandler.getInstance().removeAutomationUIListener(
491                     automationUIListener);
492         }
493
494         super.dispose();
495     }
496
497     @Override
498     public void setFocus() {
499
500     }
501 }