f81bda6d73f0b79b553176445f8640a4fa7aa3a8
[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 import java.util.Map;
21
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;
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 import org.oic.simulator.SimulatorResourceModel;
59
60 /**
61  * This class manages and shows the attribute view in the perspective.
62  */
63 public class AttributeView extends ViewPart {
64
65     public static final String              VIEW_ID        = "oic.simulator.serviceprovider.view.attribute";
66
67     private TreeViewer                      attViewer;
68
69     private AttributeEditingSupport         attributeEditor;
70
71     private ISelectionChangedUIListener     resourceSelectionChangedListener;
72     private IResourceModelChangedUIListener resourceModelChangedUIListener;
73     private IAutomationUIListener           automationUIListener;
74
75     private final String[]                  attTblHeaders  = { "Name", "Value",
76             "Automation"                                  };
77     private final Integer[]                 attTblColWidth = { 150, 190, 150 };
78
79     private ResourceManager                 resourceManager;
80
81     public AttributeView() {
82
83         resourceManager = Activator.getDefault().getResourceManager();
84
85         resourceSelectionChangedListener = new ISelectionChangedUIListener() {
86
87             @Override
88             public void onResourceSelectionChange(final Resource resource) {
89                 Display.getDefault().asyncExec(new Runnable() {
90                     @Override
91                     public void run() {
92                         if (null != attViewer) {
93                             updateViewer(getData(resource));
94                             Tree tree = attViewer.getTree();
95                             if (!tree.isDisposed()) {
96                                 if (null != resource
97                                         && (resource instanceof SingleResource && ((SingleResource) resource)
98                                                 .isResourceAutomationInProgress())) {
99                                     tree.setEnabled(false);
100                                 } else {
101                                     tree.setEnabled(true);
102                                 }
103                             }
104                         }
105                     }
106                 });
107             }
108
109             @Override
110             public void onDeviceSelectionChange(Device dev) {
111                 Display.getDefault().asyncExec(new Runnable() {
112                     @Override
113                     public void run() {
114                         updateViewer(null);
115                     }
116                 });
117             }
118         };
119
120         resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
121
122             @Override
123             public void onResourceModelChange(final Resource resource) {
124                 Display.getDefault().asyncExec(new Runnable() {
125                     @Override
126                     public void run() {
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) {
132                             return;
133                         }
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
138                             // notification.
139                             return;
140                         }
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));
146                             } else {
147                                 updateViewer(getData(resource));
148                             }
149                         }
150                     }
151                 });
152             }
153         };
154
155         automationUIListener = new IAutomationUIListener() {
156
157             @Override
158             public void onResourceAutomationStart(final SingleResource resource) {
159                 Display.getDefault().asyncExec(new Runnable() {
160
161                     @Override
162                     public void run() {
163                         if (null == resource) {
164                             return;
165                         }
166                         Resource resourceInSelection = resourceManager
167                                 .getCurrentResourceInSelection();
168                         if (null == resourceInSelection) {
169                             return;
170                         }
171                         // Checking whether attributes view is currently
172                         // displaying the attributes of the
173                         // resource whose automation has just started
174                         if (resource == resourceInSelection) {
175                             Tree tree;
176                             tree = attViewer.getTree();
177                             if (!tree.isDisposed()) {
178                                 attViewer.refresh();
179
180                                 // Disabling the table to prevent interactions
181                                 // during the automation
182                                 tree.setEnabled(false);
183                                 tree.deselectAll();
184                             }
185                         }
186                     }
187                 });
188             }
189
190             @Override
191             public void onAutomationComplete(final SingleResource resource,
192                     final String attName) {
193                 // This method notifies the completion of attribute level
194                 // automation.
195                 Display.getDefault().asyncExec(new Runnable() {
196
197                     @Override
198                     public void run() {
199                         if (null == resource) {
200                             return;
201                         }
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) {
208                             return;
209                         }
210                         if (resource != resourceInSelection) {
211                             return;
212                         }
213                         Tree tree;
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,
220                                                 attName);
221                                 if (null == att) {
222                                     return;
223                                 } else {
224                                     attViewer.update(att, null);
225                                 }
226                             } else {
227                                 // Resource level automation has stopped
228                                 // Enabling the table which was disabled at the
229                                 // beginning of automation
230                                 tree.setEnabled(true);
231                                 attViewer.refresh();
232                             }
233                         }
234                     }
235                 });
236             }
237         };
238     }
239
240     @Override
241     public void createPartControl(Composite parent) {
242         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
243
244         parent.setLayout(new GridLayout());
245         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
246         parent.setLayoutData(gd);
247
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);
254
255         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
256                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
257         addressTree.setHeaderVisible(true);
258
259         attViewer = new TreeViewer(addressTree);
260
261         createAttributeColumns(attViewer);
262
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);
268
269         attViewer.setContentProvider(new AttributeContentProvider());
270         attViewer.setLabelProvider(new AttributeLabelProvider());
271
272         addManagerListeners();
273
274         // Check whether there is any resource selected already
275         List<LocalResourceAttribute> propertyList = getData(resourceManager
276                 .getCurrentResourceInSelection());
277         if (null != propertyList) {
278             updateViewer(propertyList);
279         }
280     }
281
282     public void createAttributeColumns(TreeViewer viewer) {
283         Tree tree = viewer.getTree();
284
285         attributeEditor = new AttributeEditingSupport();
286
287         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
288         attName.setWidth(attTblColWidth[0]);
289         attName.setText(attTblHeaders[0]);
290
291         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
292         attValue.setWidth(attTblColWidth[1]);
293         attValue.setText(attTblHeaders[1]);
294         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
295                 attValue);
296         attValueVwrCol.setEditingSupport(attributeEditor
297                 .createAttributeValueEditor(attViewer));
298
299         TreeColumn automation = new TreeColumn(tree, SWT.NONE);
300         automation.setWidth(attTblColWidth[2]);
301         automation.setText(attTblHeaders[2]);
302         TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
303                 automation);
304         automationVwrCol.setEditingSupport(attributeEditor
305                 .createAutomationEditor(attViewer));
306     }
307
308     private void addManagerListeners() {
309         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
310                 resourceSelectionChangedListener);
311         UiListenerHandler.getInstance().addResourceModelChangedUIListener(
312                 resourceModelChangedUIListener);
313         UiListenerHandler.getInstance().addAutomationUIListener(
314                 automationUIListener);
315     }
316
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();
323             return attList;
324         } else {
325             return null;
326         }
327     }
328
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());
335             }
336         } else {
337             // Clear the attributes table viewer
338             if (null != attViewer) {
339                 if (null != tree && !tree.isDisposed()) {
340                     // tbl.deselectAll();
341                     tree.removeAll();
342                     tree.setLinesVisible(false);
343                 }
344             }
345         }
346     }
347
348     class AttributeContentProvider implements ITreeContentProvider {
349
350         @Override
351         public void dispose() {
352         }
353
354         @Override
355         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
356         }
357
358         @Override
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();
366                     if (null == att) {
367                         return new Object[1];
368                     }
369                 } else {
370                     att = (SimulatorResourceAttribute) element;
371                 }
372                 AttributeValue val = att.value();
373                 if (null == val) {
374                     return new Object[1];
375                 }
376                 TypeInfo type = val.typeInfo();
377                 if (type.mType == ValueType.RESOURCEMODEL) {
378                     SimulatorResourceModel model = (SimulatorResourceModel) val
379                             .get();
380                     if (null == model) {
381                         return new Object[1];
382                     }
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
388                             .get();
389                     if (null == model || model.length < 1) {
390                         return new Object[1];
391                     }
392                     return resourceManager.getIndexedAttributes(model)
393                             .toArray();
394                 }
395             } else if (element instanceof SRMItem) {
396                 SRMItem item = (SRMItem) element;
397                 SimulatorResourceModel model = (SimulatorResourceModel) item
398                         .getModel();
399                 if (null == model) {
400                     return new Object[1];
401                 }
402                 return resourceManager.getAttributes(model).toArray();
403             }
404             return new Object[1];
405         }
406
407         @Override
408         public Object[] getElements(Object element) {
409             Object[] elements = (Object[]) element;
410             return elements;
411         }
412
413         @Override
414         public Object getParent(Object element) {
415             return null;
416         }
417
418         @Override
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();
426                     if (null == att) {
427                         return false;
428                     }
429                 } else {
430                     att = (SimulatorResourceAttribute) element;
431                 }
432                 AttributeValue val = att.value();
433                 if (null == val) {
434                     return false;
435                 }
436                 TypeInfo type = val.typeInfo();
437                 if (type.mType == ValueType.RESOURCEMODEL) {
438                     SimulatorResourceModel model = (SimulatorResourceModel) val
439                             .get();
440                     if (null == model) {
441                         return false;
442                     }
443                     Map<String, SimulatorResourceAttribute> attributes = model
444                             .getAttributes();
445                     if (null != attributes && attributes.size() > 0) {
446                         return true;
447                     }
448                 } else if (type.mType == ValueType.ARRAY
449                         && type.mBaseType == ValueType.RESOURCEMODEL
450                         && type.mDepth == 1) {
451                     SimulatorResourceModel[] model = (SimulatorResourceModel[]) val
452                             .get();
453                     if (null != model && model.length > 0) {
454                         return true;
455                     }
456                 }
457             } else if (element instanceof SRMItem) {
458                 SRMItem srmItem = (SRMItem) element;
459                 SimulatorResourceModel model = srmItem.getModel();
460                 if (null == model) {
461                     return false;
462                 }
463                 Map<String, SimulatorResourceAttribute> attributes = model
464                         .getAttributes();
465                 if (null != attributes && attributes.size() > 0) {
466                     return true;
467                 }
468             }
469             return false;
470         }
471     }
472
473     class AttributeLabelProvider implements ITableLabelProvider {
474
475         @Override
476         public void addListener(ILabelProviderListener arg0) {
477         }
478
479         @Override
480         public void dispose() {
481         }
482
483         @Override
484         public boolean isLabelProperty(Object arg0, String arg1) {
485             return false;
486         }
487
488         @Override
489         public void removeListener(ILabelProviderListener arg0) {
490
491         }
492
493         @Override
494         public Image getColumnImage(Object element, int col) {
495             if (col == 2) {
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();
502                     } else {
503                         att = (SimulatorResourceAttribute) element;
504                     }
505                     AttributeValue val = att.value();
506                     if (null == val) {
507                         return null;
508                     }
509                     TypeInfo type = val.typeInfo();
510                     if (type.mType == ValueType.RESOURCEMODEL
511                             || type.mType == ValueType.ARRAY) {
512                         return null;
513                     }
514                     if (element instanceof LocalResourceAttribute) {
515                         if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
516                             System.out.println("No range or allowed values");
517                             return null;
518                         }
519                         if (((LocalResourceAttribute) element)
520                                 .isAutomationInProgress()) {
521                             return Activator.getDefault().getImageRegistry()
522                                     .get(Constants.CHECKED);
523                         }
524                         return Activator.getDefault().getImageRegistry()
525                                 .get(Constants.UNCHECKED);
526                     }
527                     return null;
528                 }
529             }
530             return null;
531         }
532
533         @Override
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();
541                 } else {
542                     att = (SimulatorResourceAttribute) element;
543                 }
544                 AttributeValue val = att.value();
545                 if (null == val) {
546                     return "";
547                 }
548                 TypeInfo type = val.typeInfo();
549                 switch (col) {
550                     case 0:
551                         return att.name();
552                     case 1:
553                         if (!(type.mType == ValueType.RESOURCEMODEL || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL))) {
554                             String value = Utility
555                                     .getAttributeValueAsString(val);
556                             if (null == value) {
557                                 value = "";
558                             }
559                             return value;
560                         } else {
561                             return "";
562                         }
563                     case 2:
564                         Resource res = resourceManager
565                                 .getCurrentResourceInSelection();
566                         if (null != res && res instanceof CollectionResource) {
567                             return "-";
568                         }
569
570                         if (type.mType == ValueType.RESOURCEMODEL
571                                 || type.mType == ValueType.ARRAY) {
572                             return "";
573                         }
574                         if (element instanceof LocalResourceAttribute) {
575                             if (!resourceManager
576                                     .isAttHasRangeOrAllowedValues(att)) {
577                                 System.out
578                                         .println("No range or allowed values");
579                                 return "Read Only";
580                             }
581                             if (((LocalResourceAttribute) element)
582                                     .isAutomationInProgress()) {
583                                 return Constants.ENABLED;
584                             }
585                             return Constants.DISABLED;
586                         }
587                         return "NA";
588                 }
589             } else if (element instanceof SRMItem) {
590                 SRMItem item = (SRMItem) element;
591                 switch (col) {
592                     case 0:
593                         return "[" + item.getIndex() + "]";
594                     case 1:
595                         return "";
596                     case 2:
597                         return "";
598                 }
599             }
600             return null;
601         }
602
603     }
604
605     @Override
606     public void dispose() {
607         // Unregister the selection listener
608         if (null != resourceSelectionChangedListener) {
609             UiListenerHandler.getInstance()
610                     .removeResourceSelectionChangedUIListener(
611                             resourceSelectionChangedListener);
612         }
613
614         // Unregister the model change listener
615         if (null != resourceModelChangedUIListener) {
616             UiListenerHandler.getInstance()
617                     .removeResourceModelChangedUIListener(
618                             resourceModelChangedUIListener);
619         }
620
621         // Unregister the automation complete listener
622         if (null != automationUIListener) {
623             UiListenerHandler.getInstance().removeAutomationUIListener(
624                     automationUIListener);
625         }
626
627         super.dispose();
628     }
629
630     @Override
631     public void setFocus() {
632
633     }
634 }