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.List;
21 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.TitleAreaDialog;
25 import org.eclipse.jface.viewers.CellEditor;
26 import org.eclipse.jface.viewers.EditingSupport;
27 import org.eclipse.jface.viewers.IStructuredContentProvider;
28 import org.eclipse.jface.viewers.StyledCellLabelProvider;
29 import org.eclipse.jface.viewers.TableViewer;
30 import org.eclipse.jface.viewers.TableViewerColumn;
31 import org.eclipse.jface.viewers.TextCellEditor;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.jface.viewers.ViewerCell;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Table;
44 * This dialog is used for generating a PUT request.
46 public class PutRequestDialog extends TitleAreaDialog {
48 private TableViewer attTblViewer;
50 private final String[] attTblHeaders = { "Name", "Value" };
51 private final Integer[] attTblColWidth = { 200, 200 };
53 private List<PutPostAttributeModel> modelList = null;
55 public PutRequestDialog(Shell parentShell,
56 List<PutPostAttributeModel> modelList) {
58 this.modelList = modelList;
62 public void create() {
64 setTitle("Generate PUT Request");
65 setMessage("Dialog which takes input and generates a put request.");
69 protected Control createDialogArea(Composite parent) {
70 Composite compLayout = (Composite) super.createDialogArea(parent);
71 Composite container = new Composite(compLayout, SWT.NONE);
72 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
73 GridLayout layout = new GridLayout(1, false);
74 layout.verticalSpacing = 10;
75 layout.marginTop = 10;
76 container.setLayout(layout);
78 createTableViewer(container);
80 attTblViewer.setInput(modelList.toArray());
85 private void createTableViewer(Composite parent) {
86 attTblViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
87 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
89 createAttributeColumns(attTblViewer);
91 // make lines and header visible
92 Table table = attTblViewer.getTable();
93 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
94 table.setHeaderVisible(true);
95 table.setLinesVisible(true);
97 attTblViewer.setContentProvider(new AttributeContentProvider());
100 public void createAttributeColumns(TableViewer tableViewer) {
102 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
103 attName.getColumn().setWidth(attTblColWidth[0]);
104 attName.getColumn().setText(attTblHeaders[0]);
105 attName.setLabelProvider(new StyledCellLabelProvider() {
107 public void update(ViewerCell cell) {
108 Object element = cell.getElement();
109 if (element instanceof PutPostAttributeModel) {
110 PutPostAttributeModel entry = (PutPostAttributeModel) element;
111 cell.setText(entry.getAttName());
116 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
118 attValue.getColumn().setWidth(attTblColWidth[1]);
119 attValue.getColumn().setText(attTblHeaders[1]);
120 attValue.setLabelProvider(new StyledCellLabelProvider() {
122 public void update(ViewerCell cell) {
123 Object element = cell.getElement();
124 if (element instanceof PutPostAttributeModel) {
125 PutPostAttributeModel entry = (PutPostAttributeModel) element;
126 cell.setText(entry.getAttValue());
130 attValue.setEditingSupport(new AttributeValueEditor(attTblViewer));
133 class AttributeContentProvider implements IStructuredContentProvider {
136 public void dispose() {
140 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
144 public Object[] getElements(Object element) {
145 return (Object[]) element;
149 class AttributeValueEditor extends EditingSupport {
150 private final TableViewer viewer;
151 private final CellEditor editor;
153 public AttributeValueEditor(TableViewer viewer) {
155 this.viewer = viewer;
156 editor = new TextCellEditor(viewer.getTable());
160 protected boolean canEdit(Object arg0) {
165 protected CellEditor getCellEditor(Object element) {
170 protected Object getValue(Object element) {
171 PutPostAttributeModel model = (PutPostAttributeModel) element;
172 return model.getAttValue();
176 protected void setValue(Object element, Object value) {
177 PutPostAttributeModel model = (PutPostAttributeModel) element;
178 model.setAttValue(String.valueOf(value));
179 viewer.update(element, null);
183 public List<PutPostAttributeModel> getUpdatedModel() {
188 protected boolean isResizable() {
193 public boolean isHelpAvailable() {
198 protected Button createButton(Composite parent, int id, String label,
199 boolean defaultButton) {
200 if (id == IDialogConstants.OK_ID) {
203 return super.createButton(parent, id, label, defaultButton);