Code clean-up, Bug fixes and enhancements in eclipse plug-ins.
[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.ISelectionChangedListener;
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 ISelectionChangedListener 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 ISelectionChangedListener() {
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 Resource URI.",
158                                     Constants.INVALID_URI_MESSAGE);
159                             return;
160                         }
161
162                         result = resourceManagerRef.isPropertyValueInvalid(
163                                 resourceInSelection, properties,
164                                 Constants.RESOURCE_NAME);
165                         if (result) {
166                             MessageDialog.openError(parent.getShell(),
167                                     "Invalid Input",
168                                     "Resource Name is invalid.");
169                             return;
170                         }
171
172                         boolean update = false;
173                         boolean uriChange = false;
174                         boolean nameChange = false;
175                         if (resourceManagerRef.isPropValueChanged(
176                                 resourceInSelection, properties,
177                                 Constants.RESOURCE_NAME)) {
178                             update = true;
179                             nameChange = true;
180                         }
181                         if (resourceManagerRef.isPropValueChanged(
182                                 resourceInSelection, properties,
183                                 Constants.RESOURCE_URI)) {
184                             // Check whether the new URI is unique.
185                             if (!resourceManagerRef.isUriUnique(properties)) {
186                                 MessageDialog.openError(parent.getShell(),
187                                         "Resource URI in use",
188                                         "Resource URI is in use. Please try a different URI.");
189                                 return;
190                             }
191
192                             update = true;
193                             uriChange = true;
194
195                             if (resourceManagerRef
196                                     .isResourceStarted(resourceInSelection)) {
197                                 update = MessageDialog.openQuestion(
198                                         parent.getShell(), "Save Details",
199                                         "Resource will be restarted to take the changes."
200                                                 + " Do you want to continue?");
201                                 if (!update) {
202                                     return;
203                                 }
204                             }
205                         }
206                         if (update) {
207                             try {
208                                 result = Activator
209                                         .getDefault()
210                                         .getResourceManager()
211                                         .updateResourceProperties(
212                                                 resourceManagerRef
213                                                         .getCurrentResourceInSelection(),
214                                                 properties, uriChange,
215                                                 nameChange);
216                             } catch (SimulatorException ex) {
217                                 result = false;
218                             }
219                             if (result) {
220                                 MessageDialog.openInformation(
221                                         parent.getShell(), "Operation status",
222                                         "Resource properties updated.");
223                             } else {
224                                 MessageDialog.openInformation(
225                                         parent.getShell(), "Operation status",
226                                         "Failed to update the resource properties.");
227
228                                 // Reset the old property values.
229                                 properties = getData(resourceManagerRef
230                                         .getCurrentResourceInSelection());
231                                 updateViewer(properties);
232                             }
233                         }
234                     } else {
235                         Device dev = resourceManagerRef
236                                 .getCurrentDeviceInSelection();
237
238                         // Null check
239                         result = resourceManagerRef.isPropertyValueInvalid(dev,
240                                 properties, Constants.DEVICE_NAME);
241                         if (result) {
242                             MessageDialog.openError(parent.getShell(),
243                                     "Invalid Input", "Device Name is invalid.");
244                             return;
245                         }
246
247                         if (resourceManagerRef.isPropValueChanged(dev,
248                                 properties, Constants.DEVICE_NAME)) {
249                             resourceManagerRef.updateDeviceProperties(dev,
250                                     properties);
251                         }
252
253                     }
254                     cancelBtn.setEnabled(false);
255                     editBtn.setText("Edit");
256                     enable_edit = false;
257                 }
258             }
259         });
260
261         cancelBtn = new Button(parent, SWT.PUSH);
262         cancelBtn.setText("Cancel");
263         cancelBtn.setEnabled(false);
264         gd = new GridData();
265         gd.widthHint = 70;
266         cancelBtn.setLayoutData(gd);
267         cancelBtn.addSelectionListener(new SelectionAdapter() {
268             @Override
269             public void widgetSelected(SelectionEvent e) {
270                 Resource res = resourceManagerRef
271                         .getCurrentResourceInSelection();
272                 if (null != res) {
273                     properties = getData(res);
274                 } else {
275                     Device dev = resourceManagerRef
276                             .getCurrentDeviceInSelection();
277                     if (null != dev) {
278                         properties = getData(dev);
279                     }
280                 }
281                 updateViewer(properties);
282
283                 cancelBtn.setEnabled(false);
284                 editBtn.setText("Edit");
285                 enable_edit = false;
286             }
287         });
288
289         addManagerListeners();
290
291         // Check whether there is any resource selected already
292         Resource resource = resourceManagerRef.getCurrentResourceInSelection();
293         properties = getData(resource);
294         if (null != properties) {
295             updateViewer(properties);
296         }
297         updateEditControls(resource);
298     }
299
300     private void updateEditControls(Object obj) {
301         if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
302
303             if (editBtn.getText().equals("Save")) {
304                 editBtn.setText("Edit");
305                 enable_edit = false;
306             }
307
308             if (null == obj) {
309                 editBtn.setEnabled(false);
310             } else {
311                 editBtn.setEnabled(true);
312             }
313             cancelBtn.setEnabled(false);
314         }
315     }
316
317     private List<MetaProperty> getData(Resource resource) {
318         if (null != resource) {
319             List<MetaProperty> metaPropertyList = resourceManagerRef
320                     .getMetaProperties(resource);
321             return metaPropertyList;
322         } else {
323             return null;
324         }
325     }
326
327     private List<MetaProperty> getData(Device dev) {
328         if (null != dev) {
329             List<MetaProperty> metaPropertyList = resourceManagerRef
330                     .getMetaProperties(dev);
331             return metaPropertyList;
332         } else {
333             return null;
334         }
335     }
336
337     private void updateViewer(List<MetaProperty> metaPropertyList) {
338         if (null != tableViewer) {
339             Table tbl = tableViewer.getTable();
340             if (null != metaPropertyList) {
341                 tableViewer.setInput(metaPropertyList.toArray());
342                 if (!tbl.isDisposed()) {
343                     tbl.setLinesVisible(true);
344                 }
345             } else {
346                 if (!tbl.isDisposed()) {
347                     tbl.removeAll();
348                     tbl.setLinesVisible(false);
349                 }
350             }
351         }
352     }
353
354     public void createColumns(TableViewer tableViewer) {
355         TableViewerColumn propName = new TableViewerColumn(tableViewer,
356                 SWT.NONE);
357         propName.getColumn().setWidth(columnWidth[0]);
358         propName.getColumn().setText(columnHeaders[0]);
359         propName.setLabelProvider(new StyledCellLabelProvider() {
360             @Override
361             public void update(ViewerCell cell) {
362                 MetaProperty prop = (MetaProperty) cell.getElement();
363                 cell.setText(prop.getPropName());
364                 super.update(cell);
365             }
366         });
367
368         TableViewerColumn propValue = new TableViewerColumn(tableViewer,
369                 SWT.NONE);
370         propValue.getColumn().setWidth(columnWidth[1]);
371         propValue.getColumn().setText(columnHeaders[1]);
372         propValue.setLabelProvider(new ColumnLabelProvider() {
373             @Override
374             public String getText(Object element) {
375                 MetaProperty prop = (MetaProperty) element;
376                 if (null != prop) {
377                     return prop.getPropValue();
378                 } else {
379                     return "";
380                 }
381             }
382         });
383         propValue.setEditingSupport(new PropValueEditor(tableViewer));
384     }
385
386     private void addManagerListeners() {
387         UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
388                 resourceSelectionChangedListener);
389     }
390
391     class PropertycontentProvider implements IStructuredContentProvider {
392
393         @Override
394         public void dispose() {
395         }
396
397         @Override
398         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
399         }
400
401         @Override
402         public Object[] getElements(Object element) {
403             return (Object[]) element;
404         }
405     }
406
407     @Override
408     public void dispose() {
409         // Unregister the listener
410         if (null != resourceSelectionChangedListener) {
411             UiListenerHandler.getInstance()
412                     .removeResourceSelectionChangedUIListener(
413                             resourceSelectionChangedListener);
414         }
415         super.dispose();
416     }
417
418     class PropValueEditor extends EditingSupport {
419
420         private final TableViewer viewer;
421
422         public PropValueEditor(TableViewer viewer) {
423             super(viewer);
424             this.viewer = viewer;
425         }
426
427         @Override
428         protected boolean canEdit(Object element) {
429             return true;
430         }
431
432         @Override
433         protected CellEditor getCellEditor(Object element) {
434             if (!enable_edit) {
435                 return null;
436             }
437             // Disabling edit for resource type
438             String propName = ((MetaProperty) element).getPropName();
439             if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
440                 return null;
441             }
442             CellEditor editor = new TextCellEditor(viewer.getTable());
443             return editor;
444         }
445
446         @Override
447         protected Object getValue(Object element) {
448             return ((MetaProperty) element).getPropValue();
449         }
450
451         @Override
452         protected void setValue(Object element, Object value) {
453             MetaProperty prop = (MetaProperty) element;
454             prop.setPropValue(String.valueOf(value));
455             viewer.update(element, null);
456         }
457
458     }
459
460     @Override
461     public void setFocus() {
462     }
463 }