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 org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
23 import org.eclipse.jface.viewers.StyledCellLabelProvider;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.TableViewerColumn;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.jface.viewers.ViewerCell;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.jface.wizard.WizardPage;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Table;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
47 import oic.simulator.serviceprovider.model.AttributeHelper;
48 import oic.simulator.serviceprovider.model.AttributeHelper.ValidValuesType;
49 import oic.simulator.serviceprovider.utils.Constants;
50 import oic.simulator.serviceprovider.utils.Utility;
52 public class SimpleResourceAddAttributePage extends WizardPage {
54 private TableViewer attTblViewer;
55 private Button addBtn;
56 private Button remBtn;
57 private Button editBtn;
59 private final String[] attTblHeaders = { "Name", "Type", "Range",
60 "Allowed Values", "Default Value" };
61 private final Integer[] attTblColWidth = { 90, 75, 75, 150, 75 };
63 private Set<AttributeHelper> attributes;
65 protected SimpleResourceAddAttributePage() {
66 super("Add Attributes");
67 attributes = new HashSet<AttributeHelper>();
71 public void createControl(Composite parent) {
72 setPageComplete(false);
73 setTitle(Constants.SIMPLE_RESOURCE_ADD_ATTRIBUTE_PAGE_TITLE);
74 setMessage(Constants.SIMPLE_RESOURCE_ADD_ATTRIBUTE_PAGE_MESSAGE);
75 Composite compContent = new Composite(parent, SWT.NONE);
76 GridLayout gridLayout = new GridLayout(2, false);
77 compContent.setLayout(gridLayout);
78 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
79 compContent.setLayoutData(gd);
81 Label lbl = new Label(compContent, SWT.NULL);
82 lbl.setText("Attributes:");
84 gd.horizontalSpan = 2;
85 lbl.setLayoutData(gd);
87 setupAttributeTable(compContent);
89 setupAttributeControls(compContent);
91 setUiListeners(compContent);
93 setControl(compContent);
96 private void setupAttributeTable(Composite attComp) {
97 attTblViewer = new TableViewer(attComp, SWT.MULTI | SWT.H_SCROLL
98 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
100 createAttributeColumns(attTblViewer);
102 // Make lines and header visible
103 Table table = attTblViewer.getTable();
104 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
105 table.setHeaderVisible(true);
106 table.setLinesVisible(true);
108 attTblViewer.setContentProvider(new AttributeContentProvider());
111 public void createAttributeColumns(TableViewer tableViewer) {
113 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
114 attName.getColumn().setWidth(attTblColWidth[0]);
115 attName.getColumn().setText(attTblHeaders[0]);
116 attName.setLabelProvider(new StyledCellLabelProvider() {
118 public void update(ViewerCell cell) {
119 Object e = cell.getElement();
123 AttributeHelper att = (AttributeHelper) e;
124 cell.setText(att.getAttributeName());
129 TableViewerColumn attType = new TableViewerColumn(tableViewer, SWT.NONE);
130 attType.getColumn().setWidth(attTblColWidth[1]);
131 attType.getColumn().setText(attTblHeaders[1]);
132 attType.setLabelProvider(new StyledCellLabelProvider() {
134 public void update(ViewerCell cell) {
135 Object e = cell.getElement();
139 AttributeHelper att = (AttributeHelper) e;
140 cell.setText(att.getAttributeType());
144 TableViewerColumn attRange = new TableViewerColumn(tableViewer,
146 attRange.getColumn().setWidth(attTblColWidth[2]);
147 attRange.getColumn().setText(attTblHeaders[2]);
148 attRange.setLabelProvider(new StyledCellLabelProvider() {
150 public void update(ViewerCell cell) {
151 Object e = cell.getElement();
155 AttributeHelper att = (AttributeHelper) e;
156 if (att.getValidValuesType() != ValidValuesType.RANGE) {
159 String min = att.getMin();
160 String max = att.getMax();
161 if (null != min && null != max) {
162 cell.setText("[" + att.getMin() + " to " + att.getMax()
171 TableViewerColumn attAllwdValues = new TableViewerColumn(tableViewer,
173 attAllwdValues.getColumn().setWidth(attTblColWidth[3]);
174 attAllwdValues.getColumn().setText(attTblHeaders[3]);
175 attAllwdValues.setLabelProvider(new StyledCellLabelProvider() {
177 public void update(ViewerCell cell) {
178 Object e = cell.getElement();
182 AttributeHelper att = (AttributeHelper) e;
183 if (att.getValidValuesType() != ValidValuesType.VALUESET) {
186 cell.setText(att.getAllowedValues().toString());
191 TableViewerColumn attDflValue = new TableViewerColumn(tableViewer,
193 attDflValue.getColumn().setWidth(attTblColWidth[4]);
194 attDflValue.getColumn().setText(attTblHeaders[4]);
195 attDflValue.setLabelProvider(new StyledCellLabelProvider() {
197 public void update(ViewerCell cell) {
198 Object e = cell.getElement();
202 AttributeHelper att = (AttributeHelper) e;
203 cell.setText(att.getAttributeDflValue());
208 private void setupAttributeControls(Composite compContent) {
210 Composite innerComp = new Composite(compContent, SWT.NULL);
211 innerComp.setLayout(new GridLayout());
212 GridData gd = new GridData();
213 gd.verticalAlignment = SWT.TOP;
214 innerComp.setLayoutData(gd);
216 addBtn = new Button(innerComp, SWT.PUSH);
217 addBtn.setText("Add");
220 addBtn.setLayoutData(gd);
222 editBtn = new Button(innerComp, SWT.PUSH);
223 editBtn.setText("Edit");
224 editBtn.setEnabled(false);
227 editBtn.setLayoutData(gd);
229 remBtn = new Button(innerComp, SWT.PUSH);
230 remBtn.setText("Remove");
231 remBtn.setEnabled(false);
234 remBtn.setLayoutData(gd);
237 private void setUiListeners(Composite compContent) {
238 addBtn.addSelectionListener(new SelectionAdapter() {
240 public void widgetSelected(SelectionEvent e) {
241 Set<String> rTypes = Utility.getAttributeTypes();
242 AddAttributeDialog dialog = new AddAttributeDialog(Display
243 .getDefault().getActiveShell(), null, rTypes,
245 if (dialog.open() == Window.OK) {
246 AttributeHelper att = dialog.getAttHelper();
248 remBtn.setEnabled(true);
249 AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
250 .getContentProvider();
251 provider.addAttribute(att);
252 attTblViewer.add(att);
253 attTblViewer.getTable().deselectAll();
254 attTblViewer.getTable().select(
255 attTblViewer.getTable().getItemCount() - 1);
256 editBtn.setEnabled(true);
261 editBtn.addSelectionListener(new SelectionAdapter() {
263 public void widgetSelected(SelectionEvent e) {
264 IStructuredSelection selection = (IStructuredSelection) attTblViewer
266 if (null == selection) {
267 editBtn.setEnabled(false);
270 AttributeHelper att = (AttributeHelper) selection
273 editBtn.setEnabled(false);
276 // Opening the dialog for edit operation.
277 // Passing the attribute to populate UI controls with data.
278 Set<String> rTypes = Utility.getAttributeTypes();
279 AddAttributeDialog dialog = new AddAttributeDialog(Display
280 .getDefault().getActiveShell(), att, rTypes, attributes);
281 if (dialog.open() != Window.OK) {
282 AttributeHelper newAtt = dialog.getAttClone();
283 att.setAttributeName(newAtt.getAttributeName());
284 att.setAttributeType(newAtt.getAttributeType());
285 att.setAttributeDflValue(newAtt.getAttributeDflValue());
286 att.setValidValuesType(newAtt.getValidValuesType());
287 att.setMin(newAtt.getMin());
288 att.setMax(newAtt.getMax());
289 att.setAllowedValues(newAtt.getAllowedValues());
291 attTblViewer.update(att, null);
295 remBtn.addSelectionListener(new SelectionAdapter() {
297 public void widgetSelected(SelectionEvent e) {
298 IStructuredSelection selection = (IStructuredSelection) attTblViewer
300 if (null != selection) {
301 Iterator<?> itr = selection.iterator();
302 while (itr.hasNext()) {
303 AttributeHelper att = (AttributeHelper) itr.next();
304 attTblViewer.remove(att);
305 attributes.remove(att);
306 AttributeContentProvider provider = (AttributeContentProvider) attTblViewer
307 .getContentProvider();
308 provider.removeAttribute(att);
310 if (attributes.size() < 1) {
311 remBtn.setEnabled(false);
313 editBtn.setEnabled(false);
319 .addSelectionChangedListener(new ISelectionChangedListener() {
322 public void selectionChanged(SelectionChangedEvent event) {
323 remBtn.setEnabled(true);
324 IStructuredSelection selection = (IStructuredSelection) event
326 if (null == selection) {
329 if (selection.size() > 1) {
330 editBtn.setEnabled(false);
332 editBtn.setEnabled(true);
338 class AttributeContentProvider implements ITreeContentProvider {
340 List<AttributeHelper> attList = new ArrayList<AttributeHelper>();
342 @SuppressWarnings("unchecked")
344 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
345 attList = (List<AttributeHelper>) newInput;
349 public Object[] getChildren(Object element) {
350 return new Object[0];
354 public Object[] getElements(Object input) {
355 return attList.toArray();
359 public Object getParent(Object element) {
364 public boolean hasChildren(Object element) {
369 public void dispose() {
372 public synchronized void addAttribute(AttributeHelper newElement) {
373 attList.add(newElement);
376 public synchronized void removeAttribute(AttributeHelper element) {
377 attList.remove(element);
382 public boolean canFlipToNextPage() {
386 public Set<AttributeHelper> getAttributes() {