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 / 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         Composite container = new Composite(compLayout, SWT.NONE);
76         container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77         GridLayout layout = new GridLayout(1, false);
78         layout.verticalSpacing = 10;
79         layout.marginTop = 10;
80         container.setLayout(layout);
81
82         createTreeViewer(container);
83
84         RemoteResource resource = Activator.getDefault().getResourceManager()
85                 .getCurrentResourceInSelection();
86
87         updatedRepresentation = new ResourceRepresentation(
88                 resource.getResourceModelRef());
89
90         attViewer.setInput(updatedRepresentation);
91
92         return compLayout;
93     }
94
95     private void createTreeViewer(Composite parent) {
96         Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
97                 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
98         addressTree.setHeaderVisible(true);
99
100         attViewer = new TreeViewer(addressTree);
101
102         createAttributeColumns(attViewer);
103
104         // make lines and header visible
105         Tree tree = attViewer.getTree();
106         tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
107         tree.setHeaderVisible(true);
108         tree.setLinesVisible(true);
109
110         attViewer.setContentProvider(new AttributeContentProvider());
111         attViewer.setLabelProvider(new AttributeLabelProvider());
112     }
113
114     public void createAttributeColumns(TreeViewer viewer) {
115         Tree tree = viewer.getTree();
116
117         attributeEditor = new AttributeEditingSupport();
118
119         TreeColumn attName = new TreeColumn(tree, SWT.NONE);
120         attName.setWidth(attTblColWidth[0]);
121         attName.setText(attTblHeaders[0]);
122
123         TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
124         attValue.setWidth(attTblColWidth[1]);
125         attValue.setText(attTblHeaders[1]);
126         TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
127                 attValue);
128         attValueVwrCol.setEditingSupport(attributeEditor
129                 .createAttributeValueEditor(attViewer, this));
130     }
131
132     class AttributeContentProvider implements ITreeContentProvider {
133
134         @Override
135         public void dispose() {
136         }
137
138         @Override
139         public void inputChanged(Viewer viewer, Object oldAttribute,
140                 Object newAttribute) {
141         }
142
143         @Override
144         public Object[] getChildren(Object attribute) {
145             if (attribute instanceof AttributeElement) {
146                 return ((AttributeElement) attribute).getChildren().values()
147                         .toArray();
148             }
149
150             return new Object[0];
151         }
152
153         @Override
154         public Object getParent(Object attribute) {
155             if (attribute instanceof AttributeElement)
156                 return ((AttributeElement) attribute).getParent();
157             return null;
158         }
159
160         @Override
161         public boolean hasChildren(Object attribute) {
162             if (attribute instanceof AttributeElement)
163                 return ((AttributeElement) attribute).hasChildren();
164             return false;
165         }
166
167         @Override
168         public Object[] getElements(Object resourceModel) {
169             if (resourceModel instanceof ResourceRepresentation) {
170                 return ((ResourceRepresentation) resourceModel).getAttributes()
171                         .values().toArray();
172             }
173
174             return new Object[0];
175         }
176     }
177
178     class AttributeLabelProvider implements ITableLabelProvider {
179
180         @Override
181         public void addListener(ILabelProviderListener arg0) {
182         }
183
184         @Override
185         public void dispose() {
186         }
187
188         @Override
189         public boolean isLabelProperty(Object arg0, String arg1) {
190             return false;
191         }
192
193         @Override
194         public void removeListener(ILabelProviderListener arg0) {
195
196         }
197
198         @Override
199         public Image getColumnImage(Object element, int col) {
200             return null;
201         }
202
203         @Override
204         public String getColumnText(Object element, int column) {
205             if (element instanceof AttributeElement) {
206                 AttributeElement attrElement = (AttributeElement) element;
207                 switch (column) {
208                     case 0: // Attribute name column
209                     {
210                         SimulatorResourceAttribute attribute = attrElement
211                                 .getSimulatorResourceAttribute();
212                         return attribute.name();
213                     }
214
215                     case 1: // Attribute value column
216                     {
217                         SimulatorResourceAttribute attribute = attrElement
218                                 .getSimulatorResourceAttribute();
219
220                         if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
221                             return Utility.getAttributeValueAsString(attribute
222                                     .value());
223                         return null;
224                     }
225                 }
226             }
227             return null;
228         }
229     }
230
231     @Override
232     protected boolean isResizable() {
233         return true;
234     }
235
236     @Override
237     public boolean isHelpAvailable() {
238         return false;
239     }
240
241     @Override
242     protected Button createButton(Composite parent, int id, String label,
243             boolean defaultButton) {
244         if (id == IDialogConstants.OK_ID) {
245             label = "PUT";
246         }
247         return super.createButton(parent, id, label, defaultButton);
248     }
249
250     public ResourceRepresentation getUpdatedRepresentation() {
251         return updatedRepresentation;
252     }
253
254 }