Archived
0

implement cancel import, fix reload

This commit is contained in:
Mo
2016-04-26 16:21:54 +03:00
parent e3fc7919b5
commit f7cadbd9e7
12 changed files with 138 additions and 45 deletions

View File

@@ -10,9 +10,10 @@ import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import ru.trader.Main;
import ru.trader.World;
import ru.trader.maddavo.Parser;
import ru.trader.model.*;
import ru.trader.services.MaddavoParserTask;
import ru.trader.view.support.Localization;
import ru.trader.view.support.ViewUtils;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
@@ -45,6 +46,12 @@ public class MainController {
@FXML
private ItemsController itemsController;
@FXML
private RouteSearchController routesController;
@FXML
private SearchController searchController;
@FXML
private RouteTrackController routeController;
@FXML
private TabPane tabs;
@FXML
private Tab track;
@@ -104,8 +111,12 @@ public class MainController {
}
void init(){
profController.init();
itemsController.init();
offersController.init();
routesController.init();
searchController.init();
routeController.init();
//TODO: add init all controllers
}
@@ -266,34 +277,22 @@ public class MainController {
public void impMadSystems() {
chooseFile(new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv"), file -> {
try {
Parser.parseSystems(file, World.getMarket());
reload();
} catch (IOException e) {
LOG.error("Error on import file", e);
}
MaddavoParserTask task = new MaddavoParserTask(file, MaddavoParserTask.FILE_TYPE.SYSTEMS, World.getMarket());
Screeners.showProgress(Localization.getString("message.import.systems"), task, () -> ViewUtils.doFX(this::reload));
});
}
public void impMadStations() {
chooseFile(new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv"), file -> {
try {
Parser.parseStations(file, World.getMarket());
reload();
} catch (IOException e) {
LOG.error("Error on import file", e);
}
MaddavoParserTask task = new MaddavoParserTask(file, MaddavoParserTask.FILE_TYPE.STATIONS, World.getMarket());
Screeners.showProgress(Localization.getString("message.import.stations"), task, () -> ViewUtils.doFX(this::reload));
});
}
public void impMadOffers() {
chooseFile(new FileChooser.ExtensionFilter("Prices files (*.prices)", "*.prices"), file -> {
try {
Parser.parsePrices(file, World.getMarket());
reload();
} catch (IOException e) {
LOG.error("Error on import file", e);
}
MaddavoParserTask task = new MaddavoParserTask(file, MaddavoParserTask.FILE_TYPE.PRICES, World.getMarket());
Screeners.showProgress(Localization.getString("message.import.prices"), task, () -> ViewUtils.doFX(this::reload));
});
}

View File

