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