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 org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.CellEditor;
21 import org.eclipse.jface.viewers.EditingSupport;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
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.TextCellEditor;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.jface.viewers.ViewerCell;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.part.ViewPart;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
50 import org.oic.simulator.SimulatorException;
52 import oic.simulator.serviceprovider.Activator;
53 import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
54 import oic.simulator.serviceprovider.manager.ResourceManager;
55 import oic.simulator.serviceprovider.manager.UiListenerHandler;
56 import oic.simulator.serviceprovider.model.MetaProperty;
57 import oic.simulator.serviceprovider.model.Resource;
58 import oic.simulator.serviceprovider.model.SingleResource;
59 import oic.simulator.serviceprovider.utils.Constants;
60 import oic.simulator.serviceprovider.utils.Utility;
61 import oic.simulator.serviceprovider.view.dialogs.UpdateResourceInterfaceDialog;
64 * This class manages and shows the meta properties view in the perspective.
66 public class MetaPropertiesView extends ViewPart {
68 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.metaproperties";
70 private TableViewer tableViewer;
72 private final String[] columnHeaders = { "Property", "Value" };
74 private final Integer[] columnWidth = { 150, 150 };
76 private ISelectionChangedListener resourceSelectionChangedListener;
78 private ResourceManager resourceManagerRef;
80 private Set<String> updatedIfSet;
82 private List<MetaProperty> properties;
84 private boolean enable_edit;
85 private Button editBtn;
86 private Button cancelBtn;
88 private Map<String, String> ifTypes;
90 public MetaPropertiesView() {
92 resourceManagerRef = Activator.getDefault().getResourceManager();
94 resourceSelectionChangedListener = new ISelectionChangedListener() {
97 public void onResourceSelectionChange(final Resource resource) {
98 Display.getDefault().asyncExec(new Runnable() {
102 if (null != tableViewer) {
103 properties = getData(resource);
104 updateViewer(properties);
106 updateEditControls(resource);
114 public void createPartControl(final Composite parent) {
115 parent.setLayout(new GridLayout(2, false));
117 tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
118 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
120 createColumns(tableViewer);
122 // Make lines and header visible
123 final Table table = tableViewer.getTable();
124 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
125 gd.horizontalSpan = 2;
126 table.setLayoutData(gd);
127 table.setHeaderVisible(true);
128 table.setLinesVisible(true);
130 tableViewer.setContentProvider(new PropertycontentProvider());
132 editBtn = new Button(parent, SWT.PUSH);
133 editBtn.setText("Edit");
136 editBtn.setLayoutData(gd);
137 editBtn.addSelectionListener(new SelectionAdapter() {
139 public void widgetSelected(SelectionEvent e) {
140 if (editBtn.getText().equals("Edit")) {
141 cancelBtn.setEnabled(true);
142 editBtn.setText("Save");
145 Resource resource = resourceManagerRef
146 .getCurrentResourceInSelection();
147 if (null != resource) {
148 updatedIfSet = resource.getResourceInterfaces();
151 boolean result = false;
152 Resource resourceInSelection = resourceManagerRef
153 .getCurrentResourceInSelection();
154 if (null != resourceInSelection) {
157 result = resourceManagerRef.isPropertyValueInvalid(
158 resourceInSelection, properties,
159 Constants.RESOURCE_URI);
161 MessageDialog.openError(parent.getShell(),
162 "Invalid Resource URI.",
163 Constants.INVALID_URI_MESSAGE);
167 result = resourceManagerRef.isPropertyValueInvalid(
168 resourceInSelection, properties,
169 Constants.RESOURCE_NAME);
171 MessageDialog.openError(parent.getShell(),
173 "Resource Name is invalid.");
177 boolean update = false;
178 boolean uriChange = false;
179 boolean nameChange = false;
180 boolean interfaceChange = false;
182 if (resourceManagerRef.isPropValueChanged(
183 resourceInSelection, properties,
184 Constants.RESOURCE_NAME)) {
188 if (resourceManagerRef.isPropValueChanged(
189 resourceInSelection, properties,
190 Constants.RESOURCE_URI)) {
191 // Check whether the new URI is unique.
192 if (!resourceManagerRef.isUriUnique(properties)) {
193 MessageDialog.openError(parent.getShell(),
194 "Resource URI in use",
195 "Resource URI is in use. Please try a different URI.");
199 if (resourceManagerRef
200 .isResourceStarted(resourceInSelection)) {
201 update = MessageDialog.openQuestion(
202 parent.getShell(), "Save Details",
203 "Resource will be restarted to take the changes."
204 + " Do you want to continue?");
213 // Checking whether any changes made in resource
215 // comparing the current interface set and updated
217 if (null != updatedIfSet) {
218 Set<String> curIfSet = resourceInSelection
219 .getResourceInterfaces();
220 if (curIfSet.size() != updatedIfSet.size()) {
222 interfaceChange = true;
224 Iterator<String> itr = updatedIfSet.iterator();
225 while (itr.hasNext()) {
226 if (!curIfSet.contains(itr.next())) {
228 interfaceChange = true;
236 if (uriChange || nameChange)
239 .getResourceManager()
240 .updateResourceProperties(
242 .getCurrentResourceInSelection(),
243 properties, uriChange,
248 .getResourceManager()
249 .updateResourceInterfaces(
253 } catch (SimulatorException ex) {
257 MessageDialog.openInformation(
258 parent.getShell(), "Operation status",
259 "Failed to update the resource properties.");
261 // Reset the old property values.
262 properties = getData(resourceManagerRef
263 .getCurrentResourceInSelection());
264 updateViewer(properties);
268 cancelBtn.setEnabled(false);
269 editBtn.setText("Edit");
270 setEnableEdit(false);
275 cancelBtn = new Button(parent, SWT.PUSH);
276 cancelBtn.setText("Cancel");
277 cancelBtn.setEnabled(false);
280 cancelBtn.setLayoutData(gd);
281 cancelBtn.addSelectionListener(new SelectionAdapter() {
283 public void widgetSelected(SelectionEvent e) {
284 Resource res = resourceManagerRef
285 .getCurrentResourceInSelection();
287 properties = getData(res);
289 updateViewer(properties);
291 cancelBtn.setEnabled(false);
292 editBtn.setText("Edit");
293 setEnableEdit(false);
294 if (null != updatedIfSet)
295 updatedIfSet.clear();
299 // Get the supported interfaces.
300 Map<String, String> ifTypesSupported = Utility
301 .getResourceInterfaces(SingleResource.class);
302 if (null != ifTypesSupported && !ifTypesSupported.isEmpty()) {
303 ifTypes = new HashMap<String, String>();
305 for (Map.Entry<String, String> entry : ifTypesSupported.entrySet()) {
306 key = entry.getValue() + " (" + entry.getKey() + ")";
307 ifTypes.put(key, entry.getKey());
311 addManagerListeners();
313 // Check whether there is any resource selected already
314 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
315 properties = getData(resource);
316 if (null != properties) {
317 updateViewer(properties);
319 updateEditControls(resource);
322 private void updateEditControls(Object obj) {
323 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
325 if (editBtn.getText().equals("Save")) {
326 editBtn.setText("Edit");
327 setEnableEdit(false);
331 editBtn.setEnabled(false);
333 editBtn.setEnabled(true);
335 cancelBtn.setEnabled(false);
339 private List<MetaProperty> getData(Resource resource) {
340 if (null != resource) {
341 List<MetaProperty> metaPropertyList = resourceManagerRef
342 .getMetaProperties(resource);
343 return metaPropertyList;
349 private void updateViewer(List<MetaProperty> metaPropertyList) {
350 if (null != tableViewer) {
351 Table tbl = tableViewer.getTable();
352 if (tbl.isDisposed()) {
355 if (null != metaPropertyList) {
356 tableViewer.setInput(metaPropertyList.toArray());
357 tbl.setLinesVisible(true);
360 tbl.setLinesVisible(false);
365 public void createColumns(final TableViewer tableViewer) {
366 TableViewerColumn propName = new TableViewerColumn(tableViewer,
368 propName.getColumn().setWidth(columnWidth[0]);
369 propName.getColumn().setText(columnHeaders[0]);
370 propName.setLabelProvider(new StyledCellLabelProvider() {
372 public void update(ViewerCell cell) {
373 MetaProperty prop = (MetaProperty) cell.getElement();
374 cell.setText(prop.getPropName());
379 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
381 propValue.getColumn().setWidth(columnWidth[1]);
382 propValue.getColumn().setText(columnHeaders[1]);
383 propValue.setLabelProvider(new StyledCellLabelProvider() {
385 public void update(ViewerCell cell) {
386 MetaProperty prop = (MetaProperty) cell.getElement();
387 cell.setText(prop.getPropValue());
391 propValue.setEditingSupport(new PropValueEditor(tableViewer));
394 private void addManagerListeners() {
395 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
396 resourceSelectionChangedListener);
399 class PropertycontentProvider implements IStructuredContentProvider {
402 public void dispose() {
406 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
410 public Object[] getElements(Object element) {
411 return (Object[]) element;
416 public void dispose() {
417 // Unregister the listener
418 if (null != resourceSelectionChangedListener) {
419 UiListenerHandler.getInstance()
420 .removeResourceSelectionChangedUIListener(
421 resourceSelectionChangedListener);
426 class PropValueEditor extends EditingSupport {
428 private final TableViewer viewer;
430 public PropValueEditor(TableViewer viewer) {
432 this.viewer = viewer;
436 protected boolean canEdit(Object element) {
441 protected CellEditor getCellEditor(final Object element) {
442 if (!getEnableEdit()) {
445 // Disabling edit for resource type
446 String propName = ((MetaProperty) element).getPropName();
447 if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
451 CellEditor editor = new TextCellEditor(viewer.getTable());
452 if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) {
453 editor.setStyle(SWT.READ_ONLY);
454 final Text txt = (Text) editor.getControl();
455 txt.addModifyListener(new ModifyListener() {
457 public void modifyText(ModifyEvent e) {
458 if (null == updatedIfSet) {
461 // Form the result set of interfaces with check-box that
462 // will be shown in the dialog for editing.
463 Map<String, String> curResInterfaces = new HashMap<String, String>();
464 for (Map.Entry<String, String> entry : ifTypes
466 if (updatedIfSet.contains(entry.getValue())) {
467 curResInterfaces.put(entry.getKey(),
472 // Show the dialog for editing the resource interfaces.
473 UpdateResourceInterfaceDialog ad = new UpdateResourceInterfaceDialog(
474 Display.getDefault().getActiveShell(),
475 curResInterfaces, ifTypes);
476 if (ad.open() == Window.OK) {
477 // Update the local copy of the current resource
478 // interfaces to keep the state for save operation.
479 updatedIfSet.clear();
480 String newPropValue = "";
481 for (Map.Entry<String, String> entry : curResInterfaces
483 if (!newPropValue.isEmpty()) {
484 newPropValue += ", ";
486 String value = ifTypes.get(entry.getKey());
487 newPropValue += value;
489 updatedIfSet.add(value);
492 MetaProperty prop = (MetaProperty) element;
493 prop.setPropValue(newPropValue);
494 // Update the viewer in a separate UI thread.
495 Display.getDefault().asyncExec(new Runnable() {
498 viewer.refresh(element, true);
509 protected Object getValue(Object element) {
510 return ((MetaProperty) element).getPropValue();
514 protected void setValue(Object element, Object value) {
515 MetaProperty prop = (MetaProperty) element;
516 if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
519 prop.setPropValue(String.valueOf(value));
520 viewer.update(element, null);
524 private synchronized Boolean getEnableEdit() {
528 private synchronized void setEnableEdit(boolean value) {
533 public void setFocus() {