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);
94 attViewer.expandAll();
99 private void createTreeViewer(Composite parent) {
100 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
101 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
102 addressTree.setHeaderVisible(true);
104 attViewer = new TreeViewer(addressTree);
106 createAttributeColumns(attViewer);
108 // make lines and header visible
109 Tree tree = attViewer.getTree();
110 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
111 tree.setHeaderVisible(true);
112 tree.setLinesVisible(true);
114 attViewer.setContentProvider(new AttributeContentProvider());
115 attViewer.setLabelProvider(new AttributeLabelProvider());
118 public void createAttributeColumns(TreeViewer viewer) {
119 Tree tree = viewer.getTree();
121 attributeEditor = new AttributeEditingSupport();
123 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
124 attName.setWidth(attTblColWidth[0]);
125 attName.setText(attTblHeaders[0]);
127 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
128 attValue.setWidth(attTblColWidth[1]);
129 attValue.setText(attTblHeaders[1]);
130 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
132 attValueVwrCol.setEditingSupport(attributeEditor
133 .createAttributeValueEditor(attViewer, this));
135 TreeColumn updateColumn = new TreeColumn(tree, SWT.NONE);
136 updateColumn.setWidth(attTblColWidth[2]);
137 updateColumn.setText(attTblHeaders[2]);
138 TreeViewerColumn updateVwrCol = new TreeViewerColumn(attViewer,
140 updateVwrCol.setEditingSupport(attributeEditor
141 .createAutomationEditor(attViewer));
144 class AttributeContentProvider implements ITreeContentProvider {
147 public void dispose() {
151 public void inputChanged(Viewer viewer, Object oldAttribute,
152 Object newAttribute) {
156 public Object[] getChildren(Object attribute) {
157 if (attribute instanceof AttributeElement) {
158 return ((AttributeElement) attribute).getChildren().values()
162 return new Object[0];
166 public Object getParent(Object attribute) {
167 if (attribute instanceof AttributeElement)
168 return ((AttributeElement) attribute).getParent();
173 public boolean hasChildren(Object attribute) {
174 if (attribute instanceof AttributeElement)
175 return ((AttributeElement) attribute).hasChildren();
180 public Object[] getElements(Object resourceModel) {
181 if (resourceModel instanceof ResourceRepresentation) {
182 return ((ResourceRepresentation) resourceModel).getAttributes()
186 return new Object[0];
190 class AttributeLabelProvider implements ITableLabelProvider {
193 public void addListener(ILabelProviderListener arg0) {
197 public void dispose() {
201 public boolean isLabelProperty(Object arg0, String arg1) {
206 public void removeListener(ILabelProviderListener arg0) {
211 public Image getColumnImage(Object element, int col) {
213 if (element instanceof AttributeElement) {
215 AttributeElement attrElement = (AttributeElement) element;
216 if (attrElement.isPostSupported()) {
217 if (attrElement.getPostState()) {
218 return Activator.getDefault().getImageRegistry()
219 .get(Constants.CHECKED);
221 return Activator.getDefault().getImageRegistry()
222 .get(Constants.UNCHECKED);
232 public String getColumnText(Object element, int column) {
233 if (element instanceof AttributeElement) {
234 AttributeElement attrElement = (AttributeElement) element;
236 case 0: // Attribute name column
238 SimulatorResourceAttribute attribute = attrElement
239 .getSimulatorResourceAttribute();
240 return attribute.name();
243 case 1: // Attribute value column
245 SimulatorResourceAttribute attribute = attrElement
246 .getSimulatorResourceAttribute();
248 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
249 return Utility.getAttributeValueAsString(attribute
264 protected boolean isResizable() {
269 public boolean isHelpAvailable() {
274 protected Button createButton(Composite parent, int id, String label,
275 boolean defaultButton) {
276 if (id == IDialogConstants.OK_ID) {
279 return super.createButton(parent, id, label, defaultButton);
282 public ResourceRepresentation getUpdatedRepresentation() {
283 return updatedRepresentation;