Refactor gui.main.Main* to use new View/Model infra.

This commit is contained in:
Chris Beams 2014-11-22 18:41:00 +01:00
parent b0c5e14e1a
commit 5f69f84f85
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
6 changed files with 12 additions and 131 deletions

View File

@ -1,52 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import static com.google.common.base.Preconditions.checkNotNull;
public abstract class Controller<M extends XModel> implements Initializable {
public static final String TITLE_KEY = "view.title";
protected M model;
protected Controller(M model) {
this.model = checkNotNull(model);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
model.initialize();
doInitialize();
}
public final void terminate() {
//model.terminate();
//doTerminate();
}
protected abstract void doInitialize();
protected void doTerminate() { };
}

View File

@ -1,44 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.*;
public abstract class FxmlController<R extends Node, M extends XModel> extends Controller<M> {
protected @FXML R root;
protected FxmlController(M model) {
super(model);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
root.sceneProperty().addListener((ov, oldValue, newValue) -> {
// root node has been removed the scene
if (oldValue != null && newValue == null)
terminate();
});
super.initialize(url, rb);
}
}

View File

@ -1,22 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui;
public interface XModel {
void initialize();
}

View File

@ -17,7 +17,7 @@
-->
<?import javafx.scene.layout.*?>
<StackPane fx:id="root" fx:controller="io.bitsquare.gui.main.MainViewCB"
<StackPane fx:id="root" fx:controller="io.bitsquare.gui.main.MainView"
prefHeight="750" prefWidth="1000" stylesheets="/io/bitsquare/gui/bitsquare.css"
xmlns:fx="http://javafx.com/fxml">
</StackPane>

View File

@ -19,7 +19,6 @@ package io.bitsquare.gui.main;
import io.bitsquare.BitsquareException;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.gui.FxmlController;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.OverlayManager;
import io.bitsquare.gui.View;
@ -45,7 +44,7 @@ import javafx.scene.text.*;
import static io.bitsquare.gui.Navigation.Item.*;
import static javafx.scene.layout.AnchorPane.*;
public class MainViewCB extends FxmlController<Pane, MainModel> {
public class MainView extends View<MainViewModel> {
private final ToggleGroup navButtons = new ToggleGroup();
@ -56,8 +55,8 @@ public class MainViewCB extends FxmlController<Pane, MainModel> {
private final String title;
@Inject
public MainViewCB(MainModel model, ViewLoader viewLoader, Navigation navigation, OverlayManager overlayManager,
Transitions transitions, @Named(TITLE_KEY) String title) {
public MainView(MainViewModel model, ViewLoader viewLoader, Navigation navigation, OverlayManager overlayManager,
Transitions transitions, @Named(TITLE_KEY) String title) {
super(model);
this.viewLoader = viewLoader;
this.navigation = navigation;
@ -74,7 +73,7 @@ public class MainViewCB extends FxmlController<Pane, MainModel> {
}
@Override
public void doInitialize() {
public void initialize() {
ToggleButton homeButton = new NavButton(HOME) {{
setDisable(true); // during irc demo
}};

View File

@ -20,6 +20,7 @@ package io.bitsquare.gui.main;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.BitcoinNetwork;
import io.bitsquare.btc.WalletService;
import io.bitsquare.gui.ViewModel;
import io.bitsquare.gui.XModel;
import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.msg.MessageService;
@ -51,8 +52,8 @@ import org.slf4j.LoggerFactory;
import rx.Observable;
class MainModel implements XModel {
private static final Logger log = LoggerFactory.getLogger(MainModel.class);
class MainViewModel implements ViewModel {
private static final Logger log = LoggerFactory.getLogger(MainViewModel.class);
final DoubleProperty networkSyncProgress = new SimpleDoubleProperty(-1);
final IntegerProperty numPendingTrades = new SimpleIntegerProperty(0);
@ -83,8 +84,9 @@ class MainModel implements XModel {
@Inject
public MainModel(User user, WalletService walletService, MessageService messageService, TradeManager tradeManager,
BitcoinNetwork bitcoinNetwork, BSFormatter formatter, Persistence persistence) {
public MainViewModel(User user, WalletService walletService, MessageService messageService,
TradeManager tradeManager, BitcoinNetwork bitcoinNetwork, BSFormatter formatter,
Persistence persistence) {
this.user = user;
this.walletService = walletService;
this.messageService = messageService;
@ -93,10 +95,7 @@ class MainModel implements XModel {
this.bitcoinNetwork = bitcoinNetwork;
user.getCurrentBankAccount().addListener((observable, oldValue, newValue) -> persistence.write(user));
}
@Override
public void initialize() {
bootstrapState.addListener((ov, oldValue, newValue) -> {
if (newValue == BootstrapState.DISCOVERY_DIRECT_SUCCEEDED ||
newValue == BootstrapState.DISCOVERY_AUTO_PORT_FORWARDING_SUCCEEDED ||
@ -151,6 +150,7 @@ class MainModel implements XModel {
bankAccountsComboBoxPrompt.set(user.getBankAccounts().isEmpty() ? "No accounts" : "");
}
public Observable<?> initBackend() {
walletService.getDownloadProgress().subscribe(