6be2b35e79150b6faa5ae567fbb4284e5bc66b89
[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.HashMap;
20 import java.util.Map;
21
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
24 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
25 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
26 import oic.simulator.clientcontroller.utils.Constants;
27 import oic.simulator.clientcontroller.utils.Utility;
28 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
29
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.dialogs.TitleAreaDialog;
32 import org.eclipse.jface.viewers.ILabelProviderListener;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.jface.viewers.TreeViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.ModifyEvent;
40 import org.eclipse.swt.events.ModifyListener;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Combo;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Group;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Tree;
52 import org.eclipse.swt.widgets.TreeColumn;
53 import org.oic.simulator.AttributeValue.ValueType;
54 import org.oic.simulator.SimulatorResourceAttribute;
55
56 /**
57  * This dialog is used for generating a POST request.
58  */
59 public class PostRequestDialog extends TitleAreaDialog {
60
61     private TreeViewer              attViewer;
62     private Combo                   ifTypesCmb;
63
64     private String                  ifType;
65
66     private Map<String, String>     ifTypes;
67
68     private AttributeEditingSupport attributeEditor;
69
70     private ResourceRepresentation  updatedRepresentation;
71
72     private final String[]          attTblHeaders  = { "Name", "Value",
73             "Select"                              };
74     private final Integer[]         attTblColWidth = { 200, 200, 50 };
75
76     public PostRequestDialog(Shell parentShell) {
77         super(parentShell);
78     }
79
80     @Override
81     public void create() {
82         super.create();
83         setTitle("Generate POST Request");
84         setMessage("Dialog which takes input and generates a post request.");
85     }
86
87     @Override
88     protected Control createDialogArea(Composite parent) {
89         Composite compLayout = (Composite) super.createDialogArea(parent);
90
91         Group paramsGrp = new Group(compLayout, SWT.NONE);
92         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
93         paramsGrp.setLayoutData(gd);
94         GridLayout layout = new GridLayout(2, false);
95         layout.verticalSpacing = 10;
96         layout.marginTop = 10;
97         paramsGrp.setLayout(layout);
98
99         Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
100         ifTypeLbl.setText("Interface Type");
101
102         ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
103         gd = new GridData();
104         gd.grabExcessHorizontalSpace = true;
105         gd.horizontalAlignment = SWT.FILL;
106         ifTypesCmb.setLayoutData(gd);
107         ifTypesCmb.addModifyListener(new ModifyListener() {
108             @Override
109             public void modifyText(ModifyEvent e) {
110                 ifType = ifTypesCmb.getText();
111             }
112         });
113
114         // Set the interface types.
115         Map<String, String> ifTypes = Utility.getResourceInterfaces();
116         if (null != ifTypes && !ifTypes.isEmpty()) {
117             this.ifTypes = new HashMap<String, String>();
118             String key;
119             for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
120                 key = entry.getValue() + " (" + entry.getKey() + ")";
121                 this.ifTypes.put(key, entry.getKey());
122                 ifTypesCmb.add(key);
123             }
124         }
125
126         Composite container = new Composite(compLayout, SWT.NONE);
127         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128         layout = new GridLayout(1, false);
129         layout.verticalSpacing = 10;
130         layout.marginTop = 10;
131         container.setLayout(layout);
132
133         createTreeViewer(container);
134
135         RemoteResource resource = Activator.getDefault().getResourceManager()
136                 .getCurrentResourceInSelection();
137
138         updatedRepresentation = new ResourceRepresentation(
139                 resource.getResourceModelRef());
140
141         attViewer.setInput(updatedRepresentation);
142
143         attViewer.expandAll();
144
145         return compLayout;
146     }
147
148     private void createTreeViewer(Composite parent) {
149         Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
150                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
151         addressTree.setHeaderVisible(true);
152
153         attViewer = new TreeViewer(addressTree);
154
155         createAttributeColumns(attViewer);
156
157         // make lines and header visible
158         Tree tree = attViewer.getTree();
159         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
160         tree.setHeaderVisible(true);
161         tree.setLinesVisible(true);
162
163         attViewer.setContentProvider(new AttributeContentProvider());
164         attViewer.setLabelProvider(new AttributeLabelProvider());
165     }
166
167     public void createAttributeColumns(TreeViewer viewer) {
168         Tree tree = viewer.getTree();
169
170         attributeEditor = new AttributeEditingSupport();
171
172         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
173         attName.setWidth(attTblColWidth[0]);
174         attName.setText(attTblHeaders[0]);
175
176         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
177         attValue.setWidth(attTblColWidth[1]);
178         attValue.setText(attTblHeaders[1]);
179         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
180                 attValue);
181         attValueVwrCol.setEditingSupport(attributeEditor
182                 .createAttributeValueEditor(attViewer, this));
183
184         TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
185         updateColumn.setWidth(attTblColWidth[2]);
186         updateColumn.setText(attTblHeaders[2]);
187         TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
188                 updateColumn);
189         updateVwrCol.setEditingSupport(attributeEditor
190                 .createAutomationEditor(attViewer));
191     }
192
193     class AttributeContentProvider implements ITreeContentProvider {
194
195         @Override
196         public void dispose() {
197         }
198
199         @Override
200         public void inputChanged(Viewer viewer, Object oldAttribute,
201                 Object newAttribute) {
202         }
203
204         @Override
205         public Object[] getChildren(Object attribute) {
206             if (attribute instanceof AttributeElement) {
207                 return ((AttributeElement) attribute).getChildren().values()
208                         .toArray();
209             }
210
211             return new Object[0];
212         }
213
214         @Override
215         public Object getParent(Object attribute) {
216             if (attribute instanceof AttributeElement)
217                 return ((AttributeElement) attribute).getParent();
218             return null;
219         }
220
221         @Override
222         public boolean hasChildren(Object attribute) {
223             if (attribute instanceof AttributeElement)
224                 return ((AttributeElement) attribute).hasChildren();
225             return false;
226         }
227
228         @Override
229         public Object[] getElements(Object resourceModel) {
230             if (resourceModel instanceof ResourceRepresentation) {
231                 return ((ResourceRepresentation) resourceModel).getAttributes()
232                         .values().toArray();
233             }
234
235             return new Object[0];
236         }
237     }
238
239     class AttributeLabelProvider implements ITableLabelProvider {
240
241         @Override
242         public void addListener(ILabelProviderListener arg0) {
243         }
244
245         @Override
246         public void dispose() {
247         }
248
249         @Override
250         public boolean isLabelProperty(Object arg0, String arg1) {
251             return false;
252         }
253
254         @Override
255         public void removeListener(ILabelProviderListener arg0) {
256
257         }
258
259         @Override
260         public Image getColumnImage(Object element, int col) {
261             if (col == 2) {
262                 if (element instanceof AttributeElement) {
263
264                     AttributeElement attrElement = (AttributeElement) element;
265                     if (attrElement.isPostSupported()) {
266                         if (attrElement.getPostState()) {
267                             return Activator.getDefault().getImageRegistry()
268                                     .get(Constants.CHECKED);
269                         } else {
270                             return Activator.getDefault().getImageRegistry()
271                                     .get(Constants.UNCHECKED);
272                         }
273                     }
274                 }
275             }
276
277             return null;
278         }
279
280         @Override
281         public String getColumnText(Object element, int column) {
282             if (element instanceof AttributeElement) {
283                 AttributeElement attrElement = (AttributeElement) element;
284                 switch (column) {
285                     case 0: // Attribute name column
286                     {
287                         SimulatorResourceAttribute attribute = attrElement
288                                 .getSimulatorResourceAttribute();
289                         return attribute.name();
290                     }
291
292                     case 1: // Attribute value column
293                     {
294                         SimulatorResourceAttribute attribute = attrElement
295                                 .getSimulatorResourceAttribute();
296
297                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
298                             return Utility.getAttributeValueAsString(attribute
299                                     .value());
300                         return null;
301                     }
302
303                     case 2: {
304                         return "";
305                     }
306                 }
307             }
308             return null;
309         }
310     }
311
312     @Override
313     protected boolean isResizable() {
314         return true;
315     }
316
317     @Override
318     public boolean isHelpAvailable() {
319         return false;
320     }
321
322     @Override
323     protected Button createButton(Composite parent, int id, String label,
324             boolean defaultButton) {
325         if (id == IDialogConstants.OK_ID) {
326             label = "POST";
327         }
328         return super.createButton(parent, id, label, defaultButton);
329     }
330
331     public ResourceRepresentation getUpdatedRepresentation() {
332         return updatedRepresentation;
333     }
334
335     public String getIfType() {
336         if (ifTypes.containsKey(ifType)) {
337             return ifTypes.get(ifType);
338         }
339         return ifType;
340     }
341 }