/* 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 . */ package nsusbloader.ModelControllers; import javafx.animation.AnimationTimer; import javafx.scene.control.ProgressBar; import javafx.scene.control.ProgressIndicator; import javafx.scene.control.TextArea; import nsusbloader.Controllers.NSTableViewController; import nsusbloader.MediatorControl; import nsusbloader.NSLDataTypes.EFileStatus; import nsusbloader.NSLDataTypes.EModule; import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; public class MessagesConsumer extends AnimationTimer { private final BlockingQueue msgQueue; private final TextArea logsArea; private final BlockingQueue progressQueue; private final ProgressBar progressBar; private final HashMap statusMap; private final NSTableViewController tableViewController; private final EModule appModuleType; private AtomicBoolean oneLinerStatus; private boolean isInterrupted; MessagesConsumer(EModule appModuleType, BlockingQueue msgQueue, BlockingQueue progressQueue, HashMap statusMap, AtomicBoolean oneLinerStatus) { this.appModuleType = appModuleType; this.isInterrupted = false; this.msgQueue = msgQueue; this.logsArea = MediatorControl.getInstance().getContoller().logArea; this.progressQueue = progressQueue; this.progressBar = MediatorControl.getInstance().getContoller().progressBar; this.statusMap = statusMap; this.tableViewController = MediatorControl.getInstance().getContoller().GamesTabController.tableFilesListController; this.oneLinerStatus = oneLinerStatus; progressBar.setProgress(0.0); progressBar.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS); MediatorControl.getInstance().setBgThreadActive(true, appModuleType); } @Override public void handle(long l) { ArrayList messages = new ArrayList<>(); int msgRecieved = msgQueue.drainTo(messages); if (msgRecieved > 0) messages.forEach(logsArea::appendText); ArrayList progress = new ArrayList<>(); int progressRecieved = progressQueue.drainTo(progress); if (progressRecieved > 0) { progress.forEach(prg -> { if (prg != 1.0) progressBar.setProgress(prg); else progressBar.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS); }); } if (isInterrupted) { // It's safe 'cuz it's could't be interrupted while HashMap populating MediatorControl.getInstance().setBgThreadActive(false, appModuleType); progressBar.setProgress(0.0); if (statusMap.size() > 0){ for (String key : statusMap.keySet()) tableViewController.setFileStatus(key, statusMap.get(key)); } switch (appModuleType){ case RCM: MediatorControl.getInstance().getContoller().getRcmCtrlr().setOneLineStatus(oneLinerStatus.get()); break; case NXDT: MediatorControl.getInstance().getContoller().getNXDTabController().setOneLineStatus(oneLinerStatus.get()); break; case SPLIT_MERGE_TOOL: MediatorControl.getInstance().getContoller().getSmCtrlr().setOneLineStatus(oneLinerStatus.get()); break; } this.stop(); } } public void interrupt(){ this.isInterrupted = true; } }