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