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;
19 import java.util.List;
22 import oic.simulator.serviceprovider.Activator;
23 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
24 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
25 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
26 import oic.simulator.serviceprovider.manager.ResourceManager;
27 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
28 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
29 import oic.simulator.serviceprovider.resource.SimulatorResource;
30 import oic.simulator.serviceprovider.utils.Constants;
32 import org.eclipse.jface.viewers.ColumnLabelProvider;
33 import org.eclipse.jface.viewers.IStructuredContentProvider;
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.swt.SWT;
40 import org.eclipse.swt.graphics.Color;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.ui.part.ViewPart;
51 * This class manages and shows the attribute view in the perspective.
53 public class AttributeView extends ViewPart {
55 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
57 private TableViewer attTblViewer;
59 private AttributeEditingSupport attributeEditor;
61 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
62 private IResourceModelChangedUIListener resourceModelChangedUIListener;
63 private IAutomationUIListener automationUIListener;
65 private final String[] attTblHeaders = { "Name",
66 "Value", "Automation" };
67 private final Integer[] attTblColWidth = { 150, 190,
70 private ResourceManager resourceManager;
72 public AttributeView() {
74 resourceManager = Activator.getDefault().getResourceManager();
76 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
79 public void onResourceSelectionChange() {
80 Display.getDefault().asyncExec(new Runnable() {
84 if (null != attTblViewer) {
85 updateViewer(getData());
86 SimulatorResource resource = resourceManager
87 .getCurrentResourceInSelection();
88 Table tbl = attTblViewer.getTable();
89 if (!tbl.isDisposed()) {
92 .isResourceAutomationInProgress()) {
93 tbl.setEnabled(false);
104 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
107 public void onResourceModelChange(
108 final ModelChangeNotificationType notificationType,
109 final String resourceURI,
110 final Set<LocalResourceAttribute> valueChangeSet) {
111 Display.getDefault().asyncExec(new Runnable() {
114 // Handle the notification only if it is for the current
115 // resource in selection
116 SimulatorResource resource = resourceManager
117 .getCurrentResourceInSelection();
118 if (null == resource) {
121 if (!resourceURI.equals(resource.getResourceURI())) {
122 // This notification is for a different resource
123 // whose attributes are not
124 // currently not being shown in UI. So ignoring this
128 // Refresh the table viewers which will display
129 // the updated values
130 if (null != attTblViewer) {
131 if (notificationType == ModelChangeNotificationType.ATTRIBUTE_ADDED
132 || notificationType == ModelChangeNotificationType.ATTRIBUTE_REMOVED) {
133 updateViewer(getData());
134 } else if (notificationType == ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL) {
135 attTblViewer.setInput(null);
136 } else if (notificationType == ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED) {
137 if (null != valueChangeSet) {
139 valueChangeSet.toArray(), null);
148 automationUIListener = new IAutomationUIListener() {
151 public void onResourceAutomationStart(final String resourceURI) {
152 Display.getDefault().asyncExec(new Runnable() {
156 if (null == resourceURI) {
159 SimulatorResource resource = resourceManager
160 .getCurrentResourceInSelection();
161 if (null == resource) {
164 String uri = resource.getResourceURI();
165 // Checking whether attributes view is currently
166 // displaying the attributes of the
167 // resource whose automation has just started
168 if (null != uri && uri.equals(resourceURI)) {
170 tbl = attTblViewer.getTable();
171 if (!tbl.isDisposed()) {
172 attTblViewer.refresh();
174 // Disabling the table to prevent interactions
175 // during the automation
176 tbl.setEnabled(false);
185 public void onAutomationComplete(final String resourceURI,
186 final String attName) {
187 // This method notifies the completion of attribute level
189 Display.getDefault().asyncExec(new Runnable() {
193 if (null == resourceURI) {
196 // Check if the given resourceURI is the uri of the
197 // resource whose attributes are currently being
198 // displayed by this view.
199 SimulatorResource resource = resourceManager
200 .getCurrentResourceInSelection();
201 if (null == resource) {
204 String uri = resource.getResourceURI();
205 if (null == uri || !uri.equals(resourceURI)) {
209 tbl = attTblViewer.getTable();
210 if (!tbl.isDisposed()) {
211 if (null != attName) {
212 // Attribute level automation has stopped
213 LocalResourceAttribute att = resourceManager
214 .getAttributeByResourceURI(resourceURI,
219 attTblViewer.update(att, null);
222 // Resource level automation has stopped
223 // Enabling the table which was disabled at the
224 // beginning of automation
225 tbl.setEnabled(true);
226 attTblViewer.refresh();
236 public void createPartControl(Composite parent) {
237 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
239 parent.setLayout(new GridLayout());
240 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
241 parent.setLayoutData(gd);
243 Group attGroup = new Group(parent, SWT.NONE);
244 attGroup.setLayout(new GridLayout());
245 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
246 attGroup.setLayoutData(gd);
247 attGroup.setText("Attributes");
248 attGroup.setBackground(color);
250 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
251 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
253 createAttributeColumns(attTblViewer);
255 // make lines and header visible
256 Table table = attTblViewer.getTable();
257 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
258 table.setHeaderVisible(true);
259 table.setLinesVisible(true);
261 attTblViewer.setContentProvider(new AttributeContentProvider());
263 addManagerListeners();
265 // Check whether there is any resource selected already
266 List<LocalResourceAttribute> propertyList = getData();
267 if (null != propertyList) {
268 updateViewer(propertyList);
272 public void createAttributeColumns(TableViewer tableViewer) {
274 attributeEditor = new AttributeEditingSupport();
276 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
277 attName.getColumn().setWidth(attTblColWidth[0]);
278 attName.getColumn().setText(attTblHeaders[0]);
279 attName.setLabelProvider(new StyledCellLabelProvider() {
281 public void update(ViewerCell cell) {
282 Object element = cell.getElement();
283 if (element instanceof LocalResourceAttribute) {
284 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
285 if (null != attribute) {
286 cell.setText(attribute.getAttributeName());
292 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
294 attValue.getColumn().setWidth(attTblColWidth[1]);
295 attValue.getColumn().setText(attTblHeaders[1]);
296 attValue.setLabelProvider(new ColumnLabelProvider() {
298 public String getText(Object element) {
299 if (element instanceof LocalResourceAttribute) {
300 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
301 if (null != attribute) {
302 Object val = attribute.getAttributeValue();
304 return String.valueOf(val);
311 attValue.setEditingSupport(attributeEditor
312 .createAttributeValueEditor(attTblViewer));
314 TableViewerColumn automation = new TableViewerColumn(tableViewer,
316 automation.getColumn().setWidth(attTblColWidth[2]);
317 automation.getColumn().setText(attTblHeaders[2]);
318 automation.setLabelProvider(new ColumnLabelProvider() {
320 public String getText(Object element) {
321 LocalResourceAttribute att = (LocalResourceAttribute) element;
322 if (att.isAutomationInProgress()) {
323 return Constants.ENABLED;
325 return Constants.DISABLED;
329 public Image getImage(Object element) {
330 LocalResourceAttribute att = (LocalResourceAttribute) element;
331 if (att.isAutomationInProgress()) {
332 return Activator.getDefault().getImageRegistry()
333 .get(Constants.CHECKED);
335 return Activator.getDefault().getImageRegistry()
336 .get(Constants.UNCHECKED);
340 automation.setEditingSupport(attributeEditor
341 .createAutomationEditor(attTblViewer));
344 private void addManagerListeners() {
346 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
348 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
349 resourceManager.addAutomationUIListener(automationUIListener);
352 private List<LocalResourceAttribute> getData() {
353 SimulatorResource resourceInSelection = resourceManager
354 .getCurrentResourceInSelection();
355 if (null != resourceInSelection) {
356 List<LocalResourceAttribute> attList = resourceManager
357 .getAttributes(resourceInSelection);
364 private void updateViewer(List<LocalResourceAttribute> attList) {
366 if (null != attList) {
367 tbl = attTblViewer.getTable();
368 if (null != tbl && !tbl.isDisposed()) {
369 tbl.setLinesVisible(true);
370 attTblViewer.setInput(attList.toArray());
373 // Clear the attributes table viewer
374 if (null != attTblViewer) {
375 tbl = attTblViewer.getTable();
376 if (null != tbl && !tbl.isDisposed()) {
377 // tbl.deselectAll();
379 tbl.setLinesVisible(false);
385 class AttributeContentProvider implements IStructuredContentProvider {
388 public void dispose() {
392 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
396 public Object[] getElements(Object element) {
397 return (Object[]) element;
403 public void dispose() {
404 // Unregister the selection listener
405 if (null != resourceSelectionChangedListener) {
407 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
410 // Unregister the model change listener
411 if (null != resourceModelChangedUIListener) {
413 .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
416 // Unregister the automation complete listener
417 if (null != automationUIListener) {
418 resourceManager.removeAutomationUIListener(automationUIListener);
425 public void setFocus() {