Skip to content

Commit

Permalink
Solve - #87. Break LogPrinterGui
Browse files Browse the repository at this point in the history
  • Loading branch information
developersu committed Aug 9, 2021
1 parent 79c519b commit 1176ad9
Show file tree
Hide file tree
Showing 32 changed files with 1,350 additions and 740 deletions.
102 changes: 102 additions & 0 deletions src/main/java/nsusbloader/Controllers/BlockListViewController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2019-2021 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.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class BlockListViewController implements Initializable {

@FXML
private ListView<File> splitMergeListView;
private ObservableList<File> filesList;

private ResourceBundle resourceBundle;

private static class FileListCell extends ListCell<File>{
@Override
public void updateItem(File file, boolean isEmpty){
super.updateItem(file, isEmpty);

if (file == null || isEmpty){
setText(null);
return;
}
String fileName = file.getName();
setText(fileName);
}
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
setFilesListView();
filesList = splitMergeListView.getItems();
}
private void setFilesListView(){
splitMergeListView.setCellFactory(fileListView -> {
ListCell<File> item = new FileListCell();
setContextMenuToItem(item);
return item;
});
}

private <T> void setContextMenuToItem(ListCell<T> item){
ContextMenu contextMenu = new ContextMenu();
MenuItem deleteMenuItem = new MenuItem(resourceBundle.getString("tab1_table_contextMenu_Btn_BtnDelete"));
deleteMenuItem.setOnAction(actionEvent -> {
filesList.remove(item.getItem());
splitMergeListView.refresh();
});
MenuItem deleteAllMenuItem = new MenuItem(resourceBundle.getString("tab1_table_contextMenu_Btn_DeleteAll"));
deleteAllMenuItem.setOnAction(actionEvent -> {
filesList.clear();
splitMergeListView.refresh();
});
contextMenu.getItems().addAll(deleteMenuItem, deleteAllMenuItem);

item.setContextMenu(contextMenu);
}

public void add(File file){
if (filesList.contains(file))
return;
filesList.add(file);
}
public void addAll(List<File> files){
for (File file : files) {
add(file);
}
}
public ObservableList<File> getItems(){ return filesList; }
public void clear(){
filesList.clear();
splitMergeListView.refresh();
}
}
44 changes: 23 additions & 21 deletions src/main/java/nsusbloader/Controllers/NSLMainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class NSLMainController implements Initializable {
private Tab GamesTabHolder, RCMTabHolder, SMTabHolder;

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

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.getGenericSettings().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();
checkForUpdates();
}

openLastOpenedTab();
}

