Skip to content

Commit 1176ad9

Browse files
committed
Solve - #87. Break LogPrinterGui
1 parent 79c519b commit 1176ad9

32 files changed

+1350
-740
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2019-2021 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.collections.ObservableList;
22+
import javafx.fxml.FXML;
23+
import javafx.fxml.Initializable;
24+
import javafx.scene.control.ContextMenu;
25+
import javafx.scene.control.ListCell;
26+
import javafx.scene.control.ListView;
27+
import javafx.scene.control.MenuItem;
28+
29+
import java.io.File;
30+
import java.net.URL;
31+
import java.util.List;
32+
import java.util.ResourceBundle;
33+
34+
public class BlockListViewController implements Initializable {
35+
36+
@FXML
37+
private ListView<File> splitMergeListView;
38+
private ObservableList<File> filesList;
39+
40+
private ResourceBundle resourceBundle;
41+
42+
private static class FileListCell extends ListCell<File>{
43+
@Override
44+
public void updateItem(File file, boolean isEmpty){
45+
super.updateItem(file, isEmpty);
46+
47+
if (file == null || isEmpty){
48+
setText(null);
49+
return;
50+
}
51+
String fileName = file.getName();
52+
setText(fileName);
53+
}
54+
}
55+
56+
@Override
57+
public void initialize(URL url, ResourceBundle resourceBundle) {
58+
this.resourceBundle = resourceBundle;
59+
setFilesListView();
60+
filesList = splitMergeListView.getItems();
61+
}
62+
private void setFilesListView(){
63+
splitMergeListView.setCellFactory(fileListView -> {
64+
ListCell<File> item = new FileListCell();
65+
setContextMenuToItem(item);
66+
return item;
67+
});
68+
}
69+
70+
private <T> void setContextMenuToItem(ListCell<T> item){
71+
ContextMenu contextMenu = new ContextMenu();
72+
MenuItem deleteMenuItem = new MenuItem(resourceBundle.getString("tab1_table_contextMenu_Btn_BtnDelete"));
73+
deleteMenuItem.setOnAction(actionEvent -> {
74+
filesList.remove(item.getItem());
75+
splitMergeListView.refresh();
76+
});
77+
MenuItem deleteAllMenuItem = new MenuItem(resourceBundle.getString("tab1_table_contextMenu_Btn_DeleteAll"));
78+
deleteAllMenuItem.setOnAction(actionEvent -> {
79+
filesList.clear();
80+
splitMergeListView.refresh();
81+
});
82+
contextMenu.getItems().addAll(deleteMenuItem, deleteAllMenuItem);
83+
84+
item.setContextMenu(contextMenu);
85+
}
86+
87+
public void add(File file){
88+
if (filesList.contains(file))
89+
return;
90+
filesList.add(file);
91+
}
92+
public void addAll(List<File> files){
93+
for (File file : files) {
94+
add(file);
95+
}
96+
}
97+
public ObservableList<File> getItems(){ return filesList; }
98+
public void clear(){
99+
filesList.clear();
100+
splitMergeListView.refresh();
101+
}
102+
}

