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.Utility;
54 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
57 * This dialog is used for generating a PUT request.
59 public class PutRequestDialog 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" };
73 private final Integer[] attTblColWidth = { 200, 200 };
75 public PutRequestDialog(Shell parentShell) {
80 public void create() {
82 setTitle("Generate PUT Request");
83 setMessage("Dialog which takes input and generates a put request.");
87 protected Control createDialogArea(Composite parent) {
88 Composite compLayout = (Composite) super.createDialogArea(parent);
90 Group paramsGrp = new Group(compLayout, SWT.NONE);
91 paramsGrp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
92 GridLayout layout = new GridLayout(2, false);
93 layout.verticalSpacing = 10;
94 layout.marginTop = 10;
95 paramsGrp.setLayout(layout);
97 Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
98 ifTypeLbl.setText("Interface Type");
100 ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
101 GridData gd = new GridData();
102 gd.grabExcessHorizontalSpace = true;
103 gd.horizontalAlignment = SWT.FILL;
104 ifTypesCmb.setLayoutData(gd);
105 ifTypesCmb.addModifyListener(new ModifyListener() {
107 public void modifyText(ModifyEvent e) {
108 ifType = ifTypesCmb.getText();
112 // Set the interface types.
113 Map<String, String> ifTypes = Utility.getResourceInterfaces();
114 if (null != ifTypes && !ifTypes.isEmpty()) {
115 this.ifTypes = new HashMap<String, String>();
117 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
118 key = entry.getValue() + " (" + entry.getKey() + ")";
119 this.ifTypes.put(key, entry.getKey());
124 Composite container = new Composite(compLayout, SWT.NONE);
125 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
126 layout = new GridLayout(1, false);
127 layout.verticalSpacing = 10;
128 layout.marginTop = 10;
129 container.setLayout(layout);
131 createTreeViewer(container);
133 RemoteResource resource = Activator.getDefault().getResourceManager()
134 .getCurrentResourceInSelection();
136 updatedRepresentation = new ResourceRepresentation(
137 resource.getResourceModelRef());
139 attViewer.setInput(updatedRepresentation);
141 attViewer.expandAll();
146 private void createTreeViewer(Composite parent) {
147 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
148 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
149 addressTree.setHeaderVisible(true);
151 attViewer = new TreeViewer(addressTree);
153 createAttributeColumns(attViewer);
155 // make lines and header visible
156 Tree tree = attViewer.getTree();
157 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
158 tree.setHeaderVisible(true);
159 tree.setLinesVisible(true);
161 attViewer.setContentProvider(new AttributeContentProvider());
162 attViewer.setLabelProvider(new AttributeLabelProvider());
165 public void createAttributeColumns(TreeViewer viewer) {
166 Tree tree = viewer.getTree();
168 attributeEditor = new AttributeEditingSupport();
170 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
171 attName.setWidth(attTblColWidth[0]);
172 attName.setText(attTblHeaders[0]);
174 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
175 attValue.setWidth(attTblColWidth[1]);
176 attValue.setText(attTblHeaders[1]);
177 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
179 attValueVwrCol.setEditingSupport(attributeEditor
180 .createAttributeValueEditor(attViewer, this));
183 class AttributeContentProvider implements ITreeContentProvider {
186 public void dispose() {
190 public void inputChanged(Viewer viewer, Object oldAttribute,
191 Object newAttribute) {
195 public Object[] getChildren(Object attribute) {
196 if (attribute instanceof AttributeElement) {
197 return ((AttributeElement) attribute).getChildren().values()
201 return new Object[0];
205 public Object getParent(Object attribute) {
206 if (attribute instanceof AttributeElement)
207 return ((AttributeElement) attribute).getParent();
212 public boolean hasChildren(Object attribute) {
213 if (attribute instanceof AttributeElement)
214 return ((AttributeElement) attribute).hasChildren();
219 public Object[] getElements(Object resourceModel) {
220 if (resourceModel instanceof ResourceRepresentation) {
221 return ((ResourceRepresentation) resourceModel).getAttributes()
225 return new Object[0];
229 class AttributeLabelProvider implements ITableLabelProvider {
232 public void addListener(ILabelProviderListener arg0) {
236 public void dispose() {
240 public boolean isLabelProperty(Object arg0, String arg1) {
245 public void removeListener(ILabelProviderListener arg0) {
250 public Image getColumnImage(Object element, int col) {
255 public String getColumnText(Object element, int column) {
256 if (element instanceof AttributeElement) {
257 AttributeElement attrElement = (AttributeElement) element;
259 case 0: // Attribute name column
261 SimulatorResourceAttribute attribute = attrElement
262 .getSimulatorResourceAttribute();
263 return attribute.name();
266 case 1: // Attribute value column
268 SimulatorResourceAttribute attribute = attrElement
269 .getSimulatorResourceAttribute();
271 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
272 return Utility.getAttributeValueAsString(attribute
283 protected boolean isResizable() {
288 public boolean isHelpAvailable() {
293 protected Button createButton(Composite parent, int id, String label,
294 boolean defaultButton) {
295 if (id == IDialogConstants.OK_ID) {
298 return super.createButton(parent, id, label, defaultButton);
301 public ResourceRepresentation getUpdatedRepresentation() {
302 return updatedRepresentation;
305 public String getIfType() {
306 if (ifTypes.containsKey(ifType)) {
307 return ifTypes.get(ifType);