1. Add 'check for new versions' feature.
2. Fixed broken (was it broken?) error pop-up window.
This commit is contained in:
Dmitry Isaenko 2019-03-23 20:05:48 +03:00
parent 4c1fc36f9d
commit cc00294b06
15 changed files with 229 additions and 21 deletions

View file

@ -22,7 +22,8 @@ public class AppPreferences {
boolean NotServe,
String HostIp,
String HostPort,
String HostExtra
String HostExtra,
boolean autoCheck4Updates
){
setProtocol(Protocol);
setRecent(PreviouslyOpened);
@ -36,6 +37,7 @@ public class AppPreferences {
setHostIp(HostIp);
setHostPort(HostPort);
setHostExtra(HostExtra);
setAutoCheckUpdates(autoCheck4Updates);
}
public String getTheme(){
String theme = preferences.get("THEME", "/res/app_dark.css"); // Don't let user to change settings manually
@ -95,4 +97,7 @@ public class AppPreferences {
public String getHostExtra(){ return preferences.get("HOSTEXTRA", "").replaceAll("(\\s)|(\t)", "");} // oh just shut up...
public void setHostExtra(String postfix){preferences.put("HOSTEXTRA", postfix);}
public boolean getAutoCheckUpdates(){return preferences.getBoolean("AUTOCHECK4UPDATES", false); }
public void setAutoCheckUpdates(boolean prop){preferences.putBoolean("AUTOCHECK4UPDATES", prop); }
}

View file

@ -1,5 +1,6 @@
package nsusbloader.Controllers;
import javafx.application.HostServices;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
@ -11,11 +12,9 @@ import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.stage.FileChooser;
import nsusbloader.AppPreferences;
import nsusbloader.MediatorControl;
import nsusbloader.*;
import nsusbloader.ModelControllers.UpdatesChecker;
import nsusbloader.NET.NETCommunications;
import nsusbloader.NSLMain;
import nsusbloader.ServiceWindow;
import nsusbloader.USB.UsbCommunications;
import java.io.File;
@ -140,7 +139,28 @@ public class NSLMainController implements Initializable {
this.switchThemeBtn.setOnAction(e->switchTheme());
previouslyOpenedPath = AppPreferences.getInstance().getRecent();
if (AppPreferences.getInstance().getAutoCheckUpdates()){
Task<List<String>> updTask = new UpdatesChecker();
updTask.setOnSucceeded(event->{
List<String> result = updTask.getValue();
if (result != null){
if (!result.get(0).isEmpty())
SettingsTabController.setNewVersionLink(result.get(0));
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionAval"), resourceBundle.getString("windowTitleNewVersionAval")+": "+result.get(0) + "\n\n" + result.get(1));
}
else
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionUnknown"), resourceBundle.getString("windowBodyNewVersionUnknown"));
});
Thread updates = new Thread(updTask);
updates.setDaemon(true);
updates.start();
}
}
/**
* Provide hostServices to Settings tab
* */
public void setHostServices(HostServices hs ){ SettingsTabController.registerHostServices(hs);}
/**
* Changes UI theme on the go
* */
@ -329,7 +349,8 @@ public class NSLMainController implements Initializable {
SettingsTabController.getNotServeSelected(),
SettingsTabController.getHostIp(),
SettingsTabController.getHostPort(),
SettingsTabController.getHostExtra()
SettingsTabController.getHostExtra(),
SettingsTabController.getAutoCheckForUpdates()
);
}
}

View file

@ -1,15 +1,18 @@
package nsusbloader.Controllers;
import javafx.application.HostServices;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.*;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import nsusbloader.AppPreferences;
import nsusbloader.ServiceWindow;
import nsusbloader.ModelControllers.UpdatesChecker;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
public class SettingsController implements Initializable {
@ -36,6 +39,15 @@ public class SettingsController implements Initializable {
@FXML
private VBox expertSettingsVBox;
@FXML
private CheckBox autoCheckUpdCb;
@FXML
private Hyperlink newVersionLink;
@FXML
private Button checkForUpdBtn;
private HostServices hs;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
validateNSHostNameCb.setSelected(AppPreferences.getInstance().getNsIpValidationNeeded());
@ -131,6 +143,39 @@ public class SettingsController implements Initializable {
else
return change;
}));
newVersionLink.setVisible(false);
newVersionLink.setOnAction(e->{
hs.showDocument(newVersionLink.getText());
});
autoCheckUpdCb.setSelected(AppPreferences.getInstance().getAutoCheckUpdates());
Region btnSwitchImage = new Region();
btnSwitchImage.getStyleClass().add("regionUpdatesCheck");
checkForUpdBtn.setGraphic(btnSwitchImage);
checkForUpdBtn.setOnAction(e->{
Task<List<String>> updTask = new UpdatesChecker();
updTask.setOnSucceeded(event->{
List<String> result = updTask.getValue();
if (result != null){
if (result.get(0).isEmpty()){
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionNOTAval"), resourceBundle.getString("windowBodyNewVersionNOTAval"));
}
else {
setNewVersionLink(result.get(0));
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionAval"), resourceBundle.getString("windowTitleNewVersionAval")+": "+result.get(0) + "\n\n" + result.get(1));
}
}
else {
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionUnknown"), resourceBundle.getString("windowBodyNewVersionUnknown"));
}
});
Thread updates = new Thread(updTask);
updates.setDaemon(true);
updates.start();
});
}
public boolean getExpertModeSelected(){ return expertModeCb.isSelected(); }
@ -143,4 +188,12 @@ public class SettingsController implements Initializable {
public String getHostIp(){ return pcIpTextField.getText(); }
public String getHostPort(){ return pcPortTextField.getText(); }
public String getHostExtra(){ return pcExtraTextField.getText(); }
public boolean getAutoCheckForUpdates(){ return autoCheckUpdCb.isSelected(); }
public void registerHostServices(HostServices hostServices){this.hs = hostServices;}
public void setNewVersionLink(String newVer){
newVersionLink.setVisible(true);
newVersionLink.setText("https://github.com/developersu/ns-usbloader/releases/tag/"+newVer);
}
}

