1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.TextFormatter;
import javafx.scene.input.InputEvent;
import javafx.util.StringConverter;
public class TimeSpinner extends Spinner<LocalTime> {
// Mode represents the unit that is currently being edited.
// For convenience expose methods for incrementing and decrementing that
// unit, and for selecting the appropriate portion in a spinner's editor
enum Mode {
HOURS {
@Override
LocalTime increment(LocalTime time, int steps) {
return time.plusHours(steps);
}
@Override
void select(TimeSpinner spinner) {
int index = spinner.getEditor().getText().indexOf(':');
spinner.getEditor().selectRange(0, index);
}
},
MINUTES {
@Override
LocalTime increment(LocalTime time, int steps) {
return time.plusMinutes(steps);
}
@Override
void select(TimeSpinner spinner) {
int hrIndex = spinner.getEditor().getText().indexOf(':');
int minIndex = spinner.getEditor().getText().indexOf(':', hrIndex + 1);
spinner.getEditor().selectRange(hrIndex+1, minIndex);
}
},
SECONDS {
@Override
LocalTime increment(LocalTime time, int steps) {
return time.plusSeconds(steps);
}
@Override
void select(TimeSpinner spinner) {
int index = spinner.getEditor().getText().lastIndexOf(':');
spinner.getEditor().selectRange(index+1, spinner.getEditor().getText().length());
}
};
abstract LocalTime increment(LocalTime time, int steps);
abstract void select(TimeSpinner spinner);
LocalTime decrement(LocalTime time, int steps) {
return increment(time, -steps);
}
}
// Property containing the current editing mode:
private final ObjectProperty<Mode> mode = new SimpleObjectProperty<>(Mode.HOURS) ;
public ObjectProperty<Mode> modeProperty() {
return mode;
}
public final Mode getMode() {
return modeProperty().get();
}
public final void setMode(Mode mode) {
modeProperty().set(mode);
}
public TimeSpinner(LocalTime time) {
setEditable(true);
// Create a StringConverter for converting between the text in the
// editor and the actual value:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
StringConverter<LocalTime> localTimeConverter = new StringConverter<LocalTime>() {
@Override
public String toString(LocalTime time) {
return formatter.format(time);
}
@Override
public LocalTime fromString(String string) {
String[] tokens = string.split(":");
int hours = getIntField(tokens, 0);
int minutes = getIntField(tokens, 1) ;
int seconds = getIntField(tokens, 2);
int totalSeconds = (hours * 60 + minutes) * 60 + seconds ;
return LocalTime.of((totalSeconds / 3600) % 24, (totalSeconds / 60) % 60, seconds % 60);
}
private int getIntField(String[] tokens, int index) {
if (tokens.length <= index || tokens[index].isEmpty()) {
return 0 ;
}
return Integer.parseInt(tokens[index]);
}
};
// The textFormatter both manages the text <-> LocalTime conversion,
// and vetoes any edits that are not valid. We just make sure we have
// two colons and only digits in between:
TextFormatter<LocalTime> textFormatter = new TextFormatter<LocalTime>(localTimeConverter, LocalTime.now(), c -> {
String newText = c.getControlNewText();
if (newText.matches("[0-9]{0,2}:[0-9]{0,2}:[0-9]{0,2}")) {
return c ;
}
return null ;
});
// The spinner value factory defines increment and decrement by
// delegating to the current editing mode:
SpinnerValueFactory<LocalTime> valueFactory = new SpinnerValueFactory<LocalTime>() {
{
setConverter(localTimeConverter);
setValue(time);
}
@Override
public void decrement(int steps) {
setValue(mode.get().decrement(getValue(), steps));
mode.get().select(TimeSpinner.this);
}
@Override
public void increment(int steps) {
setValue(mode.get().increment(getValue(), steps));
mode.get().select(TimeSpinner.this);
}
};
this.setValueFactory(valueFactory);
this.getEditor().setTextFormatter(textFormatter);
// Update the mode when the user interacts with the editor.
// This is a bit of a hack, e.g. calling spinner.getEditor().positionCaret()
// could result in incorrect state. Directly observing the caretPostion
// didn't work well though; getting that to work properly might be
// a better approach in the long run.
this.getEditor().addEventHandler(InputEvent.ANY, e -> {
int caretPos = this.getEditor().getCaretPosition();
int hrIndex = this.getEditor().getText().indexOf(':');
int minIndex = this.getEditor().getText().indexOf(':', hrIndex + 1);
if (caretPos <= hrIndex) {
mode.set( Mode.HOURS );
} else if (caretPos <= minIndex) {
mode.set( Mode.MINUTES );
} else {
mode.set( Mode.SECONDS );
}
});
// When the mode changes, select the new portion:
mode.addListener((obs, oldMode, newMode) -> newMode.select(this));
}
public TimeSpinner() {
this(LocalTime.now());
}
} |
Partager