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