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