-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
185 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package app.nush.examclock.display; | ||
|
||
import app.nush.examclock.model.CustomBinding; | ||
import javafx.beans.property.SimpleIntegerProperty; | ||
import javafx.beans.property.SimpleObjectProperty; | ||
import javafx.geometry.Bounds; | ||
import javafx.scene.control.TextField; | ||
|
||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import static app.nush.examclock.controllers.AddExamController.parseTime; | ||
|
||
public class TimePicker extends TextField { | ||
public static final DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern("hh:mm a"); | ||
private final TimePopup popup; | ||
public SimpleObjectProperty<LocalTime> timeProperty; | ||
public SimpleIntegerProperty hour; | ||
public SimpleIntegerProperty minute; | ||
|
||
public TimePicker() { | ||
timeProperty = new SimpleObjectProperty<>(); | ||
hour = new SimpleIntegerProperty(0); | ||
minute = new SimpleIntegerProperty(0); | ||
|
||
popup = new TimePopup(this); | ||
|
||
CustomBinding.bindBidirectional(timeProperty, hour, LocalTime::getHour, hour -> LocalTime.of(hour.intValue(), minute.get())); | ||
CustomBinding.bindBidirectional(timeProperty, minute, LocalTime::getMinute, minute -> LocalTime.of(hour.get(), minute.intValue())); | ||
CustomBinding.bindBidirectional(timeProperty, textProperty(), localTime -> localTime.format(defaultFormatter), text -> { | ||
try { | ||
return parseTime(text, 0); | ||
} catch (Exception ignored) { | ||
return timeProperty.get(); | ||
} | ||
}); | ||
|
||
setPromptText("00:00 am"); | ||
focusedProperty().addListener((observable, oldValue, newValue) -> { | ||
if (!oldValue && newValue) { //gain focus | ||
Bounds bounds = localToScreen(getBoundsInLocal()); | ||
popup.show(TimePicker.this, bounds.getMinX(), bounds.getMaxY()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package app.nush.examclock.display; | ||
|
||
import app.nush.examclock.model.CustomBinding; | ||
import javafx.scene.Node; | ||
import javafx.scene.control.PopupControl; | ||
import javafx.scene.control.ScrollPane; | ||
import javafx.scene.control.Skin; | ||
import javafx.scene.layout.HBox; | ||
import tornadofx.control.ListItem; | ||
import tornadofx.control.ListMenu; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class TimePopup extends PopupControl { | ||
|
||
public TimePopup(TimePicker picker) { | ||
HBox root = new HBox(); | ||
root.setPrefHeight(300); | ||
ListMenu hourMenu = new ListMenu(); | ||
List<ListItem> hourMenuItems = IntStream.range(0, 24).mapToObj(i -> new ListItem(String.valueOf(i))).collect(Collectors.toList()); | ||
hourMenu.getChildren().addAll(hourMenuItems); | ||
|
||
ListMenu minuteMenu = new ListMenu(); | ||
List<ListItem> minuteMenuItems = IntStream.range(0, 60).mapToObj(i -> new ListItem(String.valueOf(i))).collect(Collectors.toList()); | ||
minuteMenu.getChildren().addAll(minuteMenuItems); | ||
|
||
CustomBinding.bindBidirectional(hourMenu.activeProperty(), picker.hour, listItem -> Integer.parseInt(listItem.getText()), hour -> hourMenuItems.get((Integer) hour)); | ||
CustomBinding.bindBidirectional(minuteMenu.activeProperty(), picker.minute, listItem -> Integer.parseInt(listItem.getText()), minute -> minuteMenuItems.get((Integer) minute)); | ||
root.getChildren().addAll(new ScrollPane(hourMenu) {{ | ||
setVbarPolicy(ScrollBarPolicy.ALWAYS); | ||
setHbarPolicy(ScrollBarPolicy.NEVER); | ||
}}, new ScrollPane(minuteMenu) {{ | ||
setVbarPolicy(ScrollBarPolicy.ALWAYS); | ||
setHbarPolicy(ScrollBarPolicy.NEVER); | ||
}}); | ||
|
||
setSkin(new Skin<TimePopup>() { | ||
public TimePopup getSkinnable() { | ||
return TimePopup.this; | ||
} | ||
|
||
public Node getNode() { | ||
return root; | ||
} | ||
|
||
public void dispose() { | ||
} | ||
}); | ||
setHideOnEscape(true); | ||
setAutoFix(true); | ||
setAutoHide(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package app.nush.examclock.model; | ||
|
||
import javafx.beans.property.Property; | ||
import javafx.beans.value.ChangeListener; | ||
import javafx.beans.value.ObservableValue; | ||
import javafx.beans.value.WritableValue; | ||
|
||
import java.util.function.Function; | ||
|
||
public class CustomBinding { | ||
|
||
public static <A, B> void bindBidirectional(Property<A> propertyA, Property<B> propertyB, Function<A, B> updateB, Function<B, A> updateA) { | ||
addFlaggedChangeListener(propertyA, propertyB, updateB); | ||
addFlaggedChangeListener(propertyB, propertyA, updateA); | ||
} | ||
|
||
public static <A, B> void bind(Property<A> propertyA, Property<B> propertyB, Function<A, B> updateB) { | ||
addFlaggedChangeListener(propertyA, propertyB, updateB); | ||
} | ||
|
||
private static <X, Y> void addFlaggedChangeListener(ObservableValue<X> propertyX, WritableValue<Y> propertyY, Function<X, Y> updateY) { | ||
propertyX.addListener(new ChangeListener<X>() { | ||
private boolean alreadyCalled = false; | ||
|
||
@Override | ||
public void changed(ObservableValue<? extends X> observable, X oldValue, X newValue) { | ||
if (alreadyCalled) return; | ||
try { | ||
alreadyCalled = true; | ||
propertyY.setValue(updateY.apply(newValue)); | ||
} finally { | ||
alreadyCalled = false; | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters