Imported Upstream version 1.1.0
[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 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                     if (null != newAtt) {
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                 }
293                 attTblViewer.update(att, null);
294             }
295         });
296
297         remBtn.addSelectionListener(new SelectionAdapter() {
298             @Override
299             public void widgetSelected(SelectionEvent e) {
300                 IStructuredSelection selection = (IStructuredSelection) attTblViewer
301                         .getSelection();
302                 if (null != selection) {
303                     Iterator<?> itr = selection.iterator();
304                     while (itr.hasNext()) {
305                         AttributeHelper att = (AttributeHelper) itr.next();
306                         attTblViewer.remove(att);
307                         attributes.remove(att);
308                         AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
309                                 .getContentProvider();
310                         provider.removeAttribute(att);
311                     }
312                     if (attributes.size() < 1) {
313                         remBtn.setEnabled(false);
314                     }
315                     editBtn.setEnabled(false);
316                 }
317             }
318         });
319
320         attTblViewer
321                 .addSelectionChangedListener(new ISelectionChangedListener() {
322
323                     @Override
324                     public void selectionChanged(SelectionChangedEvent event) {
325                         remBtn.setEnabled(true);
326                         IStructuredSelection selection = (IStructuredSelection) event
327                                 .getSelection();
328                         if (null == selection) {
329                             return;
330                         }
331                         if (selection.size() > 1) {
332                             editBtn.setEnabled(false);
333                         } else {
334                             editBtn.setEnabled(true);
335                         }
336                     }
337                 });
338     }
339
340     private static class AttributeContentProvider implements
341             ITreeContentProvider {
342
343         List<AttributeHelper> attList = new ArrayList<AttributeHelper>();
344
345         @SuppressWarnings("unchecked")
346         @Override
347         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
348             attList = (List<AttributeHelper>) newInput;
349         }
350
351         @Override
352         public Object[] getChildren(Object element) {
353             return new Object[0];
354         }
355
356         @Override
357         public Object[] getElements(Object input) {
358             return attList.toArray();
359         }
360
361         @Override
362         public Object getParent(Object element) {
363             return null;
364         }
365
366         @Override
367         public boolean hasChildren(Object element) {
368             return false;
369         }
370
371         @Override
372         public void dispose() {
373         }
374
375         public synchronized void addAttribute(AttributeHelper newElement) {
376             attList.add(newElement);
377         }
378
379         public synchronized void removeAttribute(AttributeHelper element) {
380             attList.remove(element);
381         }
382     }
383
384     @Override
385     public boolean canFlipToNextPage() {
386         return false;
387     }
388
389     public Set<AttributeHelper> getAttributes() {
390         return attributes;
391     }
392 }