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.Utility;
27 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.dialogs.TitleAreaDialog;
31 import org.eclipse.jface.viewers.ILabelProviderListener;
32 import org.eclipse.jface.viewers.ITableLabelProvider;
33 import org.eclipse.jface.viewers.ITreeContentProvider;
34 import org.eclipse.jface.viewers.TreeViewer;
35 import org.eclipse.jface.viewers.TreeViewerColumn;
36 import org.eclipse.jface.viewers.Viewer;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.graphics.Image;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Combo;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Group;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.swt.widgets.Tree;
51 import org.eclipse.swt.widgets.TreeColumn;
52 import org.oic.simulator.AttributeValue.ValueType;
53 import org.oic.simulator.SimulatorResourceAttribute;
56 * This dialog is used for generating a PUT request.
58 public class PutRequestDialog extends TitleAreaDialog {
60 private TreeViewer attViewer;
61 private Combo ifTypesCmb;
63 private String ifType;
65 private Map<String, String> ifTypes;
67 private AttributeEditingSupport attributeEditor;
69 private ResourceRepresentation updatedRepresentation;
71 private final String[] attTblHeaders = { "Name", "Value" };
72 private final Integer[] attTblColWidth = { 200, 200 };
74 public PutRequestDialog(Shell parentShell) {
79 public void create() {
81 setTitle("Generate PUT Request");
82 setMessage("Dialog which takes input and generates a put request.");
86 protected Control createDialogArea(Composite parent) {
87 Composite compLayout = (Composite) super.createDialogArea(parent);
89 Group paramsGrp = new Group(compLayout, SWT.NONE);
90 paramsGrp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91 GridLayout layout = new GridLayout(2, false);
92 layout.verticalSpacing = 10;
93 layout.marginTop = 10;
94 paramsGrp.setLayout(layout);
96 Label ifTypeLbl = new Label(paramsGrp, SWT.NONE);
97 ifTypeLbl.setText("Interface Type");
99 ifTypesCmb = new Combo(paramsGrp, SWT.NULL);
100 GridData gd = new GridData();
101 gd.grabExcessHorizontalSpace = true;
102 gd.horizontalAlignment = SWT.FILL;
103 ifTypesCmb.setLayoutData(gd);
104 ifTypesCmb.addModifyListener(new ModifyListener() {
106 public void modifyText(ModifyEvent e) {
107 ifType = ifTypesCmb.getText();
111 // Set the interface types.
112 Map<String, String> ifTypes = Utility.getResourceInterfaces();
113 if (null != ifTypes && !ifTypes.isEmpty()) {
114 this.ifTypes = new HashMap<String, String>();
116 for (Map.Entry<String, String> entry : ifTypes.entrySet()) {
117 key = entry.getValue() + " (" + entry.getKey() + ")";
118 this.ifTypes.put(key, entry.getKey());
123 Composite container = new Composite(compLayout, SWT.NONE);
124 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
125 layout = new GridLayout(1, false);
126 layout.verticalSpacing = 10;
127 layout.marginTop = 10;
128 container.setLayout(layout);
130 createTreeViewer(container);
132 RemoteResource resource = Activator.getDefault().getResourceManager()
133 .getCurrentResourceInSelection();
135 updatedRepresentation = new ResourceRepresentation(
136 resource.getResourceModelRef());
138 attViewer.setInput(updatedRepresentation);
140 attViewer.expandAll();
145 private void createTreeViewer(Composite parent) {
146 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
147 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
148 addressTree.setHeaderVisible(true);
150 attViewer = new TreeViewer(addressTree);
152 createAttributeColumns(attViewer);
154 // make lines and header visible
155 Tree tree = attViewer.getTree();
156 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
157 tree.setHeaderVisible(true);
158 tree.setLinesVisible(true);
160 attViewer.setContentProvider(new AttributeContentProvider());
161 attViewer.setLabelProvider(new AttributeLabelProvider());
164 public void createAttributeColumns(TreeViewer viewer) {
165 Tree tree = viewer.getTree();
167 attributeEditor = new AttributeEditingSupport();
169 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
170 attName.setWidth(attTblColWidth[0]);
171 attName.setText(attTblHeaders[0]);
173 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
174 attValue.setWidth(attTblColWidth[1]);
175 attValue.setText(attTblHeaders[1]);
176 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
178 attValueVwrCol.setEditingSupport(attributeEditor
179 .createAttributeValueEditor(attViewer, this));
182 class AttributeContentProvider implements ITreeContentProvider {
185 public void dispose() {
189 public void inputChanged(Viewer viewer, Object oldAttribute,
190 Object newAttribute) {
194 public Object[] getChildren(Object attribute) {
195 if (attribute instanceof AttributeElement) {
196 return ((AttributeElement) attribute).getChildren().values()
200 return new Object[0];
204 public Object getParent(Object attribute) {
205 if (attribute instanceof AttributeElement)
206 return ((AttributeElement) attribute).getParent();
211 public boolean hasChildren(Object attribute) {
212 if (attribute instanceof AttributeElement)
213 return ((AttributeElement) attribute).hasChildren();
218 public Object[] getElements(Object resourceModel) {
219 if (resourceModel instanceof ResourceRepresentation) {
220 return ((ResourceRepresentation) resourceModel).getAttributes()
224 return new Object[0];
228 class AttributeLabelProvider implements ITableLabelProvider {
231 public void addListener(ILabelProviderListener arg0) {
235 public void dispose() {
239 public boolean isLabelProperty(Object arg0, String arg1) {
244 public void removeListener(ILabelProviderListener arg0) {
249 public Image getColumnImage(Object element, int col) {
254 public String getColumnText(Object element, int column) {
255 if (element instanceof AttributeElement) {
256 AttributeElement attrElement = (AttributeElement) element;
258 case 0: // Attribute name column
260 SimulatorResourceAttribute attribute = attrElement
261 .getSimulatorResourceAttribute();
262 return attribute.name();
265 case 1: // Attribute value column
267 SimulatorResourceAttribute attribute = attrElement
268 .getSimulatorResourceAttribute();
270 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
271 return Utility.getAttributeValueAsString(attribute
282 protected boolean isResizable() {
287 public boolean isHelpAvailable() {
292 protected Button createButton(Composite parent, int id, String label,
293 boolean defaultButton) {
294 if (id == IDialogConstants.OK_ID) {
297 return super.createButton(parent, id, label, defaultButton);
300 public ResourceRepresentation getUpdatedRepresentation() {
301 return updatedRepresentation;
304 public String getIfType() {
305 if (ifTypes.containsKey(ifType)) {
306 return ifTypes.get(ifType);