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.serviceprovider.view.dialogs;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
25 import oic.simulator.serviceprovider.model.AttributeHelper;
26 import oic.simulator.serviceprovider.model.AttributeHelper.ValidValuesType;
27 import oic.simulator.serviceprovider.utils.Constants;
28 import oic.simulator.serviceprovider.utils.Utility;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.ITreeContentProvider;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.StyledCellLabelProvider;
35 import org.eclipse.jface.viewers.TableViewer;
36 import org.eclipse.jface.viewers.TableViewerColumn;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerCell;
39 import org.eclipse.jface.window.Window;
40 import org.eclipse.jface.wizard.WizardPage;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Table;
51 import org.oic.simulator.AttributeProperty.Type;
53 public class SimpleResourceAddAttributePage extends WizardPage {
55 private TableViewer attTblViewer;
56 private Button addBtn;
57 private Button remBtn;
58 private Button editBtn;
60 private final String[] attTblHeaders = { "Name", "Type", "Range",
61 "Allowed Values", "Default Value" };
62 private final Integer[] attTblColWidth = { 90, 75, 75, 150, 75 };
64 private Set<AttributeHelper> attributes;
66 protected SimpleResourceAddAttributePage() {
67 super("Add Attributes");
68 attributes = new HashSet<AttributeHelper>();
72 public void createControl(Composite parent) {
73 setPageComplete(false);
74 setTitle(Constants.SIMPLE_RESOURCE_ADD_ATTRIBUTE_PAGE_TITLE);
75 setMessage(Constants.SIMPLE_RESOURCE_ADD_ATTRIBUTE_PAGE_MESSAGE);
76 Composite compContent = new Composite(parent, SWT.NONE);
77 GridLayout gridLayout = new GridLayout(2, false);
78 compContent.setLayout(gridLayout);
79 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
80 compContent.setLayoutData(gd);
82 Label lbl = new Label(compContent, SWT.NULL);
83 lbl.setText("Attributes:");
85 gd.horizontalSpan = 2;
86 lbl.setLayoutData(gd);
88 setupAttributeTable(compContent);
90 setupAttributeControls(compContent);
92 setUiListeners(compContent);
94 setControl(compContent);
97 private void setupAttributeTable(Composite attComp) {
98 attTblViewer = new TableViewer(attComp, SWT.MULTI | SWT.H_SCROLL
99 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
101 createAttributeColumns(attTblViewer);
103 // Make lines and header visible
104 Table table = attTblViewer.getTable();
105 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
106 table.setHeaderVisible(true);
107 table.setLinesVisible(true);
109 attTblViewer.setContentProvider(new AttributeContentProvider());
112 public void createAttributeColumns(TableViewer tableViewer) {
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 e = cell.getElement();
124 AttributeHelper att = (AttributeHelper) e;
125 cell.setText(att.getAttributeName());
130 TableViewerColumn attType = new TableViewerColumn(tableViewer, SWT.NONE);
131 attType.getColumn().setWidth(attTblColWidth[1]);
132 attType.getColumn().setText(attTblHeaders[1]);
133 attType.setLabelProvider(new StyledCellLabelProvider() {
135 public void update(ViewerCell cell) {
136 Object e = cell.getElement();
140 AttributeHelper att = (AttributeHelper) e;
141 cell.setText(att.getAttributeType());
145 TableViewerColumn attRange = new TableViewerColumn(tableViewer,
147 attRange.getColumn().setWidth(attTblColWidth[2]);
148 attRange.getColumn().setText(attTblHeaders[2]);
149 attRange.setLabelProvider(new StyledCellLabelProvider() {
151 public void update(ViewerCell cell) {
152 Object e = cell.getElement();
156 AttributeHelper att = (AttributeHelper) e;
157 if (att.getValidValuesType() != ValidValuesType.RANGE) {
160 String min = att.getMin();
161 String max = att.getMax();
162 if (null != min && null != max) {
163 cell.setText("[" + att.getMin() + " to " + att.getMax()
172 TableViewerColumn attAllwdValues = new TableViewerColumn(tableViewer,
174 attAllwdValues.getColumn().setWidth(attTblColWidth[3]);
175 attAllwdValues.getColumn().setText(attTblHeaders[3]);
176 attAllwdValues.setLabelProvider(new StyledCellLabelProvider() {
178 public void update(ViewerCell cell) {
179 Object e = cell.getElement();
183 AttributeHelper att = (AttributeHelper) e;
184 if (att.getValidValuesType() != ValidValuesType.VALUESET) {
187 cell.setText(att.getAllowedValues().toString());
192 TableViewerColumn attDflValue = new TableViewerColumn(tableViewer,
194 attDflValue.getColumn().setWidth(attTblColWidth[4]);
195 attDflValue.getColumn().setText(attTblHeaders[4]);
196 attDflValue.setLabelProvider(new StyledCellLabelProvider() {
198 public void update(ViewerCell cell) {
199 Object e = cell.getElement();
203 AttributeHelper att = (AttributeHelper) e;
204 cell.setText(att.getAttributeDflValue());
209 private void setupAttributeControls(Composite compContent) {
211 Composite innerComp = new Composite(compContent, SWT.NULL);
212 innerComp.setLayout(new GridLayout());
213 GridData gd = new GridData();
214 gd.verticalAlignment = SWT.TOP;
215 innerComp.setLayoutData(gd);
217 addBtn = new Button(innerComp, SWT.PUSH);
218 addBtn.setText("Add");
221 addBtn.setLayoutData(gd);
223 editBtn = new Button(innerComp, SWT.PUSH);
224 editBtn.setText("Edit");
225 editBtn.setEnabled(false);
228 editBtn.setLayoutData(gd);
230 remBtn = new Button(innerComp, SWT.PUSH);
231 remBtn.setText("Remove");
232 remBtn.setEnabled(false);
235 remBtn.setLayoutData(gd);
238 private void setUiListeners(Composite compContent) {
239 addBtn.addSelectionListener(new SelectionAdapter() {
241 public void widgetSelected(SelectionEvent e) {
242 Set<String> rTypes = Utility.getAttributeTypes();
243 AddAttributeDialog dialog = new AddAttributeDialog(Display
244 .getDefault().getActiveShell(), null, rTypes,
246 if (dialog.open() == Window.OK) {
247 AttributeHelper att = dialog.getAttHelper();
249 remBtn.setEnabled(true);
250 AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
251 .getContentProvider();
252 provider.addAttribute(att);
253 attTblViewer.add(att);
254 attTblViewer.getTable().deselectAll();
255 attTblViewer.getTable().select(
256 attTblViewer.getTable().getItemCount() - 1);
257 editBtn.setEnabled(true);
262 editBtn.addSelectionListener(new SelectionAdapter() {
264 public void widgetSelected(SelectionEvent e) {
265 IStructuredSelection selection = (IStructuredSelection) attTblViewer
267 if (null == selection) {
268 editBtn.setEnabled(false);
271 AttributeHelper att = (AttributeHelper) selection
274 editBtn.setEnabled(false);
277 // Opening the dialog for edit operation.
278 // Passing the attribute to populate UI controls with data.
279 Set<String> rTypes = Utility.getAttributeTypes();
280 AddAttributeDialog dialog = new AddAttributeDialog(Display
281 .getDefault().getActiveShell(), att, rTypes, attributes);
282 if (dialog.open() != Window.OK) {
283 AttributeHelper newAtt = dialog.getAttClone();
284 att.setAttributeName(newAtt.getAttributeName());
285 att.setAttributeType(newAtt.getAttributeType());
286 att.setAttributeDflValue(newAtt.getAttributeDflValue());
287 att.setValidValuesType(newAtt.getValidValuesType());
288 att.setMin(newAtt.getMin());
289 att.setMax(newAtt.getMax());
290 att.setAllowedValues(newAtt.getAllowedValues());
292 attTblViewer.update(att, null);
296 remBtn.addSelectionListener(new SelectionAdapter() {
298 public void widgetSelected(SelectionEvent e) {
299 IStructuredSelection selection = (IStructuredSelection) attTblViewer
301 if (null != selection) {
302 Iterator<?> itr = selection.iterator();
303 while (itr.hasNext()) {
304 AttributeHelper att = (AttributeHelper) itr.next();
305 attTblViewer.remove(att);
306 attributes.remove(att);
307 AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
308 .getContentProvider();
309 provider.removeAttribute(att);
311 if (attributes.size() < 1) {
312 remBtn.setEnabled(false);
314 editBtn.setEnabled(false);
320 .addSelectionChangedListener(new ISelectionChangedListener() {
323 public void selectionChanged(SelectionChangedEvent event) {
324 remBtn.setEnabled(true);
325 IStructuredSelection selection = (IStructuredSelection) event
327 if (null == selection) {
330 if (selection.size() > 1) {
331 editBtn.setEnabled(false);
333 editBtn.setEnabled(true);
339 class AttributeContentProvider implements ITreeContentProvider {
341 List<AttributeHelper> attList = new ArrayList<AttributeHelper>();
343 @SuppressWarnings("unchecked")
345 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
346 attList = (List<AttributeHelper>) newInput;
350 public Object[] getChildren(Object element) {
351 return new Object[0];
355 public Object[] getElements(Object input) {
356 return attList.toArray();
360 public Object getParent(Object element) {
365 public boolean hasChildren(Object element) {
370 public void dispose() {
373 public synchronized void addAttribute(AttributeHelper newElement) {
374 attList.add(newElement);
377 public synchronized void removeAttribute(AttributeHelper element) {
378 attList.remove(element);
383 public boolean canFlipToNextPage() {
387 public Set<AttributeHelper> getAttributes() {