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.Activator;
22 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
23 import oic.simulator.clientcontroller.utils.Constants;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.dialogs.TitleAreaDialog;
27 import org.eclipse.jface.viewers.CellEditor;
28 import org.eclipse.jface.viewers.CheckboxCellEditor;
29 import org.eclipse.jface.viewers.ColumnLabelProvider;
30 import org.eclipse.jface.viewers.EditingSupport;
31 import org.eclipse.jface.viewers.IStructuredContentProvider;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.StyledCellLabelProvider;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.viewers.TableViewerColumn;
36 import org.eclipse.jface.viewers.TextCellEditor;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerCell;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Shell;
49 import org.eclipse.swt.widgets.Table;
50 import org.eclipse.swt.widgets.Text;
53 * This dialog is used for generating a POST request.
55 public class PostRequestDialog extends TitleAreaDialog {
57 private TableViewer attTblViewer;
59 private final String[] attTblHeaders = { "Name", "Value",
61 private final Integer[] attTblColWidth = { 200, 200, 50 };
63 private List<PutPostAttributeModel> modelList = null;
65 public PostRequestDialog(Shell parentShell,
66 List<PutPostAttributeModel> modelList) {
68 this.modelList = modelList;
72 public void create() {
74 setTitle("Generate POST Request");
75 setMessage("Dialog which takes input and generates a post request.");
79 protected Control createDialogArea(Composite parent) {
80 Composite compLayout = (Composite) super.createDialogArea(parent);
81 Composite container = new Composite(compLayout, SWT.NONE);
82 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
83 GridLayout layout = new GridLayout(1, false);
84 layout.verticalSpacing = 10;
85 layout.marginTop = 10;
86 container.setLayout(layout);
88 createTableViewer(container);
90 attTblViewer.setInput(modelList.toArray());
95 private void createTableViewer(Composite parent) {
96 attTblViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
97 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
99 createAttributeColumns(attTblViewer);
101 // make lines and header visible
102 Table table = attTblViewer.getTable();
103 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
104 table.setHeaderVisible(true);
105 table.setLinesVisible(true);
107 attTblViewer.setContentProvider(new AttributeContentProvider());
110 public void createAttributeColumns(TableViewer tableViewer) {
112 // attributeEditor = new AttributeEditingSupport();
114 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
115 attName.getColumn().setWidth(attTblColWidth[0]);
116 attName.getColumn().setText(attTblHeaders[0]);
117 attName.setLabelProvider(new StyledCellLabelProvider() {
119 public void update(ViewerCell cell) {
120 Object element = cell.getElement();
121 if (element instanceof PutPostAttributeModel) {
122 PutPostAttributeModel entry = (PutPostAttributeModel) element;
123 cell.setText(entry.getAttName());
128 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
130 attValue.getColumn().setWidth(attTblColWidth[1]);
131 attValue.getColumn().setText(attTblHeaders[1]);
132 attValue.setLabelProvider(new StyledCellLabelProvider() {
134 public void update(ViewerCell cell) {
135 Object element = cell.getElement();
136 if (element instanceof PutPostAttributeModel) {
137 PutPostAttributeModel entry = (PutPostAttributeModel) element;
138 cell.setText(entry.getAttValue());
142 attValue.setEditingSupport(new AttributeValueEditor(attTblViewer));
144 TableViewerColumn updateColumn = new TableViewerColumn(tableViewer,
146 updateColumn.getColumn().setWidth(attTblColWidth[2]);
147 updateColumn.getColumn().setText(attTblHeaders[2]);
148 updateColumn.setLabelProvider(new ColumnLabelProvider() {
150 public String getText(Object element) {
155 public Image getImage(Object element) {
156 PutPostAttributeModel model = (PutPostAttributeModel) element;
157 if (model.isModified()) {
158 return Activator.getDefault().getImageRegistry()
159 .get(Constants.CHECKED);
161 return Activator.getDefault().getImageRegistry()
162 .get(Constants.UNCHECKED);
165 updateColumn.setEditingSupport(new UpdateEditor(attTblViewer));
169 protected boolean isResizable() {
174 public boolean isHelpAvailable() {
179 protected Button createButton(Composite parent, int id, String label,
180 boolean defaultButton) {
181 if (id == IDialogConstants.OK_ID) {
184 return super.createButton(parent, id, label, defaultButton);
187 class AttributeContentProvider implements IStructuredContentProvider {
190 public void dispose() {
194 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
198 public Object[] getElements(Object element) {
199 return (Object[]) element;
204 class AttributeValueEditor extends EditingSupport {
205 private final TableViewer viewer;
206 private final CellEditor editor;
207 private final Text txt;
208 public AttributeValueEditor(TableViewer viewer) {
210 this.viewer = viewer;
211 editor = new TextCellEditor(viewer.getTable());
212 txt = (Text)editor.getControl();
214 txt.addModifyListener(new ModifyListener() {
216 public void modifyText(ModifyEvent e) {
217 IStructuredSelection selection = (IStructuredSelection)AttributeValueEditor.this.viewer.getSelection();
218 PutPostAttributeModel att = (PutPostAttributeModel)selection.getFirstElement();
222 String newValue = txt.getText();
223 if(null != newValue && !newValue.isEmpty()) {
224 att.setModified(true);
227 att.setModified(false);
229 AttributeValueEditor.this.viewer.update(att, null);
237 protected boolean canEdit(Object arg0) {
242 protected CellEditor getCellEditor(Object element) {
247 protected Object getValue(Object element) {
248 PutPostAttributeModel model = (PutPostAttributeModel) element;
249 return model.getAttValue();
253 protected void setValue(Object element, Object value) {
254 PutPostAttributeModel model = (PutPostAttributeModel) element;
255 // Compare the actual value and the new value
256 // If there is a change, then its corresponding check box should be
258 String newValue = String.valueOf(value);
259 model.setAttValue(newValue);
260 viewer.update(element, null);
264 class UpdateEditor extends EditingSupport {
266 private final TableViewer viewer;
268 public UpdateEditor(TableViewer viewer) {
270 this.viewer = viewer;
274 protected boolean canEdit(Object arg0) {
279 protected CellEditor getCellEditor(Object element) {
280 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
284 protected Object getValue(Object element) {
285 PutPostAttributeModel model = (PutPostAttributeModel) element;
286 return model.isModified();
290 protected void setValue(Object element, Object value) {
291 PutPostAttributeModel model = (PutPostAttributeModel) element;
292 boolean status = (boolean) value;
293 model.setModified(status);
294 viewer.update(element, null);