View file

@ -0,0 +1,63 @@
package nsusbloader.ModelControllers;
import javafx.concurrent.Task;
import nsusbloader.NSLMain;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class UpdatesChecker extends Task<List<String>> {
@Override
protected List<String> call() {
String respondedJson;
try {
URL gitHubUrl = new URL("https://api.github.com/repos/developersu/ns-usbloader/releases/latest");
HttpsURLConnection connection = (HttpsURLConnection) gitHubUrl.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/vnd.github.v3+json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
respondedJson = br.readLine();
br.close();
connection.disconnect();
if (respondedJson == null)
return null;
}
catch (IOException mue){
return null;
}
String newVersion = respondedJson.replaceAll("(^.*\"tag_name\":\")(.?|.+?)(\".+$)", "$2");
String changeLog = respondedJson.replaceAll("(^.*\"body\":\")(.?|.+?)(\".+$)", "$2")
.replaceAll("\\\\r\\\\n","\n")
.replaceAll("#+?\\s", ""); // replace #### dsds | # dsds
if (newVersion.matches("^v(([0-9])+?\\.)+[0-9]+(-.+)$")) // if new version have postfix like v0.1-Experimental
newVersion = newVersion.replaceAll("(-.*$)", ""); // cut postfix
if ( ! newVersion.matches("^v(([0-9])+?\\.)+[0-9]+$")) { // check if new version structure valid
return null;
}
List<String> returningValue = new ArrayList<>();
if (!newVersion.equals(NSLMain.appVersion)) { // if latest noted version in GitHub is different to this version
returningValue.add(newVersion);
returningValue.add(changeLog);
return returningValue;
}
returningValue.add("");
returningValue.add("");
return returningValue;
}
}

View file

@ -12,7 +12,7 @@ import java.util.Locale;
import java.util.ResourceBundle;
public class NSLMain extends Application {
public static final String appVersion = "v0.3";
public static final String appVersion = "v0.3.1";
@Override
public void start(Stage primaryStage) throws Exception{
@ -48,6 +48,7 @@ public class NSLMain extends Application {
});
NSLMainController controller = loader.getController();
controller.setHostServices(getHostServices());
primaryStage.setOnHidden(e-> controller.exit());
}

View file

@ -2,24 +2,36 @@ package nsusbloader;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import java.util.Optional;
public class ServiceWindow {
/**
* Create window with notification
* */
/** Create window with error notification */
public static void getErrorNotification(String title, String body){
Alert alertBox = new Alert(Alert.AlertType.ERROR);
getNotification(title, body, Alert.AlertType.ERROR);
}
/** Create window with information notification */
public static void getInfoNotification(String title, String body){
getNotification(title, body, Alert.AlertType.INFORMATION);
}
/** Real window creator */
private static void getNotification(String title, String body, Alert.AlertType type){
Alert alertBox = new Alert(type);
alertBox.setTitle(title);
alertBox.setHeaderText(null);
alertBox.setContentText(body);
alertBox.getDialogPane().setMinWidth(Region.USE_PREF_SIZE);
alertBox.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alertBox.setResizable(true); // Java bug workaround for JDR11/OpenJFX. TODO: nothing. really.
alertBox.setResizable(false);
alertBox.getDialogPane().getStylesheets().add(AppPreferences.getInstance().getTheme());
Stage dialogStage = (Stage) alertBox.getDialogPane().getScene().getWindow();
dialogStage.setAlwaysOnTop(true);
dialogStage.toFront();
alertBox.show();
}
/**