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 "Resource properties updated.");
261 MessageDialog.openInformation(
262 parent.getShell(), "Operation status",
263 "Failed to update the resource properties.");
265 // Reset the old property values.
266 properties = getData(resourceManagerRef
267 .getCurrentResourceInSelection());
268 updateViewer(properties);
272 cancelBtn.setEnabled(false);
273 editBtn.setText("Edit");
274 setEnableEdit(false);
279 cancelBtn = new Button(parent, SWT.PUSH);
280 cancelBtn.setText("Cancel");
281 cancelBtn.setEnabled(false);
284 cancelBtn.setLayoutData(gd);
285 cancelBtn.addSelectionListener(new SelectionAdapter() {
287 public void widgetSelected(SelectionEvent e) {
288 Resource res = resourceManagerRef
289 .getCurrentResourceInSelection();
291 properties = getData(res);
293 updateViewer(properties);
295 cancelBtn.setEnabled(false);
296 editBtn.setText("Edit");
297 setEnableEdit(false);
298 if (null != updatedIfSet)
299 updatedIfSet.clear();
303 // Get the supported interfaces.
304 Map<String, String> ifTypesSupported = Utility
305 .getResourceInterfaces(SingleResource.class);
306 if (null != ifTypesSupported && !ifTypesSupported.isEmpty()) {
307 ifTypes = new HashMap<String, String>();
309 for (Map.Entry<String, String> entry : ifTypesSupported.entrySet()) {
310 key = entry.getValue() + " (" + entry.getKey() + ")";
311 ifTypes.put(key, entry.getKey());
315 addManagerListeners();
317 // Check whether there is any resource selected already
318 Resource resource = resourceManagerRef.getCurrentResourceInSelection();
319 properties = getData(resource);
320 if (null != properties) {
321 updateViewer(properties);
323 updateEditControls(resource);
326 private void updateEditControls(Object obj) {
327 if (!editBtn.isDisposed() && !cancelBtn.isDisposed()) {
329 if (editBtn.getText().equals("Save")) {
330 editBtn.setText("Edit");
331 setEnableEdit(false);
335 editBtn.setEnabled(false);
337 editBtn.setEnabled(true);
339 cancelBtn.setEnabled(false);
343 private List<MetaProperty> getData(Resource resource) {
344 if (null != resource) {
345 List<MetaProperty> metaPropertyList = resourceManagerRef
346 .getMetaProperties(resource);
347 return metaPropertyList;
353 private void updateViewer(List<MetaProperty> metaPropertyList) {
354 if (null != tableViewer) {
355 Table tbl = tableViewer.getTable();
356 if (tbl.isDisposed()) {
359 if (null != metaPropertyList) {
360 tableViewer.setInput(metaPropertyList.toArray());
361 tbl.setLinesVisible(true);
364 tbl.setLinesVisible(false);
369 public void createColumns(final TableViewer tableViewer) {
370 TableViewerColumn propName = new TableViewerColumn(tableViewer,
372 propName.getColumn().setWidth(columnWidth[0]);
373 propName.getColumn().setText(columnHeaders[0]);
374 propName.setLabelProvider(new StyledCellLabelProvider() {
376 public void update(ViewerCell cell) {
377 MetaProperty prop = (MetaProperty) cell.getElement();
378 cell.setText(prop.getPropName());
383 TableViewerColumn propValue = new TableViewerColumn(tableViewer,
385 propValue.getColumn().setWidth(columnWidth[1]);
386 propValue.getColumn().setText(columnHeaders[1]);
387 propValue.setLabelProvider(new StyledCellLabelProvider() {
389 public void update(ViewerCell cell) {
390 MetaProperty prop = (MetaProperty) cell.getElement();
391 cell.setText(prop.getPropValue());
395 propValue.setEditingSupport(new PropValueEditor(tableViewer));
398 private void addManagerListeners() {
399 UiListenerHandler.getInstance().addResourceSelectionChangedUIListener(
400 resourceSelectionChangedListener);
403 class PropertycontentProvider implements IStructuredContentProvider {
406 public void dispose() {
410 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
414 public Object[] getElements(Object element) {
415 return (Object[]) element;
420 public void dispose() {
421 // Unregister the listener
422 if (null != resourceSelectionChangedListener) {
423 UiListenerHandler.getInstance()
424 .removeResourceSelectionChangedUIListener(
425 resourceSelectionChangedListener);
430 class PropValueEditor extends EditingSupport {
432 private final TableViewer viewer;
434 public PropValueEditor(TableViewer viewer) {
436 this.viewer = viewer;
440 protected boolean canEdit(Object element) {
445 protected CellEditor getCellEditor(final Object element) {
446 if (!getEnableEdit()) {
449 // Disabling edit for resource type
450 String propName = ((MetaProperty) element).getPropName();
451 if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
455 CellEditor editor = new TextCellEditor(viewer.getTable());
456 if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) {
457 editor.setStyle(SWT.READ_ONLY);
458 final Text txt = (Text) editor.getControl();
459 txt.addModifyListener(new ModifyListener() {
461 public void modifyText(ModifyEvent e) {
462 if (null == updatedIfSet) {
465 // Form the result set of interfaces with check-box that
466 // will be shown in the dialog for editing.
467 Map<String, String> curResInterfaces = new HashMap<String, String>();
468 for (Map.Entry<String, String> entry : ifTypes
470 if (updatedIfSet.contains(entry.getValue())) {
471 curResInterfaces.put(entry.getKey(),
476 // Show the dialog for editing the resource interfaces.
477 UpdateResourceInterfaceDialog ad = new UpdateResourceInterfaceDialog(
478 Display.getDefault().getActiveShell(),
479 curResInterfaces, ifTypes);
480 if (ad.open() == Window.OK) {
481 // Update the local copy of the current resource
482 // interfaces to keep the state for save operation.
483 updatedIfSet.clear();
484 String newPropValue = "";
485 for (Map.Entry<String, String> entry : curResInterfaces
487 if (!newPropValue.isEmpty()) {
488 newPropValue += ", ";
490 String value = ifTypes.get(entry.getKey());
491 newPropValue += value;
493 updatedIfSet.add(value);
496 MetaProperty prop = (MetaProperty) element;
497 prop.setPropValue(newPropValue);
498 // Update the viewer in a separate UI thread.
499 Display.getDefault().asyncExec(new Runnable() {
502 viewer.refresh(element, true);
513 protected Object getValue(Object element) {
514 return ((MetaProperty) element).getPropValue();
518 protected void setValue(Object element, Object value) {
519 MetaProperty prop = (MetaProperty) element;
520 if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
523 prop.setPropValue(String.valueOf(value));
524 viewer.update(element, null);
528 private synchronized Boolean getEnableEdit() {
532 private synchronized void setEnableEdit(boolean value) {
537 public void setFocus() {