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