1 /*
2 * Copyright (c) Patrick Magauran 2018.
3 * Licensed under the AGPLv3. All conditions of said license apply.
4 * This file is part of ABOS.
5 *
6 * ABOS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ABOS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with ABOS. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package Launchers;
21
22 import Controllers.MainController;
23 import Utilities.LogToFile;
24 import Utilities.Severity;
25 import com.mchange.v2.log.slf4j.Slf4jMLog;
26 import javafx.application.Application;
27 import javafx.fxml.FXML;
28 import javafx.fxml.FXMLLoader;
29 import javafx.scene.Parent;
30 import javafx.scene.Scene;
31 import javafx.scene.control.Alert;
32 import javafx.scene.control.ButtonBar;
33 import javafx.scene.control.ButtonType;
34 import javafx.scene.control.TreeView;
35 import javafx.stage.Stage;
36 import javafx.stage.StageStyle;
37
38 import java.awt.*;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43 import java.net.URI;
44 import java.net.URISyntaxException;
45 import java.net.URL;
46 import java.net.URLConnection;
47 import java.util.Objects;
48 import java.util.Optional;
49 import java.util.Properties;
50
51 /**
52 * Sample application to demonstrate programming an FXML interface.
53 */
54 public class Main extends Application {
55 @FXML
56 private TreeView<String> selectNav;
57
58 // main method is only for legacy support - java 8 won't call it for a javafx application.
59 public static void main(String[] args) { launch(args); }
60
61 public Boolean checkUpdates() {
62 // Make a URL to the web page
63 try {
64 URL url = new URL("https://abos-software.gitlab.io/version-2.html");
65
66 // Get the input stream through URL Connection
67 URLConnection con = url.openConnection();
68 InputStream is = con.getInputStream();
69
70 // Once you have the Input Stream, it's just plain old Java IO stuff.
71
72 // For this case, since you are interested in getting plain-text web page
73 // I'll use a reader and output the text content to System.out.
74
75 // For binary content, it's better to directly read the bytes from stream and write
76 // to the target file.
77
78
79 BufferedReader br = new BufferedReader(new InputStreamReader(is));
80
81 String line;
82 final Properties properties = new Properties();
83 properties.load(this.getClass().getResourceAsStream("/CompileProps.properties"));
84 // read each line and write to System.out
85 while ((line = br.readLine()) != null) {
86 if (!Objects.equals(properties.getProperty("Version"), line)) {
87 return true;
88 }
89 }
90 } catch (IOException e) {
91 LogToFile.log(e, Severity.WARNING, "Error checking for updates. If error persists, reinstall and contact developer.");
92 }
93
94
95 return false;
96 }
97
98 @Override
99 public void start(final Stage stage) throws Exception {
100 // load the scene fxml UI.
101 // grabs the UI scenegraph view from the loader.
102 // grabs the UI controller for the view from the loader.
103 Slf4jMLog.config("");
104 final FXMLLoader loader = new FXMLLoader(getClass().getResource("/UI/Main.fxml"));
105 final Parent root = loader.load();
106 final MainController controller = loader.getController();
107 if (checkUpdates()) {
108 Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
109 alert.setTitle("Update Available");
110 alert.setHeaderText("An Update is Available");
111 alert.setContentText("If you choose to download, download the latest tag's java Artifacts");
112
113 ButtonType buttonTypeOne = new ButtonType("Download");
114 ButtonType buttonTypeTwo = new ButtonType("Remind Me Later", ButtonBar.ButtonData.CANCEL_CLOSE);
115
116 alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo);
117
118 Optional<ButtonType> result = alert.showAndWait();
119 if (result.get() == buttonTypeOne) {
120 if (Desktop.isDesktopSupported()) {
121 new Thread(() -> {
122 try {
123 Desktop.getDesktop().browse(new URI("https://abos-software.gitlab.io/download/"));
124
125 } catch (URISyntaxException | IOException e) {
126 LogToFile.log(e, Severity.WARNING, "Error opening download window. Please try navigating to https://gitlab.com/RoatingFans/ABOS/tags");
127 }
128 }).start();
129
130 }
131 }
132 }
133 // continuously refresh the TreeItems.
134 // demonstrates using controller methods to manipulate the controlled UI.
135 /* final Timeline timeline = new Timeline(
136 new KeyFrame(
137 Duration.seconds(3),
138 new TreeLoadingEventHandler(controller)
139 )
140 );
141 timeline.setCycleCount(Timeline.INDEFINITE);
142 timeline.play();*/
143
144 // close the app if the user clicks on anywhere on the window.
145 // just provides a simple way to kill the demo app.
146 /* root.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
147 @Override public void handle(MouseEvent t) {
148 stage.hide();
149 }
150 });*/
151
152 // initialize the stage.
153 stage.setScene(new Scene(root));
154 stage.getScene().getStylesheets().add("UI/Main.css");
155 stage.initStyle(StageStyle.UNIFIED);
156 stage.setMinWidth(700);
157 stage.setMinHeight(600);
158 stage.setOnCloseRequest(windowEvent -> {
159 if (windowEvent.getSource() != stage.getOwner()) {
160 stage.close();
161 } else {
162 windowEvent.consume();
163 }
164 });
165 stage.setMaximized(true);
166 Stage masterStage = stage;
167
168 controller.initialize(stage, getParameters());
169
170 stage.show();
171 }
172
173 private void updateSelectedItem(Object newValue) {
174 }
175
176
177
178
179 /*
180 Adds the year buttons to the main panel.
181 */
182 /* private void addYears() {
183 Collection<String> ret = new ArrayList<>();
184 ///Select all years
185 try (PreparedStatement prep = Utilities.DbInt.getPrep("Set", "SELECT Years.YEARS FROM Years");
186 ResultSet rs = prep.executeQuery()
187 ) {
188
189
190 while (rs.next()) {
191
192 ret.add(rs.getString(1));
193
194 }
195
196 rs.close();
197 if (Utilities.DbInt.pCon != null) {
198 //Utilities.DbInt.pCon.close();
199 Utilities.DbInt.pCon = null;
200 }
201 } catch (Exception e) {
202 Utilities.LogToFile.log(e, Utilities.Severity.SEVERE, "Error while Selecting years from Database");
203
204 //e.printStackTrace();
205 // System.out.println("Error Start");
206
207 // System.out.println(e.getErrorCode());
208 // System.out.println(e.getSQLState());
209 // System.out.println(e.getLocalizedMessage());
210 // System.out.println(e.getMessage());
211 // System.out.println("Error end");
212
213
214 }
215 //Create a button for each year
216 for (String aRet : ret) {
217 JButton b = new JButton(aRet);
218 b.addActionListener(e -> {
219 //On button click open Utilities.Year window
220 new YearWindow(((AbstractButton) e.getSource()).getText());
221
222 });
223 panel_1.add(b);
224 }
225
226 }*/
227 }