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