Skip to content

Commit d1500f2

Browse files
committed
Settings-tab refactoring: move Tinfoil-related options block to separate fxml, create controller, update names, remove code redundancy and simplify.
1 parent ceb6949 commit d1500f2

File tree

6 files changed

+262
-200
lines changed

6 files changed

+262
-200
lines changed

src/main/java/nsusbloader/Controllers/FrontController.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private void selectFilesBtnAction(){
199199
else
200200
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
201201

202-
if (getSelectedProtocol().equals("TinFoil") && MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTfXciNszXczSupport())
202+
if (getSelectedProtocol().equals("TinFoil") && MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTinfoilSettings().isXciNszXczSupport())
203203
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP/XCI/NSZ/XCZ", "*.nsp", "*.xci", "*.nsz", "*.xcz"));
204204
else if (getSelectedProtocol().equals("GoldLeaf") && (! MediatorControl.getInstance().getContoller().getSettingsCtrlr().getNSPFileFilterForGL()))
205205
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Any file", "*.*"),
@@ -273,24 +273,25 @@ else if (( getSelectedProtocol().equals("TinFoil") && getSelectedNetUsb().equals
273273
}
274274
else { // NET INSTALL OVER TINFOIL
275275
final String ipValidationPattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
276+
final SettingsBlockTinfoilController tinfoilSettings = settings.getTinfoilSettings();
276277

277-
if (settings.isNsIpValidate() && ! getNsIp().matches(ipValidationPattern)) {
278+
if (tinfoilSettings.isValidateNSHostName() && ! getNsIp().matches(ipValidationPattern)) {
278279
if (!ServiceWindow.getConfirmationWindow(resourceBundle.getString("windowTitleBadIp"), resourceBundle.getString("windowBodyBadIp")))
279280
return;
280281
}
281282

282283
String nsIP = getNsIp();
283284

284-
if (! settings.getExpertModeSelected())
285+
if (! tinfoilSettings.isExpertModeSelected())
285286
usbNetCommunications = new NETCommunications(nspToUpload, nsIP, false, "", "", "");
286287
else {
287288
usbNetCommunications = new NETCommunications(
288289
nspToUpload,
289290
nsIP,
290-
settings.getNotServeSelected(),
291-
settings.getAutoIpSelected()?"":settings.getHostIp(),
292-
settings.getRandPortSelected()?"":settings.getHostPort(),
293-
settings.getNotServeSelected()?settings.getHostExtra():""
291+
tinfoilSettings.isNoRequestsServe(),
292+
tinfoilSettings.isAutoDetectIp()?"":tinfoilSettings.getHostIp(),
293+
tinfoilSettings.isRandomlySelectPort()?"":tinfoilSettings.getHostPort(),
294+
tinfoilSettings.isNoRequestsServe()?tinfoilSettings.getHostExtra():""
294295
);
295296
}
296297
}
@@ -330,8 +331,9 @@ private void handleDragOver(DragEvent event){
330331
private void handleDrop(DragEvent event){
331332
List<File> filesDropped = event.getDragboard().getFiles();
332333
SettingsController settingsController = MediatorControl.getInstance().getContoller().getSettingsCtrlr();
334+
SettingsBlockTinfoilController tinfoilSettings = settingsController.getTinfoilSettings();
333335

334-
if (getSelectedProtocol().equals("TinFoil") && settingsController.getTfXciNszXczSupport())
336+
if (getSelectedProtocol().equals("TinFoil") && tinfoilSettings.isXciNszXczSupport())
335337
filesDropped.removeIf(file -> ! file.getName().toLowerCase().matches("(.*\\.nsp$)|(.*\\.xci$)|(.*\\.nsz$)|(.*\\.xcz$)"));
336338
else if (getSelectedProtocol().equals("GoldLeaf") && (! settingsController.getNSPFileFilterForGL()))
337339
filesDropped.removeIf(file -> (file.isDirectory() && ! file.getName().toLowerCase().matches(".*\\.nsp$")));
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU 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+
NS-USBloader 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 General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.Controllers;
20+
21+
import javafx.fxml.FXML;
22+
import javafx.fxml.Initializable;
23+
import javafx.scene.control.CheckBox;
24+
import javafx.scene.control.TextField;
25+
import javafx.scene.control.TextFormatter;
26+
import javafx.scene.layout.VBox;
27+
import nsusbloader.AppPreferences;
28+
import nsusbloader.ServiceWindow;
29+
30+
import java.net.URL;
31+
import java.util.ResourceBundle;
32+
33+
public class SettingsBlockTinfoilController implements Initializable {
34+
@FXML
35+
private CheckBox xciNszXczSupportCB,
36+
validateNSHostNameCB,
37+
networkExpertModeCB,
38+
autoDetectIpCB,
39+
randomlySelectPortCB,
40+
noRequestsServeCB;
41+
42+
@FXML
43+
private VBox networkExpertSettingsVBox;
44+
45+
@FXML
46+
private TextField pcIpTF,
47+
pcPortTF,
48+
pcExtraTF;
49+
50+
private ResourceBundle resourceBundle;
51+
52+
@Override
53+
public void initialize(URL url, ResourceBundle resourceBundle) {
54+
this.resourceBundle = resourceBundle;
55+
56+
final AppPreferences preferences = AppPreferences.getInstance();
57+
58+
networkExpertSettingsVBox.disableProperty().bind(networkExpertModeCB.selectedProperty().not());
59+
60+
pcIpTF.disableProperty().bind(autoDetectIpCB.selectedProperty());
61+
pcPortTF.disableProperty().bind(randomlySelectPortCB.selectedProperty());
62+
pcExtraTF.disableProperty().bind(noRequestsServeCB.selectedProperty().not());
63+
64+
xciNszXczSupportCB.setSelected(preferences.getTfXCI());
65+
validateNSHostNameCB.setSelected(preferences.getNsIpValidationNeeded());
66+
networkExpertModeCB.setSelected(preferences.getExpertMode());
67+
pcIpTF.setText(preferences.getHostIp());
68+
pcPortTF.setText(preferences.getHostPort());
69+
pcExtraTF.setText(preferences.getHostExtra());
70+
autoDetectIpCB.setSelected(preferences.getAutoDetectIp());
71+
randomlySelectPortCB.setSelected(preferences.getRandPort());
72+
boolean noServeRequestsFlag = preferences.getNotServeRequests();
73+
if (noServeRequestsFlag){
74+
noServeRequestAction(true);
75+
}
76+
noRequestsServeCB.setSelected(noServeRequestsFlag);
77+
78+
pcIpTF.setTextFormatter(buildSpacelessTextFormatter());
79+
pcPortTF.setTextFormatter(buildPortTextFormatter());
80+
pcExtraTF.setTextFormatter(buildSpacelessTextFormatter());
81+
82+
autoDetectIpCB.setOnAction(e->pcIpTF.requestFocus());
83+
randomlySelectPortCB.setOnAction(e->pcPortTF.requestFocus());
84+
noRequestsServeCB.selectedProperty().addListener(((observableValue, oldValue, newValue) -> noServeRequestAction(newValue)));
85+
}
86+
87+
private TextFormatter buildSpacelessTextFormatter(){
88+
return new TextFormatter<>(change -> {
89+
String text = change.getControlNewText();
90+
91+
if (text.contains(" ") || text.contains("\t")){
92+
return null;
93+
}
94+
return change;
95+
});
96+
}
97+
98+
private TextFormatter buildPortTextFormatter(){
99+
final String PORT_NUMBER_PATTERN = "^[0-9]{0,5}$";
100+
101+
return new TextFormatter<>(change -> {
102+
String text = change.getControlNewText();
103+
if (text.isEmpty()) {
104+
return change;
105+
}
106+
107+
if (! text.matches(PORT_NUMBER_PATTERN)) {
108+
return null;
109+
}
110+
111+
int newPortNumber = Integer.parseInt(text);
112+
113+
if (newPortNumber > 65535 || newPortNumber == 0) {
114+
ServiceWindow.getErrorNotification(resourceBundle.getString("windowTitleErrorPort"),
115+
resourceBundle.getString("windowBodyErrorPort"));
116+
return null;
117+
}
118+
119+
return change;
120+
});
121+
}
122+
123+
private void noServeRequestAction(boolean isNoServe){
124+
if (isNoServe){
125+
autoDetectIpCB.setDisable(true);
126+
autoDetectIpCB.setSelected(false);
127+
randomlySelectPortCB.setDisable(true);
128+
randomlySelectPortCB.setSelected(false);
129+
}
130+
else {
131+
autoDetectIpCB.setDisable(false);
132+
autoDetectIpCB.setSelected(true);
133+
randomlySelectPortCB.setDisable(false);
134+
randomlySelectPortCB.setSelected(true);
135+
}
136+
}
137+
138+
public String getHostIp(){ return pcIpTF.getText(); }
139+
public String getHostPort(){ return pcPortTF.getText(); }
140+
public String getHostExtra(){ return pcExtraTF.getText(); }
141+
public boolean isXciNszXczSupport(){ return xciNszXczSupportCB.isSelected(); }
142+
public boolean isExpertModeSelected(){ return networkExpertModeCB.isSelected(); }
143+
public boolean isAutoDetectIp(){ return autoDetectIpCB.isSelected(); }
144+
public boolean isRandomlySelectPort(){ return randomlySelectPortCB.isSelected(); }
145+
public boolean isNoRequestsServe(){ return noRequestsServeCB.isSelected(); }
146+
public boolean isValidateNSHostName(){ return validateNSHostNameCB.isSelected(); }
147+
148+
public void updatePreferencesOnExit(){
149+
AppPreferences preferences = AppPreferences.getInstance();
150+
151+
preferences.setNsIpValidationNeeded(isValidateNSHostName());
152+
preferences.setExpertMode(isExpertModeSelected());
153+
preferences.setAutoDetectIp(isAutoDetectIp());
154+
preferences.setRandPort(isRandomlySelectPort());
155+
preferences.setNotServeRequests(isNoRequestsServe());
156+
preferences.setHostIp(getHostIp());
157+
preferences.setHostPort(getHostPort());
158+
preferences.setHostExtra(getHostExtra());
159+
preferences.setTfXCI(isXciNszXczSupport());
160+
}
161+
}

src/main/java/nsusbloader/Controllers/SettingsController.java

Lines changed: 9 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,7 @@
3838
public class SettingsController implements Initializable {
3939
@FXML
4040
private CheckBox nspFilesFilterForGLCB,
41-
validateNSHostNameCb,
42-
expertModeCb,
43-
autoDetectIpCb,
44-
randPortCb,
45-
dontServeCb,
46-
autoCheckUpdCb,
47-
tfXciSpprtCb;
48-
49-
@FXML
50-
private TextField pcIpTextField,
51-
pcPortTextField,
52-
pcExtraTextField;
53-
54-
@FXML
55-
private VBox expertSettingsVBox;
41+
autoCheckUpdCb;
5642

5743
@FXML
5844
private Hyperlink newVersionLink;
@@ -67,6 +53,9 @@ public class SettingsController implements Initializable {
6753
@FXML
6854
private ChoiceBox<String> glVersionChoiceBox;
6955

56+
@FXML
57+
private SettingsBlockTinfoilController settingsBlockTinfoilController;
58+
7059
private HostServices hostServices;
7160

7261
public static final String[] glSupportedVersions = {"v0.5", "v0.7.x", "v0.8"};
@@ -79,95 +68,8 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
7968
final AppPreferences preferences = AppPreferences.getInstance();
8069

8170
nspFilesFilterForGLCB.setSelected(preferences.getNspFileFilterGL());
82-
validateNSHostNameCb.setSelected(preferences.getNsIpValidationNeeded());
83-
expertSettingsVBox.setDisable(! preferences.getExpertMode());
84-
expertModeCb.setSelected(preferences.getExpertMode());
85-
expertModeCb.setOnAction(e-> expertSettingsVBox.setDisable(! expertModeCb.isSelected()));
86-
87-
autoDetectIpCb.setSelected(preferences.getAutoDetectIp());
88-
pcIpTextField.setDisable(preferences.getAutoDetectIp());
89-
autoDetectIpCb.setOnAction(e->{
90-
pcIpTextField.setDisable(autoDetectIpCb.isSelected());
91-
if (! autoDetectIpCb.isSelected())
92-
pcIpTextField.requestFocus();
93-
});
94-
95-
randPortCb.setSelected(preferences.getRandPort());
96-
pcPortTextField.setDisable(preferences.getRandPort());
97-
randPortCb.setOnAction(e->{
98-
pcPortTextField.setDisable(randPortCb.isSelected());
99-
if (! randPortCb.isSelected())
100-
pcPortTextField.requestFocus();
101-
});
102-
103-
if (preferences.getNotServeRequests()){
104-
dontServeCb.setSelected(true);
105-
106-
autoDetectIpCb.setSelected(false);
107-
autoDetectIpCb.setDisable(true);
108-
pcIpTextField.setDisable(false);
109-
110-
randPortCb.setSelected(false);
111-
randPortCb.setDisable(true);
112-
pcPortTextField.setDisable(false);
113-
}
114-
pcExtraTextField.setDisable(! preferences.getNotServeRequests());
11571

116-
dontServeCb.setOnAction(e->{
117-
if (dontServeCb.isSelected()){
118-
autoDetectIpCb.setSelected(false);
119-
autoDetectIpCb.setDisable(true);
120-
pcIpTextField.setDisable(false);
12172

122-
randPortCb.setSelected(false);
123-
randPortCb.setDisable(true);
124-
pcPortTextField.setDisable(false);
125-
126-
pcExtraTextField.setDisable(false);
127-
pcIpTextField.requestFocus();
128-
}
129-
else {
130-
autoDetectIpCb.setDisable(false);
131-
autoDetectIpCb.setSelected(true);
132-
pcIpTextField.setDisable(true);
133-
134-
randPortCb.setDisable(false);
135-
randPortCb.setSelected(true);
136-
pcPortTextField.setDisable(true);
137-
138-
pcExtraTextField.setDisable(true);
139-
}
140-
});
141-
142-
pcIpTextField.setText(preferences.getHostIp());
143-
pcPortTextField.setText(preferences.getHostPort());
144-
pcExtraTextField.setText(preferences.getHostExtra());
145-
146-
pcIpTextField.setTextFormatter(new TextFormatter<>(change -> {
147-
if (change.getControlNewText().contains(" ") | change.getControlNewText().contains("\t"))
148-
return null;
149-
else
150-
return change;
151-
}));
152-
pcPortTextField.setTextFormatter(new TextFormatter<>(change -> {
153-
if (change.getControlNewText().matches("^[0-9]{0,5}$")) {
154-
if (!change.getControlNewText().isEmpty()
155-
&& ((Integer.parseInt(change.getControlNewText()) > 65535) || (Integer.parseInt(change.getControlNewText()) == 0))
156-
) {
157-
ServiceWindow.getErrorNotification(resourceBundle.getString("windowTitleErrorPort"), resourceBundle.getString("windowBodyErrorPort"));
158-
return null;
159-
}
160-
return change;
161-
}
162-
else
163-
return null;
164-
}));
165-
pcExtraTextField.setTextFormatter(new TextFormatter<>(change -> {
166-
if (change.getControlNewText().contains(" ") | change.getControlNewText().contains("\t"))
167-
return null;
168-
else
169-
return change;
170-
}));
17173

17274
newVersionLink.setVisible(false);
17375
newVersionLink.setOnAction(e-> hostServices.showDocument(newVersionLink.getText()));
@@ -182,8 +84,6 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
18284

18385
setDriversInstallFeature();
18486

185-
tfXciSpprtCb.setSelected(preferences.getTfXCI());
186-
18787
SettingsLanguagesSetup settingsLanguagesSetup = new SettingsLanguagesSetup();
18888
langCB.setItems(settingsLanguagesSetup.getLanguages());
18989
langCB.getSelectionModel().select(settingsLanguagesSetup.getRecentLanguage());
@@ -247,18 +147,8 @@ private void languageButtonAction(){
247147
}
248148

249149
public boolean getNSPFileFilterForGL(){return nspFilesFilterForGLCB.isSelected(); }
250-
public boolean getExpertModeSelected(){ return expertModeCb.isSelected(); }
251-
public boolean getAutoIpSelected(){ return autoDetectIpCb.isSelected(); }
252-
public boolean getRandPortSelected(){ return randPortCb.isSelected(); }
253-
public boolean getNotServeSelected(){ return dontServeCb.isSelected(); }
254-
255-
public boolean isNsIpValidate(){ return validateNSHostNameCb.isSelected(); }
256150

257-
public String getHostIp(){ return pcIpTextField.getText(); }
258-
public String getHostPort(){ return pcPortTextField.getText(); }
259-
public String getHostExtra(){ return pcExtraTextField.getText(); }
260151
public boolean getAutoCheckForUpdates(){ return autoCheckUpdCb.isSelected(); }
261-
public boolean getTfXciNszXczSupport(){ return tfXciSpprtCb.isSelected(); } // Used also for NSZ/XCZ
262152

263153
public void registerHostServices(HostServices hostServices){this.hostServices = hostServices;}
264154

@@ -270,21 +160,16 @@ public void setNewVersionLink(String newVer){
270160
public String getGlVer() {
271161
return glVersionChoiceBox.getValue();
272162
}
273-
163+
164+
public SettingsBlockTinfoilController getTinfoilSettings(){ return settingsBlockTinfoilController; }
165+
274166
public void updatePreferencesOnExit(){
275167
AppPreferences preferences = AppPreferences.getInstance();
276168

277-
preferences.setNsIpValidationNeeded(isNsIpValidate());
278-
preferences.setExpertMode(getExpertModeSelected());
279-
preferences.setAutoDetectIp(getAutoIpSelected());
280-
preferences.setRandPort(getRandPortSelected());
281-
preferences.setNotServeRequests(getNotServeSelected());
282-
preferences.setHostIp(getHostIp());
283-
preferences.setHostPort(getHostPort());
284-
preferences.setHostExtra(getHostExtra());
285169
preferences.setAutoCheckUpdates(getAutoCheckForUpdates());
286-
preferences.setTfXCI(getTfXciNszXczSupport());
287170
preferences.setNspFileFilterGL(getNSPFileFilterForGL());
288171
preferences.setGlVersion(getGlVer());
172+
173+
settingsBlockTinfoilController.updatePreferencesOnExit();
289174
}
290175
}

0 commit comments

Comments
 (0)