19
19
package nsusbloader .Controllers ;
20
20
21
21
import javafx .application .HostServices ;
22
- import javafx .collections .FXCollections ;
23
- import javafx .collections .ObservableList ;
24
22
import javafx .concurrent .Task ;
25
23
import javafx .fxml .FXML ;
26
24
import javafx .fxml .Initializable ;
30
28
import nsusbloader .AppPreferences ;
31
29
import nsusbloader .ServiceWindow ;
32
30
import nsusbloader .ModelControllers .UpdatesChecker ;
33
- import nsusbloader .UI .LocaleUiStringHolder ;
31
+ import nsusbloader .UI .LocaleHolder ;
32
+ import nsusbloader .UI .SettingsLanguagesSetup ;
34
33
import nsusbloader .Utilities .WindowsDrivers .DriversInstall ;
35
34
36
- import java .io .File ;
37
- import java .io .IOException ;
38
- import java .io .UnsupportedEncodingException ;
39
35
import java .net .URL ;
40
- import java .net .URLDecoder ;
41
36
import java .util .*;
42
- import java .util .jar .JarEntry ;
43
- import java .util .jar .JarFile ;
44
37
45
38
public class SettingsController implements Initializable {
46
39
@ FXML
47
40
private CheckBox nspFilesFilterForGLCB ,
48
41
validateNSHostNameCb ,
49
42
expertModeCb ,
50
43
autoDetectIpCb ,
51
- randPortCb ;
44
+ randPortCb ,
45
+ dontServeCb ,
46
+ autoCheckUpdCb ,
47
+ tfXciSpprtCb ;
52
48
53
49
@ FXML
54
50
private TextField pcIpTextField ,
55
51
pcPortTextField ,
56
52
pcExtraTextField ;
57
53
58
- @ FXML
59
- private CheckBox dontServeCb ;
60
-
61
54
@ FXML
62
55
private VBox expertSettingsVBox ;
63
56
64
- @ FXML
65
- private CheckBox autoCheckUpdCb ;
66
57
@ FXML
67
58
private Hyperlink newVersionLink ;
68
59
69
- @ FXML
70
- private CheckBox tfXciSpprtCb ;
71
-
72
60
@ FXML
73
61
private Button langBtn ,
74
62
checkForUpdBtn ,
75
63
drvInstBtn ;
76
64
@ FXML
77
- private ChoiceBox <LocaleUiStringHolder > langCB ;
65
+ private ChoiceBox <LocaleHolder > langCB ;
78
66
79
67
@ FXML
80
68
private ChoiceBox <String > glVersionChoiceBox ;
81
69
82
- private HostServices hs ;
70
+ private HostServices hostServices ;
83
71
84
72
public static final String [] glSupportedVersions = {"v0.5" , "v0.7.x" , "v0.8" };
85
73
74
+ private ResourceBundle resourceBundle ;
75
+
86
76
@ Override
87
77
public void initialize (URL url , ResourceBundle resourceBundle ) {
88
- nspFilesFilterForGLCB .setSelected (AppPreferences .getInstance ().getNspFileFilterGL ());
78
+ this .resourceBundle = resourceBundle ;
79
+ final AppPreferences preferences = AppPreferences .getInstance ();
89
80
90
- validateNSHostNameCb .setSelected (AppPreferences .getInstance ().getNsIpValidationNeeded ());
81
+ 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 ()));
91
86
92
- expertSettingsVBox .setDisable (!AppPreferences .getInstance ().getExpertMode ());
93
-
94
- expertModeCb .setSelected (AppPreferences .getInstance ().getExpertMode ());
95
- expertModeCb .setOnAction (e -> expertSettingsVBox .setDisable (!expertModeCb .isSelected ()));
96
-
97
- autoDetectIpCb .setSelected (AppPreferences .getInstance ().getAutoDetectIp ());
98
- pcIpTextField .setDisable (AppPreferences .getInstance ().getAutoDetectIp ());
87
+ autoDetectIpCb .setSelected (preferences .getAutoDetectIp ());
88
+ pcIpTextField .setDisable (preferences .getAutoDetectIp ());
99
89
autoDetectIpCb .setOnAction (e ->{
100
90
pcIpTextField .setDisable (autoDetectIpCb .isSelected ());
101
- if (!autoDetectIpCb .isSelected ())
91
+ if (! autoDetectIpCb .isSelected ())
102
92
pcIpTextField .requestFocus ();
103
93
});
104
94
105
- randPortCb .setSelected (AppPreferences . getInstance () .getRandPort ());
106
- pcPortTextField .setDisable (AppPreferences . getInstance () .getRandPort ());
95
+ randPortCb .setSelected (preferences .getRandPort ());
96
+ pcPortTextField .setDisable (preferences .getRandPort ());
107
97
randPortCb .setOnAction (e ->{
108
98
pcPortTextField .setDisable (randPortCb .isSelected ());
109
- if (!randPortCb .isSelected ())
99
+ if (! randPortCb .isSelected ())
110
100
pcPortTextField .requestFocus ();
111
101
});
112
102
113
- if (AppPreferences . getInstance () .getNotServeRequests ()){
103
+ if (preferences .getNotServeRequests ()){
114
104
dontServeCb .setSelected (true );
115
105
116
106
autoDetectIpCb .setSelected (false );
@@ -121,7 +111,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
121
111
randPortCb .setDisable (true );
122
112
pcPortTextField .setDisable (false );
123
113
}
124
- pcExtraTextField .setDisable (!AppPreferences . getInstance () .getNotServeRequests ());
114
+ pcExtraTextField .setDisable (! preferences .getNotServeRequests ());
125
115
126
116
dontServeCb .setOnAction (e ->{
127
117
if (dontServeCb .isSelected ()){
@@ -149,9 +139,9 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
149
139
}
150
140
});
151
141
152
- pcIpTextField .setText (AppPreferences . getInstance () .getHostIp ());
153
- pcPortTextField .setText (AppPreferences . getInstance () .getHostPort ());
154
- pcExtraTextField .setText (AppPreferences . getInstance () .getHostExtra ());
142
+ pcIpTextField .setText (preferences .getHostIp ());
143
+ pcPortTextField .setText (preferences .getHostPort ());
144
+ pcExtraTextField .setText (preferences .getHostExtra ());
155
145
156
146
pcIpTextField .setTextFormatter (new TextFormatter <>(change -> {
157
147
if (change .getControlNewText ().contains (" " ) | change .getControlNewText ().contains ("\t " ))
@@ -180,128 +170,82 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
180
170
}));
181
171
182
172
newVersionLink .setVisible (false );
183
- newVersionLink .setOnAction (e -> hs .showDocument (newVersionLink .getText ()));
173
+ newVersionLink .setOnAction (e -> hostServices .showDocument (newVersionLink .getText ()));
184
174
185
- autoCheckUpdCb .setSelected (AppPreferences . getInstance () .getAutoCheckUpdates ());
175
+ autoCheckUpdCb .setSelected (preferences .getAutoCheckUpdates ());
186
176
187
177
Region btnSwitchImage = new Region ();
188
178
btnSwitchImage .getStyleClass ().add ("regionUpdatesCheck" );
189
179
checkForUpdBtn .setGraphic (btnSwitchImage );
190
180
191
- checkForUpdBtn .setOnAction (e ->{
192
- Task <List <String >> updTask = new UpdatesChecker ();
193
- updTask .setOnSucceeded (event ->{
194
- List <String > result = updTask .getValue ();
195
- if (result != null ){
196
- if (result .get (0 ).isEmpty ()){
197
- ServiceWindow .getInfoNotification (resourceBundle .getString ("windowTitleNewVersionNOTAval" ), resourceBundle .getString ("windowBodyNewVersionNOTAval" ));
198
- }
199
- else {
200
- setNewVersionLink (result .get (0 ));
201
- ServiceWindow .getInfoNotification (resourceBundle .getString ("windowTitleNewVersionAval" ), resourceBundle .getString ("windowTitleNewVersionAval" )+": " +result .get (0 ) + "\n \n " + result .get (1 ));
202
- }
203
- }
204
- else {
205
- ServiceWindow .getInfoNotification (resourceBundle .getString ("windowTitleNewVersionUnknown" ), resourceBundle .getString ("windowBodyNewVersionUnknown" ));
206
- }
207
- });
208
- Thread updates = new Thread (updTask );
209
- updates .setDaemon (true );
210
- updates .start ();
211
- });
181
+ checkForUpdBtn .setOnAction (e ->checkForUpdatesAction ());
212
182
213
- if (isWindows ()){
214
- Region btnDrvImage = new Region ();
215
- btnDrvImage .getStyleClass ().add ("regionWindows" );
216
- drvInstBtn .setGraphic (btnDrvImage );
217
- drvInstBtn .setVisible (true );
218
- drvInstBtn .setOnAction (actionEvent -> new DriversInstall (resourceBundle ));
219
- }
183
+ setDriversInstallFeature ();
220
184
221
- tfXciSpprtCb .setSelected (AppPreferences . getInstance () .getTfXCI ());
185
+ tfXciSpprtCb .setSelected (preferences .getTfXCI ());
222
186
223
- // Language settings area
224
- ObservableList < LocaleUiStringHolder > langCBObsList = FXCollections . observableArrayList ( );
225
- //langCBObsList.add(new LocaleUiStringHolder(new Locale("en", "US") ));
187
+ SettingsLanguagesSetup settingsLanguagesSetup = new SettingsLanguagesSetup ();
188
+ langCB . setItems ( settingsLanguagesSetup . getLanguages () );
189
+ langCB . getSelectionModel (). select ( settingsLanguagesSetup . getRecentLanguage ( ));
226
190
227
- File jarFile ;
228
- try {
229
- String encodedJarLocation = getClass ().getProtectionDomain ().getCodeSource ().getLocation ().getPath ().replace ("+" , "%2B" );
230
- jarFile = new File (URLDecoder .decode (encodedJarLocation , "UTF-8" ));
231
- }
232
- catch (UnsupportedEncodingException uee ){
233
- uee .printStackTrace ();
234
- jarFile = null ;
235
- }
191
+ configureLanguageButton ();
236
192
237
- if (jarFile != null && jarFile .isFile ()) { // Run with JAR file
238
- try {
239
- JarFile jar = new JarFile (jarFile );
240
- Enumeration <JarEntry > entries = jar .entries (); //gives ALL entries in jar
241
- while (entries .hasMoreElements ()) {
242
- String name = entries .nextElement ().getName ();
243
- if (name .startsWith ("locale_" )){
244
- try {
245
- langCBObsList .add (new LocaleUiStringHolder (name ));
246
- }
247
- catch (Exception e ){
248
- e .printStackTrace ();
249
- }
250
- }
251
- }
252
- jar .close ();
253
- }
254
- catch (IOException ioe ){
255
- ioe .printStackTrace (); // TODO: think about better solution?
256
- }
257
- }
258
- else { // Run within IDE
259
- URL resourceURL = this .getClass ().getResource ("/" );
260
- String [] filesList = new File (resourceURL .getFile ()).list (); // Screw it. This WON'T produce NullPointerException
261
-
262
- for (String jarFileName : filesList )
263
- if (jarFileName .startsWith ("locale_" )){
264
- try {
265
- langCBObsList .add (new LocaleUiStringHolder (jarFileName ));
266
- }
267
- catch (Exception e ){
268
- e .printStackTrace ();
269
- }
270
- }
271
- }
272
- langCBObsList .sort (Comparator .comparing (LocaleUiStringHolder ::toString ));
273
-
274
- langCB .setItems (langCBObsList );
275
- // TODO: REFACTOR THIS SHIT; INCAPSULATE AND MOVE OUT FROM HERE
276
- Locale localeFromPrefs = AppPreferences .getInstance ().getLocale ();
277
- boolean notExists = true ;
278
- for (LocaleUiStringHolder holderItem : langCBObsList ){
279
- if (holderItem .getLocale ().equals (localeFromPrefs )){
280
- langCB .getSelectionModel ().select (holderItem );
281
- notExists = false ;
282
- break ;
283
- }
284
- }
285
- if (notExists )
286
- langCB .getSelectionModel ().select (0 );
287
-
288
- langBtn .setOnAction (e ->{
289
- LocaleUiStringHolder localeHolder = langCB .getSelectionModel ().getSelectedItem ();
290
- AppPreferences .getInstance ().setLocale (localeHolder .getLocaleCode ());
291
- Locale newLocale = localeHolder .getLocale ();
292
- ServiceWindow .getInfoNotification ("" ,
293
- ResourceBundle .getBundle ("locale" , newLocale ).getString ("windowBodyRestartToApplyLang" ));
294
- });
295
193
// Set supported old versions
296
194
glVersionChoiceBox .getItems ().addAll (glSupportedVersions );
297
- String oldVer = AppPreferences . getInstance () .getGlVersion (); // Overhead; Too much validation of consistency
195
+ String oldVer = preferences .getGlVersion (); // Overhead; Too much validation of consistency
298
196
glVersionChoiceBox .getSelectionModel ().select (oldVer );
299
197
}
300
198
199
+ private void checkForUpdatesAction (){
200
+ Task <List <String >> updTask = new UpdatesChecker ();
201
+ updTask .setOnSucceeded (event ->{
202
+ List <String > result = updTask .getValue ();
203
+
204
+ if (result == null ){
205
+ ServiceWindow .getInfoNotification (resourceBundle .getString ("windowTitleNewVersionUnknown" ),
206
+ resourceBundle .getString ("windowBodyNewVersionUnknown" ));
207
+ return ;
208
+ }
209
+
210
+ if (result .get (0 ).isEmpty ()){
211
+ ServiceWindow .getInfoNotification (resourceBundle .getString ("windowTitleNewVersionNOTAval" ),
212
+ resourceBundle .getString ("windowBodyNewVersionNOTAval" ));
213
+ return ;
214
+ }
215
+
216
+ setNewVersionLink (result .get (0 ));
217
+ ServiceWindow .getInfoNotification (resourceBundle .getString ("windowTitleNewVersionAval" ),
218
+ resourceBundle .getString ("windowTitleNewVersionAval" )+": " +result .get (0 ) + "\n \n " + result .get (1 ));
219
+ });
220
+ Thread updates = new Thread (updTask );
221
+ updates .setDaemon (true );
222
+ updates .start ();
223
+ }
224
+
225
+ private void setDriversInstallFeature (){
226
+ if (isWindows ()){
227
+ Region btnDrvImage = new Region ();
228
+ btnDrvImage .getStyleClass ().add ("regionWindows" );
229
+ drvInstBtn .setGraphic (btnDrvImage );
230
+ drvInstBtn .setVisible (true );
231
+ drvInstBtn .setOnAction (actionEvent -> new DriversInstall (resourceBundle ));
232
+ }
233
+ }
301
234
private boolean isWindows (){
302
235
return System .getProperty ("os.name" ).toLowerCase ().replace (" " , "" ).contains ("windows" );
303
236
}
304
237
238
+ private void configureLanguageButton (){
239
+ langBtn .setOnAction (e ->languageButtonAction ());
240
+ }
241
+ private void languageButtonAction (){
242
+ LocaleHolder localeHolder = langCB .getSelectionModel ().getSelectedItem ();
243
+ AppPreferences .getInstance ().setLocale (localeHolder .getLocaleCode ());
244
+ Locale newLocale = localeHolder .getLocale ();
245
+ ServiceWindow .getInfoNotification ("" ,
246
+ ResourceBundle .getBundle ("locale" , newLocale ).getString ("windowBodyRestartToApplyLang" ));
247
+ }
248
+
305
249
public boolean getNSPFileFilterForGL (){return nspFilesFilterForGLCB .isSelected (); }
306
250
public boolean getExpertModeSelected (){ return expertModeCb .isSelected (); }
307
251
public boolean getAutoIpSelected (){ return autoDetectIpCb .isSelected (); }
@@ -316,7 +260,7 @@ private boolean isWindows(){
316
260
public boolean getAutoCheckForUpdates (){ return autoCheckUpdCb .isSelected (); }
317
261
public boolean getTfXciNszXczSupport (){ return tfXciSpprtCb .isSelected (); } // Used also for NSZ/XCZ
318
262
319
- public void registerHostServices (HostServices hostServices ){this .hs = hostServices ;}
263
+ public void registerHostServices (HostServices hostServices ){this .hostServices = hostServices ;}
320
264
321
265
public void setNewVersionLink (String newVer ){
322
266
newVersionLink .setVisible (true );
0 commit comments