Archived
0

implement profile controller and model

This commit is contained in:
iMoHax
2015-08-05 16:10:42 +03:00
parent 70ea0182ce
commit 5109fe89d5
3 changed files with 375 additions and 1 deletions

View File

@@ -35,15 +35,17 @@ public class MainController {
private final static Logger LOG = LoggerFactory.getLogger(MainController.class);
private static MarketModel world = new MarketModel(World.getMarket());
private static ProfileModel profile = new ProfileModel(Main.SETTINGS.getProfile(), world);
private static MarketModel market = world;
@FXML
private BorderPane mainPane;
@FXML
private Menu langs;
@FXML
private ProfileController profController;
@FXML
private OffersController offersController;
@FXML
@@ -54,6 +56,7 @@ public class MainController {
@FXML
private void initialize() {
fillLangs();
profController.setProfile(profile);
}
private void fillLangs() {
@@ -95,6 +98,10 @@ public class MainController {
return world;
}
public static ProfileModel getProfile() {
return profile;
}
public void setMarket(MarketModel market) {
MainController.market = market;
Screeners.reinitAll();

View File

@@ -0,0 +1,161 @@
package ru.trader.controllers;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;
import ru.trader.core.Engine;
import ru.trader.model.MarketModel;
import ru.trader.model.ProfileModel;
import ru.trader.model.StationModel;
import ru.trader.model.SystemModel;
import ru.trader.view.support.NumberField;
import ru.trader.view.support.autocomplete.AutoCompletion;
import ru.trader.view.support.autocomplete.SystemsProvider;
public class ProfileController {
@FXML
private TextField name;
@FXML
private NumberField balance;
@FXML
private TextField systemText;
@FXML
private ComboBox<StationModel> station;
@FXML
private NumberField mass;
@FXML
private NumberField tank;
@FXML
private NumberField cargo;
@FXML
private ComboBox<Engine> engine;
@FXML
private Button btnAddSystem;
@FXML
private Button btnAddStation;
private AutoCompletion<SystemModel> system;
private ProfileModel profile;
private boolean ignoreChanges;
@FXML
private void initialize() {
profile = MainController.getProfile();
MarketModel world = MainController.getWorld();
SystemsProvider provider = new SystemsProvider(world);
system = new AutoCompletion<>(systemText, provider, provider.getConverter());
engine.setItems(FXCollections.observableList(Engine.getEngines()));
engine.setConverter(new EngineStringConverter());
btnAddSystem.setOnAction(e -> Screeners.showSystemsEditor(null));
btnAddStation.setOnAction(e -> Screeners.showAddStation(profile.getSystem()));
initListeners();
}
private void initListeners(){
name.textProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setName(n);
});
balance.numberProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setBalance(n.doubleValue());
});
system.completionProperty().addListener((ov, o , n) -> {
if (!ignoreChanges){
profile.setSystem(n);
}
station.setItems(n.getStationsList());
});
station.valueProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setStation(n);
});
mass.numberProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setShipMass(n.doubleValue());
});
tank.numberProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setShipTank(n.doubleValue());
});
cargo.numberProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setShipCargo(n.intValue());
});
engine.valueProperty().addListener((ov, o, n) -> {
if (!ignoreChanges)
profile.setShipEngine(n);
});
}
public void setProfile(ProfileModel profile){
if (this.profile != null){
unbind();
}
this.profile = profile;
ignoreChanges = true;
name.setText(profile.getName());
balance.setValue(profile.getBalance());
system.setValue(profile.getSystem());
station.setValue(profile.getStation());
mass.setValue(profile.getShipMass());
tank.setValue(profile.getShipTank());
cargo.setValue(profile.getShipCargo());
engine.setValue(profile.getShipEngine());
bind();
ignoreChanges = false;
}
private void bind(){
profile.nameProperty().addListener(nameListener);
profile.balanceProperty().addListener(balanceListener);
profile.systemProperty().addListener(systemListener);
profile.stationProperty().addListener(stationListener);
profile.shipMassProperty().addListener(massListener);
profile.shipTankProperty().addListener(tankListener);
profile.shipCargoProperty().addListener(cargoListener);
profile.shipEngineProperty().addListener(engineListener);
}
private void unbind(){
profile.nameProperty().removeListener(nameListener);
profile.balanceProperty().removeListener(balanceListener);
profile.systemProperty().removeListener(systemListener);
profile.stationProperty().removeListener(stationListener);
profile.shipMassProperty().removeListener(massListener);
profile.shipTankProperty().removeListener(tankListener);
profile.shipCargoProperty().removeListener(cargoListener);
profile.shipEngineProperty().removeListener(engineListener);
}
private final ChangeListener<String> nameListener = (ov, o, n) -> name.setText(n);
private final ChangeListener<Number> balanceListener = (ov, o, n) -> balance.setValue(n);
private final ChangeListener<SystemModel> systemListener = (ov, o, n) -> system.setValue(n);
private final ChangeListener<StationModel> stationListener = (ov, o, n) -> station.setValue(n);
private final ChangeListener<Number> massListener = (ov, o, n) -> mass.setValue(n);
private final ChangeListener<Number> tankListener = (ov, o, n) -> tank.setValue(n);
private final ChangeListener<Number> cargoListener = (ov, o, n) -> cargo.setValue(n);
private final ChangeListener<Engine> engineListener = (ov, o, n) -> engine.setValue(n);
private class EngineStringConverter extends StringConverter<Engine> {
@Override
public String toString(Engine engine) {
return ""+engine.getClazz()+engine.getRating();
}
@Override
public Engine fromString(String string) {
throw new UnsupportedOperationException();
}
}
}

