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