|
| 1 | +package app; |
| 2 | + |
| 3 | +import javafx.animation.TranslateTransition; |
| 4 | +import javafx.beans.binding.BooleanBinding; |
| 5 | +import javafx.beans.property.BooleanProperty; |
| 6 | +import javafx.beans.property.IntegerProperty; |
| 7 | +import javafx.beans.property.SimpleBooleanProperty; |
| 8 | +import javafx.beans.property.SimpleIntegerProperty; |
| 9 | +import javafx.collections.ObservableList; |
| 10 | +import javafx.collections.transformation.FilteredList; |
| 11 | +import javafx.collections.transformation.SortedList; |
| 12 | +import javafx.fxml.FXMLLoader; |
| 13 | +import javafx.fxml.Initializable; |
| 14 | +import javafx.geometry.Pos; |
| 15 | +import javafx.scene.Parent; |
| 16 | +import javafx.scene.control.*; |
| 17 | +import javafx.scene.layout.*; |
| 18 | +import javafx.util.Callback; |
| 19 | +import javafx.util.Duration; |
| 20 | +import model.Track; |
| 21 | +import model.TrackDatabase; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.URL; |
| 25 | +import java.time.LocalDate; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.ResourceBundle; |
| 30 | + |
| 31 | +public class Controller implements Initializable { |
| 32 | + |
| 33 | + |
| 34 | + public static Map<View, Parent> cache = new HashMap<>(); |
| 35 | + public ListView<Track> unitListView; |
| 36 | + public BorderPane mainPane; |
| 37 | + public VBox vbCenter; |
| 38 | + public VBox vbRight; |
| 39 | + public ToggleGroup topBarGroupBtn; |
| 40 | + public Button createBtn; |
| 41 | + public TextField trackTextField; |
| 42 | + public TextField groupTextField; |
| 43 | + public TextField searchTextField; |
| 44 | + public Button showRightBtn; |
| 45 | + TrackDatabase data = TrackDatabase.getINSTANCE(); |
| 46 | + ObservableList<Track> list = data.getTracks(); |
| 47 | + public static IntegerProperty trackIndex = new SimpleIntegerProperty(-1); |
| 48 | + public final String TEXT_LABEL_STYLE = "-fx-font: 14px \"Segoe UI\";\n" + |
| 49 | + " -fx-text-fill: white;"; |
| 50 | + |
| 51 | + public ToggleButton pomodoroTogBtn; |
| 52 | + public ToggleButton stopwatchTogBtn; |
| 53 | + public ToggleButton timerTogBtn; |
| 54 | + public ToggleButton reportTogBtn; |
| 55 | + boolean isShowPane = true; |
| 56 | + BooleanProperty reportMode = new SimpleBooleanProperty(false); |
| 57 | + @Override |
| 58 | + public void initialize(URL location, ResourceBundle resources) { |
| 59 | + showRightBtn.disableProperty().bind(reportMode); |
| 60 | + unitListView.setItems(list); |
| 61 | + topBarGroupBtn.selectedToggleProperty().addListener((obsVal, oldVal, newVal) -> { |
| 62 | + if (newVal == null) |
| 63 | + oldVal.setSelected(true); |
| 64 | + }); |
| 65 | + |
| 66 | + |
| 67 | + unitListView.setCellFactory(getCellFactory()); |
| 68 | + BooleanBinding bb = new BooleanBinding() { |
| 69 | + { |
| 70 | + super.bind(trackTextField.textProperty(), |
| 71 | + groupTextField.textProperty()); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected boolean computeValue() { |
| 76 | + return (trackTextField.getText().isBlank() |
| 77 | + || groupTextField.getText().isBlank()); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + createBtn.disableProperty().bind(bb); |
| 82 | + |
| 83 | + unitListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { |
| 84 | + trackIndex.setValue(list.indexOf(newValue)); |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + loadCenterPane(View.POMODORO); |
| 91 | + |
| 92 | + FilteredList<Track> filteredList = new FilteredList<>(list, b -> true); |
| 93 | + searchTextField.textProperty().addListener((observable, oldValue, newValue) -> filteredList.setPredicate(track -> { |
| 94 | + if (newValue == null || newValue.isEmpty()) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + String lowerCaseFilter = newValue.toLowerCase(); |
| 98 | + return track.getTrackName().toLowerCase().contains(lowerCaseFilter); |
| 99 | + })); |
| 100 | + |
| 101 | + SortedList<Track> sortedList = new SortedList<>(filteredList); |
| 102 | + unitListView.setItems(sortedList); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + private Callback<ListView<Track>, ListCell<Track>> getCellFactory() { |
| 107 | + return new Callback<>() { |
| 108 | + @Override |
| 109 | + public ListCell<Track> call(ListView<Track> param) { |
| 110 | + return new ListCell<>() { |
| 111 | + @Override |
| 112 | + protected void updateItem(Track item, boolean empty) { |
| 113 | + super.updateItem(item, empty); |
| 114 | + if (!empty) { |
| 115 | + HBox box = new HBox(); |
| 116 | + box.setSpacing(50); |
| 117 | + Label name = new Label(); |
| 118 | + VBox box1 = new VBox(); |
| 119 | + box1.setPrefWidth(136); |
| 120 | + box1.setMaxWidth(box1.getPrefWidth()); |
| 121 | + name.textProperty().bind(item.trackNameProperty()); |
| 122 | + name.setStyle(TEXT_LABEL_STYLE); |
| 123 | + box1.getChildren().add(name); |
| 124 | + Label time = new Label(); |
| 125 | + time.setStyle(TEXT_LABEL_STYLE); |
| 126 | + time.textProperty().bind(item.timeProperty()); |
| 127 | + HBox timeBox = new HBox(); |
| 128 | + timeBox.getChildren().add(time); |
| 129 | + timeBox.setAlignment(Pos.CENTER_RIGHT); |
| 130 | + HBox.setHgrow(timeBox, Priority.ALWAYS); |
| 131 | + box.getChildren().addAll(box1, timeBox); |
| 132 | + setGraphic(box); |
| 133 | + |
| 134 | + } else { |
| 135 | + setGraphic(null); |
| 136 | + } |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + public void manageRightPane() { |
| 145 | + isShowPane = !isShowPane; |
| 146 | + if (isShowPane) { |
| 147 | + showRightPane(); |
| 148 | + } else { |
| 149 | + hideRightPane(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public void getCenterPane() { |
| 154 | + reportMode.setValue(false); |
| 155 | + showRightPane(); |
| 156 | + isShowPane = true; |
| 157 | + if (pomodoroTogBtn.isSelected()) { |
| 158 | + loadCenterPane(View.POMODORO); |
| 159 | + } else if (timerTogBtn.isSelected()) { |
| 160 | + loadCenterPane(View.TIMER); |
| 161 | + } else if (stopwatchTogBtn.isSelected()) { |
| 162 | + loadCenterPane(View.STOPWATCH); |
| 163 | + } else if (reportTogBtn.isSelected()) { |
| 164 | + reportMode.setValue(true); |
| 165 | + vbRight.setVisible(false); |
| 166 | + vbRight.setManaged(false); |
| 167 | + hideRightPane(); |
| 168 | + isShowPane = false; |
| 169 | + loadCenterPane(View.REPORT); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public void showRightPane() { |
| 174 | + vbRight.setManaged(true); |
| 175 | + vbRight.setVisible(true); |
| 176 | + TranslateTransition slide = new TranslateTransition(); |
| 177 | + slide.setDuration(Duration.seconds(0.4)); |
| 178 | + slide.setNode(vbRight); |
| 179 | + slide.setToX(0); |
| 180 | + slide.play(); |
| 181 | + slide.setOnFinished(e -> { |
| 182 | + } |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + public void hideRightPane() { |
| 187 | + TranslateTransition slide = new TranslateTransition(); |
| 188 | + slide.setDuration(Duration.seconds(0.4)); |
| 189 | + slide.setNode(vbRight); |
| 190 | + slide.setToX(305); |
| 191 | + slide.play(); |
| 192 | + slide.setOnFinished(e -> { |
| 193 | + if (vbRight.isManaged()) { |
| 194 | + vbRight.setVisible(false); |
| 195 | + vbRight.setManaged(false); |
| 196 | + } |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + public void loadCenterPane(View view) { |
| 201 | + |
| 202 | + |
| 203 | + try { |
| 204 | + Parent root; |
| 205 | + if (cache.containsKey(view)) { |
| 206 | + root = cache.get(view); |
| 207 | + } else { |
| 208 | + root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource(view.getFilename()))); |
| 209 | + cache.put(view, root); |
| 210 | + } |
| 211 | + |
| 212 | + mainPane.setCenter(root); |
| 213 | + } catch (IOException e) { |
| 214 | + e.printStackTrace(); |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + |
| 220 | + public void create() { |
| 221 | + String dateCreated = LocalDate.now().toString(); |
| 222 | + list.add(new Track(groupTextField.getText(), trackTextField.getText(), |
| 223 | + dateCreated)); |
| 224 | + groupTextField.clear(); |
| 225 | + trackTextField.clear(); |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + public void onEnter() { |
| 230 | + if (!createBtn.isDisable()) { |
| 231 | + createBtn.fire(); |
| 232 | + trackTextField.requestFocus(); |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments