Displaying and editing the complex value types for attributes.
[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         return compLayout;
95     }
96
97     private void createTreeViewer(Composite parent) {
98         Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
99                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
100         addressTree.setHeaderVisible(true);
101
102         attViewer = new TreeViewer(addressTree);
103
104         createAttributeColumns(attViewer);
105
106         // make lines and header visible
107         Tree tree = attViewer.getTree();
108         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
109         tree.setHeaderVisible(true);
110         tree.setLinesVisible(true);
111
112         attViewer.setContentProvider(new AttributeContentProvider());
113         attViewer.setLabelProvider(new AttributeLabelProvider());
114     }
115
116     public void createAttributeColumns(TreeViewer viewer) {
117         Tree tree = viewer.getTree();
118
119         attributeEditor = new AttributeEditingSupport();
120
121         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
122         attName.setWidth(attTblColWidth[0]);
123         attName.setText(attTblHeaders[0]);
124
125         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
126         attValue.setWidth(attTblColWidth[1]);
127         attValue.setText(attTblHeaders[1]);
128         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
129                 attValue);
130         attValueVwrCol.setEditingSupport(attributeEditor
131                 .createAttributeValueEditor(attViewer, this));
132
133         TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
134         updateColumn.setWidth(attTblColWidth[2]);
135         updateColumn.setText(attTblHeaders[2]);
136         TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
137                 updateColumn);
138         updateVwrCol.setEditingSupport(attributeEditor
139                 .createAutomationEditor(attViewer));
140     }
141
142     class AttributeContentProvider implements ITreeContentProvider {
143
144         @Override
145         public void dispose() {
146         }
147
148         @Override
149         public void inputChanged(Viewer viewer, Object oldAttribute,
150                 Object newAttribute) {
151         }
152
153         @Override
154         public Object[] getChildren(Object attribute) {
155             if (attribute instanceof AttributeElement) {
156                 return ((AttributeElement) attribute).getChildren().values()
157                         .toArray();
158             }
159
160             return new Object[0];
161         }
162
163         @Override
164         public Object getParent(Object attribute) {
165             if (attribute instanceof AttributeElement)
166                 return ((AttributeElement) attribute).getParent();
167             return null;
168         }
169
170         @Override
171         public boolean hasChildren(Object attribute) {
172             if (attribute instanceof AttributeElement)
173                 return ((AttributeElement) attribute).hasChildren();
174             return false;
175         }
176
177         @Override
178         public Object[] getElements(Object resourceModel) {
179             if (resourceModel instanceof ResourceRepresentation) {
180                 return ((ResourceRepresentation) resourceModel).getAttributes()
181                         .values().toArray();
182             }
183
184             return new Object[0];
185         }
186     }
187
188     class AttributeLabelProvider implements ITableLabelProvider {
189
190         @Override
191         public void addListener(ILabelProviderListener arg0) {
192         }
193
194         @Override
195         public void dispose() {
196         }
197
198         @Override
199         public boolean isLabelProperty(Object arg0, String arg1) {
200             return false;
201         }
202
203         @Override
204         public void removeListener(ILabelProviderListener arg0) {
205
206         }
207
208         @Override
209         public Image getColumnImage(Object element, int col) {
210             if (col == 2) {
211                 if (element instanceof AttributeElement) {
212
213                     AttributeElement attrElement = (AttributeElement) element;
214                     if (attrElement.isPostSupported()) {
215                         if (attrElement.getPostState()) {
216                             return Activator.getDefault().getImageRegistry()
217                                     .get(Constants.CHECKED);
218                         } else {
219                             return Activator.getDefault().getImageRegistry()
220                                     .get(Constants.UNCHECKED);
221                         }
222                     }
223                 }
224             }
225
226             return null;
227         }
228
229         @Override
230         public String getColumnText(Object element, int column) {
231             if (element instanceof AttributeElement) {
232                 AttributeElement attrElement = (AttributeElement) element;
233                 switch (column) {
234                     case 0: // Attribute name column
235                     {
236                         SimulatorResourceAttribute attribute = attrElement
237                                 .getSimulatorResourceAttribute();
238                         return attribute.name();
239                     }
240
241                     case 1: // Attribute value column
242                     {
243                         SimulatorResourceAttribute attribute = attrElement
244                                 .getSimulatorResourceAttribute();
245
246                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
247                             return Utility.getAttributeValueAsString(attribute
248                                     .value());
249                         return null;
250                     }
251
252                     case 2: {
253                         return "";
254                     }
255                 }
256             }
257             return null;
258         }
259     }
260
261     @Override
262     protected boolean isResizable() {
263         return true;
264     }
265
266     @Override
267     public boolean isHelpAvailable() {
268         return false;
269     }
270
271     @Override
272     protected Button createButton(Composite parent, int id, String label,
273             boolean defaultButton) {
274         if (id == IDialogConstants.OK_ID) {
275             label = "POST";
276         }
277         return super.createButton(parent, id, label, defaultButton);
278     }
279
280     public ResourceRepresentation getUpdatedRepresentation() {
281         return updatedRepresentation;
282     }
283 }