src/main/java/nsusbloader/Controllers/NSLMainController.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class NSLMainController implements Initializable {
4646
private Tab GamesTabHolder, RCMTabHolder, SMTabHolder;
4747

4848
@FXML
49-
private GamesController GamesTabController; // Accessible from Mediator | todo: incapsulate
49+
private GamesController GamesTabController;
5050
@FXML
5151
private SettingsController SettingsTabController;
5252
@FXML
@@ -69,30 +69,32 @@ public void initialize(URL url, ResourceBundle rb) {
6969
MediatorControl.getInstance().setController(this);
7070

7171
if (AppPreferences.getInstance().getAutoCheckUpdates()){
72-
Task<List<String>> updTask = new UpdatesChecker();
73-
updTask.setOnSucceeded(event->{
74-
List<String> result = updTask.getValue();
75-
if (result != null){
76-
if (!result.get(0).isEmpty()) {
77-
SettingsTabController.getGenericSettings().setNewVersionLink(result.get(0));
78-
ServiceWindow.getInfoNotification(
79-
resourceBundle.getString("windowTitleNewVersionAval"),
80-
resourceBundle.getString("windowTitleNewVersionAval") + ": " + result.get(0) + "\n\n" + result.get(1));
81-
}
82-
}
83-
else
84-
ServiceWindow.getInfoNotification(
85-
resourceBundle.getString("windowTitleNewVersionUnknown"),
86-
resourceBundle.getString("windowBodyNewVersionUnknown"));
87-
});
88-
Thread updates = new Thread(updTask);
89-
updates.setDaemon(true);
90-
updates.start();
72+
checkForUpdates();
9173
}
9274

9375
openLastOpenedTab();
9476
}
95-
77+
private void checkForUpdates(){
78+
Task<List<String>> updTask = new UpdatesChecker();
79+
updTask.setOnSucceeded(event->{
80+
List<String> result = updTask.getValue();
81+
if (result != null){
82+
if (!result.get(0).isEmpty()) {
83+
SettingsTabController.getGenericSettings().setNewVersionLink(result.get(0));
84+
ServiceWindow.getInfoNotification(
85+
resourceBundle.getString("windowTitleNewVersionAval"),
86+
resourceBundle.getString("windowTitleNewVersionAval") + ": " + result.get(0) + "\n\n" + result.get(1));
87+
}
88+
}
89+
else
90+
ServiceWindow.getInfoNotification(
91+
resourceBundle.getString("windowTitleNewVersionUnknown"),
92+
resourceBundle.getString("windowBodyNewVersionUnknown"));
93+
});
94+
Thread updates = new Thread(updTask);
95+
updates.setDaemon(true);
96+
updates.start();
97+
}
9698
/**
9799
* Get resources
98100
* TODO: Find better solution; used in UsbCommunications() -> GL -> SelectFile command

src/main/java/nsusbloader/Controllers/SplitMergeController.java

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package nsusbloader.Controllers;
2020

21+
import javafx.beans.binding.Bindings;
2122
import javafx.fxml.FXML;
2223
import javafx.fxml.Initializable;
2324
import javafx.scene.control.*;
@@ -33,11 +34,11 @@
3334
import nsusbloader.ModelControllers.CancellableRunnable;
3435
import nsusbloader.NSLDataTypes.EModule;
3536
import nsusbloader.ServiceWindow;
36-
import nsusbloader.Utilities.splitmerge.MergeTask;
37-
import nsusbloader.Utilities.splitmerge.SplitTask;
37+
import nsusbloader.Utilities.splitmerge.SplitMergeTaskExecutor;
3838

3939
import java.io.File;
4040
import java.net.URL;
41+
import java.util.List;
4142
import java.util.ResourceBundle;
4243

4344
public class SplitMergeController implements Initializable {
@@ -53,40 +54,39 @@ public class SplitMergeController implements Initializable {
5354
changeSaveToBtn,
5455
convertBtn;
5556
@FXML
56-
private Label fileFolderLabelLbl,
57-
fileFolderActualPathLbl,
58-
saveToPathLbl,
57+
private Label saveToPathLbl,
5958
statusLbl;
6059

60+
@FXML
61+
private BlockListViewController BlockListViewController;
62+
6163
private ResourceBundle resourceBundle;
6264

6365
private Region convertRegion;
6466
private Thread smThread;
65-
private CancellableRunnable smTask;
67+
private Runnable smTask;
6668

6769
@Override
6870
public void initialize(URL url, ResourceBundle resourceBundle) {
6971
this.resourceBundle = resourceBundle;
72+
7073
convertRegion = new Region();
7174
convertBtn.setGraphic(convertRegion);
75+
convertBtn.disableProperty().bind(Bindings.isEmpty(BlockListViewController.getItems()));
7276

7377
splitRad.setOnAction((actionEvent -> {
7478
statusLbl.setText("");
7579
convertRegion.getStyleClass().clear();
7680
convertRegion.getStyleClass().add("regionSplitToOne");
77-
fileFolderLabelLbl.setText(resourceBundle.getString("tabSplMrg_Txt_File"));
7881
selectFileFolderBtn.setText(resourceBundle.getString("tabSplMrg_Btn_SelectFile"));
79-
fileFolderActualPathLbl.setText("");
80-
convertBtn.setDisable(true);
82+
BlockListViewController.clear();
8183
}));
8284
mergeRad.setOnAction((actionEvent -> {
8385
statusLbl.setText("");
8486
convertRegion.getStyleClass().clear();
8587
convertRegion.getStyleClass().add("regionOneToSplit");
86-
fileFolderLabelLbl.setText(resourceBundle.getString("tabSplMrg_Txt_Folder"));
8788
selectFileFolderBtn.setText(resourceBundle.getString("tabSplMrg_Btn_SelectFolder"));
88-
fileFolderActualPathLbl.setText("");
89-
convertBtn.setDisable(true);
89+
BlockListViewController.clear();
9090
}));
9191

9292
if (AppPreferences.getInstance().getSplitMergeType() == 0)
@@ -110,42 +110,36 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
110110

111111
selectFileFolderBtn.setOnAction(actionEvent -> {
112112
statusLbl.setText("");
113+
List<File> alreadyAddedFiles = BlockListViewController.getItems();
113114
if (splitRad.isSelected()) {
114115
FileChooser fc = new FileChooser();
115116
fc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFile"));
116-
if (! fileFolderActualPathLbl.getText().isEmpty()){
117-
File temporaryFile = new File(fileFolderActualPathLbl.getText()).getParentFile();
118-
if (temporaryFile != null && temporaryFile.exists())
119-
fc.setInitialDirectory(temporaryFile);
120-
else
121-
fc.setInitialDirectory(new File(System.getProperty("user.home")));
117+
if (! alreadyAddedFiles.isEmpty()){
118+
String recentLocation = FilesHelper.getRealFolder(alreadyAddedFiles.get(0).getParentFile().getAbsolutePath());
119+
fc.setInitialDirectory(new File(recentLocation));
122120
}
123121
else
124122
fc.setInitialDirectory(new File(System.getProperty("user.home")));
125-
File fileFile = fc.showOpenDialog(changeSaveToBtn.getScene().getWindow());
126-
if (fileFile == null)
123+
List<File> files = fc.showOpenMultipleDialog(changeSaveToBtn.getScene().getWindow());
124+
if (files == null || files.isEmpty())
127125
return;
128-
fileFolderActualPathLbl.setText(fileFile.getAbsolutePath());
126+
this.BlockListViewController.addAll(files);
129127
}
130128
else{
131129
DirectoryChooser dc = new DirectoryChooser();
132130
dc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFolder"));
133-
if (! fileFolderActualPathLbl.getText().isEmpty()){
134-
File temporaryFile = new File(fileFolderActualPathLbl.getText());
135-
if (temporaryFile.exists())
136-
dc.setInitialDirectory(temporaryFile);
137-
else
138-
dc.setInitialDirectory(new File(System.getProperty("user.home")));
131+
if (! alreadyAddedFiles.isEmpty()){
132+
String recentLocation = FilesHelper.getRealFolder(alreadyAddedFiles.get(0).getParentFile().getAbsolutePath());
133+
dc.setInitialDirectory(new File(recentLocation));
139134
}
140135
else
141136
dc.setInitialDirectory(new File(System.getProperty("user.home")));
142137

143138
File folderFile = dc.showDialog(changeSaveToBtn.getScene().getWindow());
144139
if (folderFile == null)
145140
return;
146-
fileFolderActualPathLbl.setText(folderFile.getAbsolutePath());
141+
this.BlockListViewController.add(folderFile);
147142
}
148-
convertBtn.setDisable(false);
149143
});
150144

151145
convertBtn.setOnAction(actionEvent -> setConvertBtnAction());
@@ -192,7 +186,7 @@ public void notifyThreadStarted(boolean isStart, EModule type){ // todo: refacto
192186
* */
193187
private void stopBtnAction(){
194188
if (smThread != null && smThread.isAlive()) {
195-
smTask.cancel();
189+
smThread.interrupt();
196190
}
197191
}
198192
/**
@@ -209,9 +203,9 @@ private void setConvertBtnAction(){
209203
}
210204

211205
if (splitRad.isSelected())
212-
smTask = new SplitTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
206+
smTask = new SplitMergeTaskExecutor(true, BlockListViewController.getItems(), saveToPathLbl.getText());
213207
else
214-
smTask = new MergeTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
208+
smTask = new SplitMergeTaskExecutor(false, BlockListViewController.getItems(), saveToPathLbl.getText());
215209
smThread = new Thread(smTask);
216210
smThread.setDaemon(true);
217211
smThread.start();
@@ -230,14 +224,16 @@ private void handleDragOver(DragEvent event){
230224
* */
231225
@FXML
232226
private void handleDrop(DragEvent event) {
233-
File fileDrpd = event.getDragboard().getFiles().get(0);
227+
List<File> files = event.getDragboard().getFiles();
228+
File firstFile = files.get(0);
234229

235-
if (fileDrpd.isDirectory())
230+
if (firstFile.isDirectory())
236231
mergeRad.fire();
237232
else
238233
splitRad.fire();
239-
fileFolderActualPathLbl.setText(fileDrpd.getAbsolutePath());
240-
convertBtn.setDisable(false);
234+
235+
this.BlockListViewController.addAll(files);
236+
241237
event.setDropCompleted(true);
242238
event.consume();
243239
}

src/main/java/nsusbloader/ModelControllers/ILogPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import java.util.HashMap;
2727

2828
public interface ILogPrinter {
29-
void print(String message, EMsgType type);
30-
void updateProgress(Double value);
29+
void print(String message, EMsgType type) throws InterruptedException;
30+
void updateProgress(Double value) throws InterruptedException;
3131
void update(HashMap<String, File> nspMap, EFileStatus status);
3232
void update(File file, EFileStatus status);
3333
void updateOneLinerStatus(boolean status);

0 commit comments

Comments
 (0)