diff --git a/client/src/main/java/ru/trader/World.java b/client/src/main/java/ru/trader/World.java index 6c28ef0..2a4d164 100644 --- a/client/src/main/java/ru/trader/World.java +++ b/client/src/main/java/ru/trader/World.java @@ -1,5 +1,7 @@ package ru.trader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; import ru.trader.core.Market; import ru.trader.core.MarketAnalyzer; @@ -17,6 +19,7 @@ import java.io.UnsupportedEncodingException; public class World { private static Market world; private static final String STORE_FILE="world.xml"; + private final static Logger LOG = LoggerFactory.getLogger(World.class); static { try { @@ -33,11 +36,24 @@ public class World { world.commit(); } + public static void saveTo(File file) throws FileNotFoundException, UnsupportedEncodingException, XMLStreamException { + Store.saveToFile(world, file); + world.commit(); + } + public static void imp(File file) throws IOException, SAXException { + LOG.info("Import from {}", file.getName()); XSSFImporter xssfImporter = new XSSFImporter(file); world = xssfImporter.doImport(); } + public static void impXml(File file) throws ParserConfigurationException, SAXException, IOException { + LOG.info("Import from {}", file.getName()); + Market market = Store.loadFromFile(file); + world.add(market); + } + + public static Market getMarket() { return world; } diff --git a/client/src/main/java/ru/trader/controllers/MainController.java b/client/src/main/java/ru/trader/controllers/MainController.java index 629e5fc..345ded1 100644 --- a/client/src/main/java/ru/trader/controllers/MainController.java +++ b/client/src/main/java/ru/trader/controllers/MainController.java @@ -18,6 +18,7 @@ import ru.trader.World; import ru.trader.model.*; import ru.trader.view.support.Localization; +import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamException; import java.io.File; import java.io.FileNotFoundException; @@ -111,19 +112,83 @@ public class MainController { public void importWorld(ActionEvent actionEvent) { try { FileChooser fileChooser = new FileChooser(); - FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Excel files (*.xlsx)", "*.xlsx"); + FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XML bd files (*.xml)", "*.xml"); fileChooser.getExtensionFilters().add(extFilter); fileChooser.setInitialDirectory(new File(".")); File file = fileChooser.showOpenDialog(null); if (file !=null) { - World.imp(file); + World.impXml(file); reload(); } - } catch (SAXException | IOException e) { + } catch (ParserConfigurationException | SAXException | IOException e) { LOG.error("Error on import file", e); } } + public void exportWorld(ActionEvent actionEvent) { + try { + FileChooser fileChooser = new FileChooser(); + FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XML bd files (*.xml)", "*.xml"); + fileChooser.getExtensionFilters().add(extFilter); + fileChooser.setInitialDirectory(new File(".")); + File file = fileChooser.showSaveDialog(null); + if (file !=null) { + World.saveTo(file); + reload(); + } + } catch (FileNotFoundException | UnsupportedEncodingException | XMLStreamException e) { + LOG.error("Error on save as file", e); + } + } + + public void clear(ActionEvent actionEvent){ + Action res = Screeners.showConfirm(String.format(Localization.getString("dialog.confirm.remove"), Localization.getString("market.all"))); + if (res == Dialog.ACTION_YES) { + market.clear(); + reload(); + } + } + + public void clearOffers(ActionEvent actionEvent){ + Action res = Screeners.showConfirm(String.format(Localization.getString("dialog.confirm.remove"), Localization.getString("market.offers"))); + if (res == Dialog.ACTION_YES) { + market.clearOffers(); + reload(); + } + } + + public void clearStations(ActionEvent actionEvent){ + Action res = Screeners.showConfirm(String.format(Localization.getString("dialog.confirm.remove"), Localization.getString("market.stations"))); + if (res == Dialog.ACTION_YES) { + market.clearStations(); + reload(); + } + } + + public void clearSystems(ActionEvent actionEvent){ + Action res = Screeners.showConfirm(String.format(Localization.getString("dialog.confirm.remove"), Localization.getString("market.systems"))); + if (res == Dialog.ACTION_YES) { + market.clearSystems(); + reload(); + } + } + + public void clearItems(ActionEvent actionEvent){ + Action res = Screeners.showConfirm(String.format(Localization.getString("dialog.confirm.remove"), Localization.getString("market.items"))); + if (res == Dialog.ACTION_YES) { + market.clearItems(); + reload(); + } + } + + public void clearGroups(ActionEvent actionEvent){ + Action res = Screeners.showConfirm(String.format(Localization.getString("dialog.confirm.remove"), Localization.getString("market.groups"))); + if (res == Dialog.ACTION_YES) { + market.clearGroups(); + reload(); + } + } + public Optional addGroup(){ GroupModel group = Screeners.showAddGroup(market); return Optional.ofNullable(group); @@ -189,6 +254,7 @@ public class MainController { } private void reload(){ + if (world != null) world.getModeler().clear(); world = new MarketModel(World.getMarket()); market = world; Screeners.reinitAll(); diff --git a/client/src/main/java/ru/trader/controllers/OffersController.java b/client/src/main/java/ru/trader/controllers/OffersController.java index 2d8e98b..f227561 100644 --- a/client/src/main/java/ru/trader/controllers/OffersController.java +++ b/client/src/main/java/ru/trader/controllers/OffersController.java @@ -101,6 +101,8 @@ public class OffersController { } void init(){ + station = null; + system = null; MarketModel market = MainController.getMarket(); market.getNotificator().add(new OffersChangeListener()); systems.setItems(market.systemsProperty()); diff --git a/client/src/main/java/ru/trader/controllers/Screeners.java b/client/src/main/java/ru/trader/controllers/Screeners.java index 172596f..d8c1972 100644 --- a/client/src/main/java/ru/trader/controllers/Screeners.java +++ b/client/src/main/java/ru/trader/controllers/Screeners.java @@ -248,6 +248,8 @@ public class Screeners { public static void reinitAll() { mainController.init(); + systemsEditorController.init(); + vEditorController.init(); filterController.init(); EMDNUpdater.setMarket(MainController.getMarket()); } diff --git a/client/src/main/java/ru/trader/controllers/StationEditorController.java b/client/src/main/java/ru/trader/controllers/StationEditorController.java index c3e3ed1..2ef0731 100644 --- a/client/src/main/java/ru/trader/controllers/StationEditorController.java +++ b/client/src/main/java/ru/trader/controllers/StationEditorController.java @@ -92,7 +92,7 @@ public class StationEditorController { init(); } - private void init(){ + void init(){ if (updater != null){ name.textProperty().unbindBidirectional(updater.nameProperty()); distance.numberProperty().unbindBidirectional(updater.distanceProperty()); diff --git a/client/src/main/java/ru/trader/controllers/SystemsEditorController.java b/client/src/main/java/ru/trader/controllers/SystemsEditorController.java index ec22cd4..8570baf 100644 --- a/client/src/main/java/ru/trader/controllers/SystemsEditorController.java +++ b/client/src/main/java/ru/trader/controllers/SystemsEditorController.java @@ -94,7 +94,7 @@ public class SystemsEditorController { init(); } - private void init(){ + void init(){ market = MainController.getMarket(); system1.setItems(market.systemsProperty()); system2.setItems(market.systemsProperty()); diff --git a/client/src/main/java/ru/trader/model/MarketModel.java b/client/src/main/java/ru/trader/model/MarketModel.java index 434b2ae..0737e56 100644 --- a/client/src/main/java/ru/trader/model/MarketModel.java +++ b/client/src/main/java/ru/trader/model/MarketModel.java @@ -203,4 +203,33 @@ public class MarketModel { return modeler.get(p); } + public void clear(){ + LOG.info("Clear market"); + market.clear(); + } + + public void clearOffers(){ + LOG.info("Clear offers"); + market.clearOffers(); + } + + public void clearStations(){ + LOG.info("Clear stations"); + market.clearVendors(); + } + + public void clearSystems(){ + LOG.info("Clear systems"); + market.clearPlaces(); + } + + public void clearItems(){ + LOG.info("Clear items"); + market.clearItems(); + } + + public void clearGroups(){ + LOG.info("Clear groups"); + market.clearGroups(); + } } diff --git a/client/src/main/resources/lang/locale_en_US.properties b/client/src/main/resources/lang/locale_en_US.properties index c34f939..434c311 100644 --- a/client/src/main/resources/lang/locale_en_US.properties +++ b/client/src/main/resources/lang/locale_en_US.properties @@ -1,6 +1,8 @@ # Market +market.all=All market.systems=Systems market.stations=Stations +market.groups=Commodities groups market.items=Commods market.offers=Offers market.item.name=Commodity @@ -46,6 +48,7 @@ main.title=Trader main.menu.file=File main.menu.file.save=Save main.menu.file.import=Import... +main.menu.file.export=Export... main.menu.edit=Edit main.menu.edit.addSystem=Add System main.menu.edit.editSystem=Edit System @@ -54,6 +57,7 @@ main.menu.edit.addStation=Add Station main.menu.edit.editStation=Edit Station main.menu.edit.removeStation=Delete Station main.menu.edit.addItem=Add Commodity +main.menu.edit.clear=Clear main.menu.settings=Settings main.menu.settings.language=Language main.menu.settings.language.item=English diff --git a/client/src/main/resources/lang/locale_ru_RU.properties b/client/src/main/resources/lang/locale_ru_RU.properties index c03c2da..bd786ff 100644 --- a/client/src/main/resources/lang/locale_ru_RU.properties +++ b/client/src/main/resources/lang/locale_ru_RU.properties @@ -1,6 +1,8 @@ # Market +market.all=\u0412\u0441\u0435 market.systems=\u0421\u0438\u0441\u0442\u0435\u043C\u044B market.stations=\u0421\u0442\u0430\u043D\u0446\u0438\u0438 +market.groups=\u0413\u0440\u0443\u043F\u043F\u044B \u0442\u043E\u0432\u0430\u0440\u043E\u0432 market.items=\u0422\u043E\u0432\u0430\u0440\u044B market.offers=\u0417\u0430\u043A\u0430\u0437\u044B market.item.name=\u0422\u043E\u0432\u0430\u0440 @@ -47,6 +49,7 @@ main.title=\u0422\u043E\u0440\u0433\u043E\u0439\u0434 main.menu.file=\u0424\u0430\u0439\u043B main.menu.file.save=\u0421\u043E\u0445\u0440\u0430\u043D\u0438\u0442\u044C main.menu.file.import=\u0418\u043C\u043F\u043E\u0440\u0442... +main.menu.file.export=\u042D\u043A\u0441\u043F\u043E\u0440\u0442... main.menu.edit=\u041F\u0440\u0430\u0432\u043A\u0430 main.menu.edit.addSystem=\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0441\u0438\u0441\u0442\u0435\u043C\u0443 main.menu.edit.editSystem=\u0420\u0435\u0434\u0430\u043A\u0442\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u0441\u0438\u0441\u0442\u0435\u043C\u0443 @@ -55,6 +58,7 @@ main.menu.edit.addStation=\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u044 main.menu.edit.editStation=\u0420\u0435\u0434\u0430\u043A\u0442\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u0441\u0442\u0430\u043D\u0446\u0438\u044E main.menu.edit.removeStation=\u0423\u0434\u0430\u043B\u0438\u0442\u044C \u0441\u0442\u0430\u043D\u0446\u0438\u044E main.menu.edit.addItem=\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0442\u043E\u0432\u0430\u0440 +main.menu.edit.clear=\u041E\u0447\u0438\u0441\u0442\u0438\u0442\u044C main.menu.settings=\u041D\u0430\u0441\u0442\u0440\u043E\u0439\u043A\u0438 main.menu.settings.language=\u042F\u0437\u044B\u043A main.menu.settings.language.item=\u0420\u0443\u0441\u0441\u043A\u0438\u0439 diff --git a/client/src/main/resources/view/main.fxml b/client/src/main/resources/view/main.fxml index 8e90b3d..c99fa86 100644 --- a/client/src/main/resources/view/main.fxml +++ b/client/src/main/resources/view/main.fxml @@ -12,6 +12,7 @@ + @@ -20,6 +21,14 @@ + + + + + + + + diff --git a/core/src/main/java/ru/trader/core/Market.java b/core/src/main/java/ru/trader/core/Market.java index 2baa347..fb698e0 100644 --- a/core/src/main/java/ru/trader/core/Market.java +++ b/core/src/main/java/ru/trader/core/Market.java @@ -1,6 +1,8 @@ package ru.trader.core; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Optional; public interface Market { @@ -51,4 +53,98 @@ public interface Market { default Collection getBuy(Item item){ return getStatBuy(item).getOffers(); } + + default void add(Market market){ + // add groups + Collection groups = market.getGroups(); + HashMap mapGroups = new HashMap<>(groups.size(), 0.9f); + for (Group group : groups) { + Group nGroup = this.addGroup(group.getName(), group.getType()); + mapGroups.put(group, nGroup); + } + // add items + Collection items = market.getItems(); + HashMap mapItems = new HashMap<>(items.size(), 0.9f); + for (Item item : items) { + Item nItem = this.addItem(item.getName(), mapGroups.get(item.getGroup())); + mapItems.put(item, nItem); + } + mapGroups.clear(); + // add places and vendors + for (Place place : market.get()) { + Place nPlace = this.addPlace(place.getName(), place.getX(), place.getY(), place.getZ()); + for (Vendor vendor : place.get()) { + Vendor nVendor = nPlace.addVendor(vendor.getName()); + nVendor.setDistance(vendor.getDistance()); + // add services + for (SERVICE_TYPE service : vendor.getServices()) { + nVendor.add(service); + } + // add offers + for (Offer offer : vendor.getAllBuyOffers()) { + nVendor.addOffer(offer.getType(), mapItems.get(offer.getItem()), offer.getPrice(), offer.getCount()); + } + for (Offer offer : vendor.getAllSellOffers()) { + nVendor.addOffer(offer.getType(), mapItems.get(offer.getItem()), offer.getPrice(), offer.getCount()); + } + } + } + } + + default void clear(){ + clear(true, true, true, true, true); + } + + default void clearGroups(){ + clear(true, false, false, true, true); + } + + default void clearItems(){ + clear(true, false, false, true, false); + } + + default void clearPlaces(){ + clear(false, false, true, false, false); + } + + default void clearVendors(){ + clear(false, true, false, false, false); + } + + default void clearOffers(){ + clear(true, false, false, false, false); + } + + + default void clear(boolean offers, boolean vendors, boolean places, boolean items, boolean groups){ + if (places){ + Collection p = new ArrayList<>(get()); + for (Place place : p) { + remove(place); + } + } else { + if (vendors || offers){ + for (Place place : get()) { + if (vendors) { + place.clear(); + } else { + place.clearOffers(); + } + } + } + } + if (items){ + Collection i = new ArrayList<>(getItems()); + for (Item item : i) { + remove(item); + } + } + if (groups){ + Collection g = new ArrayList<>(getGroups()); + for (Group group : g) { + remove(group); + } + } + } + } diff --git a/core/src/main/java/ru/trader/core/Place.java b/core/src/main/java/ru/trader/core/Place.java index 45c8402..9d7cb67 100644 --- a/core/src/main/java/ru/trader/core/Place.java +++ b/core/src/main/java/ru/trader/core/Place.java @@ -2,6 +2,7 @@ package ru.trader.core; import ru.trader.graph.Connectable; +import java.util.ArrayList; import java.util.Collection; import java.util.Optional; @@ -46,4 +47,16 @@ public interface Place extends Connectable { return Math.sqrt(Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + Math.pow(z - getZ(), 2)); } + default void clear(){ + Collection vendors = new ArrayList<>(get()); + for (Vendor vendor : vendors) { + remove(vendor); + } + } + + default void clearOffers(){ + for (Vendor vendor : get()) { + vendor.clear(); + } + } } diff --git a/core/src/main/java/ru/trader/core/Vendor.java b/core/src/main/java/ru/trader/core/Vendor.java index 48c3d46..e98aaf1 100644 --- a/core/src/main/java/ru/trader/core/Vendor.java +++ b/core/src/main/java/ru/trader/core/Vendor.java @@ -1,6 +1,8 @@ package ru.trader.core; import ru.trader.graph.Connectable; + +import java.util.ArrayList; import java.util.Collection; public interface Vendor extends Connectable { @@ -65,4 +67,12 @@ public interface Vendor extends Connectable { default boolean canRefill(){ return getPlace().canRefill(); } + + default void clear(){ + Collection offers = new ArrayList<>(getAllSellOffers()); + offers.addAll(getAllBuyOffers()); + for (Offer offer : offers) { + remove(offer); + } + } }