2e9bc3c1eb246d7c9a78f5a488ae38a6179860d4
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / PostRequestDialog.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.clientcontroller.view.dialogs;
18
19 import java.util.Iterator;
20 import java.util.List;
21
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.manager.ResourceManager;
24 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
25 import oic.simulator.clientcontroller.utils.Constants;
26
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.dialogs.TitleAreaDialog;
30 import org.eclipse.jface.viewers.CellEditor;
31 import org.eclipse.jface.viewers.CheckboxCellEditor;
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.ComboBoxCellEditor;
34 import org.eclipse.jface.viewers.EditingSupport;
35 import org.eclipse.jface.viewers.IStructuredContentProvider;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.StyledCellLabelProvider;
38 import org.eclipse.jface.viewers.TableViewer;
39 import org.eclipse.jface.viewers.TableViewerColumn;
40 import org.eclipse.jface.viewers.Viewer;
41 import org.eclipse.jface.viewers.ViewerCell;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.custom.CCombo;
44 import org.eclipse.swt.events.ModifyEvent;
45 import org.eclipse.swt.events.ModifyListener;
46 import org.eclipse.swt.graphics.Image;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Display;
53 import org.eclipse.swt.widgets.Shell;
54 import org.eclipse.swt.widgets.Table;
55
56 /**
57  * This dialog is used for generating a POST request.
58  */
59 public class PostRequestDialog extends TitleAreaDialog {
60
61     private TableViewer                 attTblViewer;
62
63     private final String[]              attTblHeaders  = { "Name", "Value",
64             "Select"                                  };
65     private final Integer[]             attTblColWidth = { 200, 200, 50 };
66
67     private List<PutPostAttributeModel> modelList      = null;
68
69     public PostRequestDialog(Shell parentShell,
70             List<PutPostAttributeModel> modelList) {
71         super(parentShell);
72         this.modelList = modelList;
73     }
74
75     @Override
76     public void create() {
77         super.create();
78         setTitle("Generate POST Request");
79         setMessage("Dialog which takes input and generates a post request.");
80     }
81
82     @Override
83     protected Control createDialogArea(Composite parent) {
84         Composite compLayout = (Composite) super.createDialogArea(parent);
85         Composite container = new Composite(compLayout, SWT.NONE);
86         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
87         GridLayout layout = new GridLayout(1, false);
88         layout.verticalSpacing = 10;
89         layout.marginTop = 10;
90         container.setLayout(layout);
91
92         createTableViewer(container);
93
94         attTblViewer.setInput(modelList.toArray());
95
96         return compLayout;
97     }
98
99     private void createTableViewer(Composite parent) {
100         attTblViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
101                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
102
103         createAttributeColumns(attTblViewer);
104
105         // make lines and header visible
106         Table table = attTblViewer.getTable();
107         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
108         table.setHeaderVisible(true);
109         table.setLinesVisible(true);
110
111         attTblViewer.setContentProvider(new AttributeContentProvider());
112     }
113
114     public void createAttributeColumns(TableViewer tableViewer) {
115
116         // attributeEditor = new AttributeEditingSupport();
117
118         TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
119         attName.getColumn().setWidth(attTblColWidth[0]);
120         attName.getColumn().setText(attTblHeaders[0]);
121         attName.setLabelProvider(new StyledCellLabelProvider() {
122             @Override
123             public void update(ViewerCell cell) {
124                 Object element = cell.getElement();
125                 if (element instanceof PutPostAttributeModel) {
126                     PutPostAttributeModel entry = (PutPostAttributeModel) element;
127                     cell.setText(entry.getAttName());
128                 }
129             }
130         });
131
132         TableViewerColumn attValue = new TableViewerColumn(tableViewer,
133                 SWT.NONE);
134         attValue.getColumn().setWidth(attTblColWidth[1]);
135         attValue.getColumn().setText(attTblHeaders[1]);
136         attValue.setLabelProvider(new StyledCellLabelProvider() {
137             @Override
138             public void update(ViewerCell cell) {
139                 Object element = cell.getElement();
140                 if (element instanceof PutPostAttributeModel) {
141                     PutPostAttributeModel entry = (PutPostAttributeModel) element;
142                     cell.setText(entry.getAttValue());
143                 }
144             }
145         });
146
147         attValue.setEditingSupport(new AttributeValueEditor(attTblViewer));
148
149         TableViewerColumn updateColumn = new TableViewerColumn(tableViewer,
150                 SWT.NONE);
151         updateColumn.getColumn().setWidth(attTblColWidth[2]);
152         updateColumn.getColumn().setText(attTblHeaders[2]);
153         updateColumn.setLabelProvider(new ColumnLabelProvider() {
154             @Override
155             public String getText(Object element) {
156                 return "";
157             }
158
159             @Override
160             public Image getImage(Object element) {
161                 PutPostAttributeModel model = (PutPostAttributeModel) element;
162                 if (model.isModified()) {
163                     return Activator.getDefault().getImageRegistry()
164                             .get(Constants.CHECKED);
165                 }
166                 return Activator.getDefault().getImageRegistry()
167                         .get(Constants.UNCHECKED);
168             }
169         });
170         updateColumn.setEditingSupport(new UpdateEditor(attTblViewer));
171     }
172
173     @Override
174     protected boolean isResizable() {
175         return true;
176     }
177
178     @Override
179     public boolean isHelpAvailable() {
180         return false;
181     }
182
183     @Override
184     protected Button createButton(Composite parent, int id, String label,
185             boolean defaultButton) {
186         if (id == IDialogConstants.OK_ID) {
187             label = "POST";
188         }
189         return super.createButton(parent, id, label, defaultButton);
190     }
191
192     class AttributeContentProvider implements IStructuredContentProvider {
193
194         @Override
195         public void dispose() {
196         }
197
198         @Override
199         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
200         }
201
202         @Override
203         public Object[] getElements(Object element) {
204             return (Object[]) element;
205         }
206
207     }
208
209     class AttributeValueEditor extends EditingSupport {
210         private final TableViewer viewer;
211         private CCombo            comboBox;
212
213         public AttributeValueEditor(TableViewer viewer) {
214             super(viewer);
215             this.viewer = viewer;
216         }
217
218         @Override
219         protected boolean canEdit(Object arg0) {
220             return true;
221         }
222
223         @Override
224         protected CellEditor getCellEditor(Object element) {
225             PutPostAttributeModel attributeInSelection = (PutPostAttributeModel) element;
226
227             String values[] = null;
228             List<String> valueSet = attributeInSelection.getValues();
229             values = convertListToStringArray(valueSet);
230
231             ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
232                     viewer.getTable(), values);
233             comboBox = (CCombo) comboEditor.getControl();
234             if (null != comboBox) {
235                 comboBox.addModifyListener(new ModifyListener() {
236                     @Override
237                     public void modifyText(ModifyEvent e) {
238                         IStructuredSelection selection = (IStructuredSelection) AttributeValueEditor.this.viewer
239                                 .getSelection();
240                         PutPostAttributeModel att = (PutPostAttributeModel) selection
241                                 .getFirstElement();
242                         if (null == att) {
243                             return;
244                         }
245                         String newValue = comboBox.getText();
246                         if (null != newValue && !newValue.isEmpty()) {
247                             att.setModified(true);
248                         } else {
249                             att.setModified(false);
250                         }
251                         AttributeValueEditor.this.viewer.update(att, null);
252                     }
253                 });
254             }
255             return comboEditor;
256         }
257
258         @Override
259         protected Object getValue(Object element) {
260             int indexOfItem = 0;
261             PutPostAttributeModel att = (PutPostAttributeModel) element;
262             String valueString = att.getAttValue();
263             List<String> valueSet = att.getValues();
264             if (null != valueSet) {
265                 indexOfItem = valueSet.indexOf(valueString);
266             }
267             if (indexOfItem == -1) {
268                 indexOfItem = 0;
269             }
270             return indexOfItem;
271         }
272
273         @Override
274         protected void setValue(Object element, Object value) {
275             PutPostAttributeModel att = (PutPostAttributeModel) element;
276             int index;
277             try {
278                 index = Integer.parseInt(String.valueOf(value));
279             } catch (NumberFormatException nfe) {
280                 index = -1;
281             }
282             String newValue;
283             if (index == -1) {
284                 newValue = comboBox.getText();
285                 att.prependNewValue(newValue);
286             } else {
287                 newValue = att.getValues().get(index);
288             }
289             att.setAttValue(newValue);
290             viewer.update(element, null);
291         }
292
293         public String[] convertListToStringArray(List<String> valueList) {
294             String[] strArr;
295             if (null != valueList && valueList.size() > 0) {
296                 strArr = valueList.toArray(new String[1]);
297             } else {
298                 strArr = new String[1];
299             }
300             return strArr;
301         }
302     }
303
304     class UpdateEditor extends EditingSupport {
305
306         private final TableViewer viewer;
307
308         public UpdateEditor(TableViewer viewer) {
309             super(viewer);
310             this.viewer = viewer;
311         }
312
313         @Override
314         protected boolean canEdit(Object arg0) {
315             return true;
316         }
317
318         @Override
319         protected CellEditor getCellEditor(Object element) {
320             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
321         }
322
323         @Override
324         protected Object getValue(Object element) {
325             PutPostAttributeModel model = (PutPostAttributeModel) element;
326             return model.isModified();
327         }
328
329         @Override
330         protected void setValue(Object element, Object value) {
331             PutPostAttributeModel model = (PutPostAttributeModel) element;
332             boolean status = (Boolean) value;
333             model.setModified(status);
334             viewer.update(element, null);
335         }
336     }
337
338     @Override
339     protected void okPressed() {
340         String value;
341         PutPostAttributeModel attModel;
342         Iterator<PutPostAttributeModel> itr;
343         itr = modelList.iterator();
344         while (itr.hasNext()) {
345             attModel = itr.next();
346             if (null == attModel) {
347                 return;
348             }
349             value = attModel.getAttValue();
350             if (null == value || value.isEmpty()) {
351                 MessageDialog.openError(Display.getDefault().getActiveShell(),
352                         "Empty value", "Attribute value should not be empty.");
353                 return;
354             }
355         }
356         close();
357     }
358 }