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 org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.TitleAreaDialog;
21 import org.eclipse.jface.viewers.ILabelProviderListener;
22 import org.eclipse.jface.viewers.ITableLabelProvider;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.TreeViewer;
25 import org.eclipse.jface.viewers.TreeViewerColumn;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Combo;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Tree;
41 import org.eclipse.swt.widgets.TreeColumn;
43 import java.util.HashMap;
46 import org.oic.simulator.AttributeValue.ValueType;
47 import org.oic.simulator.SimulatorResourceAttribute;
49 import oic.simulator.clientcontroller.Activator;
50 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
51 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
52 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
53 import oic.simulator.clientcontroller.utils.Constants;
54 import oic.simulator.clientcontroller.utils.Utility;
55 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
58 * This dialog is used for generating a POST request.
60 public class PostRequestDialog extends TitleAreaDialog {
62 private TreeViewer attViewer;
63 private Combo ifTypesCmb;
65 private String ifType;
67 private Map<String, String> ifTypes;
69 private AttributeEditingSupport attributeEditor;
71 private ResourceRepresentation updatedRepresentation;
73 private final String[] attTblHeaders = { "Name", "Value",
75 private final Integer[] attTblColWidth = { 200, 200, 50 };
77 public PostRequestDialog(Shell parentShell) {
82 public void create() {
84 setTitle("Generate POST Request");
85 setMessage("Dialog which takes input and generates a post request.");
89 protected Control createDialogArea(Composite parent) {
90 Composite compLayout = (Composite) super.createDialogArea(parent);
92 Group paramsGrp = new Group(compLayout, SWT.NONE);
93 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
94 paramsGrp.setLayoutData(gd);
95 GridLayout layout = new GridLayout(2, false);
96 layout.verticalSpacing = 10;
97 layout.marginTop = 10;
98 paramsGrp.setLayout(layout);
100 Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
101 ifTypeLbl.setText("Interface Type");
103 ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
105 gd.grabExcessHorizontalSpace = true;
106 gd.horizontalAlignment = SWT.FILL;
107 ifTypesCmb.setLayoutData(gd);
108 ifTypesCmb.addModifyListener(new ModifyListener() {
110 public void modifyText(ModifyEvent e) {
111 ifType = ifTypesCmb.getText();
115 // Set the interface types.
116 Map<String, String> ifTypes = Utility.getResourceInterfaces();
117 if (null != ifTypes && !ifTypes.isEmpty()) {
118 this.ifTypes = new HashMap<String, String>();
120 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
121 key = entry.getValue() + " (" + entry.getKey() + ")";
122 this.ifTypes.put(key, entry.getKey());
127 Composite container = new Composite(compLayout, SWT.NONE);
128 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
129 layout = new GridLayout(1, false);
130 layout.verticalSpacing = 10;
131 layout.marginTop = 10;
132 container.setLayout(layout);
134 createTreeViewer(container);
136 RemoteResource resource = Activator.getDefault().getResourceManager()
137 .getCurrentResourceInSelection();
139 updatedRepresentation = new ResourceRepresentation(
140 resource.getResourceModelRef());
142 attViewer.setInput(updatedRepresentation);
144 attViewer.expandAll();
149 private void createTreeViewer(Composite parent) {
150 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
151 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
152 addressTree.setHeaderVisible(true);
154 attViewer = new TreeViewer(addressTree);
156 createAttributeColumns(attViewer);
158 // make lines and header visible
159 Tree tree = attViewer.getTree();
160 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
161 tree.setHeaderVisible(true);
162 tree.setLinesVisible(true);
164 attViewer.setContentProvider(new AttributeContentProvider());
165 attViewer.setLabelProvider(new AttributeLabelProvider());
168 public void createAttributeColumns(TreeViewer viewer) {
169 Tree tree = viewer.getTree();
171 attributeEditor = new AttributeEditingSupport();
173 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
174 attName.setWidth(attTblColWidth[0]);
175 attName.setText(attTblHeaders[0]);
177 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
178 attValue.setWidth(attTblColWidth[1]);
179 attValue.setText(attTblHeaders[1]);
180 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
182 attValueVwrCol.setEditingSupport(attributeEditor
183 .createAttributeValueEditor(attViewer, this));
185 TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
186 updateColumn.setWidth(attTblColWidth[2]);
187 updateColumn.setText(attTblHeaders[2]);
188 TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
190 updateVwrCol.setEditingSupport(attributeEditor
191 .createAutomationEditor(attViewer));
194 class AttributeContentProvider implements ITreeContentProvider {
197 public void dispose() {
201 public void inputChanged(Viewer viewer, Object oldAttribute,
202 Object newAttribute) {
206 public Object[] getChildren(Object attribute) {
207 if (attribute instanceof AttributeElement) {
208 return ((AttributeElement) attribute).getChildren().values()
212 return new Object[0];
216 public Object getParent(Object attribute) {
217 if (attribute instanceof AttributeElement)
218 return ((AttributeElement) attribute).getParent();
223 public boolean hasChildren(Object attribute) {
224 if (attribute instanceof AttributeElement)
225 return ((AttributeElement) attribute).hasChildren();
230 public Object[] getElements(Object resourceModel) {
231 if (resourceModel instanceof ResourceRepresentation) {
232 return ((ResourceRepresentation) resourceModel).getAttributes()
236 return new Object[0];
240 class AttributeLabelProvider implements ITableLabelProvider {
243 public void addListener(ILabelProviderListener arg0) {
247 public void dispose() {
251 public boolean isLabelProperty(Object arg0, String arg1) {
256 public void removeListener(ILabelProviderListener arg0) {
261 public Image getColumnImage(Object element, int col) {
263 if (element instanceof AttributeElement) {
265 AttributeElement attrElement = (AttributeElement) element;
266 if (attrElement.isPostSupported()) {
267 if (attrElement.getPostState()) {
268 return Activator.getDefault().getImageRegistry()
269 .get(Constants.CHECKED);
271 return Activator.getDefault().getImageRegistry()
272 .get(Constants.UNCHECKED);
282 public String getColumnText(Object element, int column) {
283 if (element instanceof AttributeElement) {
284 AttributeElement attrElement = (AttributeElement) element;
286 case 0: // Attribute name column
288 SimulatorResourceAttribute attribute = attrElement
289 .getSimulatorResourceAttribute();
290 return attribute.name();
293 case 1: // Attribute value column
295 SimulatorResourceAttribute attribute = attrElement
296 .getSimulatorResourceAttribute();
298 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
299 return Utility.getAttributeValueAsString(attribute
314 protected boolean isResizable() {
319 public boolean isHelpAvailable() {
324 protected Button createButton(Composite parent, int id, String label,
325 boolean defaultButton) {
326 if (id == IDialogConstants.OK_ID) {
329 return super.createButton(parent, id, label, defaultButton);
332 public ResourceRepresentation getUpdatedRepresentation() {
333 return updatedRepresentation;
336 public String getIfType() {
337 if (ifTypes.containsKey(ifType)) {
338 return ifTypes.get(ifType);