Imported Upstream version 1.0.1
[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.List;
20
21 import oic.simulator.clientcontroller.Activator;
22 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
23 import oic.simulator.clientcontroller.utils.Constants;
24
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.dialogs.TitleAreaDialog;
27 import org.eclipse.jface.viewers.CellEditor;
28 import org.eclipse.jface.viewers.CheckboxCellEditor;
29 import org.eclipse.jface.viewers.ColumnLabelProvider;
30 import org.eclipse.jface.viewers.EditingSupport;
31 import org.eclipse.jface.viewers.IStructuredContentProvider;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.StyledCellLabelProvider;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.viewers.TableViewerColumn;
36 import org.eclipse.jface.viewers.TextCellEditor;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerCell;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Shell;
49 import org.eclipse.swt.widgets.Table;
50 import org.eclipse.swt.widgets.Text;
51
52 /**
53  * This dialog is used for generating a POST request.
54  */
55 public class PostRequestDialog extends TitleAreaDialog {
56
57     private TableViewer                 attTblViewer;
58
59     private final String[]              attTblHeaders  = { "Name", "Value",
60             "Select"                                  };
61     private final Integer[]             attTblColWidth = { 200, 200, 50 };
62
63     private List<PutPostAttributeModel> modelList      = null;
64     
65     public PostRequestDialog(Shell parentShell,
66             List<PutPostAttributeModel> modelList) {
67         super(parentShell);
68         this.modelList = modelList;
69     }
70
71     @Override
72     public void create() {
73         super.create();
74         setTitle("Generate POST Request");
75         setMessage("Dialog which takes input and generates a post request.");
76     }
77
78     @Override
79     protected Control createDialogArea(Composite parent) {
80         Composite compLayout = (Composite) super.createDialogArea(parent);
81         Composite container = new Composite(compLayout, SWT.NONE);
82         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
83         GridLayout layout = new GridLayout(1, false);
84         layout.verticalSpacing = 10;
85         layout.marginTop = 10;
86         container.setLayout(layout);
87
88         createTableViewer(container);
89
90         attTblViewer.setInput(modelList.toArray());
91
92         return compLayout;
93     }
94
95     private void createTableViewer(Composite parent) {
96         attTblViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
97                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
98
99         createAttributeColumns(attTblViewer);
100
101         // make lines and header visible
102         Table table = attTblViewer.getTable();
103         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
104         table.setHeaderVisible(true);
105         table.setLinesVisible(true);
106
107         attTblViewer.setContentProvider(new AttributeContentProvider());
108     }
109
110     public void createAttributeColumns(TableViewer tableViewer) {
111
112         // attributeEditor = new AttributeEditingSupport();
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 element = cell.getElement();
121                 if (element instanceof PutPostAttributeModel) {
122                     PutPostAttributeModel entry = (PutPostAttributeModel) element;
123                     cell.setText(entry.getAttName());
124                 }
125             }
126         });
127
128         TableViewerColumn attValue = new TableViewerColumn(tableViewer,
129                 SWT.NONE);
130         attValue.getColumn().setWidth(attTblColWidth[1]);
131         attValue.getColumn().setText(attTblHeaders[1]);
132         attValue.setLabelProvider(new StyledCellLabelProvider() {
133             @Override
134             public void update(ViewerCell cell) {
135                 Object element = cell.getElement();
136                 if (element instanceof PutPostAttributeModel) {
137                     PutPostAttributeModel entry = (PutPostAttributeModel) element;
138                     cell.setText(entry.getAttValue());
139                 }
140             }
141         });
142         attValue.setEditingSupport(new AttributeValueEditor(attTblViewer));
143
144         TableViewerColumn updateColumn = new TableViewerColumn(tableViewer,
145                 SWT.NONE);
146         updateColumn.getColumn().setWidth(attTblColWidth[2]);
147         updateColumn.getColumn().setText(attTblHeaders[2]);
148         updateColumn.setLabelProvider(new ColumnLabelProvider() {
149             @Override
150             public String getText(Object element) {
151                 return "";
152             }
153
154             @Override
155             public Image getImage(Object element) {
156                 PutPostAttributeModel model = (PutPostAttributeModel) element;
157                 if (model.isModified()) {
158                     return Activator.getDefault().getImageRegistry()
159                             .get(Constants.CHECKED);
160                 }
161                 return Activator.getDefault().getImageRegistry()
162                         .get(Constants.UNCHECKED);
163             }
164         });
165         updateColumn.setEditingSupport(new UpdateEditor(attTblViewer));
166     }
167
168     @Override
169     protected boolean isResizable() {
170         return true;
171     }
172
173     @Override
174     public boolean isHelpAvailable() {
175         return false;
176     }
177
178     @Override
179     protected Button createButton(Composite parent, int id, String label,
180             boolean defaultButton) {
181         if (id == IDialogConstants.OK_ID) {
182             label = "POST";
183         }
184         return super.createButton(parent, id, label, defaultButton);
185     }
186
187     class AttributeContentProvider implements IStructuredContentProvider {
188
189         @Override
190         public void dispose() {
191         }
192
193         @Override
194         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
195         }
196
197         @Override
198         public Object[] getElements(Object element) {
199             return (Object[]) element;
200         }
201
202     }
203
204     class AttributeValueEditor extends EditingSupport {
205         private final TableViewer viewer;
206         private final CellEditor  editor;
207         private final Text txt;
208         public AttributeValueEditor(TableViewer viewer) {
209             super(viewer);
210             this.viewer = viewer;
211             editor = new TextCellEditor(viewer.getTable());
212             txt = (Text)editor.getControl();
213             if(null != txt) {
214                 txt.addModifyListener(new ModifyListener() {
215                     @Override
216                     public void modifyText(ModifyEvent e) {                 
217                         IStructuredSelection selection = (IStructuredSelection)AttributeValueEditor.this.viewer.getSelection();
218                         PutPostAttributeModel att = (PutPostAttributeModel)selection.getFirstElement();             
219                         if(null == att) {
220                             return;
221                         }
222                         String newValue = txt.getText();
223                         if(null != newValue && !newValue.isEmpty()) {
224                             att.setModified(true);
225                         }
226                         else {
227                             att.setModified(false);
228                         }
229                         AttributeValueEditor.this.viewer.update(att, null);
230                     }
231                 });
232               
233             }
234         }
235
236         @Override
237         protected boolean canEdit(Object arg0) {
238             return true;
239         }
240
241         @Override
242         protected CellEditor getCellEditor(Object element) {
243             return editor;
244         }
245
246         @Override
247         protected Object getValue(Object element) {
248             PutPostAttributeModel model = (PutPostAttributeModel) element;
249             return model.getAttValue();
250         }
251
252         @Override
253         protected void setValue(Object element, Object value) {
254             PutPostAttributeModel model = (PutPostAttributeModel) element;
255             // Compare the actual value and the new value
256             // If there is a change, then its corresponding check box should be
257             // checked.
258             String newValue = String.valueOf(value);
259             model.setAttValue(newValue);
260             viewer.update(element, null);
261         }
262     }
263
264     class UpdateEditor extends EditingSupport {
265
266         private final TableViewer viewer;
267
268         public UpdateEditor(TableViewer viewer) {
269             super(viewer);
270             this.viewer = viewer;
271         }
272
273         @Override
274         protected boolean canEdit(Object arg0) {
275             return true;
276         }
277
278         @Override
279         protected CellEditor getCellEditor(Object element) {
280             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
281         }
282
283         @Override
284         protected Object getValue(Object element) {
285             PutPostAttributeModel model = (PutPostAttributeModel) element;
286             return model.isModified();
287         }
288
289         @Override
290         protected void setValue(Object element, Object value) {
291             PutPostAttributeModel model = (PutPostAttributeModel) element;
292             boolean status = (boolean) value;
293             model.setModified(status);
294             viewer.update(element, null);
295         }
296     }
297 }