2 * Copyright 2016 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.dialogs.TitleAreaDialog;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.viewers.TreeViewerColumn;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Group;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Tree;
38 import org.eclipse.swt.widgets.TreeColumn;
40 import org.oic.simulator.AttributeValue.ValueType;
41 import org.oic.simulator.SimulatorResourceAttribute;
43 import oic.simulator.serviceprovider.Activator;
44 import oic.simulator.serviceprovider.manager.ResourceManager;
45 import oic.simulator.serviceprovider.model.AttributeElement;
46 import oic.simulator.serviceprovider.model.Resource;
47 import oic.simulator.serviceprovider.model.ResourceRepresentation;
48 import oic.simulator.serviceprovider.utils.Utility;
49 import oic.simulator.serviceprovider.view.AttributeEditingSupport;
52 * This class manages and shows the automation settings dialog from the
55 public class ModelArrayAddItemDialog extends TitleAreaDialog {
57 private TreeViewer attViewer;
59 private AttributeEditingSupport attributeEditor;
61 private final String[] attTblHeaders = { "Attribute Name",
63 private final Integer[] attTblColWidth = { 200, 300 };
65 private ResourceManager resourceManager;
67 private ResourceRepresentation representation;
69 public ModelArrayAddItemDialog(Shell parentShell,
70 ResourceRepresentation representation) {
72 resourceManager = Activator.getDefault().getResourceManager();
73 this.representation = representation;
77 public void create() {
79 setTitle("Add Items To Complex Array");
80 setMessage("Add one or more items in the complex array type attribute");
84 protected Control createDialogArea(Composite parent) {
85 Composite compLayout = (Composite) super.createDialogArea(parent);
87 compLayout.setLayout(new GridLayout());
88 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
89 compLayout.setLayoutData(gd);
91 Group attGroup = new Group(compLayout, SWT.NONE);
92 attGroup.setLayout(new GridLayout());
93 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
94 attGroup.setLayoutData(gd);
95 attGroup.setText("Attributes");
97 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
98 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
99 addressTree.setHeaderVisible(true);
101 attViewer = new TreeViewer(addressTree);
103 createAttributeColumns(attViewer);
105 // make lines and header visible
106 Tree tree = attViewer.getTree();
107 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
108 tree.setHeaderVisible(true);
109 tree.setLinesVisible(true);
111 attViewer.setContentProvider(new AttributeContentProvider());
112 attViewer.setLabelProvider(new AttributeLabelProvider());
114 // Check whether there is any resource selected already
115 Resource resource = resourceManager.getCurrentResourceInSelection();
116 if (resource != null) {
117 attViewer.setInput(representation);
118 attViewer.expandAll();
124 public void createAttributeColumns(TreeViewer viewer) {
125 Tree tree = viewer.getTree();
127 attributeEditor = new AttributeEditingSupport();
129 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
130 attName.setWidth(attTblColWidth[0]);
131 attName.setText(attTblHeaders[0]);
133 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
134 attValue.setWidth(attTblColWidth[1]);
135 attValue.setText(attTblHeaders[1]);
137 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
139 attValueVwrCol.setEditingSupport(attributeEditor
140 .createAttributeValueEditor(attViewer, false));
142 addColumnListeners();
145 private void addColumnListeners() {
146 TreeColumn[] columns = attViewer.getTree().getColumns();
147 for (TreeColumn column : columns) {
148 column.addSelectionListener(new SelectionAdapter() {
150 public void widgetSelected(SelectionEvent e) {
151 // Refreshing the viewer. If combo list is open,
152 // then click events on other parts of the view or outside
153 // the combo should hide the editor.
154 // Refreshing the viewer hides the combo and other editors
162 class AttributeContentProvider implements ITreeContentProvider {
165 public void dispose() {
169 public void inputChanged(Viewer viewer, Object oldAttribute,
170 Object newAttribute) {
174 public Object[] getChildren(Object attribute) {
175 if (attribute instanceof AttributeElement) {
176 return ((AttributeElement) attribute).getChildren().values()
180 return new Object[0];
184 public Object getParent(Object attribute) {
185 if (attribute instanceof AttributeElement)
186 return ((AttributeElement) attribute).getParent();
191 public boolean hasChildren(Object attribute) {
192 if (attribute instanceof AttributeElement)
193 return ((AttributeElement) attribute).hasChildren();
198 public Object[] getElements(Object resourceModel) {
199 if (resourceModel instanceof ResourceRepresentation) {
200 return ((ResourceRepresentation) resourceModel).getAttributes()
204 return new Object[0];
208 class AttributeLabelProvider implements ITableLabelProvider {
211 public void addListener(ILabelProviderListener arg0) {
215 public void dispose() {
219 public boolean isLabelProperty(Object arg0, String arg1) {
224 public void removeListener(ILabelProviderListener arg0) {
229 public Image getColumnImage(Object element, int col) {
234 public String getColumnText(Object element, int column) {
235 if (element instanceof AttributeElement) {
236 AttributeElement attrElement = (AttributeElement) element;
238 case 0: // Attribute name column
240 SimulatorResourceAttribute attribute = attrElement
241 .getSimulatorResourceAttribute();
242 return attribute.name();
245 case 1: // Attribute value column
247 SimulatorResourceAttribute attribute = attrElement
248 .getSimulatorResourceAttribute();
250 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
251 String value = Utility
252 .getAttributeValueAsString(attribute
270 protected Point getInitialSize() {
271 Point initialPoint = super.getInitialSize();
272 initialPoint.y += 100;
277 protected boolean isResizable() {
282 public boolean isHelpAvailable() {