diff --git a/client/src/main/java/ru/trader/EDCE.java b/client/src/main/java/ru/trader/EDCE.java index 7737ed3..96cbd7e 100644 --- a/client/src/main/java/ru/trader/EDCE.java +++ b/client/src/main/java/ru/trader/EDCE.java @@ -1,6 +1,9 @@ package ru.trader; import javafx.application.Platform; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.ReadOnlyBooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.util.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +40,8 @@ public class EDCE { private final StationUpdater updater; private final EDSession session; private int errors; - private long interval; + private final Settings.EDCESettings settings; + private final BooleanProperty active; private boolean forceUpdate; public EDCE(ProfileModel profile, MarketModel world) throws IOException, ClassNotFoundException { @@ -45,7 +49,17 @@ public class EDCE { this.world = world; this.session = new EDSession(); this.updater = new StationUpdater(world); - interval = 20; + this.settings = Main.SETTINGS.getEdce(); + active = new SimpleBooleanProperty(settings.isActive()); + settings.activeProperty().addListener((ov, o, n) -> { + if (n) run(); + else stop(); + }); + if (active.get()) run(); + } + + public ReadOnlyBooleanProperty activeProperty() { + return active; } public void setForceUpdate(boolean forceUpdate) { @@ -201,11 +215,13 @@ public class EDCE { public void run(){ if (executor == null) executor = Executors.newSingleThreadScheduledExecutor(); - LOG.info("Start EDCE checker each {} sec", interval); - checker = executor.scheduleAtFixedRate(new EDCEChecker(), 1, interval, TimeUnit.SECONDS); + LOG.info("Start EDCE checker each {} sec", settings.getInterval()); + active.set(true); + checker = executor.scheduleAtFixedRate(new EDCEChecker(), 1, settings.getInterval(), TimeUnit.SECONDS); } public void stop(){ LOG.info("Stop EDCE checker"); + active.set(false); if (checker != null){ checker.cancel(false); } diff --git a/client/src/main/java/ru/trader/ServicesManager.java b/client/src/main/java/ru/trader/ServicesManager.java index 9b0bfd9..6334476 100644 --- a/client/src/main/java/ru/trader/ServicesManager.java +++ b/client/src/main/java/ru/trader/ServicesManager.java @@ -3,6 +3,7 @@ package ru.trader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.trader.controllers.MainController; +import ru.trader.controllers.Screeners; import java.io.IOException; @@ -10,7 +11,9 @@ public class ServicesManager { private final static Logger LOG = LoggerFactory.getLogger(ServicesManager.class); private static EDCE edce; - + public static EDCE getEdce() { + return edce; + } public static void runAll(){ runEDCE(); @@ -25,7 +28,7 @@ public class ServicesManager { private static void runEDCE() { try { edce = new EDCE(MainController.getProfile(), MainController.getWorld()); - edce.run(); + Screeners.getMainController().initEDCE(); } catch (IOException | ClassNotFoundException e) { LOG.warn("Error on init EDCE", e); } diff --git a/client/src/main/java/ru/trader/Settings.java b/client/src/main/java/ru/trader/Settings.java index f67acf2..a9332b1 100644 --- a/client/src/main/java/ru/trader/Settings.java +++ b/client/src/main/java/ru/trader/Settings.java @@ -1,8 +1,12 @@ package ru.trader; +import javafx.beans.property.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import ru.trader.core.*; +import ru.trader.core.Market; +import ru.trader.core.MarketFilter; +import ru.trader.core.Profile; +import ru.trader.core.Ship; import java.io.*; import java.util.Locale; @@ -14,16 +18,19 @@ public class Settings { private final Properties values = new Properties(); private final File file; private Profile profile; + private final EDCESettings edce; public Settings() { this.file = null; profile = new Profile(new Ship()); + edce = new EDCESettings(); } public Settings(File file) { this.file = file; profile = new Profile(new Ship()); + edce = new EDCESettings(); } public void load(Market market) { @@ -35,12 +42,14 @@ public class Settings { LOG.error("Error on load settings", e); } profile = Profile.readFrom(values, market); + edce.readFrom(values); } public void save(){ try (OutputStream os = new FileOutputStream(file)) { profile.writeTo(values); - values.store(os,"settings"); + edce.writeTo(values); + values.store(os, "settings"); } catch (IOException e) { LOG.error("Error on load settings", e); } @@ -84,7 +93,7 @@ public class Settings { } public long getEMDNAutoUpdate(){ - return Long.valueOf(values.getProperty("emdn.auto","0")); + return Long.valueOf(values.getProperty("emdn.auto", "0")); } public void setBalance(double balance){ @@ -138,4 +147,70 @@ public class Settings { public Profile getProfile() { return profile; } + + public EDCESettings getEdce(){ + return edce; + } + + public final class EDCESettings { + private final BooleanProperty active; + private final StringProperty email; + private final IntegerProperty interval; + + public EDCESettings() { + interval = new SimpleIntegerProperty(); + email = new SimpleStringProperty(); + active = new SimpleBooleanProperty(); + } + + public boolean isActive() { + return active.get(); + } + + public BooleanProperty activeProperty() { + return active; + } + + public void setActive(boolean active) { + this.active.set(active); + } + + public String getEmail() { + return email.get(); + } + + public StringProperty emailProperty() { + return email; + } + + public void setEmail(String email) { + this.email.set(email); + } + + public int getInterval() { + return interval.get(); + } + + public IntegerProperty intervalProperty() { + return interval; + } + + public void setInterval(int interval) { + this.interval.set(interval); + } + + public void readFrom(Properties values){ + setActive(!"0".equals(values.getProperty("edce.active", "0"))); + setEmail(values.getProperty("edce.mail", "example@mail.com")); + setInterval(Integer.valueOf(values.getProperty("edce.interval", "20"))); + } + + public void writeTo(Properties values){ + values.setProperty("edce.active", isActive() ? "1":"0"); + values.setProperty("edce.mail", getEmail()); + values.setProperty("edce.interval", String.valueOf(getInterval())); + } + + + } } diff --git a/client/src/main/java/ru/trader/controllers/LoginController.java b/client/src/main/java/ru/trader/controllers/LoginController.java index 6fad1f5..d775b05 100644 --- a/client/src/main/java/ru/trader/controllers/LoginController.java +++ b/client/src/main/java/ru/trader/controllers/LoginController.java @@ -8,6 +8,7 @@ import javafx.scene.control.*; import javafx.util.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ru.trader.Main; import ru.trader.view.support.Localization; import java.util.Optional; @@ -35,6 +36,7 @@ public class LoginController { loginButton.setDisable(true); email.textProperty().addListener((observable, oldValue, newValue) -> { loginButton.setDisable(newValue.trim().isEmpty()); + Main.SETTINGS.getEdce().setEmail(newValue); }); dialog.setResultConverter(dialogButton -> { if (dialogButton == loginButtonType) { diff --git a/client/src/main/java/ru/trader/controllers/MainController.java b/client/src/main/java/ru/trader/controllers/MainController.java index fbd9ec2..e470bdd 100644 --- a/client/src/main/java/ru/trader/controllers/MainController.java +++ b/client/src/main/java/ru/trader/controllers/MainController.java @@ -112,6 +112,10 @@ public class MainController { routerController.init(); } + public void initEDCE(){ + profController.initEDCEBtn(); + } + public void save(ActionEvent actionEvent) { try { World.save(); diff --git a/client/src/main/java/ru/trader/controllers/ProfileController.java b/client/src/main/java/ru/trader/controllers/ProfileController.java index a1e3e05..6905734 100644 --- a/client/src/main/java/ru/trader/controllers/ProfileController.java +++ b/client/src/main/java/ru/trader/controllers/ProfileController.java @@ -8,6 +8,8 @@ import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.layout.Pane; import javafx.util.StringConverter; +import ru.trader.Main; +import ru.trader.ServicesManager; import ru.trader.core.Engine; import ru.trader.model.*; import ru.trader.view.support.NumberField; @@ -48,6 +50,8 @@ public class ProfileController { private Button btnAddSystem; @FXML private Button btnAddStation; + @FXML + private ToggleButton btnEDCE; private AutoCompletion system; private ProfileModel profile; @@ -57,22 +61,25 @@ public class ProfileController { private void initialize() { init(); profile = MainController.getProfile(); - system.valueProperty().addListener((ov, o , n) -> { + system.valueProperty().addListener((ov, o, n) -> { doAndConsumeChanges(() -> { station.setItems(n.getStationNamesList()); station.getSelectionModel().selectFirst(); }); - consumeChanges(() -> {profile.setSystem(n); profile.setStation(ModelFabric.NONE_STATION);}); + consumeChanges(() -> { + profile.setSystem(n); + profile.setStation(ModelFabric.NONE_STATION); + }); }); engine.setItems(FXCollections.observableList(Engine.getEngines())); engine.setConverter(new EngineStringConverter()); btnAddSystem.setOnAction(e -> { if (ModelFabric.isFake(profile.getSystem())) Screeners.showSystemsEditor(null); - else Screeners.showSystemsEditor(profile.getSystem()); + else Screeners.showSystemsEditor(profile.getSystem()); }); btnAddStation.setOnAction(e -> { if (ModelFabric.isFake(profile.getStation())) Screeners.showAddStation(profile.getSystem()); - else Screeners.showEditStation(profile.getStation()); + else Screeners.showEditStation(profile.getStation()); }); shipInfo.setVisible(false); initListeners(); @@ -130,6 +137,29 @@ public class ProfileController { engine.valueProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setShipEngine(n))); } + public void initEDCEBtn(){ + btnEDCE.selectedProperty().bindBidirectional(Main.SETTINGS.getEdce().activeProperty()); + setEDCEBtnStyles(btnEDCE.isSelected()); + ServicesManager.getEdce().activeProperty().addListener((ov, o, n) -> { + setEDCEBtnStyles(n); + }); + + } + + private void setEDCEBtnStyles(boolean active){ + final String CSS_OK_CLASS = "service-ok"; + final String CSS_WARNING_CLASS = "service-warning"; + if (active) { + btnEDCE.getStyleClass().remove(CSS_WARNING_CLASS); + btnEDCE.getStyleClass().add(CSS_OK_CLASS); + } else { + btnEDCE.getStyleClass().remove(CSS_OK_CLASS); + if (btnEDCE.isSelected()) { + btnEDCE.getStyleClass().add(CSS_WARNING_CLASS); + } + } + } + public void setProfile(ProfileModel profile){ if (this.profile != null){ unbind(); diff --git a/client/src/main/java/ru/trader/controllers/Screeners.java b/client/src/main/java/ru/trader/controllers/Screeners.java index d0f8532..adb4032 100644 --- a/client/src/main/java/ru/trader/controllers/Screeners.java +++ b/client/src/main/java/ru/trader/controllers/Screeners.java @@ -63,7 +63,7 @@ public class Screeners { return mainScreen; } - private static void addStylesheet(Parent screen){ + private static void addStylesheet(Parent screen) { screen.getStylesheets().addAll(mainScreen.getStylesheets()); } @@ -187,6 +187,8 @@ public class Screeners { return mainScreen; } + public static MainController getMainController(){return mainController;} + public static void changeItemDesc(ItemModel item){ itemDescController.setItemDesc(item); } diff --git a/client/src/main/resources/view/profile.fxml b/client/src/main/resources/view/profile.fxml index dbf4d6a..639b763 100644 --- a/client/src/main/resources/view/profile.fxml +++ b/client/src/main/resources/view/profile.fxml @@ -1,12 +1,10 @@ - - - - - + + + @@ -44,4 +42,5 @@ +