Collection resource support in plugin UI.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / MetaPropertiesView.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
21 import oic.simulator.serviceprovider.Activator;
22 import oic.simulator.serviceprovider.listener.ISelectionChangedUIListener;
23 import oic.simulator.serviceprovider.manager.ResourceManager;
24 import oic.simulator.serviceprovider.manager.UiListenerHandler;
25 import oic.simulator.serviceprovider.model.Device;
26 import oic.simulator.serviceprovider.model.MetaProperty;
27 import oic.simulator.serviceprovider.model.Resource;
28 import oic.simulator.serviceprovider.utils.Constants;
29
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.viewers.CellEditor;
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.EditingSupport;
34 import org.eclipse.jface.viewers.IStructuredContentProvider;
35 import org.eclipse.jface.viewers.StyledCellLabelProvider;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.viewers.TableViewerColumn;
38 import org.eclipse.jface.viewers.TextCellEditor;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.jface.viewers.ViewerCell;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Table;
50 import org.eclipse.ui.part.ViewPart;
51 import org.oic.simulator.SimulatorException;
52
53 /**
54  * This class manages and shows the meta properties view in the perspective.
55  */
56 public class MetaPropertiesView extends ViewPart {
57
58     public static final String          VIEW_ID       = "oic.simulator.serviceprovider.view.metaproperties";
59
60     private TableViewer                 tableViewer;
61
62     private final String[]              columnHeaders = { "Property", "Value" };
63
64     private final Integer[]             columnWidth   = { 150, 150 };
65
66     private ISelectionChangedUIListener resourceSelectionChangedListener;
67
68     private ResourceManager             resourceManagerRef;
69
70     private List<MetaProperty>          properties;
71
72     private boolean                     enable_edit;
73     private Button                      editBtn;
74     private Button                      cancelBtn;
75
76     public MetaPropertiesView() {
77
78         resourceManagerRef = Activator.getDefault().getResourceManager();
79
80         resourceSelectionChangedListener = new ISelectionChangedUIListener() {
81
82             @Override
83             public void onResourceSelectionChange(final Resource resource) {
84                 Display.getDefault().asyncExec(new Runnable() {
85
86                     @Override
87                     public void run() {
88                         if (null != tableViewer) {
89                             properties = getData(resource);
90                             updateViewer(properties);
91                         }
92                         updateEditControls(resource);
93                     }
94                 });
95             }
96
97             @Override
98             public void onDeviceSelectionChange(final Device dev) {
99                 Display.getDefault().asyncExec(new Runnable() {
100
101                     @Override
102                     public void run() {
103                         if (null != tableViewer) {
104                             properties = getData(dev);
105                             updateViewer(properties);
106                         }
107                         updateEditControls(dev);
108                     }
109                 });
110             }
111         };
112     }
113
114     @Override
115     public void createPartControl(final Composite parent) {
116         parent.setLayout(new GridLayout(2, false));
117
118         tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
119                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
120
121         createColumns(tableViewer);
122
123         // Make lines and header visible
124         final Table table = tableViewer.getTable();
125         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
126         gd.horizontalSpan = 2;
127         table.setLayoutData(gd);
128         table.setHeaderVisible(true);
129         table.setLinesVisible(true);
130
131         tableViewer.setContentProvider(new PropertycontentProvider());
132
133         editBtn = new Button(parent, SWT.PUSH);
134         editBtn.setText("Edit");
135         gd = new GridData();
136         gd.widthHint = 50;
137         editBtn.setLayoutData(gd);
138         editBtn.addSelectionListener(new SelectionAdapter() {
139             @Override
140             public void widgetSelected(SelectionEvent e) {
141                 if (editBtn.getText().equals("Edit")) {
142                     cancelBtn.setEnabled(true);
143                     editBtn.setText("Save");
144                     enable_edit = true;
145                 } else {
146                     boolean result = false;
147                     Resource resourceInSelection = resourceManagerRef
148                             .getCurrentResourceInSelection();
149                     if (null != resourceInSelection) {
150
151                         // Null Check
152                         result = resourceManagerRef.isPropertyValueInvalid(
153                                 resourceInSelection, properties,
154                                 Constants.RESOURCE_URI);
155                         if (result) {
156                             MessageDialog.openError(parent.getShell(),
157                                     "Invalid Input", "Resource URI is invalid.");
158                             return;
159                         }
160
161                         result = resourceManagerRef.isPropertyValueInvalid(
162                                 resourceInSelection, properties,
163                                 Constants.RESOURCE_NAME);
164                         if (result) {
165                             MessageDialog.openError(parent.getShell(),
166                                     "Invalid Input",
167                                     "Resource Name is invalid.");
168                             return;
169                         }
170
171                         boolean update = false;
172                         boolean uriChange = false;
173                         boolean nameChange = false;
174                         if (resourceManagerRef.isPropValueChanged(
175                                 resourceInSelection, properties,
176                                 Constants.RESOURCE_NAME)) {
177                             update = true;
178                             nameChange = true;
179                         }
180                         if (resourceManagerRef.isPropValueChanged(
181                                 resourceInSelection, properties,
182                                 Constants.RESOURCE_URI)) {
183                             // Check whether the new URI is unique.
184                             if (!resourceManagerRef.isUriUnique(properties)) {
185                                 MessageDialog.openError(parent.getShell(),
186                                         "Resource URI in use",
187                                         "Resource URI is in use. Please try a different URI.");
188                                 return;
189                             }
190
191                             update = true;
192                             uriChange = true;
193
194                             if (resourceManagerRef
195                                     .isResourceStarted(resourceInSelection)) {
196                                 update = MessageDialog.openQuestion(
197                                         parent.getShell(), "Save Details",
198                                         "Resource will be restarted to take the changes."
199                                                 + " Do you want to continue?");
200                                 if (!update) {
201                                     return;
202                                 }
203                             }
204                         }
205                         if (update) {
206                             try {
207                                 result = Activator
208                                         .getDefault()
209                                         .getResourceManager()
210                                         .updateResourceProperties(
211                                                 resourceManagerRef
212                                                         .getCurrentResourceInSelection(),
213                                                 properties, uriChange,
214                                                 nameChange);
215                             } catch (SimulatorException ex) {
216                                 result = false;
217                             }
218                             if (result) {
219                                 MessageDialog.openInformation(
220                                         parent.getShell(), "Operation status",
221                                         "Resource properties updated.");
222                             } else {
223                                 MessageDialog.openInformation(
224                                         parent.getShell(), "Operation status",
225                                         "Failed to update the resource properties.");
226
227                                 // Reset the old property values.
228                                 properties = getData(resourceManagerRef
229                                         .getCurrentResourceInSelection());
230                                 updateViewer(properties);
231                             }
232                         }
233                     } else {
234                         Device dev = resourceManagerRef
235                                 .getCurrentDeviceInSelection();
236
237                         // Null check
238                         result = resourceManagerRef.isPropertyValueInvalid(dev,
239                                 properties, Constants.DEVICE_NAME);
240                         if (result) {
241                             MessageDialog.openError(parent.getShell(),
242                                     "Invalid Input", "Device Name is invalid.");
243                             return;
244                         }
245
246                         if (resourceManagerRef.isPropValueChanged(dev,
247                                 properties, Constants.DEVICE_NAME)) {
248                             resourceManagerRef.updateDeviceProperties(dev,
249                                     properties);
250                         }
251
252                     }
253                     cancelBtn.setEnabled(false);
254                     editBtn.setText("Edit");
255                     enable_edit = false;
256                 }
257             }
258         });
259
260         cancelBtn = new Button(parent, SWT.PUSH);
261         cancelBtn.setText("Cancel");
262         cancelBtn.setEnabled(false);
263         gd = new GridData();
264         gd.widthHint = 70;
265         cancelBtn.setLayoutData(gd);
266         cancelBtn.addSelectionListener(new SelectionAdapter() {
267             @Override
268             public void widgetSelected(SelectionEvent e) {
269                 Resource res = resourceManagerRef
270                         .getCurrentResourceInSelection();
271                 if (null != res) {
272                     properties = getData(res);
273                 } else {
274                     Device dev = resourceManagerRef
275                             .getCurrentDeviceInSelection();
276                     if (null != dev) {
277                         properties = getData(dev);
278                     }
279                 }
280                 updateViewer(properties);
281
282                 cancelBtn.setEnabled(false);
283                 editBtn.setText("Edit");
284                 enable_edit = false;
285             }
286         });
287
288         addManagerListeners();
289
290         // Check whether there is any resource selected already
291         Resource resource = resourceManagerRef.getCurrentResourceInSelection();
292         properties = getData(resource);
293         if (null != properties) {
294             updateViewer(properties);
295         }
296         updateEditControls(resource);
297     }
298
299     private void updateEditControls(Object obj) {
300         if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
301
302             if (editBtn.getText().equals("Save")) {
303                 editBtn.setText("Edit");
304                 enable_edit = false;
305             }
306
307             if (null == obj) {
308                 editBtn.setEnabled(false);
309             } else {
310                 editBtn.setEnabled(true);
311             }
312             cancelBtn.setEnabled(false);
313         }
314     }
315
316     private List<MetaProperty> getData(Resource resource) {
317         if (null != resource) {
318             List<MetaProperty> metaPropertyList = resourceManagerRef
319                     .getMetaProperties(resource);
320             return metaPropertyList;
321         } else {
322             return null;
323         }
324     }
325
326     private List<MetaProperty> getData(Device dev) {
327         if (null != dev) {
328             List<MetaProperty> metaPropertyList = resourceManagerRef
329                     .getMetaProperties(dev);
330             return metaPropertyList;
331         } else {
332             return null;
333         }
334     }
335
336     private void updateViewer(List<MetaProperty> metaPropertyList) {
337         if (null != tableViewer) {
338             Table tbl = tableViewer.getTable();
339             if (null != metaPropertyList) {
340                 tableViewer.setInput(metaPropertyList.toArray());
341                 if (!tbl.isDisposed()) {
342                     tbl.setLinesVisible(true);
343                 }
344             } else {
345                 if (!tbl.isDisposed()) {
346                     tbl.removeAll();
347                     tbl.setLinesVisible(false);
348                 }
349             }
350         }
351     }
352
353     public void createColumns(TableViewer tableViewer) {
354         TableViewerColumn propName = new TableViewerColumn(tableViewer,
355                 SWT.NONE);
356         propName.getColumn().setWidth(columnWidth[0]);
357         propName.getColumn().setText(columnHeaders[0]);
358         propName.setLabelProvider(new StyledCellLabelProvider() {
359             @Override
360             public void update(ViewerCell cell) {
361                 MetaProperty prop = (MetaProperty) cell.getElement();
362                 cell.setText(prop.getPropName());
363                 super.update(cell);
364             }
365         });
366
367         TableViewerColumn propValue = new TableViewerColumn(tableViewer,
368                 SWT.NONE);
369         propValue.getColumn().setWidth(columnWidth[1]);
370         propValue.getColumn().setText(columnHeaders[1]);
371         propValue.setLabelProvider(new ColumnLabelProvider() {
372             @Override
373             public String getText(Object element) {
374                 MetaProperty prop = (MetaProperty) element;
375                 if (null != prop) {
376                     return prop.getPropValue();
377                 } else {
378                     return "";
379                 }
380             }
381         });
382         propValue.setEditingSupport(new PropValueEditor(tableViewer));
383     }
384
385     private void addManagerListeners() {
386         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
387                 resourceSelectionChangedListener);
388     }
389
390     class PropertycontentProvider implements IStructuredContentProvider {
391
392         @Override
393         public void dispose() {
394         }
395
396         @Override
397         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
398         }
399
400         @Override
401         public Object[] getElements(Object element) {
402             return (Object[]) element;
403         }
404     }
405
406     @Override
407     public void dispose() {
408         // Unregister the listener
409         if (null != resourceSelectionChangedListener) {
410             UiListenerHandler.getInstance()
411                     .removeResourceSelectionChangedUIListener(
412                             resourceSelectionChangedListener);
413         }
414         super.dispose();
415     }
416
417     class PropValueEditor extends EditingSupport {
418
419         private final TableViewer viewer;
420
421         public PropValueEditor(TableViewer viewer) {
422             super(viewer);
423             this.viewer = viewer;
424         }
425
426         @Override
427         protected boolean canEdit(Object element) {
428             return true;
429         }
430
431         @Override
432         protected CellEditor getCellEditor(Object element) {
433             if (!enable_edit) {
434                 return null;
435             }
436             // Disabling edit for resource type
437             String propName = ((MetaProperty) element).getPropName();
438             if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
439                 return null;
440             }
441             CellEditor editor = new TextCellEditor(viewer.getTable());
442             return editor;
443         }
444
445         @Override
446         protected Object getValue(Object element) {
447             return ((MetaProperty) element).getPropValue();
448         }
449
450         @Override
451         protected void setValue(Object element, Object value) {
452             MetaProperty prop = (MetaProperty) element;
453             prop.setPropValue(String.valueOf(value));
454             viewer.update(element, null);
455         }
456
457     }
458
459     @Override
460     public void setFocus() {
461     }
462 }