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.Utility;
24 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
26 import org.eclipse.jface.dialogs.IDialogConstants;
27 import org.eclipse.jface.dialogs.TitleAreaDialog;
28 import org.eclipse.jface.viewers.ILabelProviderListener;
29 import org.eclipse.jface.viewers.ITableLabelProvider;
30 import org.eclipse.jface.viewers.ITreeContentProvider;
31 import org.eclipse.jface.viewers.TreeViewer;
32 import org.eclipse.jface.viewers.TreeViewerColumn;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Tree;
43 import org.eclipse.swt.widgets.TreeColumn;
44 import org.oic.simulator.AttributeValue.ValueType;
45 import org.oic.simulator.SimulatorResourceAttribute;
48 * This dialog is used for generating a PUT request.
50 public class PutRequestDialog extends TitleAreaDialog {
52 private TreeViewer attViewer;
54 private AttributeEditingSupport attributeEditor;
56 private ResourceRepresentation updatedRepresentation;
58 private final String[] attTblHeaders = { "Name", "Value" };
59 private final Integer[] attTblColWidth = { 200, 200 };
61 public PutRequestDialog(Shell parentShell) {
66 public void create() {
68 setTitle("Generate PUT Request");
69 setMessage("Dialog which takes input and generates a put request.");
73 protected Control createDialogArea(Composite parent) {
74 Composite compLayout = (Composite) super.createDialogArea(parent);
76 Composite container = new Composite(compLayout, SWT.NONE);
77 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
78 GridLayout layout = new GridLayout(1, false);
79 layout.verticalSpacing = 10;
80 layout.marginTop = 10;
81 container.setLayout(layout);
83 createTreeViewer(container);
85 RemoteResource resource = Activator.getDefault().getResourceManager()
86 .getCurrentResourceInSelection();
88 updatedRepresentation = new ResourceRepresentation(
89 resource.getResourceModelRef());
91 attViewer.setInput(updatedRepresentation);
93 attViewer.expandAll();
98 private void createTreeViewer(Composite parent) {
99 Tree addressTree = new Tree(parent, SWT.SINGLE | SWT.BORDER
100 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
101 addressTree.setHeaderVisible(true);
103 attViewer = new TreeViewer(addressTree);
105 createAttributeColumns(attViewer);
107 // make lines and header visible
108 Tree tree = attViewer.getTree();
109 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
110 tree.setHeaderVisible(true);
111 tree.setLinesVisible(true);
113 attViewer.setContentProvider(new AttributeContentProvider());
114 attViewer.setLabelProvider(new AttributeLabelProvider());
117 public void createAttributeColumns(TreeViewer viewer) {
118 Tree tree = viewer.getTree();
120 attributeEditor = new AttributeEditingSupport();
122 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
123 attName.setWidth(attTblColWidth[0]);
124 attName.setText(attTblHeaders[0]);
126 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
127 attValue.setWidth(attTblColWidth[1]);
128 attValue.setText(attTblHeaders[1]);
129 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
131 attValueVwrCol.setEditingSupport(attributeEditor
132 .createAttributeValueEditor(attViewer, this));
135 class AttributeContentProvider implements ITreeContentProvider {
138 public void dispose() {
142 public void inputChanged(Viewer viewer, Object oldAttribute,
143 Object newAttribute) {
147 public Object[] getChildren(Object attribute) {
148 if (attribute instanceof AttributeElement) {
149 return ((AttributeElement) attribute).getChildren().values()
153 return new Object[0];
157 public Object getParent(Object attribute) {
158 if (attribute instanceof AttributeElement)
159 return ((AttributeElement) attribute).getParent();
164 public boolean hasChildren(Object attribute) {
165 if (attribute instanceof AttributeElement)
166 return ((AttributeElement) attribute).hasChildren();
171 public Object[] getElements(Object resourceModel) {
172 if (resourceModel instanceof ResourceRepresentation) {
173 return ((ResourceRepresentation) resourceModel).getAttributes()
177 return new Object[0];
181 class AttributeLabelProvider implements ITableLabelProvider {
184 public void addListener(ILabelProviderListener arg0) {
188 public void dispose() {
192 public boolean isLabelProperty(Object arg0, String arg1) {
197 public void removeListener(ILabelProviderListener arg0) {
202 public Image getColumnImage(Object element, int col) {
207 public String getColumnText(Object element, int column) {
208 if (element instanceof AttributeElement) {
209 AttributeElement attrElement = (AttributeElement) element;
211 case 0: // Attribute name column
213 SimulatorResourceAttribute attribute = attrElement
214 .getSimulatorResourceAttribute();
215 return attribute.name();
218 case 1: // Attribute value column
220 SimulatorResourceAttribute attribute = attrElement
221 .getSimulatorResourceAttribute();
223 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL)
224 return Utility.getAttributeValueAsString(attribute
235 protected boolean isResizable() {
240 public boolean isHelpAvailable() {
245 protected Button createButton(Composite parent, int id, String label,
246 boolean defaultButton) {
247 if (id == IDialogConstants.OK_ID) {
250 return super.createButton(parent, id, label, defaultButton);
253 public ResourceRepresentation getUpdatedRepresentation() {
254 return updatedRepresentation;