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