Module org.controlsfx.controls
Class SpreadsheetCellType<T>
- java.lang.Object
-
- org.controlsfx.control.spreadsheet.SpreadsheetCellType<T>
-
- Direct Known Subclasses:
SpreadsheetCellType.DateType
,SpreadsheetCellType.DoubleType
,SpreadsheetCellType.IntegerType
,SpreadsheetCellType.ListType
,SpreadsheetCellType.ObjectType
,SpreadsheetCellType.StringType
public abstract class SpreadsheetCellType<T> extends Object
When instantiating aSpreadsheetCell
, its SpreadsheetCellType will specify which values the cell can accept as user input, and whichSpreadsheetCellEditor
it will use to receive user input.
Different static methods are provided in order to give you access to basic types, and to createSpreadsheetCell
easily:- String: Accessible with
SpreadsheetCellType.StringType.createCell(int, int, int, int, String)
. - List: Accessible with
SpreadsheetCellType.ListType.createCell(int, int, int, int, String)
. - Double: Accessible with
SpreadsheetCellType.DoubleType.createCell(int, int, int, int, Double)
. - Integer: Accessible with
SpreadsheetCellType.IntegerType.createCell(int, int, int, int, Integer)
. - Date: Accessible with
SpreadsheetCellType.DateType.createCell(int, int, int, int, LocalDate)
.
Value verification
You can specify two levels of verification in your types.
- The first one is defined by
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 theSpreadsheetView
when trying to set values for example.
- The second level is defined by
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 aSpreadsheetCell
call this method when its value has changed in order to react accordingly if the value is in error. (see example below).
Converter
You will have to specify a converter for your type. It will handle all the conversion between your real value type (Double, Integer, LocalDate etc) and its string representation for the cell.
You can either use a pre-builtStringConverter
or ourStringConverterWithFormat
. This one just add one method (StringConverterWithFormat.toStringFormat(Object, String)
which will convert your value with a String format (found inSpreadsheetCell.getFormat()
).Example
You can create several types which are using the same editor. Suppose you want to handle Double values. You will implement thecreateEditor(SpreadsheetView)
method and use theSpreadsheetCellEditor.DoubleEditor
.
Then for each type you will provide your own policy inmatch(Object)
and inisError(Object)
, which most of the time will use yourconverter
.
Here is an example of how to create aStringConverterWithFormat
: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; } } }
- See Also:
SpreadsheetView
,SpreadsheetCellEditor
,SpreadsheetCell
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SpreadsheetCellType.DateType
TheSpreadsheetCell
LocalDate
type base class.static class
SpreadsheetCellType.DoubleType
TheSpreadsheetCell
Double
type base class.static class
SpreadsheetCellType.IntegerType
TheSpreadsheetCell
Integer
type base class.static class
SpreadsheetCellType.ListType
TheSpreadsheetCell
List
type base class.static class
SpreadsheetCellType.ObjectType
TheSpreadsheetCell
Object
type base class.static class
SpreadsheetCellType.StringType
TheSpreadsheetCell
String
type base class.
-
Field Summary
Fields Modifier and Type Field Description protected StringConverter<T>
converter
An instance of converter from string to cell type.static SpreadsheetCellType.DateType
DATE
TheSpreadsheetCell
LocalDate
type instance.static SpreadsheetCellType.DoubleType
DOUBLE
TheSpreadsheetCell
Double
type instance.static SpreadsheetCellType.IntegerType
INTEGER
TheSpreadsheetCell
Integer
type instance.static SpreadsheetCellType<Object>
OBJECT
TheSpreadsheetCell
Object
type instance.static SpreadsheetCellType.StringType
STRING
TheSpreadsheetCell
String
type instance.
-
Constructor Summary
Constructors Constructor Description SpreadsheetCellType()
Default constructor.SpreadsheetCellType(StringConverter<T> converter)
Constructor with the StringConverter directly provided.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptDrop()
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 callmatch(Object)
before to see if this method will succeed.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.ListType
LIST(List<String> items)
Creates aSpreadsheetCellType.ListType
.boolean
match(Object value)
Verify that the upcoming value can be set to the current cell.abstract boolean
match(Object value, Object... options)
Verify that the upcoming value can be set to the current cell.This is the first level of verification to prevent affecting a text to a double or a double to a date.abstract String
toString(T object)
Return a string representation of the given item for theSpreadsheetView
to display using the innerconverter
.String
toString(T object, String format)
Return a string representation of the given item for theSpreadsheetView
to display using the innerconverter
and the specified format.
-
-
-
Field Detail
-
converter
protected StringConverter<T> converter
An instance of converter from string to cell type.
-
OBJECT
public static final SpreadsheetCellType<Object> OBJECT
TheSpreadsheetCell
Object
type instance.
-
STRING
public static final SpreadsheetCellType.StringType STRING
TheSpreadsheetCell
String
type instance.
-
DOUBLE
public static final SpreadsheetCellType.DoubleType DOUBLE
TheSpreadsheetCell
Double
type instance.
-
INTEGER
public static final SpreadsheetCellType.IntegerType INTEGER
TheSpreadsheetCell
Integer
type instance.
-
DATE
public static final SpreadsheetCellType.DateType DATE
TheSpreadsheetCell
LocalDate
type instance.
-
-
Constructor Detail
-
SpreadsheetCellType
public SpreadsheetCellType()
Default constructor.
-
SpreadsheetCellType
public SpreadsheetCellType(StringConverter<T> converter)
Constructor with the StringConverter directly provided.- Parameters:
converter
- The converter to use
-
-
Method Detail
-
createEditor
public abstract SpreadsheetCellEditor createEditor(SpreadsheetView view)
Creates an editor for this type of cells.- Parameters:
view
- the spreadsheet that will own this editor- Returns:
- the editor instance
-
toString
public String toString(T object, String format)
Return a string representation of the given item for theSpreadsheetView
to display using the innerconverter
and the specified format.- Parameters:
object
-format
-- Returns:
- a string representation of the given item.
-
toString
public abstract String toString(T object)
Return a string representation of the given item for theSpreadsheetView
to display using the innerconverter
.- Parameters:
object
-- Returns:
- a string representation of the given item.
-
match
public boolean match(Object value)
Verify that the upcoming value can be set to the current cell. This is the first level of verification to prevent affecting a text to a double or a double to a date. For closer verification, useisError(Object)
.- Parameters:
value
- the value to test- Returns:
- true if it matches.
-
match
public abstract boolean match(Object value, Object... options)
Verify that the upcoming value can be set to the current cell.This is the first level of verification to prevent affecting a text to a double or a double to a date. For closer verification, useisError(Object)
.- Parameters:
value
- the value to testoptions
- the options given bySpreadsheetCell.getOptionsForEditor()
- Returns:
- true if it matches.
-
isError
public boolean isError(Object value)
Returns true if the value is an error regarding the specification of its type.- Parameters:
value
-- Returns:
- true if the value is an error.
-
acceptDrop
public boolean acceptDrop()
- Returns:
- true if this SpreadsheetCellType accepts Objects to be dropped on
the
SpreadsheetCell
. Currently only Files can be dropped. If accepted, prepare to receive them inmatch(java.lang.Object)
andconvertValue(java.lang.Object)
.
-
convertValue
public 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 callmatch(Object)
before to see if this method will succeed.- Parameters:
value
-- Returns:
- null if not valid or the correct value otherwise.
-
LIST
public static final SpreadsheetCellType.ListType LIST(List<String> items)
Creates aSpreadsheetCellType.ListType
.- Parameters:
items
- the list items- Returns:
- the instance
-
-