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 Resource resourceInSelection = resourceManagerRef
152 .getCurrentResourceInSelection();
153 if (null != resourceInSelection) {
156 boolean result = resourceManagerRef
157 .isPropertyValueInvalid(resourceInSelection,
158 properties, Constants.RESOURCE_URI);
160 MessageDialog.openError(parent.getShell(),
161 "Invalid Resource URI.",
162 Constants.INVALID_URI_MESSAGE);
166 result = resourceManagerRef.isPropertyValueInvalid(
167 resourceInSelection, properties,
168 Constants.RESOURCE_NAME);
170 MessageDialog.openError(parent.getShell(),
172 "Resource Name is invalid.");
176 result = resourceManagerRef.isPropertyValueInvalid(
177 resourceInSelection, properties,
178 Constants.RESOURCE_TYPE);
180 MessageDialog.openError(parent.getShell(),
181 "Invalid Resource Type.",
182 Constants.INVALID_RESOURCE_TYPE_MESSAGE);
186 boolean update = false;
187 boolean uriChange = false;
188 boolean typeChange = false;
189 boolean nameChange = false;
190 boolean interfaceChange = false;
192 if (resourceManagerRef.isPropValueChanged(
193 resourceInSelection, properties,
194 Constants.RESOURCE_NAME)) {
199 if (resourceManagerRef.isPropValueChanged(
200 resourceInSelection, properties,
201 Constants.RESOURCE_URI)) {
202 // Check whether the new URI is unique.
203 if (!resourceManagerRef.isUriUnique(properties)) {
204 MessageDialog.openError(parent.getShell(),
205 "Resource URI in use",
206 "Resource URI is in use. Please try a different URI.");
214 if (resourceManagerRef.isPropValueChanged(
215 resourceInSelection, properties,
216 Constants.RESOURCE_TYPE)) {
220 // Checking whether any changes made in resource
222 // comparing the current interface set and updated
224 Set<String> curIfSet = resourceInSelection
225 .getResourceInterfaces();
226 if (null != curIfSet && null != updatedIfSet) {
227 if (curIfSet.size() != updatedIfSet.size()) {
229 interfaceChange = true;
231 Iterator<String> itr = updatedIfSet.iterator();
232 while (itr.hasNext()) {
233 if (!curIfSet.contains(itr.next())) {
235 interfaceChange = true;
242 if (uriChange || typeChange || interfaceChange) {
243 if (resourceManagerRef
244 .isResourceStarted(resourceInSelection)) {
245 update = MessageDialog.openQuestion(
248 "Resource will be restarted to take the changes."
249 + " Do you want to continue?");
256 if (uriChange || nameChange || typeChange)
259 .getResourceManager()
260 .updateResourceProperties(
262 .getCurrentResourceInSelection(),
263 properties, uriChange,
264 nameChange, typeChange);
268 .getResourceManager()
269 .updateResourceInterfaces(
273 } catch (SimulatorException ex) {
277 MessageDialog.openInformation(
278 parent.getShell(), "Operation status",
279 "Failed to update the resource properties.");
281 // Reset the old property values.
282 properties = getData(resourceManagerRef
283 .getCurrentResourceInSelection());
284 updateViewer(properties);
288 cancelBtn.setEnabled(false);
289 editBtn.setText("Edit");
290 setEnableEdit(false);
295 cancelBtn = new Button(parent, SWT.PUSH);
296 cancelBtn.setText("Cancel");
297 cancelBtn.setEnabled(false);
300 cancelBtn.setLayoutData(gd);
301 cancelBtn.addSelectionListener(new SelectionAdapter() {
303 public void widgetSelected(SelectionEvent e) {
304 Resource res = resourceManagerRef
305 .getCurrentResourceInSelection();
307 properties = getData(res);
309 updateViewer(properties);
311 cancelBtn.setEnabled(false);
312 editBtn.setText("Edit");
313 setEnableEdit(false);
314 if (null != updatedIfSet)
315 updatedIfSet.clear();
319 // Get the supported interfaces.
320 Map<String, String> ifTypesSupported = Utility
321 .getResourceInterfaces(SingleResource.class);
322 if (null != ifTypesSupported && !ifTypesSupported.isEmpty()) {
323 ifTypes = new HashMap<String, String>();
325 for (Map.Entry<String, String> entry : ifTypesSupported.entrySet()) {
326 key = entry.getValue() + " (" + entry.getKey() + ")";
327 ifTypes.put(key, entry.getKey());
331 addManagerListeners();
333 // Check whether there is any resource selected already
334 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
335 properties = getData(resource);
336 if (null != properties) {
337 updateViewer(properties);
339 updateEditControls(resource);
342 private void updateEditControls(Object obj) {
343 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
345 if (editBtn.getText().equals("Save")) {
346 editBtn.setText("Edit");
347 setEnableEdit(false);
351 editBtn.setEnabled(false);
353 editBtn.setEnabled(true);
355 cancelBtn.setEnabled(false);
359 private List<MetaProperty> getData(Resource resource) {
360 if (null != resource) {
361 List<MetaProperty> metaPropertyList = resourceManagerRef
362 .getMetaProperties(resource);
363 return metaPropertyList;
369 private void updateViewer(List<MetaProperty> metaPropertyList) {
370 if (null != tableViewer) {
371 Table tbl = tableViewer.getTable();
372 if (tbl.isDisposed()) {
375 if (null != metaPropertyList) {
376 tableViewer.setInput(metaPropertyList.toArray());
377 tbl.setLinesVisible(true);
380 tbl.setLinesVisible(false);
385 public void createColumns(final TableViewer tableViewer) {
386 TableViewerColumn propName = new TableViewerColumn(tableViewer,
388 propName.getColumn().setWidth(columnWidth[0]);
389 propName.getColumn().setText(columnHeaders[0]);
390 propName.setLabelProvider(new StyledCellLabelProvider() {
392 public void update(ViewerCell cell) {
393 MetaProperty prop = (MetaProperty) cell.getElement();
394 cell.setText(prop.getPropName());
399 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
401 propValue.getColumn().setWidth(columnWidth[1]);
402 propValue.getColumn().setText(columnHeaders[1]);
403 propValue.setLabelProvider(new StyledCellLabelProvider() {
405 public void update(ViewerCell cell) {
406 MetaProperty prop = (MetaProperty) cell.getElement();
407 cell.setText(prop.getPropValue());
411 propValue.setEditingSupport(new PropValueEditor(tableViewer));
414 private void addManagerListeners() {
415 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
416 resourceSelectionChangedListener);
419 class PropertycontentProvider implements IStructuredContentProvider {
422 public void dispose() {
426 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
430 public Object[] getElements(Object element) {
431 return (Object[]) element;
436 public void dispose() {
437 // Unregister the listener
438 if (null != resourceSelectionChangedListener) {
439 UiListenerHandler.getInstance()
440 .removeResourceSelectionChangedUIListener(
441 resourceSelectionChangedListener);
446 class PropValueEditor extends EditingSupport {
448 private final TableViewer viewer;
450 public PropValueEditor(TableViewer viewer) {
452 this.viewer = viewer;
456 protected boolean canEdit(Object element) {
461 protected CellEditor getCellEditor(final Object element) {
462 if (!getEnableEdit()) {
465 String propName = ((MetaProperty) element).getPropName();
466 CellEditor editor = new TextCellEditor(viewer.getTable());
467 if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) {
468 editor.setStyle(SWT.READ_ONLY);
469 final Text txt = (Text) editor.getControl();
470 txt.addModifyListener(new ModifyListener() {
472 public void modifyText(ModifyEvent e) {
473 if (null == updatedIfSet) {
476 // Form the result set of interfaces with check-box that
477 // will be shown in the dialog for editing.
478 Map<String, String> curResInterfaces = new HashMap<String, String>();
479 for (Map.Entry<String, String> entry : ifTypes
481 if (updatedIfSet.contains(entry.getValue())) {
482 curResInterfaces.put(entry.getKey(),
487 // Show the dialog for editing the resource interfaces.
488 UpdateResourceInterfaceDialog ad = new UpdateResourceInterfaceDialog(
489 Display.getDefault().getActiveShell(),
490 curResInterfaces, ifTypes);
491 if (ad.open() == Window.OK) {
492 // Update the local copy of the current resource
493 // interfaces to keep the state for save operation.
494 updatedIfSet.clear();
495 StringBuilder newPropValue = new StringBuilder();
496 for (Map.Entry<String, String> entry : curResInterfaces
498 if (!newPropValue.toString().isEmpty()) {
499 newPropValue.append(", ");
501 String value = ifTypes.get(entry.getKey());
502 newPropValue.append(value);
504 updatedIfSet.add(value);
507 MetaProperty prop = (MetaProperty) element;
508 prop.setPropValue(newPropValue.toString());
509 // Update the viewer in a separate UI thread.
510 Display.getDefault().asyncExec(new Runnable() {
513 viewer.refresh(element, true);
524 protected Object getValue(Object element) {
525 return ((MetaProperty) element).getPropValue();
529 protected void setValue(Object element, Object value) {
530 MetaProperty prop = (MetaProperty) element;
531 if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
534 prop.setPropValue(String.valueOf(value));
535 viewer.update(element, null);
539 private synchronized Boolean getEnableEdit() {
543 private synchronized void setEnableEdit(boolean value) {
548 public void setFocus() {