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.clientcontroller.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;
42 import org.oic.simulator.AttributeValue.ValueType;
43 import org.oic.simulator.SimulatorResourceAttribute;
45 import oic.simulator.clientcontroller.Activator;
46 import oic.simulator.clientcontroller.manager.ResourceManager;
47 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
48 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
49 import oic.simulator.clientcontroller.remoteresource.ResourceRepresentation;
50 import oic.simulator.clientcontroller.utils.Utility;
51 import oic.simulator.clientcontroller.view.AttributeEditingSupport;
54 * This class manages and shows the automation settings dialog from the
57 public class ModelArrayAddItemDialog extends TitleAreaDialog {
59 private TreeViewer attViewer;
61 private AttributeEditingSupport attributeEditor;
63 private final String[] attTblHeaders = { "Attribute Name",
65 private final Integer[] attTblColWidth = { 200, 300 };
67 private ResourceManager resourceManager;
69 private ResourceRepresentation representation;
71 private TitleAreaDialog dialog;
73 public ModelArrayAddItemDialog(Shell parentShell, TitleAreaDialog dialog,
74 ResourceRepresentation representation) {
76 resourceManager = Activator.getDefault().getResourceManager();
78 this.representation = representation;
82 public void create() {
84 setTitle("Add Items To Complex Array");
85 setMessage("Add one or more items in the complex array type attribute");
89 protected Control createDialogArea(Composite parent) {
90 Composite compLayout = (Composite) super.createDialogArea(parent);
92 compLayout.setLayout(new GridLayout());
93 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
94 compLayout.setLayoutData(gd);
96 Group attGroup = new Group(compLayout, SWT.NONE);
97 attGroup.setLayout(new GridLayout());
98 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
99 attGroup.setLayoutData(gd);
100 attGroup.setText("Attributes");
102 Tree addressTree = new Tree(attGroup, SWT.SINGLE | SWT.BORDER
103 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
104 addressTree.setHeaderVisible(true);
106 attViewer = new TreeViewer(addressTree);
108 createAttributeColumns(attViewer);
110 // make lines and header visible
111 Tree tree = attViewer.getTree();
112 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
113 tree.setHeaderVisible(true);
114 tree.setLinesVisible(true);
116 attViewer.setContentProvider(new AttributeContentProvider());
117 attViewer.setLabelProvider(new AttributeLabelProvider());
119 // Check whether there is any resource selected already
120 RemoteResource resource = resourceManager
121 .getCurrentResourceInSelection();
122 if (resource != null) {
123 attViewer.setInput(representation);
124 attViewer.expandAll();
130 public void createAttributeColumns(TreeViewer viewer) {
131 Tree tree = viewer.getTree();
133 attributeEditor = new AttributeEditingSupport();
135 TreeColumn attName = new TreeColumn(tree, SWT.NONE);
136 attName.setWidth(attTblColWidth[0]);
137 attName.setText(attTblHeaders[0]);
139 TreeColumn attValue = new TreeColumn(tree, SWT.NONE);
140 attValue.setWidth(attTblColWidth[1]);
141 attValue.setText(attTblHeaders[1]);
143 TreeViewerColumn attValueVwrCol = new TreeViewerColumn(attViewer,
145 attValueVwrCol.setEditingSupport(attributeEditor
146 .createAttributeValueEditor(attViewer, dialog));
148 addColumnListeners();
151 private void addColumnListeners() {
152 TreeColumn[] columns = attViewer.getTree().getColumns();
153 for (TreeColumn column : columns) {
154 column.addSelectionListener(new SelectionAdapter() {
156 public void widgetSelected(SelectionEvent e) {
157 // Refreshing the viewer. If combo list is open,
158 // then click events on other parts of the view or outside
159 // the combo should hide the editor.
160 // Refreshing the viewer hides the combo and other editors
168 class AttributeContentProvider implements ITreeContentProvider {
171 public void dispose() {
175 public void inputChanged(Viewer viewer, Object oldAttribute,
176 Object newAttribute) {
180 public Object[] getChildren(Object attribute) {
181 if (attribute instanceof AttributeElement) {
182 Map<String, AttributeElement> children = ((AttributeElement) attribute)
184 if (null != children) {
185 return children.values().toArray();
189 return new Object[0];
193 public Object getParent(Object attribute) {
194 if (attribute instanceof AttributeElement)
195 return ((AttributeElement) attribute).getParent();
200 public boolean hasChildren(Object attribute) {
201 if (attribute instanceof AttributeElement)
202 return ((AttributeElement) attribute).hasChildren();
207 public Object[] getElements(Object resourceModel) {
208 if (resourceModel instanceof ResourceRepresentation) {
209 return ((ResourceRepresentation) resourceModel).getAttributes()
213 return new Object[0];
217 class AttributeLabelProvider implements ITableLabelProvider {
220 public void addListener(ILabelProviderListener arg0) {
224 public void dispose() {
228 public boolean isLabelProperty(Object arg0, String arg1) {
233 public void removeListener(ILabelProviderListener arg0) {
238 public Image getColumnImage(Object element, int col) {
243 public String getColumnText(Object element, int column) {
244 if (element instanceof AttributeElement) {
245 AttributeElement attrElement = (AttributeElement) element;
247 case 0: // Attribute name column
249 SimulatorResourceAttribute attribute = attrElement
250 .getSimulatorResourceAttribute();
251 return attribute.name();
254 case 1: // Attribute value column
256 SimulatorResourceAttribute attribute = attrElement
257 .getSimulatorResourceAttribute();
259 if (attribute.value().typeInfo().mBaseType != ValueType.RESOURCEMODEL) {
260 String value = Utility
261 .getAttributeValueAsString(attribute
279 protected Point getInitialSize() {
280 Point initialPoint = super.getInitialSize();
281 initialPoint.y += 100;
286 protected boolean isResizable() {
291 public boolean isHelpAvailable() {