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.manager.ResourceManager;
23 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
24 import oic.simulator.clientcontroller.utils.Constants;
26 import org.eclipse.jface.dialogs.IDialogConstants;
27 import org.eclipse.jface.dialogs.TitleAreaDialog;
28 import org.eclipse.jface.viewers.CellEditor;
29 import org.eclipse.jface.viewers.CheckboxCellEditor;
30 import org.eclipse.jface.viewers.ColumnLabelProvider;
31 import org.eclipse.jface.viewers.EditingSupport;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
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.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.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.swt.widgets.Table;
50 * This dialog is used for generating a POST request.
52 public class PostRequestDialog extends TitleAreaDialog {
54 private TableViewer attTblViewer;
56 private final String[] attTblHeaders = { "Name", "Value",
58 private final Integer[] attTblColWidth = { 200, 200, 50 };
60 private ResourceManager resourceManager;
62 private List<PutPostAttributeModel> modelList = null;
64 public PostRequestDialog(Shell parentShell,
65 List<PutPostAttributeModel> modelList) {
67 this.modelList = modelList;
68 resourceManager = Activator.getDefault().getResourceManager();
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;
208 public AttributeValueEditor(TableViewer viewer) {
210 this.viewer = viewer;
211 editor = new TextCellEditor(viewer.getTable());
215 protected boolean canEdit(Object arg0) {
220 protected CellEditor getCellEditor(Object element) {
225 protected Object getValue(Object element) {
226 PutPostAttributeModel model = (PutPostAttributeModel) element;
227 return model.getAttValue();
231 protected void setValue(Object element, Object value) {
232 PutPostAttributeModel model = (PutPostAttributeModel) element;
233 // Compare the actual value and the new value
234 // If there is a change, then its corresponding check box should be
236 String newValue = String.valueOf(value);
237 String actualValue = resourceManager.getAttributeValue(
238 resourceManager.getCurrentResourceInSelection(),
240 if (newValue.equals(actualValue)) {
241 model.setModified(false);
243 model.setModified(true);
245 model.setAttValue(newValue);
246 viewer.update(element, null);
250 class UpdateEditor extends EditingSupport {
252 private final TableViewer viewer;
254 public UpdateEditor(TableViewer viewer) {
256 this.viewer = viewer;
260 protected boolean canEdit(Object arg0) {
265 protected CellEditor getCellEditor(Object element) {
266 return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
270 protected Object getValue(Object element) {
271 PutPostAttributeModel model = (PutPostAttributeModel) element;
272 return model.isModified();
276 protected void setValue(Object element, Object value) {
277 PutPostAttributeModel model = (PutPostAttributeModel) element;
278 boolean status = (boolean) value;
279 model.setModified(status);
280 viewer.update(element, null);