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 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;
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;
49 * This dialog is used for generating a POST request.
51 public class PostRequestDialog extends TitleAreaDialog {
53 private TreeViewer attViewer;
55 private AttributeEditingSupport attributeEditor;
57 private ResourceRepresentation updatedRepresentation;
59 private final String[] attTblHeaders = { "Name", "Value",
61 private final Integer[] attTblColWidth = { 200, 200, 50 };
63 public PostRequestDialog(Shell parentShell) {
68 public void create() {
70 setTitle("Generate POST Request");
71 setMessage("Dialog which takes input and generates a post request.");
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);
84 createTreeViewer(container);
86 RemoteResource resource = Activator.getDefault().getResourceManager()
87 .getCurrentResourceInSelection();
89 updatedRepresentation = new ResourceRepresentation(
90 resource.getResourceModelRef());
92 attViewer.setInput(updatedRepresentation);
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);
102 attViewer = new TreeViewer(addressTree);
104 createAttributeColumns(attViewer);
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);
112 attViewer.setContentProvider(new AttributeContentProvider());
113 attViewer.setLabelProvider(new AttributeLabelProvider());
116 public void createAttributeColumns(TreeViewer viewer) {
117 Tree tree = viewer.getTree();
119 attributeEditor = new AttributeEditingSupport();
121 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
122 attName.setWidth(attTblColWidth[0]);
123 attName.setText(attTblHeaders[0]);
125 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
126 attValue.setWidth(attTblColWidth[1]);
127 attValue.setText(attTblHeaders[1]);
128 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
130 attValueVwrCol.setEditingSupport(attributeEditor
131 .createAttributeValueEditor(attViewer, this));
133 TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
134 updateColumn.setWidth(attTblColWidth[2]);
135 updateColumn.setText(attTblHeaders[2]);
136 TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
138 updateVwrCol.setEditingSupport(attributeEditor
139 .createAutomationEditor(attViewer));
142 class AttributeContentProvider implements ITreeContentProvider {
145 public void dispose() {
149 public void inputChanged(Viewer viewer, Object oldAttribute,
150 Object newAttribute) {
154 public Object[] getChildren(Object attribute) {
155 if (attribute instanceof AttributeElement) {
156 return ((AttributeElement) attribute).getChildren().values()
160 return new Object[0];
164 public Object getParent(Object attribute) {
165 if (attribute instanceof AttributeElement)
166 return ((AttributeElement) attribute).getParent();
171 public boolean hasChildren(Object attribute) {
172 if (attribute instanceof AttributeElement)
173 return ((AttributeElement) attribute).hasChildren();
178 public Object[] getElements(Object resourceModel) {
179 if (resourceModel instanceof ResourceRepresentation) {
180 return ((ResourceRepresentation) resourceModel).getAttributes()
184 return new Object[0];
188 class AttributeLabelProvider implements ITableLabelProvider {
191 public void addListener(ILabelProviderListener arg0) {
195 public void dispose() {
199 public boolean isLabelProperty(Object arg0, String arg1) {
204 public void removeListener(ILabelProviderListener arg0) {
209 public Image getColumnImage(Object element, int col) {
211 if (element instanceof AttributeElement) {
213 AttributeElement attrElement = (AttributeElement) element;
214 if (attrElement.isPostSupported()) {
215 if (attrElement.getPostState()) {
216 return Activator.getDefault().getImageRegistry()
217 .get(Constants.CHECKED);
219 return Activator.getDefault().getImageRegistry()
220 .get(Constants.UNCHECKED);
230 public String getColumnText(Object element, int column) {
231 if (element instanceof AttributeElement) {
232 AttributeElement attrElement = (AttributeElement) element;
234 case 0: // Attribute name column
236 SimulatorResourceAttribute attribute = attrElement
237 .getSimulatorResourceAttribute();
238 return attribute.name();
241 case 1: // Attribute value column
243 SimulatorResourceAttribute attribute = attrElement
244 .getSimulatorResourceAttribute();
246 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
247 return Utility.getAttributeValueAsString(attribute
262 protected boolean isResizable() {
267 public boolean isHelpAvailable() {
272 protected Button createButton(Composite parent, int id, String label,
273 boolean defaultButton) {
274 if (id == IDialogConstants.OK_ID) {
277 return super.createButton(parent, id, label, defaultButton);
280 public ResourceRepresentation getUpdatedRepresentation() {
281 return updatedRepresentation;