Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / view / dialogs / PutRequestDialog.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.clientcontroller.view.dialogs;
18
19 import java.util.List;
20
21 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
22
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;
42
43 /**
44  * This dialog is used for generating a PUT request.
45  */
46 public class PutRequestDialog extends TitleAreaDialog {
47
48     private TableViewer                 attTblViewer;
49
50     private final String[]              attTblHeaders  = { "Name", "Value" };
51     private final Integer[]             attTblColWidth = { 200, 200 };
52
53     private List<PutPostAttributeModel> modelList      = null;
54
55     public PutRequestDialog(Shell parentShell,
56             List<PutPostAttributeModel> modelList) {
57         super(parentShell);
58         this.modelList = modelList;
59     }
60
61     @Override
62     public void create() {
63         super.create();
64         setTitle("Generate PUT Request");
65         setMessage("Dialog which takes input and generates a put request.");
66     }
67
68     @Override
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);
77
78         createTableViewer(container);
79
80         attTblViewer.setInput(modelList.toArray());
81
82         return compLayout;
83     }
84
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);
88
89         createAttributeColumns(attTblViewer);
90
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);
96
97         attTblViewer.setContentProvider(new AttributeContentProvider());
98     }
99
100     public void createAttributeColumns(TableViewer tableViewer) {
101
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() {
106             @Override
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());
112                 }
113             }
114         });
115
116         TableViewerColumn attValue = new TableViewerColumn(tableViewer,
117                 SWT.NONE);
118         attValue.getColumn().setWidth(attTblColWidth[1]);
119         attValue.getColumn().setText(attTblHeaders[1]);
120         attValue.setLabelProvider(new StyledCellLabelProvider() {
121             @Override
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());
127                 }
128             }
129         });
130         attValue.setEditingSupport(new AttributeValueEditor(attTblViewer));
131     }
132
133     class AttributeContentProvider implements IStructuredContentProvider {
134
135         @Override
136         public void dispose() {
137         }
138
139         @Override
140         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
141         }
142
143         @Override
144         public Object[] getElements(Object element) {
145             return (Object[]) element;
146         }
147     }
148
149     class AttributeValueEditor extends EditingSupport {
150         private final TableViewer viewer;
151         private final CellEditor  editor;
152
153         public AttributeValueEditor(TableViewer viewer) {
154             super(viewer);
155             this.viewer = viewer;
156             editor = new TextCellEditor(viewer.getTable());
157         }
158
159         @Override
160         protected boolean canEdit(Object arg0) {
161             return true;
162         }
163
164         @Override
165         protected CellEditor getCellEditor(Object element) {
166             return editor;
167         }
168
169         @Override
170         protected Object getValue(Object element) {
171             PutPostAttributeModel model = (PutPostAttributeModel) element;
172             return model.getAttValue();
173         }
174
175         @Override
176         protected void setValue(Object element, Object value) {
177             PutPostAttributeModel model = (PutPostAttributeModel) element;
178             model.setAttValue(String.valueOf(value));
179             viewer.update(element, null);
180         }
181     }
182
183     public List<PutPostAttributeModel> getUpdatedModel() {
184         return modelList;
185     }
186
187     @Override
188     protected boolean isResizable() {
189         return true;
190     }
191
192     @Override
193     public boolean isHelpAvailable() {
194         return false;
195     }
196
197     @Override
198     protected Button createButton(Composite parent, int id, String label,
199             boolean defaultButton) {
200         if (id == IDialogConstants.OK_ID) {
201             label = "PUT";
202         }
203         return super.createButton(parent, id, label, defaultButton);
204     }
205 }