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

View File

@@ -1,13 +1,13 @@
package ru.trader.controllers; package ru.trader.controllers;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.control.ButtonType; import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog; import javafx.scene.control.Dialog;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar; import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import ru.trader.services.*;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -15,7 +15,7 @@ public class ProgressController {
private Label text; private Label text;
private ProgressBar bar; private ProgressBar bar;
private Dialog<ButtonType> dlg; private Dialog<ButtonType> dlg;
private AnalyzerTask task; private Task task;
public ProgressController(Parent owner, String title) { public ProgressController(Parent owner, String title) {
@@ -41,14 +41,14 @@ public class ProgressController {
dlg.setResultConverter(dialogButton -> { dlg.setResultConverter(dialogButton -> {
if (dialogButton == Dialogs.CANCEL) { if (dialogButton == Dialogs.CANCEL) {
if (task != null) { if (task != null) {
task.stop(); task.cancel(false);
} }
} }
return dialogButton; 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()); bar.progressProperty().bind(task.progressProperty());
text.textProperty().bind(task.messageProperty()); text.textProperty().bind(task.messageProperty());
this.task = task; this.task = task;
@@ -75,11 +75,20 @@ public class ProgressController {
task = null; 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); bind(task, onSuccess);
Platform.runLater(dlg::show); 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(); initListeners();
} }
private void init(){ void init(){
market = MainController.getMarket(); market = MainController.getMarket();
SystemsProvider provider = market.getSystemsProvider(); SystemsProvider provider = market.getSystemsProvider();
if (fromSystem == null){ if (fromSystem == null){

View File

@@ -1,6 +1,7 @@
package ru.trader.controllers; package ru.trader.controllers;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.Parent; import javafx.scene.Parent;
@@ -23,6 +24,7 @@ import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URL; import java.net.URL;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;
public class Screeners { public class Screeners {
@@ -310,6 +312,17 @@ public class Screeners {
dialog.showAndWait(); 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(){ public static void showHelper(){
helperController.show(helperScreen, false); helperController.show(helperScreen, false);

View File

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

View File

@@ -8,7 +8,6 @@ import ru.trader.analysis.AnalysisCallBack;
import ru.trader.core.MarketAnalyzer; import ru.trader.core.MarketAnalyzer;
import ru.trader.core.Profile; import ru.trader.core.Profile;
import ru.trader.model.MarketModel; import ru.trader.model.MarketModel;
import ru.trader.model.ProfileModel;
import ru.trader.view.support.Localization; import ru.trader.view.support.Localization;
import java.util.concurrent.atomic.AtomicLong; 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(); callback.cancel();
return mayInterruptIfRunning && super.cancel(true);
} }
private class AnalyzerCallBack extends AnalysisCallBack { 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 #messages
message.wrongNumber=Wrong number message.wrongNumber=Wrong number
message.wrongDuration=Wrong duration format, use like 1w1d1h1m1s 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 #labels
label.allegiance=${market.allegiance}: label.allegiance=${market.allegiance}:

View File

@@ -269,6 +269,10 @@ helper.items=\u0422\u043E\u0432\u0430\u0440\u044B:
#messages #messages
message.wrongNumber=\u0414\u043E\u0441\u0442\u0443\u043F\u043D\u044B \u0442\u043E\u043B\u044C\u043A\u043E \u0446\u0438\u0444\u0440\u044B 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.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 #labels
label.allegiance=${market.allegiance}: label.allegiance=${market.allegiance}:

View File

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

View File

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

View File

@@ -4,35 +4,46 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ru.trader.core.Market; import ru.trader.core.Market;
import java.io.*; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Parser { public class Parser {
private final static Logger LOG = LoggerFactory.getLogger(Parser.class); private final static Logger LOG = LoggerFactory.getLogger(Parser.class);
public static void parseSystems(File file, Market market) throws IOException { private boolean canceled;
public void parseSystems(File file, Market market) throws IOException {
parseFile(file, new SystemHandler(market), 1); parseFile(file, new SystemHandler(market), 1);
} }
public static void parseStations(File file, Market market) throws IOException { public void parseStations(File file, Market market) throws IOException {
parseFile(file, new StationHandler(market), 1); parseFile(file, new StationHandler(market), 1);
} }
public static void parsePrices(File file, Market market) throws IOException { public void parsePrices(File file, Market market) throws IOException {
parseFile(file, new OffersHandler(market, true), 0); parseFile(file, new OffersHandler(market, true), 0);
} }
private static void parseFile(File file, ParseHandler handler, int skip) throws IOException { private void parseFile(File file, ParseHandler handler, int skip) throws IOException {
canceled = false;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) { try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
int row = 0; int row = 0;
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (canceled) break;
row++; row++;
if (row <= skip) continue; if (row <= skip) continue;
handler.parse(line); handler.parse(line);
} }
} }
} }
public void cancel(){
this.canceled = true;
}
} }