2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.clientcontroller.view.dialogs;
19 import java.util.HashMap;
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.Constants;
27 import oic.simulator.clientcontroller.utils.Utility;
28 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.dialogs.TitleAreaDialog;
32 import org.eclipse.jface.viewers.ILabelProviderListener;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.jface.viewers.TreeViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.ModifyEvent;
40 import org.eclipse.swt.events.ModifyListener;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Combo;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Group;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Tree;
52 import org.eclipse.swt.widgets.TreeColumn;
53 import org.oic.simulator.AttributeValue.ValueType;
54 import org.oic.simulator.SimulatorResourceAttribute;
57 * This dialog is used for generating a POST request.
59 public class PostRequestDialog extends TitleAreaDialog {
61 private TreeViewer attViewer;
62 private Combo ifTypesCmb;
64 private String ifType;
66 private Map<String, String> ifTypes;
68 private AttributeEditingSupport attributeEditor;
70 private ResourceRepresentation updatedRepresentation;
72 private final String[] attTblHeaders = { "Name", "Value",
74 private final Integer[] attTblColWidth = { 200, 200, 50 };
76 public PostRequestDialog(Shell parentShell) {
81 public void create() {
83 setTitle("Generate POST Request");
84 setMessage("Dialog which takes input and generates a post request.");
88 protected Control createDialogArea(Composite parent) {
89 Composite compLayout = (Composite) super.createDialogArea(parent);
91 Group paramsGrp = new Group(compLayout, SWT.NONE);
92 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
93 paramsGrp.setLayoutData(gd);
94 GridLayout layout = new GridLayout(2, false);
95 layout.verticalSpacing = 10;
96 layout.marginTop = 10;
97 paramsGrp.setLayout(layout);
99 Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
100 ifTypeLbl.setText("Interface Type");
102 ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
104 gd.grabExcessHorizontalSpace = true;
105 gd.horizontalAlignment = SWT.FILL;
106 ifTypesCmb.setLayoutData(gd);
107 ifTypesCmb.addModifyListener(new ModifyListener() {
109 public void modifyText(ModifyEvent e) {
110 ifType = ifTypesCmb.getText();
114 // Set the interface types.
115 Map<String, String> ifTypes = Utility.getResourceInterfaces();
116 if (null != ifTypes && !ifTypes.isEmpty()) {
117 this.ifTypes = new HashMap<String, String>();
119 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
120 key = entry.getValue() + " (" + entry.getKey() + ")";
121 this.ifTypes.put(key, entry.getKey());
126 Composite container = new Composite(compLayout, SWT.NONE);
127 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128 layout = new GridLayout(1, false);
129 layout.verticalSpacing = 10;
130 layout.marginTop = 10;
131 container.setLayout(layout);
133 createTreeViewer(container);
135 RemoteResource resource = Activator.getDefault().getResourceManager()
136 .getCurrentResourceInSelection();
138 updatedRepresentation = new ResourceRepresentation(
139 resource.getResourceModelRef());
141 attViewer.setInput(updatedRepresentation);
143 attViewer.expandAll();
148 private void createTreeViewer(Composite parent) {
149 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
150 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
151 addressTree.setHeaderVisible(true);
153 attViewer = new TreeViewer(addressTree);
155 createAttributeColumns(attViewer);
157 // make lines and header visible
158 Tree tree = attViewer.getTree();
159 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
160 tree.setHeaderVisible(true);
161 tree.setLinesVisible(true);
163 attViewer.setContentProvider(new AttributeContentProvider());
164 attViewer.setLabelProvider(new AttributeLabelProvider());
167 public void createAttributeColumns(TreeViewer viewer) {
168 Tree tree = viewer.getTree();
170 attributeEditor = new AttributeEditingSupport();
172 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
173 attName.setWidth(attTblColWidth[0]);
174 attName.setText(attTblHeaders[0]);
176 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
177 attValue.setWidth(attTblColWidth[1]);
178 attValue.setText(attTblHeaders[1]);
179 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
181 attValueVwrCol.setEditingSupport(attributeEditor
182 .createAttributeValueEditor(attViewer, this));
184 TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
185 updateColumn.setWidth(attTblColWidth[2]);
186 updateColumn.setText(attTblHeaders[2]);
187 TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
189 updateVwrCol.setEditingSupport(attributeEditor
190 .createAutomationEditor(attViewer));
193 class AttributeContentProvider implements ITreeContentProvider {
196 public void dispose() {
200 public void inputChanged(Viewer viewer, Object oldAttribute,
201 Object newAttribute) {
205 public Object[] getChildren(Object attribute) {
206 if (attribute instanceof AttributeElement) {
207 return ((AttributeElement) attribute).getChildren().values()
211 return new Object[0];
215 public Object getParent(Object attribute) {
216 if (attribute instanceof AttributeElement)
217 return ((AttributeElement) attribute).getParent();
222 public boolean hasChildren(Object attribute) {
223 if (attribute instanceof AttributeElement)
224 return ((AttributeElement) attribute).hasChildren();
229 public Object[] getElements(Object resourceModel) {
230 if (resourceModel instanceof ResourceRepresentation) {
231 return ((ResourceRepresentation) resourceModel).getAttributes()
235 return new Object[0];
239 class AttributeLabelProvider implements ITableLabelProvider {
242 public void addListener(ILabelProviderListener arg0) {
246 public void dispose() {
250 public boolean isLabelProperty(Object arg0, String arg1) {
255 public void removeListener(ILabelProviderListener arg0) {
260 public Image getColumnImage(Object element, int col) {
262 if (element instanceof AttributeElement) {
264 AttributeElement attrElement = (AttributeElement) element;
265 if (attrElement.isPostSupported()) {
266 if (attrElement.getPostState()) {
267 return Activator.getDefault().getImageRegistry()
268 .get(Constants.CHECKED);
270 return Activator.getDefault().getImageRegistry()
271 .get(Constants.UNCHECKED);
281 public String getColumnText(Object element, int column) {
282 if (element instanceof AttributeElement) {
283 AttributeElement attrElement = (AttributeElement) element;
285 case 0: // Attribute name column
287 SimulatorResourceAttribute attribute = attrElement
288 .getSimulatorResourceAttribute();
289 return attribute.name();
292 case 1: // Attribute value column
294 SimulatorResourceAttribute attribute = attrElement
295 .getSimulatorResourceAttribute();
297 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
298 return Utility.getAttributeValueAsString(attribute
313 protected boolean isResizable() {
318 public boolean isHelpAvailable() {
323 protected Button createButton(Composite parent, int id, String label,
324 boolean defaultButton) {
325 if (id == IDialogConstants.OK_ID) {
328 return super.createButton(parent, id, label, defaultButton);
331 public ResourceRepresentation getUpdatedRepresentation() {
332 return updatedRepresentation;
335 public String getIfType() {
336 if (ifTypes.containsKey(ifType)) {
337 return ifTypes.get(ifType);