a4bb01e1a20d96fd2fa9aea8e5da6c1e95231643
[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.Resource;
27 import oic.simulator.serviceprovider.model.ResourceRepresentation;
28 import oic.simulator.serviceprovider.model.SingleResource;
29 import oic.simulator.serviceprovider.utils.Constants;
30 import oic.simulator.serviceprovider.utils.Utility;
31
32 import org.eclipse.jface.viewers.ILabelProviderListener;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.jface.viewers.TreeViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.graphics.Color;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.Group;
48 import org.eclipse.swt.widgets.Tree;
49 import org.eclipse.swt.widgets.TreeColumn;
50 import org.eclipse.ui.part.ViewPart;
51 import org.oic.simulator.AttributeValue;
52 import org.oic.simulator.AttributeValue.TypeInfo;
53 import org.oic.simulator.AttributeValue.ValueType;
54 import org.oic.simulator.SimulatorResourceAttribute;
55
56 /**
57  * This class manages and shows the attribute view in the perspective.
58  */
59 public class AttributeView extends ViewPart {
60
61     public static final String        VIEW_ID        = "oic.simulator.serviceprovider.view.attribute";
62
63     private TreeViewer                attViewer;
64
65     private AttributeEditingSupport   attributeEditor;
66
67     private ISelectionChangedListener resourceSelectionChangedListener;
68     private IAutomationListener       automationUIListener;
69     private IDataChangeListener       dataChangeListener;
70
71     private final String[]            attTblHeaders  = { "Name", "Value",
72             "Automation"                            };
73     private final Integer[]           attTblColWidth = { 150, 190, 150 };
74
75     private ResourceManager           resourceManager;
76
77     public AttributeView() {
78
79         resourceManager = Activator.getDefault().getResourceManager();
80
81         resourceSelectionChangedListener = new ISelectionChangedListener() {
82
83             @Override
84             public void onResourceSelectionChange(final Resource resource) {
85                 Display.getDefault().asyncExec(new Runnable() {
86                     @Override
87                     public void run() {
88                         if (null != attViewer) {
89                             Tree tree = attViewer.getTree();
90                             if (null == tree || tree.isDisposed()) {
91                                 return;
92                             }
93
94                             // Enabling/disabling the tree based on the resource
95                             // and automation status.
96                             if (resource instanceof SingleResource)
97                                 if (((SingleResource) resource)
98                                         .isResourceAutomationInProgress())
99                                     tree.setEnabled(false);
100                                 else
101                                     tree.setEnabled(true);
102
103                             if (null != resource
104                                     && null != resource
105                                             .getResourceRepresentation()) {
106                                 attViewer.setInput(resource
107                                         .getResourceRepresentation());
108                                 attViewer.expandAll();
109                                 tree.setLinesVisible(true);
110
111                             } else {
112                                 attViewer.setInput(null);
113                                 tree.setLinesVisible(false);
114                             }
115                         }
116                     }
117                 });
118             }
119         };
120
121         dataChangeListener = new IDataChangeListener() {
122
123             @Override
124             public void add(final AttributeElement attribute) {
125                 Display.getDefault().asyncExec(new Runnable() {
126                     @Override
127                     public void run() {
128                         attViewer.refresh(attribute.getParent());
129                         attViewer.expandAll();
130                     }
131                 });
132             }
133
134             @Override
135             public void remove(final AttributeElement attribute) {
136                 Display.getDefault().asyncExec(new Runnable() {
137                     @Override
138                     public void run() {
139                         attViewer.refresh(attribute.getParent());
140                         attViewer.expandAll();
141                     }
142                 });
143             }
144
145             @Override
146             public void update(final AttributeElement attribute) {
147                 Display.getDefault().asyncExec(new Runnable() {
148                     @Override
149                     public void run() {
150                         attViewer.update(attribute, null);
151                         attViewer.expandAll();
152                     }
153                 });
154             }
155         };
156
157         automationUIListener = new IAutomationListener() {
158
159             @Override
160             public void onResourceAutomationStart(final SingleResource resource) {
161                 Display.getDefault().asyncExec(new Runnable() {
162
163                     @Override
164                     public void run() {
165                         if (null == resource || null == attViewer) {
166                             return;
167                         }
168                         Resource resourceInSelection = resourceManager
169                                 .getCurrentResourceInSelection();
170                         if (null == resourceInSelection) {
171                             return;
172                         }
173                         // Checking whether attributes view is currently
174                         // displaying the attributes of the
175                         // resource whose automation has just started
176                         if (resource == resourceInSelection) {
177                             Tree tree;
178                             tree = attViewer.getTree();
179                             if (!tree.isDisposed()) {
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                             tree.setEnabled(true);
217                         }
218                     }
219                 });
220             }
221         };
222     }
223
224     @Override
225     public void createPartControl(Composite parent) {
226         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
227
228         parent.setLayout(new GridLayout());
229         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
230         parent.setLayoutData(gd);
231
232         Group attGroup = new Group(parent, SWT.NONE);
233         attGroup.setLayout(new GridLayout());
234         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
235         attGroup.setLayoutData(gd);
236         attGroup.setText("Attributes");
237         attGroup.setBackground(color);
238
239         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
240                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
241         addressTree.setHeaderVisible(true);
242
243         attViewer = new TreeViewer(addressTree);
244
245         createAttributeColumns(attViewer);
246
247         // make lines and header visible
248         Tree tree = attViewer.getTree();
249         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
250         tree.setHeaderVisible(true);
251         tree.setLinesVisible(true);
252
253         attViewer.setContentProvider(new AttributeContentProvider());
254         attViewer.setLabelProvider(new AttributeLabelProvider());
255
256         addManagerListeners();
257
258         // Check whether there is any resource selected already
259         Resource resource = resourceManager.getCurrentResourceInSelection();
260         if (resource != null) {
261             attViewer.setInput(resource.getResourceRepresentation());
262         }
263     }
264
265     public void createAttributeColumns(TreeViewer viewer) {
266         Tree tree = viewer.getTree();
267
268         attributeEditor = new AttributeEditingSupport();
269
270         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
271         attName.setWidth(attTblColWidth[0]);
272         attName.setText(attTblHeaders[0]);
273
274         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
275         attValue.setWidth(attTblColWidth[1]);
276         attValue.setText(attTblHeaders[1]);
277
278         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
279                 attValue);
280         attValueVwrCol.setEditingSupport(attributeEditor
281                 .createAttributeValueEditor(attViewer));
282
283         TreeColumn automation = new TreeColumn(tree, SWT.NONE);
284         automation.setWidth(attTblColWidth[2]);
285         automation.setText(attTblHeaders[2]);
286         TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
287                 automation);
288         automationVwrCol.setEditingSupport(attributeEditor
289                 .createAutomationEditor(attViewer));
290
291         addColumnListeners();
292     }
293
294     private void addColumnListeners() {
295         TreeColumn[] columns = attViewer.getTree().getColumns();
296         for (TreeColumn column : columns) {
297             column.addSelectionListener(new SelectionAdapter() {
298                 @Override
299                 public void widgetSelected(SelectionEvent e) {
300                     // Refreshing the viewer. If combo list is open,
301                     // then click events on other parts of the view or outside
302                     // the combo should hide the editor.
303                     // Refreshing the viewer hides the combo and other editors
304                     // which are active.
305                     attViewer.refresh();
306                 }
307             });
308         }
309     }
310
311     private void addManagerListeners() {
312         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
313                 resourceSelectionChangedListener);
314         UiListenerHandler.getInstance().addDataChangeListener(
315                 dataChangeListener);
316         UiListenerHandler.getInstance().addAutomationUIListener(
317                 automationUIListener);
318     }
319
320     class AttributeContentProvider implements ITreeContentProvider {
321
322         @Override
323         public void dispose() {
324         }
325
326         @Override
327         public void inputChanged(Viewer viewer, Object oldAttribute,
328                 Object newAttribute) {
329         }
330
331         @Override
332         public Object[] getChildren(Object attribute) {
333             if (attribute instanceof AttributeElement) {
334                 return ((AttributeElement) attribute).getChildren().values()
335                         .toArray();
336             }
337
338             return new Object[0];
339         }
340
341         @Override
342         public Object getParent(Object attribute) {
343             if (attribute instanceof AttributeElement)
344                 return ((AttributeElement) attribute).getParent();
345             return null;
346         }
347
348         @Override
349         public boolean hasChildren(Object attribute) {
350             if (attribute instanceof AttributeElement)
351                 return ((AttributeElement) attribute).hasChildren();
352             return false;
353         }
354
355         @Override
356         public Object[] getElements(Object resourceModel) {
357             if (resourceModel instanceof ResourceRepresentation) {
358                 return ((ResourceRepresentation) resourceModel).getAttributes()
359                         .values().toArray();
360             }
361
362             return new Object[0];
363         }
364     }
365
366     class AttributeLabelProvider implements ITableLabelProvider {
367
368         @Override
369         public void addListener(ILabelProviderListener arg0) {
370         }
371
372         @Override
373         public void dispose() {
374         }
375
376         @Override
377         public boolean isLabelProperty(Object arg0, String arg1) {
378             return false;
379         }
380
381         @Override
382         public void removeListener(ILabelProviderListener arg0) {
383
384         }
385
386         @Override
387         public Image getColumnImage(Object element, int col) {
388             if (col == 2) {
389                 if (element instanceof AttributeElement) {
390                     // Ignore for non-single resource
391                     Resource res = resourceManager
392                             .getCurrentResourceInSelection();
393                     if (res instanceof SingleResource) {
394                         AttributeElement attrElement = (AttributeElement) element;
395                         if (attrElement.isAutoUpdateSupport()
396                                 && !attrElement.isReadOnly()) {
397                             if (attrElement.isAutoUpdateInProgress()) {
398                                 return Activator.getDefault()
399                                         .getImageRegistry()
400                                         .get(Constants.CHECKED);
401                             } else {
402                                 return Activator.getDefault()
403                                         .getImageRegistry()
404                                         .get(Constants.UNCHECKED);
405                             }
406                         }
407                     }
408                 }
409             }
410             return null;
411         }
412
413         @Override
414         public String getColumnText(Object element, int column) {
415             if (element instanceof AttributeElement) {
416                 AttributeElement attrElement = (AttributeElement) element;
417                 switch (column) {
418                     case 0: // Attribute name column
419                     {
420                         SimulatorResourceAttribute attribute = attrElement
421                                 .getSimulatorResourceAttribute();
422                         return attribute.name();
423                     }
424
425                     case 1: // Attribute value column
426                     {
427                         SimulatorResourceAttribute attribute = attrElement
428                                 .getSimulatorResourceAttribute();
429
430                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
431                             return Utility.getAttributeValueAsString(attribute
432                                     .value());
433                         return null;
434                     }
435
436                     case 2: {
437                         // Ignore for non-single resource
438                         Resource res = resourceManager
439                                 .getCurrentResourceInSelection();
440                         if (res instanceof SingleResource) {
441                             SimulatorResourceAttribute attribute = attrElement
442                                     .getSimulatorResourceAttribute();
443                             TypeInfo type = attribute.value().typeInfo();
444                             if (type.mType == AttributeValue.ValueType.ARRAY) {
445                                 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
446                                     return "NA";
447                                 }
448                             } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
449                                 Object parent = attrElement.getParent();
450                                 if (null != parent
451                                         && !(parent instanceof ResourceRepresentation)) {
452                                     return "NA";
453                                 } else if (attrElement.isReadOnly()) {
454                                     return "NA";
455                                 } else if (attrElement.isAutoUpdateSupport()) {
456                                     if (attrElement.isAutoUpdateInProgress())
457                                         return Constants.ENABLED;
458                                     else
459                                         return Constants.DISABLED;
460                                 }
461                             }
462                         }
463
464                         return "";
465                     }
466                 }
467             }
468
469             return null;
470         }
471
472     }
473
474     @Override
475     public void dispose() {
476         // Unregister the selection listener
477         if (null != resourceSelectionChangedListener) {
478             UiListenerHandler.getInstance()
479                     .removeResourceSelectionChangedUIListener(
480                             resourceSelectionChangedListener);
481         }
482
483         // Unregister the data model change listener
484         if (null != dataChangeListener) {
485             UiListenerHandler.getInstance().removeDataChangeListener(
486                     dataChangeListener);
487         }
488
489         // Unregister the automation complete listener
490         if (null != automationUIListener) {
491             UiListenerHandler.getInstance().removeAutomationUIListener(
492                     automationUIListener);
493         }
494
495         super.dispose();
496     }
497
498     @Override
499     public void setFocus() {
500
501     }
502 }