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.Iterator;
20 import java.util.List;
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.manager.ResourceManager;
24 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
25 import oic.simulator.clientcontroller.utils.Constants;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.dialogs.TitleAreaDialog;
30 import org.eclipse.jface.viewers.CellEditor;
31 import org.eclipse.jface.viewers.CheckboxCellEditor;
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.ComboBoxCellEditor;
34 import org.eclipse.jface.viewers.EditingSupport;
35 import org.eclipse.jface.viewers.IStructuredContentProvider;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.StyledCellLabelProvider;
38 import org.eclipse.jface.viewers.TableViewer;
39 import org.eclipse.jface.viewers.TableViewerColumn;
40 import org.eclipse.jface.viewers.Viewer;
41 import org.eclipse.jface.viewers.ViewerCell;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.custom.CCombo;
44 import org.eclipse.swt.events.ModifyEvent;
45 import org.eclipse.swt.events.ModifyListener;
46 import org.eclipse.swt.graphics.Image;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Display;
53 import org.eclipse.swt.widgets.Shell;
54 import org.eclipse.swt.widgets.Table;
57 * This dialog is used for generating a POST request.
59 public class PostRequestDialog extends TitleAreaDialog {
61 private TableViewer attTblViewer;
63 private final String[] attTblHeaders = { "Name", "Value",
65 private final Integer[] attTblColWidth = { 200, 200, 50 };
67 private List<PutPostAttributeModel> modelList = null;
69 public PostRequestDialog(Shell parentShell,
70 List<PutPostAttributeModel> modelList) {
72 this.modelList = modelList;
76 public void create() {
78 setTitle("Generate POST Request");
79 setMessage("Dialog which takes input and generates a post request.");
83 protected Control createDialogArea(Composite parent) {
84 Composite compLayout = (Composite) super.createDialogArea(parent);
85 Composite container = new Composite(compLayout, SWT.NONE);
86 container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
87 GridLayout layout = new GridLayout(1, false);
88 layout.verticalSpacing = 10;
89 layout.marginTop = 10;
90 container.setLayout(layout);
92 createTableViewer(container);
94 attTblViewer.setInput(modelList.toArray());
99 private void createTableViewer(Composite parent) {
100 attTblViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
101 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
103 createAttributeColumns(attTblViewer);
105 // make lines and header visible
106 Table table = attTblViewer.getTable();
107 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
108 table.setHeaderVisible(true);
109 table.setLinesVisible(true);
111 attTblViewer.setContentProvider(new AttributeContentProvider());
114 public void createAttributeColumns(TableViewer tableViewer) {
116 // attributeEditor = new AttributeEditingSupport();
118 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
119 attName.getColumn().setWidth(attTblColWidth[0]);
120 attName.getColumn().setText(attTblHeaders[0]);
121 attName.setLabelProvider(new StyledCellLabelProvider() {
123 public void update(ViewerCell cell) {
124 Object element = cell.getElement();
125 if (element instanceof PutPostAttributeModel) {
126 PutPostAttributeModel entry = (PutPostAttributeModel) element;
127 cell.setText(entry.getAttName());
132 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
134 attValue.getColumn().setWidth(attTblColWidth[1]);
135 attValue.getColumn().setText(attTblHeaders[1]);
136 attValue.setLabelProvider(new StyledCellLabelProvider() {
138 public void update(ViewerCell cell) {
139 Object element = cell.getElement();
140 if (element instanceof PutPostAttributeModel) {
141 PutPostAttributeModel entry = (PutPostAttributeModel) element;
142 cell.setText(entry.getAttValue());
147 attValue.setEditingSupport(new AttributeValueEditor(attTblViewer));
149 TableViewerColumn updateColumn = new TableViewerColumn(tableViewer,
151 updateColumn.getColumn().setWidth(attTblColWidth[2]);
152 updateColumn.getColumn().setText(attTblHeaders[2]);
153 updateColumn.setLabelProvider(new ColumnLabelProvider() {
155 public String getText(Object element) {
160 public Image getImage(Object element) {
161 PutPostAttributeModel model = (PutPostAttributeModel) element;
162 if (model.isModified()) {
163 return Activator.getDefault().getImageRegistry()
164 .get(Constants.CHECKED);
166 return Activator.getDefault().getImageRegistry()
167 .get(Constants.UNCHECKED);
170 updateColumn.setEditingSupport(new UpdateEditor(attTblViewer));
174 protected boolean isResizable() {
179 public boolean isHelpAvailable() {
184 protected Button createButton(Composite parent, int id, String label,
185 boolean defaultButton) {
186 if (id == IDialogConstants.OK_ID) {
189 return super.createButton(parent, id, label, defaultButton);
192 class AttributeContentProvider implements IStructuredContentProvider {
195 public void dispose() {
199 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
203 public Object[] getElements(Object element) {
204 return (Object[]) element;
209 class AttributeValueEditor extends EditingSupport {
210 private final TableViewer viewer;
211 private CCombo comboBox;
213 public AttributeValueEditor(TableViewer viewer) {
215 this.viewer = viewer;
219 protected boolean canEdit(Object arg0) {
224 protected CellEditor getCellEditor(Object element) {
225 PutPostAttributeModel attributeInSelection = (PutPostAttributeModel) element;
227 String values[] = null;
228 List<String> valueSet = attributeInSelection.getValues();
229 values = convertListToStringArray(valueSet);
231 ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
232 viewer.getTable(), values);
233 comboBox = (CCombo) comboEditor.getControl();
234 if (null != comboBox) {
235 comboBox.addModifyListener(new ModifyListener() {
237 public void modifyText(ModifyEvent e) {
238 IStructuredSelection selection = (IStructuredSelection) AttributeValueEditor.this.viewer
240 PutPostAttributeModel att = (PutPostAttributeModel) selection
245 String newValue = comboBox.getText();
246 if (null != newValue && !newValue.isEmpty()) {
247 att.setModified(true);
249 att.setModified(false);
251 AttributeValueEditor.this.viewer.update(att, null);
259 protected Object getValue(Object element) {
261 PutPostAttributeModel att = (PutPostAttributeModel) element;
262 String valueString = att.getAttValue();
263 List<String> valueSet = att.getValues();
264 if (null != valueSet) {
265 indexOfItem = valueSet.indexOf(valueString);
267 if (indexOfItem == -1) {
274 protected void setValue(Object element, Object value) {
275 PutPostAttributeModel att = (PutPostAttributeModel) element;
278 index = Integer.parseInt(String.valueOf(value));
279 } catch (NumberFormatException nfe) {
284 newValue = comboBox.getText();
285 att.prependNewValue(newValue);
287 newValue = att.getValues().get(index);
289 att.setAttValue(newValue);
290 viewer.update(element, null);
293 public String[] convertListToStringArray(List<String> valueList) {
295 if (null != valueList && valueList.size() > 0) {
296 strArr = valueList.toArray(new String[1]);
298 strArr = new String[1];
304 class UpdateEditor extends EditingSupport {
306 private final TableViewer viewer;
308 public UpdateEditor(TableViewer viewer) {
310 this.viewer = viewer;
314 protected boolean canEdit(Object arg0) {
319 protected CellEditor getCellEditor(Object element) {
320 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
324 protected Object getValue(Object element) {
325 PutPostAttributeModel model = (PutPostAttributeModel) element;
326 return model.isModified();
330 protected void setValue(Object element, Object value) {
331 PutPostAttributeModel model = (PutPostAttributeModel) element;
332 boolean status = (Boolean) value;
333 model.setModified(status);
334 viewer.update(element, null);
339 protected void okPressed() {
341 PutPostAttributeModel attModel;
342 Iterator<PutPostAttributeModel> itr;
343 itr = modelList.iterator();
344 while (itr.hasNext()) {
345 attModel = itr.next();
346 if (null == attModel) {
349 value = attModel.getAttValue();
350 if (null == value || value.isEmpty()) {
351 MessageDialog.openError(Display.getDefault().getActiveShell(),
352 "Empty value", "Attribute value should not be empty.");