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