View File

@@ -0,0 +1,206 @@
package ru.trader.model;
import javafx.beans.property.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.core.Engine;
import ru.trader.core.Profile;
import ru.trader.core.Ship;
public class ProfileModel {
private final static Logger LOG = LoggerFactory.getLogger(ProfileModel.class);
private final Profile profile;
private final MarketModel market;
private final StringProperty name;
private final DoubleProperty balance;
private final ObjectProperty<SystemModel> system;
private final ObjectProperty<StationModel> station;
private final BooleanProperty docked;
private final DoubleProperty shipMass;
private final DoubleProperty shipTank;
private final IntegerProperty shipCargo;
private final ObjectProperty<Engine> shipEngine;
public ProfileModel(Profile profile, MarketModel market) {
this.market = market;
this.profile = profile;
name = new SimpleStringProperty();
balance = new SimpleDoubleProperty();
system = new SimpleObjectProperty<>();
station = new SimpleObjectProperty<>();
docked = new SimpleBooleanProperty();
shipMass = new SimpleDoubleProperty();
shipTank = new SimpleDoubleProperty();
shipCargo = new SimpleIntegerProperty();
shipEngine = new SimpleObjectProperty<>();
refresh();
initListeners();
}
private void initListeners() {
name.addListener((ov, o, n) -> {
LOG.debug("Change name, old: {}, new: {}", o, n);
profile.setName(n);
});
balance.addListener((ov, o, n) -> {
LOG.debug("Change balance, old: {}, new: {}", o, n);
profile.setBalance(n.doubleValue());
});
system.addListener((ov, o, n) -> {
LOG.debug("Change system, old: {}, new: {}", o, n);
profile.setSystem(n != null && n != ModelFabric.NONE_SYSTEM ? n.getSystem() : null);
});
station.addListener((ov, o, n) -> {
LOG.debug("Change station, old: {}, new: {}", o, n);
profile.setStation(n != null && n != ModelFabric.NONE_STATION ? n.getStation() : null);
});
docked.addListener((ov, o, n) -> {
LOG.debug("Change docked, old: {}, new: {}", o, n);
profile.setDocked(n);
});
shipMass.addListener((ov, o, n) -> {
LOG.debug("Change ship mass, old: {}, new: {}", o, n);
profile.getShip().setMass(n.doubleValue());
});
shipTank.addListener((ov, o, n) -> {
LOG.debug("Change ship tank, old: {}, new: {}", o, n);
profile.getShip().setTank(n.doubleValue());
});
shipCargo.addListener((ov, o, n) -> {
LOG.debug("Change ship cargo, old: {}, new: {}", o, n);
profile.getShip().setCargo(n.intValue());
});
shipEngine.addListener((ov, o, n) -> {
LOG.debug("Change ship engine, old: {}, new: {}", o, n);
profile.getShip().setEngine(n);
});
}
public MarketModel getMarket() {
return market;
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public double getBalance() {
return balance.get();
}
public DoubleProperty balanceProperty() {
return balance;
}
public void setBalance(double balance) {
this.balance.set(balance);
}
public SystemModel getSystem() {
return system.get();
}
public ObjectProperty<SystemModel> systemProperty() {
return system;
}
public void setSystem(SystemModel system) {
this.system.set(system);
}
public StationModel getStation() {
return station.get();
}
public ObjectProperty<StationModel> stationProperty() {
return station;
}
public void setStation(StationModel station) {
this.station.set(station);
}
public boolean getDocked() {
return docked.get();
}
public BooleanProperty dockedProperty() {
return docked;
}
public void setDocked(boolean docked) {
this.docked.set(docked);
}
public double getShipMass() {
return shipMass.get();
}
public DoubleProperty shipMassProperty() {
return shipMass;
}
public void setShipMass(double shipMass) {
this.shipMass.set(shipMass);
}
public double getShipTank() {
return shipTank.get();
}
public DoubleProperty shipTankProperty() {
return shipTank;
}
public void setShipTank(double shipTank) {
this.shipTank.set(shipTank);
}
public int getShipCargo() {
return shipCargo.get();
}
public IntegerProperty shipCargoProperty() {
return shipCargo;
}
public void setShipCargo(int shipCargo) {
this.shipCargo.set(shipCargo);
}
public Engine getShipEngine() {
return shipEngine.get();
}
public ObjectProperty<Engine> shipEngineProperty() {
return shipEngine;
}
public void setShipEngine(Engine engine) {
this.shipEngine.set(engine);
}
private void refresh(){
name.setValue(profile.getName());
balance.setValue(profile.getBalance());
system.setValue(market.getModeler().get(profile.getSystem()));
station.setValue(market.getModeler().get(profile.getStation()));
docked.setValue(profile.isDocked());
Ship ship = profile.getShip();
shipMass.setValue(ship.getMass());
shipTank.setValue(ship.getTank());
shipCargo.setValue(ship.getCargo());
shipEngine.setValue(ship.getEngine());
}
}