package oic.simulator.serviceprovider.view;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import oic.simulator.serviceprovider.Activator;
-import oic.simulator.serviceprovider.listener.ISelectionChangedUIListener;
+import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
import oic.simulator.serviceprovider.manager.ResourceManager;
import oic.simulator.serviceprovider.manager.UiListenerHandler;
-import oic.simulator.serviceprovider.model.Device;
import oic.simulator.serviceprovider.model.MetaProperty;
import oic.simulator.serviceprovider.model.Resource;
+import oic.simulator.serviceprovider.model.SingleResource;
import oic.simulator.serviceprovider.utils.Constants;
+import oic.simulator.serviceprovider.utils.Utility;
+import oic.simulator.serviceprovider.view.dialogs.UpdateResourceInterfaceDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
-import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
+import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.oic.simulator.SimulatorException;
*/
public class MetaPropertiesView extends ViewPart {
- public static final String VIEW_ID = "oic.simulator.serviceprovider.view.metaproperties";
+ public static final String VIEW_ID = "oic.simulator.serviceprovider.view.metaproperties";
- private TableViewer tableViewer;
+ private TableViewer tableViewer;
- private final String[] columnHeaders = { "Property", "Value" };
+ private final String[] columnHeaders = { "Property", "Value" };
- private final Integer[] columnWidth = { 150, 150 };
+ private final Integer[] columnWidth = { 150, 150 };
- private ISelectionChangedUIListener resourceSelectionChangedListener;
+ private ISelectionChangedListener resourceSelectionChangedListener;
- private ResourceManager resourceManagerRef;
+ private ResourceManager resourceManagerRef;
- private List<MetaProperty> properties;
+ private Set<String> updatedIfSet;
- private boolean enable_edit;
- private Button editBtn;
- private Button cancelBtn;
+ private List<MetaProperty> properties;
+
+ private boolean enable_edit;
+ private Button editBtn;
+ private Button cancelBtn;
+
+ private Map<String, String> ifTypes;
public MetaPropertiesView() {
resourceManagerRef = Activator.getDefault().getResourceManager();
- resourceSelectionChangedListener = new ISelectionChangedUIListener() {
+ resourceSelectionChangedListener = new ISelectionChangedListener() {
@Override
public void onResourceSelectionChange(final Resource resource) {
}
});
}
-
- @Override
- public void onDeviceSelectionChange(final Device dev) {
- Display.getDefault().asyncExec(new Runnable() {
-
- @Override
- public void run() {
- if (null != tableViewer) {
- properties = getData(dev);
- updateViewer(properties);
- }
- updateEditControls(dev);
- }
- });
- }
};
}
if (editBtn.getText().equals("Edit")) {
cancelBtn.setEnabled(true);
editBtn.setText("Save");
- enable_edit = true;
+ setEnableEdit(true);
+
+ Resource resource = resourceManagerRef
+ .getCurrentResourceInSelection();
+ if (null != resource) {
+ updatedIfSet = resource.getResourceInterfaces();
+ }
} else {
boolean result = false;
Resource resourceInSelection = resourceManagerRef
Constants.RESOURCE_URI);
if (result) {
MessageDialog.openError(parent.getShell(),
- "Invalid Input", "Resource URI is invalid.");
+ "Invalid Resource URI.",
+ Constants.INVALID_URI_MESSAGE);
return;
}
boolean update = false;
boolean uriChange = false;
boolean nameChange = false;
+ boolean interfaceChange = false;
+
if (resourceManagerRef.isPropValueChanged(
resourceInSelection, properties,
Constants.RESOURCE_NAME)) {
return;
}
- update = true;
- uriChange = true;
-
if (resourceManagerRef
.isResourceStarted(resourceInSelection)) {
update = MessageDialog.openQuestion(
return;
}
}
+
+ update = true;
+ uriChange = true;
+ }
+ // Checking whether any changes made in resource
+ // interfaces by
+ // comparing the current interface set and updated
+ // interface set.
+ if (null != updatedIfSet) {
+ Set<String> curIfSet = resourceInSelection
+ .getResourceInterfaces();
+ if (curIfSet.size() != updatedIfSet.size()) {
+ update = true;
+ interfaceChange = true;
+ } else {
+ Iterator<String> itr = updatedIfSet.iterator();
+ while (itr.hasNext()) {
+ if (!curIfSet.contains(itr.next())) {
+ update = true;
+ interfaceChange = true;
+ break;
+ }
+ }
+ }
}
if (update) {
try {
- result = Activator
- .getDefault()
- .getResourceManager()
- .updateResourceProperties(
- resourceManagerRef
- .getCurrentResourceInSelection(),
- properties, uriChange,
- nameChange);
+ if (uriChange || nameChange)
+ result = Activator
+ .getDefault()
+ .getResourceManager()
+ .updateResourceProperties(
+ resourceManagerRef
+ .getCurrentResourceInSelection(),
+ properties, uriChange,
+ nameChange);
+ if (interfaceChange)
+ result = Activator
+ .getDefault()
+ .getResourceManager()
+ .updateResourceInterfaces(
+ resourceInSelection,
+ updatedIfSet);
+
} catch (SimulatorException ex) {
result = false;
}
updateViewer(properties);
}
}
- } else {
- Device dev = resourceManagerRef
- .getCurrentDeviceInSelection();
-
- // Null check
- result = resourceManagerRef.isPropertyValueInvalid(dev,
- properties, Constants.DEVICE_NAME);
- if (result) {
- MessageDialog.openError(parent.getShell(),
- "Invalid Input", "Device Name is invalid.");
- return;
- }
-
- if (resourceManagerRef.isPropValueChanged(dev,
- properties, Constants.DEVICE_NAME)) {
- resourceManagerRef.updateDeviceProperties(dev,
- properties);
- }
-
}
cancelBtn.setEnabled(false);
editBtn.setText("Edit");
- enable_edit = false;
+ setEnableEdit(false);
}
}
});
.getCurrentResourceInSelection();
if (null != res) {
properties = getData(res);
- } else {
- Device dev = resourceManagerRef
- .getCurrentDeviceInSelection();
- if (null != dev) {
- properties = getData(dev);
- }
}
updateViewer(properties);
cancelBtn.setEnabled(false);
editBtn.setText("Edit");
- enable_edit = false;
+ setEnableEdit(false);
+ if (null != updatedIfSet)
+ updatedIfSet.clear();
}
});
+ // Get the supported interfaces.
+ Map<String, String> ifTypesSupported = Utility
+ .getResourceInterfaces(SingleResource.class);
+ if (null != ifTypesSupported && !ifTypesSupported.isEmpty()) {
+ ifTypes = new HashMap<String, String>();
+ String key;
+ for (Map.Entry<String, String> entry : ifTypesSupported.entrySet()) {
+ key = entry.getValue() + " (" + entry.getKey() + ")";
+ ifTypes.put(key, entry.getKey());
+ }
+ }
+
addManagerListeners();
// Check whether there is any resource selected already
if (editBtn.getText().equals("Save")) {
editBtn.setText("Edit");
- enable_edit = false;
+ setEnableEdit(false);
}
if (null == obj) {
}
}
- private List<MetaProperty> getData(Device dev) {
- if (null != dev) {
- List<MetaProperty> metaPropertyList = resourceManagerRef
- .getMetaProperties(dev);
- return metaPropertyList;
- } else {
- return null;
- }
- }
-
private void updateViewer(List<MetaProperty> metaPropertyList) {
if (null != tableViewer) {
Table tbl = tableViewer.getTable();
+ if (tbl.isDisposed()) {
+ return;
+ }
if (null != metaPropertyList) {
tableViewer.setInput(metaPropertyList.toArray());
- if (!tbl.isDisposed()) {
- tbl.setLinesVisible(true);
- }
+ tbl.setLinesVisible(true);
} else {
- if (!tbl.isDisposed()) {
- tbl.removeAll();
- tbl.setLinesVisible(false);
- }
+ tbl.removeAll();
+ tbl.setLinesVisible(false);
}
}
}
- public void createColumns(TableViewer tableViewer) {
+ public void createColumns(final TableViewer tableViewer) {
TableViewerColumn propName = new TableViewerColumn(tableViewer,
SWT.NONE);
propName.getColumn().setWidth(columnWidth[0]);
SWT.NONE);
propValue.getColumn().setWidth(columnWidth[1]);
propValue.getColumn().setText(columnHeaders[1]);
- propValue.setLabelProvider(new ColumnLabelProvider() {
+ propValue.setLabelProvider(new StyledCellLabelProvider() {
@Override
- public String getText(Object element) {
- MetaProperty prop = (MetaProperty) element;
- if (null != prop) {
- return prop.getPropValue();
- } else {
- return "";
- }
+ public void update(ViewerCell cell) {
+ MetaProperty prop = (MetaProperty) cell.getElement();
+ cell.setText(prop.getPropValue());
+ super.update(cell);
}
});
propValue.setEditingSupport(new PropValueEditor(tableViewer));
}
@Override
- protected CellEditor getCellEditor(Object element) {
- if (!enable_edit) {
+ protected CellEditor getCellEditor(final Object element) {
+ if (!getEnableEdit()) {
return null;
}
// Disabling edit for resource type
if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
return null;
}
+
CellEditor editor = new TextCellEditor(viewer.getTable());
+ if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) {
+ editor.setStyle(SWT.READ_ONLY);
+ final Text txt = (Text) editor.getControl();
+ txt.addModifyListener(new ModifyListener() {
+ @Override
+ public void modifyText(ModifyEvent e) {
+ if (null == updatedIfSet) {
+ return;
+ }
+ // Form the result set of interfaces with check-box that
+ // will be shown in the dialog for editing.
+ Map<String, String> curResInterfaces = new HashMap<String, String>();
+ for (Map.Entry<String, String> entry : ifTypes
+ .entrySet()) {
+ if (updatedIfSet.contains(entry.getValue())) {
+ curResInterfaces.put(entry.getKey(),
+ entry.getValue());
+ }
+ }
+
+ // Show the dialog for editing the resource interfaces.
+ UpdateResourceInterfaceDialog ad = new UpdateResourceInterfaceDialog(
+ Display.getDefault().getActiveShell(),
+ curResInterfaces, ifTypes);
+ if (ad.open() == Window.OK) {
+ // Update the local copy of the current resource
+ // interfaces to keep the state for save operation.
+ updatedIfSet.clear();
+ String newPropValue = "";
+ for (Map.Entry<String, String> entry : curResInterfaces
+ .entrySet()) {
+ if (!newPropValue.isEmpty()) {
+ newPropValue += ", ";
+ }
+ String value = ifTypes.get(entry.getKey());
+ newPropValue += value;
+
+ updatedIfSet.add(value);
+ }
+ // Update the model
+ MetaProperty prop = (MetaProperty) element;
+ prop.setPropValue(newPropValue);
+ // Update the viewer in a separate UI thread.
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ viewer.refresh(element, true);
+ }
+ });
+ }
+ }
+ });
+ }
return editor;
}
@Override
protected void setValue(Object element, Object value) {
MetaProperty prop = (MetaProperty) element;
+ if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
+ return;
+ }
prop.setPropValue(String.valueOf(value));
viewer.update(element, null);
}
+ }
+
+ private synchronized Boolean getEnableEdit() {
+ return enable_edit;
+ }
+ private synchronized void setEnableEdit(boolean value) {
+ enable_edit = value;
}
@Override