Skip to content
Snippets Groups Projects
Commit f21097f3 authored by Daniel Ratiu's avatar Daniel Ratiu
Browse files

added number editing support in addition to integer editing support

parent ce4458bd
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,7 @@ import org.eclipse.swt.widgets.Composite;
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 877B1F139CCBFED05771E43F31FA489F
* @ConQAT.Rating YELLOW Hash: A52D1443E8DA2144144AC2D83472C803
*/
public class TableViewerUtils {
......@@ -128,4 +128,60 @@ public class TableViewerUtils {
return true;
}
}
/** Editing support for numbers. */
public static abstract class NumberEditingSupport extends EditingSupport {
/** The parent where the editing support is needed. */
private Composite parent;
/** Constructor. */
public NumberEditingSupport(ColumnViewer viewer, Composite parent) {
super(viewer);
this.parent = parent;
}
/** {@inheritDoc} */
@Override
protected CellEditor getCellEditor(Object element) {
CellEditor ce = new TextCellEditor(parent);
ce.setValidator(new ICellEditorValidator() {
/** {@inheritDoc} */
@Override
public String isValid(Object value) {
if (getNumberFromString(value.toString()) == null) {
return "Not a valid number";
}
return null;
}
});
return ce;
}
/** {@inheritDoc} */
@Override
protected boolean canEdit(Object element) {
return true;
}
/**
* Parses a number from a string and returns it or null if parse error
* occurred.
*/
protected Number getNumberFromString(String numAsString) {
try {
Number num = Integer.parseInt(numAsString);
return num;
} catch (NumberFormatException nfe) {
try {
Number num = Double.parseDouble(numAsString);
return num;
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return null;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment