da3ccc3ecfe13f01e7625ee84787c89bed87358c
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / SimpleResourceAddAttributePage.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.dialogs;
18
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24
25 import oic.simulator.serviceprovider.model.AttributeHelper;
26 import oic.simulator.serviceprovider.model.AttributeHelper.ValidValuesType;
27 import oic.simulator.serviceprovider.utils.Constants;
28 import oic.simulator.serviceprovider.utils.Utility;
29
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.ITreeContentProvider;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.StyledCellLabelProvider;
35 import org.eclipse.jface.viewers.TableViewer;
36 import org.eclipse.jface.viewers.TableViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerCell;
39 import org.eclipse.jface.window.Window;
40 import org.eclipse.jface.wizard.WizardPage;
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.Label;
50 import org.eclipse.swt.widgets.Table;
51 import org.oic.simulator.AttributeProperty.Type;
52
53 public class SimpleResourceAddAttributePage extends WizardPage {
54
55     private TableViewer          attTblViewer;
56     private Button               addBtn;
57     private Button               remBtn;
58     private Button               editBtn;
59
60     private final String[]       attTblHeaders  = { "Name", "Type", "Range",
61             "Allowed Values", "Default Value"  };
62     private final Integer[]      attTblColWidth = { 90, 75, 75, 150, 75 };
63
64     private Set<AttributeHelper> attributes;
65
66     protected SimpleResourceAddAttributePage() {
67         super("Add Attributes");
68         attributes = new HashSet<AttributeHelper>();
69     }
70
71     @Override
72     public void createControl(Composite parent) {
73         setPageComplete(false);
74         setTitle(Constants.SIMPLE_RESOURCE_ADD_ATTRIBUTE_PAGE_TITLE);
75         setMessage(Constants.SIMPLE_RESOURCE_ADD_ATTRIBUTE_PAGE_MESSAGE);
76         Composite compContent = new Composite(parent, SWT.NONE);
77         GridLayout gridLayout = new GridLayout(2, false);
78         compContent.setLayout(gridLayout);
79         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
80         compContent.setLayoutData(gd);
81
82         Label lbl = new Label(compContent, SWT.NULL);
83         lbl.setText("Attributes:");
84         gd = new GridData();
85         gd.horizontalSpan = 2;
86         lbl.setLayoutData(gd);
87
88         setupAttributeTable(compContent);
89
90         setupAttributeControls(compContent);
91
92         setUiListeners(compContent);
93
94         setControl(compContent);
95     }
96
97     private void setupAttributeTable(Composite attComp) {
98         attTblViewer = new TableViewer(attComp, SWT.MULTI | SWT.H_SCROLL
99                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
100
101         createAttributeColumns(attTblViewer);
102
103         // Make lines and header visible
104         Table table = attTblViewer.getTable();
105         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
106         table.setHeaderVisible(true);
107         table.setLinesVisible(true);
108
109         attTblViewer.setContentProvider(new AttributeContentProvider());
110     }
111
112     public void createAttributeColumns(TableViewer tableViewer) {
113
114         TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
115         attName.getColumn().setWidth(attTblColWidth[0]);
116         attName.getColumn().setText(attTblHeaders[0]);
117         attName.setLabelProvider(new StyledCellLabelProvider() {
118             @Override
119             public void update(ViewerCell cell) {
120                 Object e = cell.getElement();
121                 if (null == e) {
122                     return;
123                 }
124                 AttributeHelper att = (AttributeHelper) e;
125                 cell.setText(att.getAttributeName());
126
127             }
128         });
129
130         TableViewerColumn attType = new TableViewerColumn(tableViewer, SWT.NONE);
131         attType.getColumn().setWidth(attTblColWidth[1]);
132         attType.getColumn().setText(attTblHeaders[1]);
133         attType.setLabelProvider(new StyledCellLabelProvider() {
134             @Override
135             public void update(ViewerCell cell) {
136                 Object e = cell.getElement();
137                 if (null == e) {
138                     return;
139                 }
140                 AttributeHelper att = (AttributeHelper) e;
141                 cell.setText(att.getAttributeType());
142             }
143         });
144
145         TableViewerColumn attRange = new TableViewerColumn(tableViewer,
146                 SWT.NONE);
147         attRange.getColumn().setWidth(attTblColWidth[2]);
148         attRange.getColumn().setText(attTblHeaders[2]);
149         attRange.setLabelProvider(new StyledCellLabelProvider() {
150             @Override
151             public void update(ViewerCell cell) {
152                 Object e = cell.getElement();
153                 if (null == e) {
154                     return;
155                 }
156                 AttributeHelper att = (AttributeHelper) e;
157                 if (att.getValidValuesType() != ValidValuesType.RANGE) {
158                     cell.setText("Nil");
159                 } else {
160                     String min = att.getMin();
161                     String max = att.getMax();
162                     if (null != min && null != max) {
163                         cell.setText("[" + att.getMin() + " to " + att.getMax()
164                                 + "]");
165                     } else {
166                         cell.setText("Nil");
167                     }
168                 }
169             }
170         });
171
172         TableViewerColumn attAllwdValues = new TableViewerColumn(tableViewer,
173                 SWT.NONE);
174         attAllwdValues.getColumn().setWidth(attTblColWidth[3]);
175         attAllwdValues.getColumn().setText(attTblHeaders[3]);
176         attAllwdValues.setLabelProvider(new StyledCellLabelProvider() {
177             @Override
178             public void update(ViewerCell cell) {
179                 Object e = cell.getElement();
180                 if (null == e) {
181                     return;
182                 }
183                 AttributeHelper att = (AttributeHelper) e;
184                 if (att.getValidValuesType() != ValidValuesType.VALUESET) {
185                     cell.setText("Nil");
186                 } else {
187                     cell.setText(att.getAllowedValues().toString());
188                 }
189             }
190         });
191
192         TableViewerColumn attDflValue = new TableViewerColumn(tableViewer,
193                 SWT.NONE);
194         attDflValue.getColumn().setWidth(attTblColWidth[4]);
195         attDflValue.getColumn().setText(attTblHeaders[4]);
196         attDflValue.setLabelProvider(new StyledCellLabelProvider() {
197             @Override
198             public void update(ViewerCell cell) {
199                 Object e = cell.getElement();
200                 if (null == e) {
201                     return;
202                 }
203                 AttributeHelper att = (AttributeHelper) e;
204                 cell.setText(att.getAttributeDflValue());
205             }
206         });
207     }
208
209     private void setupAttributeControls(Composite compContent) {
210
211         Composite innerComp = new Composite(compContent, SWT.NULL);
212         innerComp.setLayout(new GridLayout());
213         GridData gd = new GridData();
214         gd.verticalAlignment = SWT.TOP;
215         innerComp.setLayoutData(gd);
216
217         addBtn = new Button(innerComp, SWT.PUSH);
218         addBtn.setText("Add");
219         gd = new GridData();
220         gd.widthHint = 70;
221         addBtn.setLayoutData(gd);
222
223         editBtn = new Button(innerComp, SWT.PUSH);
224         editBtn.setText("Edit");
225         editBtn.setEnabled(false);
226         gd = new GridData();
227         gd.widthHint = 70;
228         editBtn.setLayoutData(gd);
229
230         remBtn = new Button(innerComp, SWT.PUSH);
231         remBtn.setText("Remove");
232         remBtn.setEnabled(false);
233         gd = new GridData();
234         gd.widthHint = 70;
235         remBtn.setLayoutData(gd);
236     }
237
238     private void setUiListeners(Composite compContent) {
239         addBtn.addSelectionListener(new SelectionAdapter() {
240             @Override
241             public void widgetSelected(SelectionEvent e) {
242                 Set<String> rTypes = Utility.getAttributeTypes();
243                 AddAttributeDialog dialog = new AddAttributeDialog(Display
244                         .getDefault().getActiveShell(), null, rTypes,
245                         attributes);
246                 if (dialog.open() == Window.OK) {
247                     AttributeHelper att = dialog.getAttHelper();
248                     attributes.add(att);
249                     remBtn.setEnabled(true);
250                     AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
251                             .getContentProvider();
252                     provider.addAttribute(att);
253                     attTblViewer.add(att);
254                     attTblViewer.getTable().deselectAll();
255                     attTblViewer.getTable().select(
256                             attTblViewer.getTable().getItemCount() - 1);
257                     editBtn.setEnabled(true);
258                 }
259             }
260         });
261
262         editBtn.addSelectionListener(new SelectionAdapter() {
263             @Override
264             public void widgetSelected(SelectionEvent e) {
265                 IStructuredSelection selection = (IStructuredSelection) attTblViewer
266                         .getSelection();
267                 if (null == selection) {
268                     editBtn.setEnabled(false);
269                     return;
270                 }
271                 AttributeHelper att = (AttributeHelper) selection
272                         .getFirstElement();
273                 if (null == att) {
274                     editBtn.setEnabled(false);
275                     return;
276                 }
277                 // Opening the dialog for edit operation.
278                 // Passing the attribute to populate UI controls with data.
279                 Set<String> rTypes = Utility.getAttributeTypes();
280                 AddAttributeDialog dialog = new AddAttributeDialog(Display
281                         .getDefault().getActiveShell(), att, rTypes, attributes);
282                 if (dialog.open() != Window.OK) {
283                     AttributeHelper newAtt = dialog.getAttClone();
284                     att.setAttributeName(newAtt.getAttributeName());
285                     att.setAttributeType(newAtt.getAttributeType());
286                     att.setAttributeDflValue(newAtt.getAttributeDflValue());
287                     att.setValidValuesType(newAtt.getValidValuesType());
288                     att.setMin(newAtt.getMin());
289                     att.setMax(newAtt.getMax());
290                     att.setAllowedValues(newAtt.getAllowedValues());
291                 }
292                 attTblViewer.update(att, null);
293             }
294         });
295
296         remBtn.addSelectionListener(new SelectionAdapter() {
297             @Override
298             public void widgetSelected(SelectionEvent e) {
299                 IStructuredSelection selection = (IStructuredSelection) attTblViewer
300                         .getSelection();
301                 if (null != selection) {
302                     Iterator<?> itr = selection.iterator();
303                     while (itr.hasNext()) {
304                         AttributeHelper att = (AttributeHelper) itr.next();
305                         attTblViewer.remove(att);
306                         attributes.remove(att);
307                         AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
308                                 .getContentProvider();
309                         provider.removeAttribute(att);
310                     }
311                     if (attributes.size() < 1) {
312                         remBtn.setEnabled(false);
313                     }
314                     editBtn.setEnabled(false);
315                 }
316             }
317         });
318
319         attTblViewer
320                 .addSelectionChangedListener(new ISelectionChangedListener() {
321
322                     @Override
323                     public void selectionChanged(SelectionChangedEvent event) {
324                         remBtn.setEnabled(true);
325                         IStructuredSelection selection = (IStructuredSelection) event
326                                 .getSelection();
327                         if (null == selection) {
328                             return;
329                         }
330                         if (selection.size() > 1) {
331                             editBtn.setEnabled(false);
332                         } else {
333                             editBtn.setEnabled(true);
334                         }
335                     }
336                 });
337     }
338
339     class AttributeContentProvider implements ITreeContentProvider {
340
341         List<AttributeHelper> attList = new ArrayList<AttributeHelper>();
342
343         @SuppressWarnings("unchecked")
344         @Override
345         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
346             attList = (List<AttributeHelper>) newInput;
347         }
348
349         @Override
350         public Object[] getChildren(Object element) {
351             return new Object[0];
352         }
353
354         @Override
355         public Object[] getElements(Object input) {
356             return attList.toArray();
357         }
358
359         @Override
360         public Object getParent(Object element) {
361             return null;
362         }
363
364         @Override
365         public boolean hasChildren(Object element) {
366             return false;
367         }
368
369         @Override
370         public void dispose() {
371         }
372
373         public synchronized void addAttribute(AttributeHelper newElement) {
374             attList.add(newElement);
375         }
376
377         public synchronized void removeAttribute(AttributeHelper element) {
378             attList.remove(element);
379         }
380     }
381
382     @Override
383     public boolean canFlipToNextPage() {
384         return false;
385     }
386
387     public Set<AttributeHelper> getAttributes() {
388         return attributes;
389     }
390 }