Skip to content

Commit 24ac2b4

Browse files
authored
Add files via upload
1 parent 037fa54 commit 24ac2b4

29 files changed

Lines changed: 2086 additions & 0 deletions

src/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: app.Main
3+

src/app/Controller.java

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
}

src/app/Main.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package app;
2+
3+
import javafx.application.Application;
4+
import javafx.beans.Observable;
5+
import javafx.beans.property.BooleanProperty;
6+
import javafx.beans.property.SimpleBooleanProperty;
7+
import javafx.collections.FXCollections;
8+
import javafx.collections.ListChangeListener;
9+
import javafx.collections.ObservableList;
10+
import javafx.fxml.FXMLLoader;
11+
import javafx.scene.Parent;
12+
import javafx.scene.Scene;
13+
import javafx.scene.image.Image;
14+
import javafx.stage.Stage;
15+
import model.Track;
16+
import model.TrackDatabase;
17+
import utils.SerializationUtils;
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.util.Objects;
21+
22+
public class Main extends Application {
23+
public static ObservableList<Track> list =
24+
FXCollections.observableArrayList(
25+
track -> new Observable[] {track.groupProperty(),
26+
track.trackNameProperty(),
27+
track.dateCreatedProperty(),
28+
track.timeProperty()});
29+
public static String getFilePath() {
30+
String path = System.getProperty("user.home") + File.separator + "Documents";
31+
path += File.separator + "JTimer";
32+
return path;
33+
}
34+
35+
36+
static File customDir = new File(getFilePath());
37+
static File file = new File(customDir, "database.db");
38+
39+
@Override
40+
public void start(Stage primaryStage) throws Exception{
41+
if (!customDir.exists()) {
42+
boolean mkdir = customDir.mkdir();
43+
}
44+
boolean empty = !file.exists() || file.length() == 0;
45+
46+
47+
if (!empty) {
48+
TrackDatabase.INSTANCE = (TrackDatabase) SerializationUtils.deserialize(file.getAbsolutePath());
49+
}
50+
51+
52+
BooleanProperty isSaved = new SimpleBooleanProperty(false);
53+
ObservableList<Track> list = TrackDatabase.INSTANCE.getTracks();
54+
list.addListener((ListChangeListener<Track>) c -> isSaved.set(true));
55+
56+
Image icon16 = new Image("/app/icon16.png");
57+
Image icon24 = new Image("/app/icon24.png");
58+
Image icon32 = new Image("/app/icon32.png");
59+
Image icon48 = new Image("/app/icon48.png");
60+
61+
primaryStage.getIcons().setAll(icon16,icon24,icon32,icon48);
62+
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("home.fxml")));
63+
primaryStage.setTitle("JTimer");
64+
primaryStage.setScene(new Scene(root));
65+
primaryStage.show();
66+
primaryStage.setOnCloseRequest(event -> {
67+
if (isSaved.get()) {
68+
save();
69+
}
70+
71+
});
72+
}
73+
74+
75+
public static void save() {
76+
try {
77+
SerializationUtils.serialize(TrackDatabase.getINSTANCE(),file.getAbsolutePath());
78+
} catch (IOException e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
83+
84+
public static void main(String[] args) {
85+
launch(args);
86+
}
87+
88+
89+
}

0 commit comments

Comments
 (0)