implement search stations
This commit is contained in:
199
client/src/main/java/ru/trader/controllers/SearchController.java
Normal file
199
client/src/main/java/ru/trader/controllers/SearchController.java
Normal file
@@ -0,0 +1,199 @@
|
||||
package ru.trader.controllers;
|
||||
|
||||
import javafx.beans.property.*;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.util.StringConverter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.core.MarketFilter;
|
||||
import ru.trader.core.SERVICE_TYPE;
|
||||
import ru.trader.model.*;
|
||||
import ru.trader.model.support.BindingsHelper;
|
||||
import ru.trader.model.support.ChangeMarketListener;
|
||||
import ru.trader.view.support.NumberField;
|
||||
import ru.trader.view.support.cells.CustomListCell;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class SearchController {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(SearchController.class);
|
||||
|
||||
@FXML
|
||||
private ComboBox<SystemModel> source;
|
||||
@FXML
|
||||
private ComboBox<ItemModel> items;
|
||||
@FXML
|
||||
private NumberField distance;
|
||||
@FXML
|
||||
private CheckBox cbMarket;
|
||||
@FXML
|
||||
private CheckBox cbBlackMarket;
|
||||
@FXML
|
||||
private CheckBox cbRepair;
|
||||
@FXML
|
||||
private CheckBox cbMunition;
|
||||
@FXML
|
||||
private CheckBox cbOutfit;
|
||||
@FXML
|
||||
private CheckBox cbShipyard;
|
||||
@FXML
|
||||
private CheckBox cbMediumLandpad;
|
||||
@FXML
|
||||
private CheckBox cbLargeLandpad;
|
||||
@FXML
|
||||
private TableView<ResultEntry> tblResults;
|
||||
|
||||
private final List<ResultEntry> results = FXCollections.observableArrayList();
|
||||
private final ObservableList<ItemModel> itemsList = FXCollections.observableArrayList();
|
||||
|
||||
private MarketModel market;
|
||||
|
||||
@FXML
|
||||
private void initialize() {
|
||||
items.setCellFactory(new CustomListCell<>(ItemModel::getName));
|
||||
items.setConverter(new StringConverter<ItemModel>() {
|
||||
@Override
|
||||
public String toString(ItemModel item) {
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemModel fromString(String string) {
|
||||
throw new UnsupportedOperationException("Is not editable field");
|
||||
}
|
||||
});
|
||||
BindingsHelper.setTableViewItems(tblResults, results);
|
||||
items.setItems(itemsList);
|
||||
init();
|
||||
}
|
||||
|
||||
void init(){
|
||||
market = MainController.getMarket();
|
||||
market.getNotificator().add(new SearchChangeListener());
|
||||
source.setItems(market.systemsProperty());
|
||||
itemsList.clear();
|
||||
itemsList.add(ModelFabric.NONE_ITEM);
|
||||
itemsList.addAll(market.itemsProperty().get());
|
||||
source.getSelectionModel().selectFirst();
|
||||
}
|
||||
|
||||
|
||||
private void addItem(ItemModel item){
|
||||
itemsList.add(item);
|
||||
}
|
||||
|
||||
private void removeItem(ItemModel item){
|
||||
itemsList.remove(item);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void searchStations(){
|
||||
MarketFilter filter = new MarketFilter();
|
||||
filter.setDistance(distance.getValue().doubleValue());
|
||||
if (cbMarket.isSelected()) filter.add(SERVICE_TYPE.MARKET); else filter.remove(SERVICE_TYPE.MARKET);
|
||||
if (cbBlackMarket.isSelected()) filter.add(SERVICE_TYPE.BLACK_MARKET); else filter.remove(SERVICE_TYPE.BLACK_MARKET);
|
||||
if (cbMunition.isSelected()) filter.add(SERVICE_TYPE.MUNITION); else filter.remove(SERVICE_TYPE.MUNITION);
|
||||
if (cbRepair.isSelected()) filter.add(SERVICE_TYPE.REPAIR); else filter.remove(SERVICE_TYPE.REPAIR);
|
||||
if (cbOutfit.isSelected()) filter.add(SERVICE_TYPE.OUTFIT); else filter.remove(SERVICE_TYPE.OUTFIT);
|
||||
if (cbShipyard.isSelected()) filter.add(SERVICE_TYPE.SHIPYARD); else filter.remove(SERVICE_TYPE.SHIPYARD);
|
||||
if (cbMediumLandpad.isSelected()) filter.add(SERVICE_TYPE.MEDIUM_LANDPAD); else filter.remove(SERVICE_TYPE.MEDIUM_LANDPAD);
|
||||
if (cbLargeLandpad.isSelected()) filter.add(SERVICE_TYPE.LARGE_LANDPAD); else filter.remove(SERVICE_TYPE.LARGE_LANDPAD);
|
||||
ItemModel item = items.getValue();
|
||||
if (item == null || item == ModelFabric.NONE_ITEM){
|
||||
Collection<StationModel> stations = market.getStations(filter);
|
||||
fill(stations);
|
||||
} else {
|
||||
Collection<OfferModel> offers = market.getOffers(item, filter);
|
||||
fill(offers);
|
||||
}
|
||||
}
|
||||
|
||||
private void fill(Collection<?> entries){
|
||||
results.clear();
|
||||
for (Object entry : entries) {
|
||||
if (entry instanceof StationModel){
|
||||
results.add(new ResultEntry((StationModel) entry));
|
||||
} else {
|
||||
if (entry instanceof OfferModel) {
|
||||
results.add(new ResultEntry((OfferModel) entry));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Argument must have StationModel or OfferModel class");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class ResultEntry {
|
||||
private final StationModel station;
|
||||
private final OfferModel offer;
|
||||
private final ReadOnlyDoubleProperty distance;
|
||||
|
||||
private ResultEntry(StationModel station) {
|
||||
this(station, null);
|
||||
}
|
||||
|
||||
private ResultEntry(OfferModel offer) {
|
||||
this(offer.getStation(), offer);
|
||||
}
|
||||
|
||||
private ResultEntry(StationModel station, OfferModel offer) {
|
||||
this.station = station;
|
||||
this.offer = offer;
|
||||
this.distance = new SimpleDoubleProperty(source.getValue().getDistance(station.getSystem()));
|
||||
}
|
||||
|
||||
public SystemModel getSystem(){
|
||||
return station.getSystem();
|
||||
}
|
||||
|
||||
private StationModel getStation(){
|
||||
return station;
|
||||
}
|
||||
|
||||
private OfferModel getOffer() {
|
||||
return offer;
|
||||
}
|
||||
|
||||
public ReadOnlyStringProperty stationProperty(){
|
||||
return new SimpleStringProperty(String.format("%s (%.0f Ls)", station.getName(), station.getDistance()));
|
||||
}
|
||||
|
||||
public ReadOnlyStringProperty nameProperty(){
|
||||
return offer != null ? offer.nameProperty() : new SimpleStringProperty("");
|
||||
}
|
||||
|
||||
public ReadOnlyDoubleProperty priceProperty(){
|
||||
return offer != null ? offer.priceProperty() : new SimpleDoubleProperty(Double.NaN);
|
||||
}
|
||||
|
||||
public ReadOnlyLongProperty countProperty(){
|
||||
return offer != null ? offer.countProperty() : new SimpleLongProperty(0);
|
||||
}
|
||||
|
||||
public ReadOnlyDoubleProperty distanceProperty(){
|
||||
return distance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class SearchChangeListener extends ChangeMarketListener {
|
||||
|
||||
@Override
|
||||
public void add(ItemModel item) {
|
||||
addItem(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(ItemModel item) {
|
||||
removeItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,12 @@ public class ItemModel {
|
||||
private final ItemStatModel statSell;
|
||||
private final ItemStatModel statBuy;
|
||||
|
||||
ItemModel() {
|
||||
this.item = null;
|
||||
this.statSell = null;
|
||||
this.statBuy = null;
|
||||
}
|
||||
|
||||
ItemModel(Item item, MarketModel market) {
|
||||
this.item = item;
|
||||
this.statSell = new ItemStatModel(market.getStat(OFFER_TYPE.SELL, item), market);
|
||||
@@ -30,7 +36,7 @@ public class ItemModel {
|
||||
|
||||
public String getId() {return item.getName();}
|
||||
|
||||
public String getName() {return name != null ? name.get() : item.getName();}
|
||||
public String getName() {return name != null ? name.get() : Localization.getString("item." + item.getName(), item.getName());}
|
||||
|
||||
public void setName(String value) {
|
||||
LOG.info("Change name of item {} to {}", item, value);
|
||||
|
||||
@@ -20,6 +20,7 @@ import ru.trader.services.OrdersSearchTask;
|
||||
import ru.trader.services.RoutesSearchTask;
|
||||
import ru.trader.view.support.Localization;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
@@ -122,6 +123,14 @@ public class MarketModel {
|
||||
return market.getStat(type, item);
|
||||
}
|
||||
|
||||
public ObservableList<OfferModel> getOffers(ItemModel item, MarketFilter filter){
|
||||
return BindingsHelper.observableList(analyzer.getOffers(item.getItem(), filter), modeler::get);
|
||||
}
|
||||
|
||||
public Collection<StationModel> getStations(MarketFilter filter){
|
||||
return BindingsHelper.observableList(analyzer.getVendors(filter), modeler::get);
|
||||
}
|
||||
|
||||
public void getOrders(SystemModel from, double balance, Consumer<ObservableList<OrderModel>> result) {
|
||||
getOrders(from, ModelFabric.NONE_STATION, ModelFabric.NONE_SYSTEM, ModelFabric.NONE_STATION, balance, result);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.model;
|
||||
|
||||
import javafx.beans.property.ReadOnlyDoubleProperty;
|
||||
import javafx.beans.property.ReadOnlyObjectProperty;
|
||||
import javafx.beans.property.ReadOnlyStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
@@ -117,6 +119,7 @@ public class ModelFabric {
|
||||
|
||||
public static SystemModel NONE_SYSTEM = new FAKE_SYSTEM_MODEL();
|
||||
public static StationModel NONE_STATION = new FAKE_STATION_MODEL();
|
||||
public static ItemModel NONE_ITEM = new FAKE_ITEM_MODEL();
|
||||
|
||||
private static class FAKE_SYSTEM_MODEL extends SystemModel {
|
||||
FAKE_SYSTEM_MODEL() {
|
||||
@@ -130,7 +133,7 @@ public class ModelFabric {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
throw new UnsupportedOperationException("Is fake system, change unsupported");
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -207,7 +210,7 @@ public class ModelFabric {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
throw new UnsupportedOperationException("Is fake system, unsupported");
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -290,4 +293,110 @@ public class ModelFabric {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static class FAKE_ITEM_MODEL extends ItemModel {
|
||||
|
||||
FAKE_ITEM_MODEL() {
|
||||
super();
|
||||
}
|
||||
|
||||
FAKE_ITEM_MODEL(Item item, MarketModel market) {
|
||||
super(item, market);
|
||||
}
|
||||
|
||||
@Override
|
||||
Item getItem() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String value) {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyStringProperty nameProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyDoubleProperty avgBuyProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyObjectProperty<OfferModel> minBuyProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyObjectProperty<OfferModel> maxBuyProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyObjectProperty<OfferModel> bestBuyProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyDoubleProperty avgSellProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyObjectProperty<OfferModel> minSellProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyObjectProperty<OfferModel> maxSellProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyObjectProperty<OfferModel> bestSellProperty() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OfferModel> getSeller() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OfferModel> getBuyer() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMarketItem() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh() {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh(OFFER_TYPE type) {
|
||||
throw new UnsupportedOperationException("Is fake item, unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user