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