Imported Upstream version 1.0.0
[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.manager.ResourceManager;
23 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
24 import oic.simulator.clientcontroller.utils.Constants;
25
26 import org.eclipse.jface.dialogs.IDialogConstants;
27 import org.eclipse.jface.dialogs.TitleAreaDialog;
28 import org.eclipse.jface.viewers.CellEditor;
29 import org.eclipse.jface.viewers.CheckboxCellEditor;
30 import org.eclipse.jface.viewers.ColumnLabelProvider;
31 import org.eclipse.jface.viewers.EditingSupport;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
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.graphics.Image;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.swt.widgets.Table;
48
49 /**
50  * This dialog is used for generating a POST request.
51  */
52 public class PostRequestDialog extends TitleAreaDialog {
53
54     private TableViewer                 attTblViewer;
55
56     private final String[]              attTblHeaders  = { "Name", "Value",
57             "Select"                                  };
58     private final Integer[]             attTblColWidth = { 200, 200, 50 };
59
60     private ResourceManager             resourceManager;
61
62     private List<PutPostAttributeModel> modelList      = null;
63
64     public PostRequestDialog(Shell parentShell,
65             List<PutPostAttributeModel> modelList) {
66         super(parentShell);
67         this.modelList = modelList;
68         resourceManager = Activator.getDefault().getResourceManager();
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
208         public AttributeValueEditor(TableViewer viewer) {
209             super(viewer);
210             this.viewer = viewer;
211             editor = new TextCellEditor(viewer.getTable());
212         }
213
214         @Override
215         protected boolean canEdit(Object arg0) {
216             return true;
217         }
218
219         @Override
220         protected CellEditor getCellEditor(Object element) {
221             return editor;
222         }
223
224         @Override
225         protected Object getValue(Object element) {
226             PutPostAttributeModel model = (PutPostAttributeModel) element;
227             return model.getAttValue();
228         }
229
230         @Override
231         protected void setValue(Object element, Object value) {
232             PutPostAttributeModel model = (PutPostAttributeModel) element;
233             // Compare the actual value and the new value
234             // If there is a change, then its corresponding check box should be
235             // checked.
236             String newValue = String.valueOf(value);
237             String actualValue = resourceManager.getAttributeValue(
238                     resourceManager.getCurrentResourceInSelection(),
239                     model.getAttName());
240             if (newValue.equals(actualValue)) {
241                 model.setModified(false);
242             } else {
243                 model.setModified(true);
244             }
245             model.setAttValue(newValue);
246             viewer.update(element, null);
247         }
248     }
249
250     class UpdateEditor extends EditingSupport {
251
252         private final TableViewer viewer;
253
254         public UpdateEditor(TableViewer viewer) {
255             super(viewer);
256             this.viewer = viewer;
257         }
258
259         @Override
260         protected boolean canEdit(Object arg0) {
261             return true;
262         }
263
264         @Override
265         protected CellEditor getCellEditor(Object element) {
266             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
267         }
268
269         @Override
270         protected Object getValue(Object element) {
271             PutPostAttributeModel model = (PutPostAttributeModel) element;
272             return model.isModified();
273         }
274
275         @Override
276         protected void setValue(Object element, Object value) {
277             PutPostAttributeModel model = (PutPostAttributeModel) element;
278             boolean status = (boolean) value;
279             model.setModified(status);
280             viewer.update(element, null);
281         }
282     }
283 }