Simulator Service Provider plug-in changes:
[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.Set;
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.IResourceSelectionChangedUIListener;
26 import oic.simulator.serviceprovider.manager.ResourceManager;
27 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
28 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
29 import oic.simulator.serviceprovider.resource.SimulatorResource;
30 import oic.simulator.serviceprovider.utils.Constants;
31
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.IStructuredContentProvider;
34 import org.eclipse.jface.viewers.StyledCellLabelProvider;
35 import org.eclipse.jface.viewers.TableViewer;
36 import org.eclipse.jface.viewers.TableViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerCell;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.graphics.Color;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.ui.part.ViewPart;
49
50 /**
51  * This class manages and shows the attribute view in the perspective.
52  */
53 public class AttributeView extends ViewPart {
54
55     public static final String                  VIEW_ID        = "oic.simulator.serviceprovider.view.attribute";
56
57     private TableViewer                         attTblViewer;
58
59     private AttributeEditingSupport             attributeEditor;
60
61     private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
62     private IResourceModelChangedUIListener     resourceModelChangedUIListener;
63     private IAutomationUIListener               automationUIListener;
64
65     private final String[]                      attTblHeaders  = { "Name",
66             "Value", "Automation"                             };
67     private final Integer[]                     attTblColWidth = { 150, 190,
68             150                                               };
69
70     private ResourceManager                     resourceManager;
71
72     public AttributeView() {
73
74         resourceManager = Activator.getDefault().getResourceManager();
75
76         resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
77
78             @Override
79             public void onResourceSelectionChange() {
80                 Display.getDefault().asyncExec(new Runnable() {
81
82                     @Override
83                     public void run() {
84                         if (null != attTblViewer) {
85                             updateViewer(getData());
86                             SimulatorResource resource = resourceManager
87                                     .getCurrentResourceInSelection();
88                             Table tbl = attTblViewer.getTable();
89                             if (!tbl.isDisposed()) {
90                                 if (null != resource
91                                         && resource
92                                                 .isResourceAutomationInProgress()) {
93                                     tbl.setEnabled(false);
94                                 } else {
95                                     tbl.setEnabled(true);
96                                 }
97                             }
98                         }
99                     }
100                 });
101             }
102         };
103
104         resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
105
106             @Override
107             public void onResourceModelChange(
108                     final ModelChangeNotificationType notificationType,
109                     final String resourceURI,
110                     final Set<LocalResourceAttribute> valueChangeSet) {
111                 Display.getDefault().asyncExec(new Runnable() {
112                     @Override
113                     public void run() {
114                         // Handle the notification only if it is for the current
115                         // resource in selection
116                         SimulatorResource resource = resourceManager
117                                 .getCurrentResourceInSelection();
118                         if (null == resource) {
119                             return;
120                         }
121                         if (!resourceURI.equals(resource.getResourceURI())) {
122                             // This notification is for a different resource
123                             // whose attributes are not
124                             // currently not being shown in UI. So ignoring this
125                             // notification.
126                             return;
127                         }
128                         // Refresh the table viewers which will display
129                         // the updated values
130                         if (null != attTblViewer) {
131                             if (notificationType == ModelChangeNotificationType.ATTRIBUTE_ADDED
132                                     || notificationType == ModelChangeNotificationType.ATTRIBUTE_REMOVED) {
133                                 updateViewer(getData());
134                             } else if (notificationType == ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL) {
135                                 attTblViewer.setInput(null);
136                             } else if (notificationType == ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED) {
137                                 if (null != valueChangeSet) {
138                                     attTblViewer.update(
139                                             valueChangeSet.toArray(), null);
140                                 }
141                             }
142                         }
143                     }
144                 });
145             }
146         };
147
148         automationUIListener = new IAutomationUIListener() {
149
150             @Override
151             public void onResourceAutomationStart(final String resourceURI) {
152                 Display.getDefault().asyncExec(new Runnable() {
153
154                     @Override
155                     public void run() {
156                         if (null == resourceURI) {
157                             return;
158                         }
159                         SimulatorResource resource = resourceManager
160                                 .getCurrentResourceInSelection();
161                         if (null == resource) {
162                             return;
163                         }
164                         String uri = resource.getResourceURI();
165                         // Checking whether attributes view is currently
166                         // displaying the attributes of the
167                         // resource whose automation has just started
168                         if (null != uri && uri.equals(resourceURI)) {
169                             Table tbl;
170                             tbl = attTblViewer.getTable();
171                             if (!tbl.isDisposed()) {
172                                 attTblViewer.refresh();
173
174                                 // Disabling the table to prevent interactions
175                                 // during the automation
176                                 tbl.setEnabled(false);
177                                 tbl.deselectAll();
178                             }
179                         }
180                     }
181                 });
182             }
183
184             @Override
185             public void onAutomationComplete(final String resourceURI,
186                     final String attName) {
187                 // This method notifies the completion of attribute level
188                 // automation.
189                 Display.getDefault().asyncExec(new Runnable() {
190
191                     @Override
192                     public void run() {
193                         if (null == resourceURI) {
194                             return;
195                         }
196                         // Check if the given resourceURI is the uri of the
197                         // resource whose attributes are currently being
198                         // displayed by this view.
199                         SimulatorResource resource = resourceManager
200                                 .getCurrentResourceInSelection();
201                         if (null == resource) {
202                             return;
203                         }
204                         String uri = resource.getResourceURI();
205                         if (null == uri || !uri.equals(resourceURI)) {
206                             return;
207                         }
208                         Table tbl;
209                         tbl = attTblViewer.getTable();
210                         if (!tbl.isDisposed()) {
211                             if (null != attName) {
212                                 // Attribute level automation has stopped
213                                 LocalResourceAttribute att = resourceManager
214                                         .getAttributeByResourceURI(resourceURI,
215                                                 attName);
216                                 if (null == att) {
217                                     return;
218                                 } else {
219                                     attTblViewer.update(att, null);
220                                 }
221                             } else {
222                                 // Resource level automation has stopped
223                                 // Enabling the table which was disabled at the
224                                 // beginning of automation
225                                 tbl.setEnabled(true);
226                                 attTblViewer.refresh();
227                             }
228                         }
229                     }
230                 });
231             }
232         };
233     }
234
235     @Override
236     public void createPartControl(Composite parent) {
237         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
238
239         parent.setLayout(new GridLayout());
240         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
241         parent.setLayoutData(gd);
242
243         Group attGroup = new Group(parent, SWT.NONE);
244         attGroup.setLayout(new GridLayout());
245         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
246         attGroup.setLayoutData(gd);
247         attGroup.setText("Attributes");
248         attGroup.setBackground(color);
249
250         attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
251                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
252
253         createAttributeColumns(attTblViewer);
254
255         // make lines and header visible
256         Table table = attTblViewer.getTable();
257         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
258         table.setHeaderVisible(true);
259         table.setLinesVisible(true);
260
261         attTblViewer.setContentProvider(new AttributeContentProvider());
262
263         addManagerListeners();
264
265         // Check whether there is any resource selected already
266         List<LocalResourceAttribute> propertyList = getData();
267         if (null != propertyList) {
268             updateViewer(propertyList);
269         }
270     }
271
272     public void createAttributeColumns(TableViewer tableViewer) {
273
274         attributeEditor = new AttributeEditingSupport();
275
276         TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
277         attName.getColumn().setWidth(attTblColWidth[0]);
278         attName.getColumn().setText(attTblHeaders[0]);
279         attName.setLabelProvider(new StyledCellLabelProvider() {
280             @Override
281             public void update(ViewerCell cell) {
282                 Object element = cell.getElement();
283                 if (element instanceof LocalResourceAttribute) {
284                     LocalResourceAttribute attribute = (LocalResourceAttribute) element;
285                     if (null != attribute) {
286                         cell.setText(attribute.getAttributeName());
287                     }
288                 }
289             }
290         });
291
292         TableViewerColumn attValue = new TableViewerColumn(tableViewer,
293                 SWT.NONE);
294         attValue.getColumn().setWidth(attTblColWidth[1]);
295         attValue.getColumn().setText(attTblHeaders[1]);
296         attValue.setLabelProvider(new ColumnLabelProvider() {
297             @Override
298             public String getText(Object element) {
299                 if (element instanceof LocalResourceAttribute) {
300                     LocalResourceAttribute attribute = (LocalResourceAttribute) element;
301                     if (null != attribute) {
302                         Object val = attribute.getAttributeValue();
303                         if (null != val) {
304                             return String.valueOf(val);
305                         }
306                     }
307                 }
308                 return "";
309             }
310         });
311         attValue.setEditingSupport(attributeEditor
312                 .createAttributeValueEditor(attTblViewer));
313
314         TableViewerColumn automation = new TableViewerColumn(tableViewer,
315                 SWT.NONE);
316         automation.getColumn().setWidth(attTblColWidth[2]);
317         automation.getColumn().setText(attTblHeaders[2]);
318         automation.setLabelProvider(new ColumnLabelProvider() {
319             @Override
320             public String getText(Object element) {
321                 LocalResourceAttribute att = (LocalResourceAttribute) element;
322                 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
323                     System.out.println("No range or allowed values");
324                     return "Read Only";
325                 }
326                 if (att.isAutomationInProgress()) {
327                     return Constants.ENABLED;
328                 }
329                 return Constants.DISABLED;
330             }
331
332             @Override
333             public Image getImage(Object element) {
334                 LocalResourceAttribute att = (LocalResourceAttribute) element;
335                 if (!resourceManager.isAttHasRangeOrAllowedValues(att)) {
336                     System.out.println("No range or allowed values");
337                     return null;
338                 }
339                 if (att.isAutomationInProgress()) {
340                     return Activator.getDefault().getImageRegistry()
341                             .get(Constants.CHECKED);
342                 } else {
343                     return Activator.getDefault().getImageRegistry()
344                             .get(Constants.UNCHECKED);
345                 }
346             }
347         });
348         automation.setEditingSupport(attributeEditor
349                 .createAutomationEditor(attTblViewer));
350     }
351
352     private void addManagerListeners() {
353         resourceManager
354                 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
355         resourceManager
356                 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
357         resourceManager.addAutomationUIListener(automationUIListener);
358     }
359
360     private List<LocalResourceAttribute> getData() {
361         SimulatorResource resourceInSelection = resourceManager
362                 .getCurrentResourceInSelection();
363         if (null != resourceInSelection) {
364             List<LocalResourceAttribute> attList = resourceManager
365                     .getAttributes(resourceInSelection);
366             return attList;
367         } else {
368             return null;
369         }
370     }
371
372     private void updateViewer(List<LocalResourceAttribute> attList) {
373         Table tbl;
374         if (null != attList) {
375             tbl = attTblViewer.getTable();
376             if (null != tbl && !tbl.isDisposed()) {
377                 tbl.setLinesVisible(true);
378                 attTblViewer.setInput(attList.toArray());
379             }
380         } else {
381             // Clear the attributes table viewer
382             if (null != attTblViewer) {
383                 tbl = attTblViewer.getTable();
384                 if (null != tbl && !tbl.isDisposed()) {
385                     // tbl.deselectAll();
386                     tbl.removeAll();
387                     tbl.setLinesVisible(false);
388                 }
389             }
390         }
391     }
392
393     class AttributeContentProvider implements IStructuredContentProvider {
394
395         @Override
396         public void dispose() {
397         }
398
399         @Override
400         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
401         }
402
403         @Override
404         public Object[] getElements(Object element) {
405             return (Object[]) element;
406         }
407
408     }
409
410     @Override
411     public void dispose() {
412         // Unregister the selection listener
413         if (null != resourceSelectionChangedListener) {
414             resourceManager
415                     .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
416         }
417
418         // Unregister the model change listener
419         if (null != resourceModelChangedUIListener) {
420             resourceManager
421                     .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
422         }
423
424         // Unregister the automation complete listener
425         if (null != automationUIListener) {
426             resourceManager.removeAutomationUIListener(automationUIListener);
427         }
428
429         super.dispose();
430     }
431
432     @Override
433     public void setFocus() {
434
435     }
436 }