package ch.openech.mj.edit.fields;

import java.math.BigDecimal;

import ch.openech.mj.db.model.InvalidValues;
import ch.openech.mj.db.model.PropertyInterface;


public class BigDecimalEditField extends NumberEditField<BigDecimal> /* implements DemoEnabled */ {

	public BigDecimalEditField(PropertyInterface property, int size, boolean negative) {
		super(property, size, 0, negative);
	}

	@Override
	public void setObject(BigDecimal number) {
		String text = null;
		if (number != null) {
			if (InvalidValues.isInvalid(number)) {
				text = InvalidValues.getInvalidValue(number);
			} else {
				text = number.toString();
			}
		}
		textField.setText(text);
	}

	@Override
	public BigDecimal getObject() {
		String text = textField.getText();
		if (text != null) {
			try {
				return new BigDecimal(text);
			} catch (NumberFormatException nfe) {
				return InvalidValues.createInvalidBigDecimal(text);
			}
		} else {
			return null;
		}
	}

//	@Override
//	public void fillWithDemoData() {
//		Random random = new Random();
//		int value = random.nextInt(10 ^ size);
//		if (!negative || random.nextBoolean()) {
//			setObject(value);
//		} else {
//			setObject(-value);
//		}
//	}

}

@@ -61,16 +61,86 @@
 cimal;%0D%0A
+import java.util.Random;%0D%0A%0D%0Aimport ch.openech.mj.autofill.DemoEnabled;
 %0D%0Aimport
@@ -305,11 +305,8 @@
 mal%3E
- /*
  imp
@@ -328,11 +328,8 @@
 bled
- */
  %7B%0D%0A
@@ -395,16 +395,35 @@
 nt size,
+ int decimalPlaces,
  boolean
@@ -460,17 +460,29 @@
 , size, 
-0
+decimalPlaces
 , negati
@@ -1077,18 +1077,16 @@
 %0D%0A%09%7D%0D%0A%0D%0A
-//
 %09@Overri
@@ -1089,18 +1089,16 @@
 erride%0D%0A
-//
 %09public 
@@ -1124,18 +1124,73 @@
 ta() %7B%0D%0A
-//
+%09%09// TODO check for valid ranges for size-decimalPlaces%0D%0A
 %09%09Random
@@ -1218,15 +1218,16 @@
 );%0D%0A
-//%09%09int
+%09%09double
  val
@@ -1246,27 +1246,51 @@
 next
-Int
+Double() * 
 (10 %5E 
+(
 size
+ - decimalPlaces)
 );%0D%0A
-//
 %09%09if
@@ -1325,26 +1325,24 @@
 olean()) %7B%0D%0A
-//
 %09%09%09setObject
@@ -1342,27 +1342,45 @@
 tObject(
-value
+BigDecimal.valueOf(value)
 );%0D%0A
-//
 %09%09%7D else
@@ -1383,18 +1383,16 @@
 else %7B%0D%0A
-//
 %09%09%09setOb
@@ -1400,27 +1400,43 @@
 ect(
+BigDecimal.valueOf(
 -value)
+)
 ;%0D%0A
-//
 %09%09%7D%0D%0A
-//
 %09%7D%0D%0A

support for decimalPlaces