Displaying and editing the complex value types for attributes.
[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 java.util.List;
20
21 import oic.simulator.serviceprovider.Activator;
22 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
23 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
24 import oic.simulator.serviceprovider.listener.ISelectionChangedUIListener;
25 import oic.simulator.serviceprovider.manager.ResourceManager;
26 import oic.simulator.serviceprovider.manager.UiListenerHandler;
27 import oic.simulator.serviceprovider.model.AttributeElement;
28 import oic.simulator.serviceprovider.model.DataChangeListener;
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.ResourceRepresentation;
33 import oic.simulator.serviceprovider.model.SingleResource;
34 import oic.simulator.serviceprovider.utils.Constants;
35 import oic.simulator.serviceprovider.utils.Utility;
36
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
59 /**
60  * This class manages and shows the attribute view in the perspective.
61  */
62 public class AttributeView extends ViewPart {
63
64     public static final String              VIEW_ID        = "oic.simulator.serviceprovider.view.attribute";
65
66     private TreeViewer                      attViewer;
67
68     private AttributeEditingSupport         attributeEditor;
69
70     private ISelectionChangedUIListener     resourceSelectionChangedListener;
71     private IResourceModelChangedUIListener resourceModelChangedUIListener;
72     private IAutomationUIListener           automationUIListener;
73
74     private final String[]                  attTblHeaders  = { "Name", "Value",
75             "Automation"                                  };
76     private final Integer[]                 attTblColWidth = { 150, 190, 150 };
77
78     private ResourceManager                 resourceManager;
79
80     public AttributeView() {
81
82         resourceManager = Activator.getDefault().getResourceManager();
83
84         resourceSelectionChangedListener = new ISelectionChangedUIListener() {
85
86             @Override
87             public void onResourceSelectionChange(final Resource resource) {
88                 Display.getDefault().asyncExec(new Runnable() {
89                     @Override
90                     public void run() {
91                         if (null != attViewer) {
92                             if (null != resource
93                                     && null != resource
94                                             .getResourceRepresentation())
95                                 attViewer.setInput(resource
96                                         .getResourceRepresentation());
97                             /*
98                              * updateViewer(getData(resource)); Tree tree =
99                              * attViewer.getTree(); if (!tree.isDisposed()) { if
100                              * (null != resource && (resource instanceof
101                              * SingleResource && ((SingleResource) resource)
102                              * .isResourceAutomationInProgress())) {
103                              * tree.setEnabled(false); } else {
104                              * tree.setEnabled(true); } }
105                              */
106                         }
107                     }
108                 });
109             }
110
111             @Override
112             public void onDeviceSelectionChange(Device dev) {
113                 Display.getDefault().asyncExec(new Runnable() {
114                     @Override
115                     public void run() {
116                         updateViewer(null);
117                     }
118                 });
119             }
120         };
121
122         resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
123
124             @Override
125             public void onResourceModelChange(final Resource resource) {
126                 Display.getDefault().asyncExec(new Runnable() {
127                     @Override
128                     public void run() {
129                         // Handle the notification only if it is for the current
130                         // resource in selection
131                         Resource resourceInSelection = resourceManager
132                                 .getCurrentResourceInSelection();
133                         if (null == resourceInSelection) {
134                             return;
135                         }
136                         /*
137                          * if (resource != resourceInSelection) { // This
138                          * notification is for a different resource // whose
139                          * attributes are not // currently not being shown in
140                          * UI. So ignoring this // notification. return; } //
141                          * Refresh the table viewers which will display // the
142                          * updated values if (null != attViewer) { if (resource
143                          * instanceof CollectionResource) {
144                          * updateViewer(getData(resource)); } else {
145                          * updateViewer(getData(resource)); } }
146                          */
147                     }
148                 });
149             }
150         };
151
152         automationUIListener = new IAutomationUIListener() {
153
154             @Override
155             public void onResourceAutomationStart(final SingleResource resource) {
156                 Display.getDefault().asyncExec(new Runnable() {
157
158                     @Override
159                     public void run() {
160                         if (null == resource) {
161                             return;
162                         }
163                         Resource resourceInSelection = resourceManager
164                                 .getCurrentResourceInSelection();
165                         if (null == resourceInSelection) {
166                             return;
167                         }
168                         // Checking whether attributes view is currently
169                         // displaying the attributes of the
170                         // resource whose automation has just started
171                         if (resource == resourceInSelection) {
172                             Tree tree;
173                             tree = attViewer.getTree();
174                             if (!tree.isDisposed()) {
175                                 attViewer.refresh();
176
177                                 // Disabling the table to prevent interactions
178                                 // during the automation
179                                 tree.setEnabled(false);
180                                 tree.deselectAll();
181                             }
182                         }
183                     }
184                 });
185             }
186
187             @Override
188             public void onAutomationComplete(final SingleResource resource,
189                     final String attName) {
190                 // This method notifies the completion of attribute level
191                 // automation.
192                 Display.getDefault().asyncExec(new Runnable() {
193
194                     @Override
195                     public void run() {
196                         if (null == resource) {
197                             return;
198                         }
199                         // Check if the given resourceURI is the uri of the
200                         // resource whose attributes are currently being
201                         // displayed by this view.
202                         Resource resourceInSelection = resourceManager
203                                 .getCurrentResourceInSelection();
204                         if (null == resourceInSelection) {
205                             return;
206                         }
207                         if (resource != resourceInSelection) {
208                             return;
209                         }
210                         Tree tree;
211                         tree = attViewer.getTree();
212                         if (!tree.isDisposed()) {
213                             if (null != attName) {
214                                 // Attribute level automation has stopped
215                                 LocalResourceAttribute att = resourceManager
216                                         .getAttributeByResourceURI(resource,
217                                                 attName);
218                                 if (null == att) {
219                                     return;
220                                 } else {
221                                     attViewer.update(att, null);
222                                 }
223                             } else {
224                                 // Resource level automation has stopped
225                                 // Enabling the table which was disabled at the
226                                 // beginning of automation
227                                 tree.setEnabled(true);
228                                 attViewer.refresh();
229                             }
230                         }
231                     }
232                 });
233             }
234         };
235     }
236
237     @Override
238     public void createPartControl(Composite parent) {
239         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
240
241         parent.setLayout(new GridLayout());
242         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
243         parent.setLayoutData(gd);
244
245         Group attGroup = new Group(parent, SWT.NONE);
246         attGroup.setLayout(new GridLayout());
247         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
248         attGroup.setLayoutData(gd);
249         attGroup.setText("Attributes");
250         attGroup.setBackground(color);
251
252         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
253                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
254         addressTree.setHeaderVisible(true);
255
256         attViewer = new TreeViewer(addressTree);
257
258         createAttributeColumns(attViewer);
259
260         // make lines and header visible
261         Tree tree = attViewer.getTree();
262         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
263         tree.setHeaderVisible(true);
264         tree.setLinesVisible(true);
265
266         attViewer.setContentProvider(new AttributeContentProvider());
267         attViewer.setLabelProvider(new AttributeLabelProvider());
268
269         addManagerListeners();
270
271         // Check whether there is any resource selected already
272         Resource resource = resourceManager.getCurrentResourceInSelection();
273         if (resource != null) {
274             attViewer.setInput(resource.getResourceRepresentation());
275         }
276     }
277
278     public void createAttributeColumns(TreeViewer viewer) {
279         Tree tree = viewer.getTree();
280
281         attributeEditor = new AttributeEditingSupport();
282
283         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
284         attName.setWidth(attTblColWidth[0]);
285         attName.setText(attTblHeaders[0]);
286
287         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
288         attValue.setWidth(attTblColWidth[1]);
289         attValue.setText(attTblHeaders[1]);
290         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
291                 attValue);
292         attValueVwrCol.setEditingSupport(attributeEditor
293                 .createAttributeValueEditor(attViewer));
294
295         TreeColumn automation = new TreeColumn(tree, SWT.NONE);
296         automation.setWidth(attTblColWidth[2]);
297         automation.setText(attTblHeaders[2]);
298         TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
299                 automation);
300         automationVwrCol.setEditingSupport(attributeEditor
301                 .createAutomationEditor(attViewer));
302     }
303
304     private void addManagerListeners() {
305         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
306                 resourceSelectionChangedListener);
307         UiListenerHandler.getInstance().addResourceModelChangedUIListener(
308                 resourceModelChangedUIListener);
309         UiListenerHandler.getInstance().addAutomationUIListener(
310                 automationUIListener);
311     }
312
313     private List<LocalResourceAttribute> getData(Resource resource) {
314         if (null != resource) {
315             List<LocalResourceAttribute> attList = resourceManager
316                     .getAttributes((Resource) resource);
317             // List<LocalResourceAttribute> attList =
318             // Utility.getDummyAttributes();
319             return attList;
320         } else {
321             return null;
322         }
323     }
324
325     private void updateViewer(List<LocalResourceAttribute> attList) {
326         Tree tree = attViewer.getTree();;
327         if (null != attList) {
328             if (null != tree && !tree.isDisposed()) {
329                 tree.setLinesVisible(true);
330                 attViewer.setInput(attList.toArray());
331             }
332         } else {
333             // Clear the attributes table viewer
334             if (null != attViewer) {
335                 if (null != tree && !tree.isDisposed()) {
336                     // tbl.deselectAll();
337                     tree.removeAll();
338                     tree.setLinesVisible(false);
339                 }
340             }
341         }
342     }
343
344     class AttributeContentProvider implements ITreeContentProvider,
345             DataChangeListener {
346
347         private TreeViewer mTreeViewer;
348
349         @Override
350         public void dispose() {
351         }
352
353         @Override
354         public void inputChanged(Viewer viewer, Object oldAttribute,
355                 Object newAttribute) {
356             mTreeViewer = (TreeViewer) viewer;
357         }
358
359         @Override
360         public Object[] getChildren(Object attribute) {
361             if (attribute instanceof AttributeElement) {
362                 return ((AttributeElement) attribute).getChildren().values()
363                         .toArray();
364             }
365
366             return new Object[0];
367         }
368
369         @Override
370         public Object getParent(Object attribute) {
371             if (attribute instanceof AttributeElement)
372                 return ((AttributeElement) attribute).getParent();
373             return null;
374         }
375
376         @Override
377         public boolean hasChildren(Object attribute) {
378             if (attribute instanceof AttributeElement)
379                 return ((AttributeElement) attribute).hasChildren();
380             return false;
381         }
382
383         @Override
384         public Object[] getElements(Object resourceModel) {
385             if (resourceModel instanceof ResourceRepresentation) {
386                 ((ResourceRepresentation) resourceModel).setListener(this);
387                 return ((ResourceRepresentation) resourceModel).getAttributes()
388                         .values().toArray();
389             }
390
391             return new Object[0];
392         }
393
394         @Override
395         public void add(final AttributeElement attribute) {
396             Display.getDefault().asyncExec(new Runnable() {
397                 @Override
398                 public void run() {
399                     mTreeViewer.refresh(attribute.getParent());
400                 }
401             });
402         }
403
404         @Override
405         public void remove(final AttributeElement attribute) {
406             Display.getDefault().asyncExec(new Runnable() {
407                 @Override
408                 public void run() {
409                     mTreeViewer.refresh(attribute.getParent());
410                 }
411             });
412         }
413
414         @Override
415         public void update(final AttributeElement attribute) {
416             Display.getDefault().asyncExec(new Runnable() {
417                 @Override
418                 public void run() {
419                     mTreeViewer.update(attribute, null);
420                 }
421             });
422         }
423     }
424
425     class AttributeLabelProvider implements ITableLabelProvider {
426
427         @Override
428         public void addListener(ILabelProviderListener arg0) {
429         }
430
431         @Override
432         public void dispose() {
433         }
434
435         @Override
436         public boolean isLabelProperty(Object arg0, String arg1) {
437             return false;
438         }
439
440         @Override
441         public void removeListener(ILabelProviderListener arg0) {
442
443         }
444
445         @Override
446         public Image getColumnImage(Object element, int col) {
447             if (col == 2) {
448                 if (element instanceof AttributeElement) {
449                     // Ignore for collection resource
450                     Resource res = resourceManager
451                             .getCurrentResourceInSelection();
452                     if (res instanceof SingleResource) {
453                         AttributeElement attrElement = (AttributeElement) element;
454                         SimulatorResourceAttribute attribute = attrElement
455                                 .getSimulatorResourceAttribute();
456                         TypeInfo type = attribute.value().typeInfo();
457                         if (attrElement.isAutoUpdateSupport()
458                                 && !attrElement.isReadOnly()) {
459                             if (attrElement.isAutoUpdateInProgress()) {
460                                 return Activator.getDefault()
461                                         .getImageRegistry()
462                                         .get(Constants.CHECKED);
463                             } else {
464                                 return Activator.getDefault()
465                                         .getImageRegistry()
466                                         .get(Constants.UNCHECKED);
467                             }
468                         }
469                     }
470                 }
471             }
472             return null;
473         }
474
475         @Override
476         public String getColumnText(Object element, int column) {
477             if (element instanceof AttributeElement) {
478                 AttributeElement attrElement = (AttributeElement) element;
479                 switch (column) {
480                     case 0: // Attribute name column
481                     {
482                         SimulatorResourceAttribute attribute = attrElement
483                                 .getSimulatorResourceAttribute();
484                         return attribute.name();
485                     }
486
487                     case 1: // Attribute value column
488                     {
489                         SimulatorResourceAttribute attribute = attrElement
490                                 .getSimulatorResourceAttribute();
491
492                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
493                             return Utility.getAttributeValueAsString(attribute
494                                     .value());
495                         return null;
496                     }
497
498                     case 2: {
499                         // Ignore for collection resource
500                         Resource res = resourceManager
501                                 .getCurrentResourceInSelection();
502                         if (res instanceof SingleResource) {
503                             SimulatorResourceAttribute attribute = attrElement
504                                     .getSimulatorResourceAttribute();
505                             TypeInfo type = attribute.value().typeInfo();
506                             if (type.mType == AttributeValue.ValueType.ARRAY) {
507                                 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
508                                     return "NA";
509                                 }
510                             } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
511                                 Object parent = attrElement.getParent();
512                                 if (null != parent
513                                         && !(parent instanceof ResourceRepresentation)) {
514                                     return "NA";
515                                 } else if (attrElement.isReadOnly()) {
516                                     return "Read-only";
517                                 } else if (attrElement.isAutoUpdateSupport()) {
518                                     if (attrElement.isAutoUpdateInProgress())
519                                         return Constants.ENABLED;
520                                     else
521                                         return Constants.DISABLED;
522                                 }
523                             }
524                         }
525
526                         return "";
527                     }
528                 }
529             }
530
531             return null;
532         }
533
534     }
535
536     @Override
537     public void dispose() {
538         // Unregister the selection listener
539         if (null != resourceSelectionChangedListener) {
540             UiListenerHandler.getInstance()
541                     .removeResourceSelectionChangedUIListener(
542                             resourceSelectionChangedListener);
543         }
544
545         // Unregister the model change listener
546         if (null != resourceModelChangedUIListener) {
547             UiListenerHandler.getInstance()
548                     .removeResourceModelChangedUIListener(
549                             resourceModelChangedUIListener);
550         }
551
552         // Unregister the automation complete listener
553         if (null != automationUIListener) {
554             UiListenerHandler.getInstance().removeAutomationUIListener(
555                     automationUIListener);
556         }
557
558         super.dispose();
559     }
560
561     @Override
562     public void setFocus() {
563
564     }
565 }