diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/utils/TableViewerUtils.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/utils/TableViewerUtils.java index da866f0dea8e5851db2e2d7f4d1498d0761ceae5..07537fffe0baedf22fc5cf186b03fa87b4282fb4 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/utils/TableViewerUtils.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/utils/TableViewerUtils.java @@ -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; + } + } }