1 package oic.simulator.serviceprovider.view;
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
8 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
9 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
10 import oic.simulator.serviceprovider.manager.ResourceManager;
11 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
12 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
13 import oic.simulator.serviceprovider.resource.SimulatorResource;
14 import oic.simulator.serviceprovider.utils.Constants;
16 import org.eclipse.jface.viewers.ColumnLabelProvider;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.StyledCellLabelProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.TableViewerColumn;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.jface.viewers.ViewerCell;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.ui.part.ViewPart;
34 public class AttributeView extends ViewPart {
36 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.attribute";
38 private TableViewer attTblViewer;
40 private AttributeEditingSupport attributeEditor;
42 private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
43 private IResourceModelChangedUIListener resourceModelChangedUIListener;
44 private IAutomationUIListener automationUIListener;
46 private final String[] attTblHeaders = { "Name",
47 "Value", "Automation" };
48 private final Integer[] attTblColWidth = { 150, 190,
51 private ResourceManager resourceManager;
53 public AttributeView() {
55 resourceManager = Activator.getDefault().getResourceManager();
57 resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
60 public void onResourceSelectionChange() {
61 Display.getDefault().asyncExec(new Runnable() {
65 if (null != attTblViewer) {
66 updateViewer(getData());
67 SimulatorResource resource = resourceManager
68 .getCurrentResourceInSelection();
69 Table tbl = attTblViewer.getTable();
70 if (!tbl.isDisposed()) {
73 .isResourceAutomationInProgress()) {
74 tbl.setEnabled(false);
85 resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
88 public void onResourceModelChange(
89 final ModelChangeNotificationType notificationType,
90 final String resourceURI,
91 final Set<LocalResourceAttribute> valueChangeSet) {
92 Display.getDefault().asyncExec(new Runnable() {
95 // Handle the notification only if it is for the current
96 // resource in selection
97 SimulatorResource resource = resourceManager
98 .getCurrentResourceInSelection();
99 if (null == resource) {
102 if (!resourceURI.equals(resource.getResourceURI())) {
103 // This notification is for a different resource
104 // whose attributes are not
105 // currently not being shown in UI. So ignoring this
109 // Refresh the table viewers which will display
110 // the updated values
111 if (null != attTblViewer) {
112 if (notificationType == ModelChangeNotificationType.ATTRIBUTE_ADDED
113 || notificationType == ModelChangeNotificationType.ATTRIBUTE_REMOVED) {
114 updateViewer(getData());
115 } else if (notificationType == ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL) {
116 attTblViewer.setInput(null);
117 } else if (notificationType == ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED) {
118 if (null != valueChangeSet) {
120 valueChangeSet.toArray(), null);
129 automationUIListener = new IAutomationUIListener() {
132 public void onResourceAutomationStart(final String resourceURI) {
133 Display.getDefault().asyncExec(new Runnable() {
137 if (null == resourceURI) {
140 SimulatorResource resource = resourceManager
141 .getCurrentResourceInSelection();
142 if (null == resource) {
145 String uri = resource.getResourceURI();
146 // Checking whether attributes view is currently
147 // displaying the attributes of the
148 // resource whose automation has just started
149 if (null != uri && uri.equals(resourceURI)) {
151 tbl = attTblViewer.getTable();
152 if (!tbl.isDisposed()) {
153 attTblViewer.refresh();
155 // Disabling the table to prevent interactions
156 // during the automation
157 tbl.setEnabled(false);
166 public void onAutomationComplete(final String resourceURI,
167 final String attName) {
168 // This method notifies the completion of attribute level
170 Display.getDefault().asyncExec(new Runnable() {
174 if (null == resourceURI) {
177 // Check if the given resourceURI is the uri of the
178 // resource whose attributes are currently being
179 // displayed by this view.
180 SimulatorResource resource = resourceManager
181 .getCurrentResourceInSelection();
182 if (null == resource) {
185 String uri = resource.getResourceURI();
186 if (null == uri || !uri.equals(resourceURI)) {
190 tbl = attTblViewer.getTable();
191 if (!tbl.isDisposed()) {
192 if (null != attName) {
193 // Attribute level automation has stopped
194 LocalResourceAttribute att = resourceManager
195 .getAttributeByResourceURI(resourceURI,
200 attTblViewer.update(att, null);
203 // Resource level automation has stopped
204 // Enabling the table which was disabled at the
205 // beginning of automation
206 tbl.setEnabled(true);
207 attTblViewer.refresh();
217 public void createPartControl(Composite parent) {
218 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
220 parent.setLayout(new GridLayout());
221 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
222 parent.setLayoutData(gd);
224 Group attGroup = new Group(parent, SWT.NONE);
225 attGroup.setLayout(new GridLayout());
226 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
227 attGroup.setLayoutData(gd);
228 attGroup.setText("Attributes");
229 attGroup.setBackground(color);
231 attTblViewer = new TableViewer(attGroup, SWT.SINGLE | SWT.H_SCROLL
232 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
234 createAttributeColumns(attTblViewer);
236 // make lines and header visible
237 Table table = attTblViewer.getTable();
238 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
239 table.setHeaderVisible(true);
240 table.setLinesVisible(true);
242 attTblViewer.setContentProvider(new AttributeContentProvider());
244 addManagerListeners();
246 // Check whether there is any resource selected already
247 List<LocalResourceAttribute> propertyList = getData();
248 if (null != propertyList) {
249 updateViewer(propertyList);
253 public void createAttributeColumns(TableViewer tableViewer) {
255 attributeEditor = new AttributeEditingSupport();
257 TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
258 attName.getColumn().setWidth(attTblColWidth[0]);
259 attName.getColumn().setText(attTblHeaders[0]);
260 attName.setLabelProvider(new StyledCellLabelProvider() {
262 public void update(ViewerCell cell) {
263 Object element = cell.getElement();
264 if (element instanceof LocalResourceAttribute) {
265 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
266 if (null != attribute) {
267 cell.setText(attribute.getAttributeName());
273 TableViewerColumn attValue = new TableViewerColumn(tableViewer,
275 attValue.getColumn().setWidth(attTblColWidth[1]);
276 attValue.getColumn().setText(attTblHeaders[1]);
277 attValue.setLabelProvider(new ColumnLabelProvider() {
279 public String getText(Object element) {
280 if (element instanceof LocalResourceAttribute) {
281 LocalResourceAttribute attribute = (LocalResourceAttribute) element;
282 if (null != attribute) {
283 Object val = attribute.getAttributeValue();
285 return String.valueOf(val);
292 attValue.setEditingSupport(attributeEditor
293 .createAttributeValueEditor(attTblViewer));
295 TableViewerColumn automation = new TableViewerColumn(tableViewer,
297 automation.getColumn().setWidth(attTblColWidth[2]);
298 automation.getColumn().setText(attTblHeaders[2]);
299 automation.setLabelProvider(new ColumnLabelProvider() {
301 public String getText(Object element) {
302 LocalResourceAttribute att = (LocalResourceAttribute) element;
303 if (att.isAutomationInProgress()) {
304 return Constants.ENABLED;
306 return Constants.DISABLED;
310 public Image getImage(Object element) {
311 LocalResourceAttribute att = (LocalResourceAttribute) element;
312 if (att.isAutomationInProgress()) {
313 return Activator.getDefault().getImageRegistry()
314 .get(Constants.CHECKED);
316 return Activator.getDefault().getImageRegistry()
317 .get(Constants.UNCHECKED);
321 automation.setEditingSupport(attributeEditor
322 .createAutomationEditor(attTblViewer));
325 private void addManagerListeners() {
327 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
329 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
330 resourceManager.addAutomationUIListener(automationUIListener);
333 private List<LocalResourceAttribute> getData() {
334 SimulatorResource resourceInSelection = resourceManager
335 .getCurrentResourceInSelection();
336 if (null != resourceInSelection) {
337 List<LocalResourceAttribute> attList = resourceManager
338 .getAttributes(resourceInSelection);
345 private void updateViewer(List<LocalResourceAttribute> attList) {
347 if (null != attList) {
348 tbl = attTblViewer.getTable();
349 if (null != tbl && !tbl.isDisposed()) {
350 tbl.setLinesVisible(true);
351 attTblViewer.setInput(attList.toArray());
354 // Clear the attributes table viewer
355 if (null != attTblViewer) {
356 tbl = attTblViewer.getTable();
357 if (null != tbl && !tbl.isDisposed()) {
358 // tbl.deselectAll();
360 tbl.setLinesVisible(false);
366 class AttributeContentProvider implements IStructuredContentProvider {
369 public void dispose() {
373 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
377 public Object[] getElements(Object element) {
378 return (Object[]) element;
384 public void dispose() {
385 // Unregister the selection listener
386 if (null != resourceSelectionChangedListener) {
388 .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
391 // Unregister the model change listener
392 if (null != resourceModelChangedUIListener) {
394 .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
397 // Unregister the automation complete listener
398 if (null != automationUIListener) {
399 resourceManager.removeAutomationUIListener(automationUIListener);
406 public void setFocus() {