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