ns-usbloader/src/main/java/nsusbloader/Controllers/SettingsController.java

322 lines
13 KiB
Java
Raw Normal View History

/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.Controllers;
import javafx.application.HostServices;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.Region;
2019-03-19 03:10:00 +03:00
import javafx.scene.layout.VBox;
import nsusbloader.AppPreferences;
2019-03-19 05:06:18 +03:00
import nsusbloader.ServiceWindow;
import nsusbloader.ModelControllers.UpdatesChecker;
2020-06-12 17:40:17 +03:00
import nsusbloader.Utilities.WindowsDrivers.DriversInstall;
import java.io.File;
2019-06-15 03:27:03 +03:00
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
2019-06-15 03:27:03 +03:00
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class SettingsController implements Initializable {
2019-08-14 05:37:58 +03:00
@FXML
2020-06-12 17:40:17 +03:00
private CheckBox nspFilesFilterForGLCB,
validateNSHostNameCb,
expertModeCb,
autoDetectIpCb,
randPortCb;
2019-03-19 03:10:00 +03:00
@FXML
2020-06-12 17:40:17 +03:00
private TextField pcIpTextField,
pcPortTextField,
pcExtraTextField;
2019-03-19 03:10:00 +03:00
@FXML
private CheckBox dontServeCb;
@FXML
private VBox expertSettingsVBox;
@FXML
private CheckBox autoCheckUpdCb;
@FXML
private Hyperlink newVersionLink;
2020-06-12 17:40:17 +03:00
@FXML
private CheckBox tfXciSpprtCb;
@FXML
2020-06-12 17:40:17 +03:00
private Button langBtn,
checkForUpdBtn,
drvInstBtn;
@FXML
private ChoiceBox<String> langCB;
@FXML
private CheckBox glOldVerCheck;
@FXML
private ChoiceBox<String> glOldVerChoice;
private HostServices hs;
private static final String[] oldGlSupportedVersions = {"v0.5", "v0.7.x"};
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
2019-08-14 05:37:58 +03:00
nspFilesFilterForGLCB.setSelected(AppPreferences.getInstance().getNspFileFilterGL());
validateNSHostNameCb.setSelected(AppPreferences.getInstance().getNsIpValidationNeeded());
2019-03-19 03:30:13 +03:00
expertSettingsVBox.setDisable(!AppPreferences.getInstance().getExpertMode());
2019-03-19 03:10:00 +03:00
2019-03-19 03:30:13 +03:00
expertModeCb.setSelected(AppPreferences.getInstance().getExpertMode());
expertModeCb.setOnAction(e-> expertSettingsVBox.setDisable(!expertModeCb.isSelected()));
2019-03-19 05:06:18 +03:00
2019-03-19 03:30:13 +03:00
autoDetectIpCb.setSelected(AppPreferences.getInstance().getAutoDetectIp());
2019-03-19 05:06:18 +03:00
pcIpTextField.setDisable(AppPreferences.getInstance().getAutoDetectIp());
autoDetectIpCb.setOnAction(e->{
pcIpTextField.setDisable(autoDetectIpCb.isSelected());
if (!autoDetectIpCb.isSelected())
pcIpTextField.requestFocus();
2019-03-19 05:06:18 +03:00
});
2019-03-19 03:30:13 +03:00
randPortCb.setSelected(AppPreferences.getInstance().getRandPort());
2019-03-19 05:06:18 +03:00
pcPortTextField.setDisable(AppPreferences.getInstance().getRandPort());
randPortCb.setOnAction(e->{
pcPortTextField.setDisable(randPortCb.isSelected());
if (!randPortCb.isSelected())
pcPortTextField.requestFocus();
2019-03-19 05:06:18 +03:00
});
2019-03-19 03:30:13 +03:00
2019-03-19 05:06:18 +03:00
if (AppPreferences.getInstance().getNotServeRequests()){
dontServeCb.setSelected(true);
autoDetectIpCb.setSelected(false);
autoDetectIpCb.setDisable(true);
pcIpTextField.setDisable(false);
randPortCb.setSelected(false);
randPortCb.setDisable(true);
pcPortTextField.setDisable(false);
}
pcExtraTextField.setDisable(!AppPreferences.getInstance().getNotServeRequests());
dontServeCb.setOnAction(e->{
if (dontServeCb.isSelected()){
autoDetectIpCb.setSelected(false);
autoDetectIpCb.setDisable(true);
pcIpTextField.setDisable(false);
randPortCb.setSelected(false);
randPortCb.setDisable(true);
pcPortTextField.setDisable(false);
pcExtraTextField.setDisable(false);
pcIpTextField.requestFocus();
2019-03-19 05:06:18 +03:00
}
else {
autoDetectIpCb.setDisable(false);
autoDetectIpCb.setSelected(true);
pcIpTextField.setDisable(true);
randPortCb.setDisable(false);
randPortCb.setSelected(true);
pcPortTextField.setDisable(true);
pcExtraTextField.setDisable(true);
}
});
2019-03-19 03:30:13 +03:00
2019-03-19 05:06:18 +03:00
pcIpTextField.setText(AppPreferences.getInstance().getHostIp());
pcPortTextField.setText(AppPreferences.getInstance().getHostPort());
pcExtraTextField.setText(AppPreferences.getInstance().getHostExtra());
2019-03-19 03:30:13 +03:00
2019-03-19 05:06:18 +03:00
pcIpTextField.setTextFormatter(new TextFormatter<>(change -> {
if (change.getControlNewText().contains(" ") | change.getControlNewText().contains("\t"))
return null;
else
return change;
}));
pcPortTextField.setTextFormatter(new TextFormatter<>(change -> {
2019-03-19 05:06:18 +03:00
if (change.getControlNewText().matches("^[0-9]{0,5}$")) {
2019-03-20 00:56:44 +03:00
if (!change.getControlNewText().isEmpty()
&& ((Integer.parseInt(change.getControlNewText()) > 65535) || (Integer.parseInt(change.getControlNewText()) == 0))
) {
2019-03-19 05:06:18 +03:00
ServiceWindow.getErrorNotification(resourceBundle.getString("windowTitleErrorPort"), resourceBundle.getString("windowBodyErrorPort"));
return null;
}
return change;
}
else
return null;
}));
pcExtraTextField.setTextFormatter(new TextFormatter<>(change -> {
if (change.getControlNewText().contains(" ") | change.getControlNewText().contains("\t"))
return null;
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();
});
2020-06-12 17:40:17 +03:00
if (isWindows()){
Region btnDrvImage = new Region();
btnDrvImage.getStyleClass().add("regionWindows");
drvInstBtn.setGraphic(btnDrvImage);
drvInstBtn.setVisible(true);
drvInstBtn.setOnAction(actionEvent -> new DriversInstall(resourceBundle));
}
tfXciSpprtCb.setSelected(AppPreferences.getInstance().getTfXCI());
// Language settings area
ObservableList<String> langCBObsList = FXCollections.observableArrayList();
langCBObsList.add("eng");
File jarFile;
try{
String encodedJarLocation = getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("+", "%2B");
jarFile = new File(URLDecoder.decode(encodedJarLocation, "UTF-8"));
}
catch (UnsupportedEncodingException uee){
uee.printStackTrace();
jarFile = null;
}
2019-06-15 03:27:03 +03:00
if(jarFile != null && jarFile.isFile()) { // Run with JAR file
2019-06-15 03:27:03 +03:00
try {
JarFile jar = new JarFile(jarFile);
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.startsWith("locale_"))
langCBObsList.add(name.substring(7, 10));
}
jar.close();
}
catch (IOException ioe){
ioe.printStackTrace(); // TODO: think about better solution?
}
}
else { // Run within IDE
URL resourceURL = this.getClass().getResource("/");
String[] filesList = new File(resourceURL.getFile()).list(); // Screw it. This WON'T produce NullPointerException
for (String jarFileName : filesList)
if (jarFileName.startsWith("locale_"))
langCBObsList.add(jarFileName.substring(7, 10));
}
langCB.setItems(langCBObsList);
if (langCBObsList.contains(AppPreferences.getInstance().getLanguage()))
langCB.getSelectionModel().select(AppPreferences.getInstance().getLanguage());
else
langCB.getSelectionModel().select("eng");
langBtn.setOnAction(e->{
AppPreferences.getInstance().setLanguage(langCB.getSelectionModel().getSelectedItem());
ServiceWindow.getInfoNotification("",
ResourceBundle.getBundle("locale", new Locale(langCB.getSelectionModel().getSelectedItem()))
.getString("windowBodyRestartToApplyLang"));
});
// Set supported old versions
glOldVerChoice.getItems().addAll(oldGlSupportedVersions);
String oldVer = AppPreferences.getInstance().getUseOldGlVersion(); // Overhead; Too much validation of consistency
if (Arrays.asList(oldGlSupportedVersions).contains(oldVer)) {
glOldVerChoice.getSelectionModel().select(oldVer);
glOldVerChoice.setDisable(false);
glOldVerCheck.setSelected(true);
}
else {
glOldVerChoice.getSelectionModel().select(0);
glOldVerChoice.setDisable(true);
glOldVerCheck.setSelected(false);
}
glOldVerCheck.setOnAction(e-> glOldVerChoice.setDisable(! glOldVerCheck.isSelected()) );
}
2020-06-12 17:40:17 +03:00
private boolean isWindows(){
return System.getProperty("os.name").toLowerCase().replace(" ", "").contains("windows");
}
2019-08-14 05:37:58 +03:00
public boolean getNSPFileFilterForGL(){return nspFilesFilterForGLCB.isSelected(); }
2019-03-19 05:06:18 +03:00
public boolean getExpertModeSelected(){ return expertModeCb.isSelected(); }
public boolean getAutoIpSelected(){ return autoDetectIpCb.isSelected(); }
public boolean getRandPortSelected(){ return randPortCb.isSelected(); }
public boolean getNotServeSelected(){ return dontServeCb.isSelected(); }
2019-03-19 03:30:13 +03:00
public boolean isNsIpValidate(){ return validateNSHostNameCb.isSelected(); }
2019-03-19 05:06:18 +03:00
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 boolean getTfXciNszXczSupport(){ return tfXciSpprtCb.isSelected(); } // Used also for NSZ/XCZ
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);
}
public String getGlOldVer() {
if (glOldVerCheck.isSelected())
return glOldVerChoice.getValue();
else
return "";
}
2019-03-19 05:06:18 +03:00
}