Imported Upstream version 1.0.0
[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 (att.isAutomationInProgress()) {
323                     return Constants.ENABLED;
324                 }
325                 return Constants.DISABLED;
326             }
327
328             @Override
329             public Image getImage(Object element) {
330                 LocalResourceAttribute att = (LocalResourceAttribute) element;
331                 if (att.isAutomationInProgress()) {
332                     return Activator.getDefault().getImageRegistry()
333                             .get(Constants.CHECKED);
334                 } else {
335                     return Activator.getDefault().getImageRegistry()
336                             .get(Constants.UNCHECKED);
337                 }
338             }
339         });
340         automation.setEditingSupport(attributeEditor
341                 .createAutomationEditor(attTblViewer));
342     }
343
344     private void addManagerListeners() {
345         resourceManager
346                 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
347         resourceManager
348                 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
349         resourceManager.addAutomationUIListener(automationUIListener);
350     }
351
352     private List<LocalResourceAttribute> getData() {
353         SimulatorResource resourceInSelection = resourceManager
354                 .getCurrentResourceInSelection();
355         if (null != resourceInSelection) {
356             List<LocalResourceAttribute> attList = resourceManager
357                     .getAttributes(resourceInSelection);
358             return attList;
359         } else {
360             return null;
361         }
362     }
363
364     private void updateViewer(List<LocalResourceAttribute> attList) {
365         Table tbl;
366         if (null != attList) {
367             tbl = attTblViewer.getTable();
368             if (null != tbl && !tbl.isDisposed()) {
369                 tbl.setLinesVisible(true);
370                 attTblViewer.setInput(attList.toArray());
371             }
372         } else {
373             // Clear the attributes table viewer
374             if (null != attTblViewer) {
375                 tbl = attTblViewer.getTable();
376                 if (null != tbl && !tbl.isDisposed()) {
377                     // tbl.deselectAll();
378                     tbl.removeAll();
379                     tbl.setLinesVisible(false);
380                 }
381             }
382         }
383     }
384
385     class AttributeContentProvider implements IStructuredContentProvider {
386
387         @Override
388         public void dispose() {
389         }
390
391         @Override
392         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
393         }
394
395         @Override
396         public Object[] getElements(Object element) {
397             return (Object[]) element;
398         }
399
400     }
401
402     @Override
403     public void dispose() {
404         // Unregister the selection listener
405         if (null != resourceSelectionChangedListener) {
406             resourceManager
407                     .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
408         }
409
410         // Unregister the model change listener
411         if (null != resourceModelChangedUIListener) {
412             resourceManager
413                     .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
414         }
415
416         // Unregister the automation complete listener
417         if (null != automationUIListener) {
418             resourceManager.removeAutomationUIListener(automationUIListener);
419         }
420
421         super.dispose();
422     }
423
424     @Override
425     public void setFocus() {
426
427     }
428 }