The parameters Map should be presented/edited by a JFace TableViewer:
- Column 1: Parameter name => map key
- Column 2: Parameter value => corresponding value
Editing can then be down "in table" by implementing EditingSupport or via a small popup dialog.
Screenshot: settings dialog with java.util.Map based JFace TableViewer |
Unfortunately org.eclipse.jface.viewers.ArrayContentProvider cannot deal with java.util.Map. The table stayed empty.
I did a quick search for another ContentProvider that supports java.util.Map - but could not find anything.
Here comes my own implementation:
/**
* IStructuredContentProvider for java.util.Map
*
* @author Peter Pinnau
*
*/
class MapContentProvider implements IStructuredContentProvider {
@Override
public void dispose() { }
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { }
@SuppressWarnings("rawtypes")
@Override
public Object[] getElements(Object inputElement) {
if (inputElement == null) return null;
if (inputElement instanceof Map) {
return ((Map) inputElement).entrySet().toArray();
}
throw new RuntimeException("Invalid input für MapContentProvider: " + inputElement.getClass());
}
}
The ContentProvider simply returns java.util.Map.entrySet().toArray().
The implementation for the TableViewer lable providers is straight forward:
The implementation for the TableViewer lable providers is straight forward:
viewerParameter = new TableViewer(composite, SWT.FULL_SELECTION);
viewerParameter.getTable().setHeaderVisible(true);
// Set the MapContentProvider
viewerParameter.setContentProvider( new MapContentProvider() );
// Column for map-key
TableViewerColumn column = new TableViewerColumn(viewerParameter, SWT.NONE);
column.getColumn().setText("Parameter");
column.getColumn().setWidth(150);
column.setLabelProvider( new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
@SuppressWarnings("rawtypes")
Object value = ((Entry) cell.getElement()).getKey();
if (value != null)
cell.setText(value.toString());
else
cell.setText("");
}
});
// Column for map-value
column = new TableViewerColumn(viewerParameter, SWT.NONE);
column.getColumn().setText("Wert");
column.getColumn().setWidth(250);
column.setLabelProvider( new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
@SuppressWarnings("rawtypes")
Object value = ((Entry) cell.getElement()).getValue();
if (value != null)
cell.setText(value.toString());
else
cell.setText("");
}
});
// Pass a java.util.Map as input
viewerParameter.setInput( mapObject );
Keine Kommentare:
Kommentar veröffentlichen