Java tutorial
/* * DoomManager * Copyright (C) 2014 Chris K * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package ca.wumbo.doommanager.client.controller.file; import javax.annotation.PostConstruct; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.Scene; import javafx.scene.control.MenuItem; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.ProgressBarTableCell; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import ca.wumbo.doommanager.client.controller.CoreController; import ca.wumbo.doommanager.client.util.Controllable; import ca.wumbo.doommanager.client.util.Resources; import ca.wumbo.doommanager.client.util.SelfInjectableController; import ca.wumbo.doommanager.file.entry.designator.EntryDesignator; import static com.google.common.base.Preconditions.*; /** * The controller for the task manager that handles any tasks that can be run * on a thread without interacting with the JavaFX scenegraph. */ public class TaskManagerController extends SelfInjectableController implements Controllable { @Autowired private CoreController coreController; @Autowired private Resources resources; @Value("${taskmanager.controller.fxmlpath}") private String fxmlPath; /** * The logger for this class. */ private static final Logger log = LogManager.getLogger(TaskManagerController.class); /** * The stage for the task manager. */ private Stage taskManagerStage; /** * The scene for the task manager. */ private Scene taskManagerScene; //========================================================================= @FXML private BorderPane rootBorderPane; @FXML private MenuItem clearFinishedMenuItem; @FXML private MenuItem stopAllMenuItem; @FXML private MenuItem closeMenuItem; @FXML private MenuItem helpMenuItem; @FXML private TableView<EntryDesignator> tableView; @FXML private TableColumn<EntryDesignator, String> fileIpColumn; @FXML private TableColumn<EntryDesignator, Double> progressBarColumn; @FXML private TableColumn<EntryDesignator, String> statusColumn; //========================================================================= private TaskManagerController() { // int numThreads = Math.max(1, Runtime.getRuntime().availableProcessors()); // log.debug("Using {} threads for the task manager.", numThreads); } @FXML private void initialize() { log.debug("Initializing scene and stage for task manager."); taskManagerScene = new Scene(getRootPane()); taskManagerStage = new Stage(); taskManagerStage.setTitle("Task Manager"); taskManagerStage.setScene(taskManagerScene); taskManagerStage.hide(); fileIpColumn.setCellValueFactory((cellData) -> cellData.getValue().dataLocationProperty()); progressBarColumn.setCellValueFactory((cellData) -> cellData.getValue().progressProperty().asObject()); progressBarColumn.setCellFactory(ProgressBarTableCell.forTableColumn()); // Needed to render a progress bar. statusColumn.setCellValueFactory((cellData) -> cellData.getValue().messageProperty()); clearFinishedMenuItem.setGraphic(new ImageView(resources.getImage("clear"))); stopAllMenuItem.setGraphic(new ImageView(resources.getImage("stop"))); closeMenuItem.setGraphic(new ImageView(resources.getImage("close"))); helpMenuItem.setGraphic(new ImageView(resources.getImage("help"))); // For some odd reason, the table is selected and it has a blue glow around it. // This is due to focusing, so we'll just focus on the root pane to fix that. Platform.runLater(() -> rootBorderPane.requestFocus()); } /** * Displays the window. */ public void showWindow() { taskManagerStage.show(); } /** * Clears out all the finished entries. */ public void clearFinished() { tableView.getItems().removeIf(EntryDesignator::isDone); } /** * Stops all the current running entries. */ public void stopAll() { tableView.getItems().stream().filter((ed) -> !ed.isDone()).forEach(EntryDesignator::cancel); } /** * Closes the window (makes it hide since this lasts until the end of the * program). */ public void close() { taskManagerStage.hide(); } /** * Starts a new task with processing entries, and will pass the completed * object to the GUI for user access. * * @param entryDesignator * The EntryDesignator to run. * * @throws NullPointerException * If the argument is null. */ public void processEntryDesignation(EntryDesignator entryDesignator) { checkNotNull(entryDesignator, "Passed a null entry designator to the task manager."); // Set up the table information in the task manager. tableView.getItems().add(entryDesignator); // Start the thread. Thread t = new Thread(entryDesignator); t.setDaemon(true); t.start(); } @PostConstruct public void loadFXML() { super.loadFXML(fxmlPath); } @Override public Pane getRootPane() { return rootBorderPane; } }