Removed collection resource and device support from simulator plug-in.
[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) {
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                                 attViewer.refresh();
181
182                                 // Disabling the table to prevent interactions
183                                 // during the automation
184                                 tree.setEnabled(false);
185                                 tree.deselectAll();
186                             }
187                         }
188                     }
189                 });
190             }
191
192             @Override
193             public void onAutomationComplete(final SingleResource resource,
194                     final String attName) {
195                 // This method notifies the completion of attribute level
196                 // automation.
197                 Display.getDefault().asyncExec(new Runnable() {
198
199                     @Override
200                     public void run() {
201                         if (null == resource) {
202                             return;
203                         }
204                         // Check if the given resourceURI is the uri of the
205                         // resource whose attributes are currently being
206                         // displayed by this view.
207                         Resource resourceInSelection = resourceManager
208                                 .getCurrentResourceInSelection();
209                         if (null == resourceInSelection) {
210                             return;
211                         }
212                         if (resource != resourceInSelection) {
213                             return;
214                         }
215                         Tree tree;
216                         tree = attViewer.getTree();
217                         if (!tree.isDisposed()) {
218                             tree.setEnabled(true);
219                             attViewer.refresh();
220                         }
221                     }
222                 });
223             }
224         };
225     }
226
227     @Override
228     public void createPartControl(Composite parent) {
229         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
230
231         parent.setLayout(new GridLayout());
232         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
233         parent.setLayoutData(gd);
234
235         Group attGroup = new Group(parent, SWT.NONE);
236         attGroup.setLayout(new GridLayout());
237         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
238         attGroup.setLayoutData(gd);
239         attGroup.setText("Attributes");
240         attGroup.setBackground(color);
241
242         Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
243                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
244         addressTree.setHeaderVisible(true);
245
246         attViewer = new TreeViewer(addressTree);
247
248         createAttributeColumns(attViewer);
249
250         // make lines and header visible
251         Tree tree = attViewer.getTree();
252         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
253         tree.setHeaderVisible(true);
254         tree.setLinesVisible(true);
255
256         attViewer.setContentProvider(new AttributeContentProvider());
257         attViewer.setLabelProvider(new AttributeLabelProvider());
258
259         addManagerListeners();
260
261         // Check whether there is any resource selected already
262         Resource resource = resourceManager.getCurrentResourceInSelection();
263         if (resource != null) {
264             attViewer.setInput(resource.getResourceRepresentation());
265         }
266     }
267
268     public void createAttributeColumns(TreeViewer viewer) {
269         Tree tree = viewer.getTree();
270
271         attributeEditor = new AttributeEditingSupport();
272
273         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
274         attName.setWidth(attTblColWidth[0]);
275         attName.setText(attTblHeaders[0]);
276
277         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
278         attValue.setWidth(attTblColWidth[1]);
279         attValue.setText(attTblHeaders[1]);
280
281         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
282                 attValue);
283         attValueVwrCol.setEditingSupport(attributeEditor
284                 .createAttributeValueEditor(attViewer));
285
286         TreeColumn automation = new TreeColumn(tree, SWT.NONE);
287         automation.setWidth(attTblColWidth[2]);
288         automation.setText(attTblHeaders[2]);
289         TreeViewerColumn automationVwrCol = new TreeViewerColumn(attViewer,
290                 automation);
291         automationVwrCol.setEditingSupport(attributeEditor
292                 .createAutomationEditor(attViewer));
293
294         addColumnListeners();
295     }
296
297     private void addColumnListeners() {
298         TreeColumn[] columns = attViewer.getTree().getColumns();
299         for (TreeColumn column : columns) {
300             column.addSelectionListener(new SelectionAdapter() {
301                 @Override
302                 public void widgetSelected(SelectionEvent e) {
303                     // Refreshing the viewer. If combo list is open,
304                     // then click events on other parts of the view or outside
305                     // the combo should hide the editor.
306                     // Refreshing the viewer hides the combo and other editors
307                     // which are active.
308                     attViewer.refresh();
309                 }
310             });
311         }
312     }
313
314     private void addManagerListeners() {
315         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
316                 resourceSelectionChangedListener);
317         UiListenerHandler.getInstance().addDataChangeListener(
318                 dataChangeListener);
319         UiListenerHandler.getInstance().addAutomationUIListener(
320                 automationUIListener);
321     }
322
323     class AttributeContentProvider implements ITreeContentProvider {
324
325         @Override
326         public void dispose() {
327         }
328
329         @Override
330         public void inputChanged(Viewer viewer, Object oldAttribute,
331                 Object newAttribute) {
332         }
333
334         @Override
335         public Object[] getChildren(Object attribute) {
336             if (attribute instanceof AttributeElement) {
337                 return ((AttributeElement) attribute).getChildren().values()
338                         .toArray();
339             }
340
341             return new Object[0];
342         }
343
344         @Override
345         public Object getParent(Object attribute) {
346             if (attribute instanceof AttributeElement)
347                 return ((AttributeElement) attribute).getParent();
348             return null;
349         }
350
351         @Override
352         public boolean hasChildren(Object attribute) {
353             if (attribute instanceof AttributeElement)
354                 return ((AttributeElement) attribute).hasChildren();
355             return false;
356         }
357
358         @Override
359         public Object[] getElements(Object resourceModel) {
360             if (resourceModel instanceof ResourceRepresentation) {
361                 return ((ResourceRepresentation) resourceModel).getAttributes()
362                         .values().toArray();
363             }
364
365             return new Object[0];
366         }
367     }
368
369     class AttributeLabelProvider implements ITableLabelProvider {
370
371         @Override
372         public void addListener(ILabelProviderListener arg0) {
373         }
374
375         @Override
376         public void dispose() {
377         }
378
379         @Override
380         public boolean isLabelProperty(Object arg0, String arg1) {
381             return false;
382         }
383
384         @Override
385         public void removeListener(ILabelProviderListener arg0) {
386
387         }
388
389         @Override
390         public Image getColumnImage(Object element, int col) {
391             if (col == 2) {
392                 if (element instanceof AttributeElement) {
393                     // Ignore for non-single resource
394                     Resource res = resourceManager
395                             .getCurrentResourceInSelection();
396                     if (res instanceof SingleResource) {
397                         AttributeElement attrElement = (AttributeElement) element;
398                         if (attrElement.isAutoUpdateSupport()
399                                 && !attrElement.isReadOnly()) {
400                             if (attrElement.isAutoUpdateInProgress()) {
401                                 return Activator.getDefault()
402                                         .getImageRegistry()
403                                         .get(Constants.CHECKED);
404                             } else {
405                                 return Activator.getDefault()
406                                         .getImageRegistry()
407                                         .get(Constants.UNCHECKED);
408                             }
409                         }
410                     }
411                 }
412             }
413             return null;
414         }
415
416         @Override
417         public String getColumnText(Object element, int column) {
418             if (element instanceof AttributeElement) {
419                 AttributeElement attrElement = (AttributeElement) element;
420                 switch (column) {
421                     case 0: // Attribute name column
422                     {
423                         SimulatorResourceAttribute attribute = attrElement
424                                 .getSimulatorResourceAttribute();
425                         return attribute.name();
426                     }
427
428                     case 1: // Attribute value column
429                     {
430                         SimulatorResourceAttribute attribute = attrElement
431                                 .getSimulatorResourceAttribute();
432
433                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
434                             return Utility.getAttributeValueAsString(attribute
435                                     .value());
436                         return null;
437                     }
438
439                     case 2: {
440                         // Ignore for non-single resource
441                         Resource res = resourceManager
442                                 .getCurrentResourceInSelection();
443                         if (res instanceof SingleResource) {
444                             SimulatorResourceAttribute attribute = attrElement
445                                     .getSimulatorResourceAttribute();
446                             TypeInfo type = attribute.value().typeInfo();
447                             if (type.mType == AttributeValue.ValueType.ARRAY) {
448                                 if (type.mBaseType != AttributeValue.ValueType.RESOURCEMODEL) {
449                                     return "NA";
450                                 }
451                             } else if (type.mType != AttributeValue.ValueType.RESOURCEMODEL) {
452                                 Object parent = attrElement.getParent();
453                                 if (null != parent
454                                         && !(parent instanceof ResourceRepresentation)) {
455                                     return "NA";
456                                 } else if (attrElement.isReadOnly()) {
457                                     return "Read-only";
458                                 } else if (attrElement.isAutoUpdateSupport()) {
459                                     if (attrElement.isAutoUpdateInProgress())
460                                         return Constants.ENABLED;
461                                     else
462                                         return Constants.DISABLED;
463                                 }
464                             }
465                         }
466
467                         return "";
468                     }
469                 }
470             }
471
472             return null;
473         }
474
475     }
476
477     @Override
478     public void dispose() {
479         // Unregister the selection listener
480         if (null != resourceSelectionChangedListener) {
481             UiListenerHandler.getInstance()
482                     .removeResourceSelectionChangedUIListener(
483                             resourceSelectionChangedListener);
484         }
485
486         // Unregister the data model change listener
487         if (null != dataChangeListener) {
488             UiListenerHandler.getInstance().removeDataChangeListener(
489                     dataChangeListener);
490         }
491
492         // Unregister the automation complete listener
493         if (null != automationUIListener) {
494             UiListenerHandler.getInstance().removeAutomationUIListener(
495                     automationUIListener);
496         }
497
498         super.dispose();
499     }
500
501     @Override
502     public void setFocus() {
503
504     }
505 }