145 lines
7.2 KiB
C#
145 lines
7.2 KiB
C#
using bts;
|
|
using bts.api;
|
|
using bts.shell;
|
|
using bts.spark;
|
|
using mbts.persist;
|
|
using mbts.ui;
|
|
using mbts.ui.settings;
|
|
using mbts.ui.spark;
|
|
using mbts.ui.utility;
|
|
using mbts.utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Windows;
|
|
using static bts.shell.SyntheticManagementWindowFactory;
|
|
|
|
namespace mbts.spark {
|
|
class SyntheticManagementWindow : SparkWindow<StartupData>, ISlowWindow {
|
|
const string _window_type = "SyntheticManagement";
|
|
SingleValuePump<SyntheticManagementDialogVM> _vm = new SingleValuePump<SyntheticManagementDialogVM>();
|
|
StartupData _state;
|
|
OrdersModel _orders_model;
|
|
SyntheticManagementView _view;
|
|
IDisposable _subscriptions;
|
|
IReadOnlyList<SparkCommand> _commands;
|
|
readonly IWindowSettingsManager<GridSettings> _settings_manager;
|
|
|
|
|
|
public SyntheticManagementWindow(SyntheticManagementWindowFactory factory, IBus bus, OrdersModel orders_model, StartupData state, WindowSettingsManager<GridSettings> settings_manager)
|
|
: base("Synthetic Management", bus, factory, SparkCommands.ShowSyntheticManagement.icon) {
|
|
_orders_model = orders_model;
|
|
_state = state;
|
|
_settings_manager = settings_manager;
|
|
Icon.Freeze();
|
|
}
|
|
|
|
public void thread_init() {
|
|
Log.notnull(_state);
|
|
|
|
var vm_connectable_builder = new ConnectableSeriesBuilder<SyntheticManagementDialogVM>(_vm.observable);
|
|
var current_row_source = vm_connectable_builder.create(vm => vm.current_row_observable);
|
|
var current_row_connectable_builder = new ConnectableSeriesBuilder<SyntheticManagementDialogVM.Row>(current_row_source);
|
|
|
|
_commands = new SparkCommand[] {
|
|
SparkCommand.create("Add", Icons.AddSynthetic, SparkTags.ShowSyntheticCreateTag).complete(
|
|
() => new SecurityCriteria(), () => true
|
|
), SparkCommand.create("Remove", Icons.RemoveSynthetic, SparkTags.SyntheticDeleteTag).complete(
|
|
current_row_connectable_builder.create(observe_row_mod_params),
|
|
can_execute, select_security_id
|
|
), SparkCommand.create("Change", Icons.ModifySynthetic, SparkTags.ShowSyntheticStrategyTag).complete(
|
|
current_row_source,
|
|
r => r != null, r => r?.security ?? SecurityDataHelper.Defaults.SecurityImmutable
|
|
), SparkCommand.create("Clone", Icons.CloneSynthetic, SparkTags.ShowSyntheticCloneTag).complete(
|
|
current_row_connectable_builder.create(observe_row_strategy),
|
|
rs => rs.HasValue, select_row_strategy_tuple
|
|
), SparkCommands.ShowLadder.complete(
|
|
vm_connectable_builder.create(vm => vm.current_security_md), md => md.HasValue, ObjectHelper.ValueOrDefault
|
|
), SparkCommands.ShowOrders.complete(() => new OrderBookWindowFactory.OrderBookStartupData {
|
|
filters = new OrderBookWindowFactory.FilterStates {
|
|
security_criteria = new SecurityCriteria(_vm.value.row_vms.Select(r => r.security))
|
|
}
|
|
}
|
|
), SparkCommands.window_settings_command(new DialogCommand(() => {
|
|
var vm = new WindowSettingsDialogVM<GridSettings, SimpleGridSettingsVM>(
|
|
_settings_manager,
|
|
us => us.synthetic_management_grid,
|
|
UserSettings.FileExtensions.GRID,
|
|
s => _vm.value.grid_settings_manager.create_vm(
|
|
// we need to get the most up to date GridSettings when launching b/c users may
|
|
// have changed some settings on the grid directly, like column widths.
|
|
s.with(_vm.value.columns.Select( c => c.create())),
|
|
this.observe_title()
|
|
)
|
|
);
|
|
// TODO: bind to main window's Title
|
|
var dialog = new SimpleGridSettingsDialog { Title = "Synthetic Management Grid Settings", DataContext = vm };
|
|
vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog);
|
|
return dialog;
|
|
}))
|
|
};
|
|
|
|
_subscriptions = Disposable.Create(
|
|
vm_connectable_builder.connect_all(),
|
|
current_row_connectable_builder.connect_all());
|
|
|
|
IObservable<RowModificationParams?> observe_row_mod_params(SyntheticManagementDialogVM.Row r) =>
|
|
r == null ? Observable.Return(new RowModificationParams?()) :
|
|
_orders_model.security_has_working_orders(r.security_key).Select(hwo => RowModificationParams.create(r.security, hwo));
|
|
|
|
IObservable<RowStrategy?> observe_row_strategy(SyntheticManagementDialogVM.Row r) =>
|
|
r == null ? Observable.Return(new RowStrategy?()) :
|
|
_orders_model.spreader.security_strategy(r.security.Id).Select(strategy => RowStrategy.create(r.security, strategy));
|
|
|
|
uint select_security_id(RowModificationParams? rmp) => rmp.HasValue ? rmp.Value.security.Id : 0;
|
|
(SecurityImmutable, ExecutionStrategyImmutable) select_row_strategy_tuple(RowStrategy? rs) => rs.HasValue ? (rs.Value.security, rs.Value.strategy) : default;
|
|
bool can_execute(RowModificationParams? rmp) => rmp.HasValue && !rmp.Value.has_working_orders;
|
|
|
|
_vm.value = new SyntheticManagementDialogVM(_bus, _orders_model, _settings_manager, _state.sorting_criteria, _state.filters ?? new FilterStates());
|
|
if (_view == null) _view = new SyntheticManagementView { DataContext = _vm.value };
|
|
else _view.DataContext = _vm.value;
|
|
}
|
|
|
|
public override string window_type => _window_type;
|
|
public override UIElement Content => _view;
|
|
public override IEnumerable<SparkCommand> Commands => _commands;
|
|
public override StartupData SaveState => _state = _vm.value.create();
|
|
public override double MinWidth =>
|
|
StartupData.DefaultGridSettings.columns.Sum(gcs => gcs.width)
|
|
+ SPARK_MENU_COLLAPSED_WIDTH
|
|
+ LAUNCHER_BUTTON_BORDER_WIDTH
|
|
+ EXTRA_FOR_NO_HORIZONTAL_SCROLLBAR_WIDTH;
|
|
|
|
public override void Dispose() {
|
|
Disposable.dispose(ref _vm);
|
|
Commands.OfType<IDisposable>().DisposeAll();
|
|
}
|
|
|
|
struct RowModificationParams {
|
|
public readonly SecurityImmutable security;
|
|
public readonly bool has_working_orders;
|
|
|
|
RowModificationParams(SecurityImmutable security, bool has_working_orders) {
|
|
this.security = security;
|
|
this.has_working_orders = has_working_orders;
|
|
}
|
|
|
|
public static RowModificationParams? create(SecurityImmutable security, bool has_working_orders) =>
|
|
new RowModificationParams(security, has_working_orders);
|
|
}
|
|
|
|
struct RowStrategy {
|
|
public readonly SecurityImmutable security;
|
|
public readonly ExecutionStrategyImmutable strategy;
|
|
|
|
RowStrategy(SecurityImmutable security, ExecutionStrategyImmutable strategy) {
|
|
this.security = security;
|
|
this.strategy = strategy;
|
|
}
|
|
|
|
public static RowStrategy? create(SecurityImmutable security, ExecutionStrategyImmutable strategy) =>
|
|
new RowStrategy(security, strategy);
|
|
}
|
|
}
|
|
} |