private void checkForUpdates(){
Task<List<String>> updTask = new UpdatesChecker();
updTask.setOnSucceeded(event->{
List<String> result = updTask.getValue();
if (result != null){
if (!result.get(0).isEmpty()) {
SettingsTabController.getGenericSettings().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();
}
/**
* Get resources
* TODO: Find better solution; used in UsbCommunications() -> GL -> SelectFile command
Expand Down
68 changes: 32 additions & 36 deletions src/main/java/nsusbloader/Controllers/SplitMergeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package nsusbloader.Controllers;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
Expand All @@ -33,11 +34,11 @@
import nsusbloader.ModelControllers.CancellableRunnable;
import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.ServiceWindow;
import nsusbloader.Utilities.splitmerge.MergeTask;
import nsusbloader.Utilities.splitmerge.SplitTask;
import nsusbloader.Utilities.splitmerge.SplitMergeTaskExecutor;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class SplitMergeController implements Initializable {
Expand All @@ -53,40 +54,39 @@ public class SplitMergeController implements Initializable {
changeSaveToBtn,
convertBtn;
@FXML
private Label fileFolderLabelLbl,
fileFolderActualPathLbl,
saveToPathLbl,
private Label saveToPathLbl,
statusLbl;

@FXML
private BlockListViewController BlockListViewController;

private ResourceBundle resourceBundle;

private Region convertRegion;
private Thread smThread;
private CancellableRunnable smTask;
private Runnable smTask;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;

convertRegion = new Region();
convertBtn.setGraphic(convertRegion);
convertBtn.disableProperty().bind(Bindings.isEmpty(BlockListViewController.getItems()));

splitRad.setOnAction((actionEvent -> {
statusLbl.setText("");
convertRegion.getStyleClass().clear();
convertRegion.getStyleClass().add("regionSplitToOne");
fileFolderLabelLbl.setText(resourceBundle.getString("tabSplMrg_Txt_File"));
selectFileFolderBtn.setText(resourceBundle.getString("tabSplMrg_Btn_SelectFile"));
fileFolderActualPathLbl.setText("");
convertBtn.setDisable(true);
BlockListViewController.clear();
}));
mergeRad.setOnAction((actionEvent -> {
statusLbl.setText("");
convertRegion.getStyleClass().clear();
convertRegion.getStyleClass().add("regionOneToSplit");
fileFolderLabelLbl.setText(resourceBundle.getString("tabSplMrg_Txt_Folder"));
selectFileFolderBtn.setText(resourceBundle.getString("tabSplMrg_Btn_SelectFolder"));
fileFolderActualPathLbl.setText("");
convertBtn.setDisable(true);
BlockListViewController.clear();
}));

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

selectFileFolderBtn.setOnAction(actionEvent -> {
statusLbl.setText("");
List<File> alreadyAddedFiles = BlockListViewController.getItems();
if (splitRad.isSelected()) {
FileChooser fc = new FileChooser();
fc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFile"));
if (! fileFolderActualPathLbl.getText().isEmpty()){
File temporaryFile = new File(fileFolderActualPathLbl.getText()).getParentFile();
if (temporaryFile != null && temporaryFile.exists())
fc.setInitialDirectory(temporaryFile);
else
fc.setInitialDirectory(new File(System.getProperty("user.home")));
if (! alreadyAddedFiles.isEmpty()){
String recentLocation = FilesHelper.getRealFolder(alreadyAddedFiles.get(0).getParentFile().getAbsolutePath());
fc.setInitialDirectory(new File(recentLocation));
}
else
fc.setInitialDirectory(new File(System.getProperty("user.home")));
File fileFile = fc.showOpenDialog(changeSaveToBtn.getScene().getWindow());
if (fileFile == null)
List<File> files = fc.showOpenMultipleDialog(changeSaveToBtn.getScene().getWindow());
if (files == null || files.isEmpty())
return;
fileFolderActualPathLbl.setText(fileFile.getAbsolutePath());
this.BlockListViewController.addAll(files);
}
else{
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFolder"));
if (! fileFolderActualPathLbl.getText().isEmpty()){
File temporaryFile = new File(fileFolderActualPathLbl.getText());
if (temporaryFile.exists())
dc.setInitialDirectory(temporaryFile);
else
dc.setInitialDirectory(new File(System.getProperty("user.home")));
if (! alreadyAddedFiles.isEmpty()){
String recentLocation = FilesHelper.getRealFolder(alreadyAddedFiles.get(0).getParentFile().getAbsolutePath());
dc.setInitialDirectory(new File(recentLocation));
}
else
dc.setInitialDirectory(new File(System.getProperty("user.home")));

File folderFile = dc.showDialog(changeSaveToBtn.getScene().getWindow());
if (folderFile == null)
return;
fileFolderActualPathLbl.setText(folderFile.getAbsolutePath());
this.BlockListViewController.add(folderFile);
}
convertBtn.setDisable(false);
});

convertBtn.setOnAction(actionEvent -> setConvertBtnAction());
Expand Down Expand Up @@ -192,7 +186,7 @@ public void notifyThreadStarted(boolean isStart, EModule type){ // todo: refacto
* */
private void stopBtnAction(){
if (smThread != null && smThread.isAlive()) {
smTask.cancel();
smThread.interrupt();
}
}
/**
Expand All @@ -209,9 +203,9 @@ private void setConvertBtnAction(){
}

if (splitRad.isSelected())
smTask = new SplitTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
smTask = new SplitMergeTaskExecutor(true, BlockListViewController.getItems(), saveToPathLbl.getText());
else
smTask = new MergeTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
smTask = new SplitMergeTaskExecutor(false, BlockListViewController.getItems(), saveToPathLbl.getText());
smThread = new Thread(smTask);
smThread.setDaemon(true);
smThread.start();
Expand All @@ -230,14 +224,16 @@ private void handleDragOver(DragEvent event){
* */
@FXML
private void handleDrop(DragEvent event) {
File fileDrpd = event.getDragboard().getFiles().get(0);
List<File> files = event.getDragboard().getFiles();
File firstFile = files.get(0);

if (fileDrpd.isDirectory())
if (firstFile.isDirectory())
mergeRad.fire();
else
splitRad.fire();
fileFolderActualPathLbl.setText(fileDrpd.getAbsolutePath());
convertBtn.setDisable(false);

this.BlockListViewController.addAll(files);

event.setDropCompleted(true);
event.consume();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nsusbloader/ModelControllers/ILogPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.util.HashMap;

public interface ILogPrinter {
void print(String message, EMsgType type);
void updateProgress(Double value);
void print(String message, EMsgType type) throws InterruptedException;
void updateProgress(Double value) throws InterruptedException;
void update(HashMap<String, File> nspMap, EFileStatus status);
void update(File file, EFileStatus status);
void updateOneLinerStatus(boolean status);
Expand Down
Loading

0 comments on commit 1176ad9

Please sign in to comment.