public abstract class SpreadsheetCellType<T> extends Object
SpreadsheetCell, its SpreadsheetCellType will
specify which values the cell can accept as user input, and which
SpreadsheetCellEditor it will use to receive user input. SpreadsheetCell easily:
SpreadsheetCellType.StringType.createCell(int, int, int, int, String)
.SpreadsheetCellType.ListType.createCell(int, int, int, int, String).SpreadsheetCellType.DoubleType.createCell(int, int, int, int, Double)
.SpreadsheetCellType.IntegerType.createCell(int, int, int, int, Integer)
.SpreadsheetCellType.DateType.createCell(int, int, int, int, LocalDate)
.match(Object). It is the first level
that tells whether or not the given value should be accepted or not. Trying
to set a String into a Double will return false for example. This method will
be use by the SpreadsheetView when trying to set values for example.
isError(Object). This is more
subtle and allow you to tell whether the given value is coherent or not
regarding the policy you gave. You can just make a SpreadsheetCell
call this method when its value has changed in order to react accordingly if
the value is in error. (see example below).StringConverter or our
StringConverterWithFormat. This one just add one method (
StringConverterWithFormat.toStringFormat(Object, String) which will
convert your value with a String format (found in
SpreadsheetCell.getFormat()).
createEditor(SpreadsheetView) method and use the
SpreadsheetCellEditor.DoubleEditor. match(Object)
and in isError(Object), which most of the time will use your
converter. StringConverterWithFormat :
StringConverterWithFormat specialConverter = new StringConverterWithFormat<Double>(new DoubleStringConverter()) {
@Override
public String toString(Double item) {
//We just redirect to the other method.
return toStringFormat(item, "");
}
@Override
public String toStringFormat(Double item, String format) {
if (item == null || Double.isNaN(item)) {
return missingLabel; // For example return something else that an empty cell.
} else{
if (!("").equals(format) && !Double.isNaN(item)) {
//We format here the value
return new DecimalFormat(format).format(item);
} else {
//We call the DoubleStringConverter that we gave in argument
return myConverter.toString(item);
}
}
}
@Override
public Double fromString(String str) {
if (str == null || str.isEmpty()) {
return Double.NaN;
} else {
try {
//Just returning the value
Double myDouble = Double.parseDouble(str);
return myDouble;
} catch (NumberFormatException e) {
return myConverter.fromString(str);
}
}
}
}
And then suppose you only want to accept double values between 0 and 100, and
that a value superior to 10 is abnormal.
@Override
public boolean isError(Object value) {
if (value instanceof Double) {
if ((Double) value > 0 && (Double) value < 10) {
return false;
}
return true;
}
return true;
}
@Override
public boolean match(Object value) {
if (value instanceof Double) {
return true;
} else {
try {
Double convertedValue = converter.fromString(value == null ? null : value.toString());
if (convertedValue >= 0 && convertedValue <= 100)
return true;
else
return false;
} catch (Exception e) {
return false;
}
}
}
SpreadsheetView,
SpreadsheetCellEditor,
SpreadsheetCell| Modifier and Type | Class and Description |
|---|---|
static class |
SpreadsheetCellType.DateType
The
SpreadsheetCell LocalDate type base class. |
static class |
SpreadsheetCellType.DoubleType
The
SpreadsheetCell Double type base class. |
static class |
SpreadsheetCellType.IntegerType
The
SpreadsheetCell Integer type base class. |
static class |
SpreadsheetCellType.ListType
The
SpreadsheetCell List type base class. |
static class |
SpreadsheetCellType.ObjectType
The
SpreadsheetCell Object type base class. |
static class |
SpreadsheetCellType.StringType
The
SpreadsheetCell String type base class. |
| Modifier and Type | Field and Description |
|---|---|
protected StringConverter<T> |
converter
An instance of converter from string to cell type.
|
static SpreadsheetCellType<java.time.LocalDate> |
DATE
The
SpreadsheetCell LocalDate type instance. |
static SpreadsheetCellType<Double> |
DOUBLE
The
SpreadsheetCell Double type instance. |
static SpreadsheetCellType<Integer> |
INTEGER
The
SpreadsheetCell Integer type instance. |
static SpreadsheetCellType<Object> |
OBJECT
The
SpreadsheetCell Object type instance. |
static SpreadsheetCellType<String> |
STRING
The
SpreadsheetCell String type instance. |
| Constructor and Description |
|---|
SpreadsheetCellType()
Default constructor.
|
SpreadsheetCellType(StringConverter<T> converter)
Constructor with the StringConverter directly provided.
|
| Modifier and Type | Method and Description |
|---|---|
abstract T |
convertValue(Object value)
This method will be called when a commit is happening.
This method will try to convert the value, be sure to call match(Object) before to see if this method will succeed. |
abstract SpreadsheetCell |
createCell(int row,
int column,
int rowSpan,
int columnSpan,
T value)
Creates a cell that hold a
|
abstract SpreadsheetCellEditor |
createEditor(SpreadsheetView view)
Creates an editor for this type of cells.
|
boolean |
isError(Object value)
Returns true if the value is an error regarding the specification of its
type.
|
static SpreadsheetCellType<String> |
LIST(List<String> items)
Creates a
SpreadsheetCellType.ListType. |
abstract boolean |
match(Object value)
Verify that the upcoming value can be set to the current cell.
|
abstract String |
toString(T object)
Return a string representation of the given item for the
SpreadsheetView to display using the inner
converter. |
String |
toString(T object,
String format)
Return a string representation of the given item for the
SpreadsheetView to display using the inner
converter and the specified format. |
protected StringConverter<T> converter
public static final SpreadsheetCellType<Object> OBJECT
SpreadsheetCell Object type instance.public static final SpreadsheetCellType<String> STRING
SpreadsheetCell String type instance.public static final SpreadsheetCellType<Double> DOUBLE
SpreadsheetCell Double type instance.public static final SpreadsheetCellType<Integer> INTEGER
SpreadsheetCell Integer type instance.public static final SpreadsheetCellType<java.time.LocalDate> DATE
SpreadsheetCell LocalDate type instance.public SpreadsheetCellType()
public SpreadsheetCellType(StringConverter<T> converter)
converter - The converter to usepublic abstract SpreadsheetCell createCell(int row, int column, int rowSpan, int columnSpan, T value)
row - row numbercolumn - column numberrowSpan - rowSpan (1 is normal)columnSpan - ColumnSpan (1 is normal)value - the value to displaySpreadsheetCellpublic abstract SpreadsheetCellEditor createEditor(SpreadsheetView view)
view - the spreadsheet that will own this editorpublic String toString(T object, String format)
SpreadsheetView to display using the inner
converter and the specified format.object - format - public abstract String toString(T object)
SpreadsheetView to display using the inner
converter.object - public abstract boolean match(Object value)
isError(Object).value - the value to testpublic boolean isError(Object value)
value - public abstract T convertValue(Object value)
match(Object) before to see if this method will succeed.value - public static final SpreadsheetCellType<String> LIST(List<String> items)
SpreadsheetCellType.ListType.items - the list items