@@ -1,13 +1,13 @@
package ru.trader.controllers;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Parent;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import ru.trader.services.*;
import java.util.function.Consumer;
@@ -15,7 +15,7 @@ public class ProgressController {
private Label text;
private ProgressBar bar;
private Dialog<ButtonType> dlg;
private AnalyzerTask task;
private Task task;
public ProgressController(Parent owner, String title) {
@@ -40,15 +40,15 @@ public class ProgressController {
dlg.setResultConverter(dialogButton -> {
if (dialogButton == Dialogs.CANCEL) {
if (task != null){
task.stop();
if (task != null) {
task.cancel(false);
}
}
return dialogButton;
});
}
private <T> void bind(AnalyzerTask<T> task, Consumer<T> onSuccess){
private <T> void bind(Task<T> task, Consumer<T> onSuccess){
bar.progressProperty().bind(task.progressProperty());
text.textProperty().bind(task.messageProperty());
this.task = task;
@@ -75,11 +75,20 @@ public class ProgressController {
task = null;
}
public <T> void run(AnalyzerTask<T> task, Consumer<T> onSuccess){
public <T> void run(Task<T> task, Consumer<T> onSuccess){
bind(task, onSuccess);
Platform.runLater(dlg::show);
new Thread(task).start();
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}
public <T> void run(Task<T> task){
bind(task, t -> {
});
Platform.runLater(dlg::show);
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}
}

View File

@@ -45,7 +45,7 @@ public class RouteSearchController {
initListeners();
}
private void init(){
void init(){
market = MainController.getMarket();
SystemsProvider provider = market.getSystemsProvider();
if (fromSystem == null){

View File

@@ -1,6 +1,7 @@
package ru.trader.controllers;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
@@ -23,6 +24,7 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.Optional;
import java.util.function.Consumer;
public class Screeners {
@@ -310,6 +312,17 @@ public class Screeners {
dialog.showAndWait();
}
public static void showProgress(String title, Task<Void> task, Runnable onSuccess){
showProgress(title, task, (t) -> onSuccess.run(), true);
}
public static <T> void showProgress(String title, Task<T> task, Consumer<T> onSuccess, boolean showFinishInfo){
ProgressController progress = new ProgressController(mainScreen, title);
progress.run(task, t -> {
if (showFinishInfo) showInfo(title, Localization.getString("message.finish"), null);
onSuccess.accept(t);
});
}
public static void showHelper(){
helperController.show(helperScreen, false);

View File

@@ -1,13 +1,19 @@
package ru.trader.db.controllers;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import ru.trader.controllers.MainController;
import ru.trader.model.MarketModel;
public class DBEditorController {
private Stage stage;
@FXML
private ItemsController itemsController;
@FXML
private SystemsController systemsController;
@FXML
private StationsController stationsController;
public void show(Parent content, boolean toggle) {
if (stage == null){
@@ -25,8 +31,9 @@ public class DBEditorController {
}
public void init(){
MarketModel market = MainController.getMarket();
//TODO: add init all controllers
itemsController.init();
systemsController.init();
stationsController.init();
}
public void close() {

View File

@@ -8,7 +8,6 @@ import ru.trader.analysis.AnalysisCallBack;
import ru.trader.core.MarketAnalyzer;
import ru.trader.core.Profile;
import ru.trader.model.MarketModel;
import ru.trader.model.ProfileModel;
import ru.trader.view.support.Localization;
import java.util.concurrent.atomic.AtomicLong;
@@ -50,8 +49,10 @@ public abstract class AnalyzerTask<T> extends Task<T> {
}
public void stop(){
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
callback.cancel();
return mayInterruptIfRunning && super.cancel(true);
}
private class AnalyzerCallBack extends AnalysisCallBack {

View File

@@ -0,0 +1,46 @@
package ru.trader.services;
import javafx.concurrent.Task;
import ru.trader.core.Market;
import ru.trader.maddavo.Parser;
import java.io.File;
public class MaddavoParserTask extends Task<Void> {
private final File file;
private final FILE_TYPE type;
private final Market market;
private final Parser parser;
public MaddavoParserTask(File file, FILE_TYPE type, Market market) {
this.file = file;
this.type = type;
this.market = market;
this.parser = new Parser();
}
@Override
protected void cancelled() {
parser.cancel();
}
@Override
protected Void call() throws Exception {
switch (type){
case SYSTEMS: parser.parseSystems(file, market);
break;
case STATIONS: parser.parseStations(file, market);
break;
case PRICES: parser.parsePrices(file, market);
break;
}
return null;
}
public enum FILE_TYPE {
SYSTEMS, STATIONS, PRICES
}
}

View File

@@ -268,6 +268,10 @@ helper.items=Commodities:
#messages
message.wrongNumber=Wrong number
message.wrongDuration=Wrong duration format, use like 1w1d1h1m1s
message.import.systems=Import systems
message.import.stations=Import stations
message.import.prices=Import prices
message.finish=Finish
#labels
label.allegiance=${market.allegiance}:

View File

@@ -269,6 +269,10 @@ helper.items=\u0422\u043E\u0432\u0430\u0440\u044B:
#messages
message.wrongNumber=\u0414\u043E\u0441\u0442\u0443\u043F\u043D\u044B \u0442\u043E\u043B\u044C\u043A\u043E \u0446\u0438\u0444\u0440\u044B
message.wrongDuration=\u041D\u0435\u0432\u0435\u0440\u043D\u044B\u0439 \u0444\u043E\u0440\u043C\u0430\u0442 \u043F\u0435\u0440\u0438\u043E\u0434\u0430, \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u0439\u0442\u0435 \u0444\u043E\u0440\u043C\u0430\u0442 1w1d1h1m1s
message.import.systems=\u0418\u043C\u043F\u043E\u0440\u0442 \u0441\u0438\u0441\u0442\u0435\u043C
message.import.stations=\u0418\u043C\u043F\u043E\u0440\u0442 \u0441\u0442\u0430\u043D\u0446\u0438\u0439
message.import.prices=\u0418\u043C\u043F\u043E\u0440\u0442 \u0446\u0435\u043D
message.finish=\u0413\u043E\u0442\u043E\u0432\u043E
#labels
label.allegiance=${market.allegiance}:

View File

@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<TabPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="ru.trader.db.controllers.DBEditorController"
tabClosingPolicy="UNAVAILABLE"

View File

@@ -57,13 +57,13 @@
<fx:include fx:id="offers" source="offers.fxml"/>
</Tab>
<Tab text="%main.tab.routes">
<fx:include source="routeSearch.fxml"/>
<fx:include fx:id="routes" source="routeSearch.fxml"/>
</Tab>
<Tab text="%main.tab.search">
<fx:include source="search.fxml"/>
<fx:include fx:id="search" source="search.fxml"/>
</Tab>
<Tab fx:id="track" text="%main.tab.route">
<fx:include source="routeTrack.fxml"/>
<fx:include fx:id="route" source="routeTrack.fxml"/>
</Tab>
</TabPane>
</center>