using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using TradeIdeas.TIQ.GUI; using TradeIdeas.TIQData; using TradeIdeas.ServerConnection; using TradeIdeas.XML; using System.IO; using TradeIdeas.MiscSupport; // TODO I disabled the old code but haven't added the new code. // This doesn't listen for results yet. // At this time (7/20/2015) there is no server code waiting for this. // At one time work stopped on the server because it was getting difficult. // Now that we're using coroutines and top list is a simple library call, // this should be much easier. // 2/10/2015 The interface for sending and canceling messages has changed. // That was required to fix a bug. I've been fixing the other code, // but it seemed pointless to try to make this file work. I just commented // out any code that caused an error or warning. namespace TradeIdeas.TIQ { public partial class TwoStageHistory : Form,ISaveLayout { private readonly ConnectionMaster _connectionMaster; private Int64 _messageId = 0; private const string FORM_TYPE = "TIQ_TWO_STAGE_HISTORY"; private string _fileNameSaved; private StringBuilder _responseInProgress; public TwoStageHistory() { _connectionMaster = GUI.Environment.DefaultConnectionMaster; InitializeComponent(); universePrototypeEditor.ColumnHeadingsChanged += universePrototypeEditor_ColumnHeadingsChanged; eachBarPrototypeEditor.ColumnHeadingsChanged += eachBarPrototypeEditor_ColumnHeadingsChanged; UpdateColumnNames(universePrototypeEditor, filterColumnComboBox); UpdateColumnNames(eachBarPrototypeEditor, cboEachBar); } void universePrototypeEditor_ColumnHeadingsChanged(PrototypeEditor obj, bool onlyLookAtNames) { UpdateColumnNames(obj, filterColumnComboBox); } void eachBarPrototypeEditor_ColumnHeadingsChanged(PrototypeEditor obj, bool onlyLookAtNames) { UpdateColumnNames(obj, cboEachBar); } private void UpdateColumnNames(PrototypeEditor editor, ComboBox combo) { List columnHeadings = editor.GetColumnHeadings(); if (columnHeadings.Count != 0) { PrototypeEditor.ColumnHeading previousFilter = (combo.SelectedItem as PrototypeEditor.ColumnHeading) ?? columnHeadings[0]; combo.Items.Clear(); foreach (object column in columnHeadings) { combo.Items.Add(column); if (previousFilter.Equals(column)) combo.SelectedItem = column; } } if (null == combo.SelectedItem) combo.SelectedIndex = 0; System.Diagnostics.Debug.Assert(null != combo.SelectedItem); } static public void RegisterLayout() { LayoutManager.Instance().AddRestoreRule(FORM_TYPE, (RestoreLayout)delegate(XmlNode description, bool ignorePosition, bool cascadePosition) { TwoStageHistory form = new TwoStageHistory(); LayoutManager.RestoreBase(description, form, ignorePosition, cascadePosition); form.GetContents(description); }); } void ISaveLayout.SaveLayout(XmlNode parent) { XmlNode description = LayoutManager.SaveBase(parent, this, FORM_TYPE); SetContents(description); } public void SetContents(XmlNode destination) { //Unlike other windows, we'll save the prototype editors here with // the SetContents. We make a new node for each of the prototype //editors and save-we don't want a "more-than-one-top-level-XML-node error" XmlNode settings = destination.NewNode("SETTINGS"); XmlNode universe = destination.NewNode("UNIVERSE"); XmlNode eachBar = destination.NewNode("EACH_BAR"); //save the prototype editors universePrototypeEditor.SetContents(universe); eachBarPrototypeEditor.SetContents(eachBar); //row radio button setting, textbox,datepicker settings.SetProperty("ROW_NUMBER_RADIO", rowNumberRadioButton.Checked); settings.SetProperty("ROW_TIME_RADIO", rowTimeRadioButton.Checked); settings.SetProperty("ROW_BEGINNING_TIME_RADIO", beginningRadioButton.Checked); settings.SetProperty("ROWS_NUMBER_TEXT", rowNumberTextBox.Text); settings.SetProperty("ROW_TIME", ServerFormats.ToTimeT(rowDateTimePicker.Value)); //selected indices of combo boxes... settings.SetProperty("DAILY_FILTER_COLUMN_SELECTED_INDEX", filterColumnComboBox.SelectedIndex); settings.SetProperty("BAR_FILTER_COLUMN_SELECTED_INDEX", cboEachBar.SelectedIndex); } public void GetContents(XmlNode parent) { XmlNode description = parent.Node("SETTINGS"); XmlNode universe = parent.Node("UNIVERSE"); XmlNode bar = parent.Node("EACH_BAR"); //get the prototype editor grids.. universePrototypeEditor.GetContents(universe); eachBarPrototypeEditor.GetContents(bar); //row radio button setting, textbox, datepicker rowNumberRadioButton.Checked = description.Property("ROW_NUMBER_RADIO", false); rowTimeRadioButton.Checked = description.Property("ROW_TIME_RADIO", false); beginningRadioButton.Checked = description.Property("ROW_BEGINNING_TIME_RADIO", true); rowNumberTextBox.Text = description.Property("ROWS_NUMBER_TEXT", ""); //retore the row_time: DateTime value; long time = Convert.ToInt64(description.Property("ROW_TIME")); DateTime? dT = ServerFormats.FromTimeT(time); if (dT == null) { value = DateTime.Now; } else { value = (DateTime)dT; } rowDateTimePicker.Value = value; UpdateColumnNames(universePrototypeEditor,filterColumnComboBox); UpdateColumnNames(eachBarPrototypeEditor, cboEachBar); //Now we can set the selected indices of comboboxes... try { filterColumnComboBox.SelectedIndex = Convert.ToInt32(description.Property("DAILY_FILTER_COLUMN_SELECTED_INDEX")); } catch { filterColumnComboBox.SelectedIndex = 0; } try { cboEachBar.SelectedIndex = Convert.ToInt32(description.Property("BAR_FILTER_COLUMN_SELECTED_INDEX")); } catch { cboEachBar.SelectedIndex = 0; } } private void btnRequestAlerts_Click(object sender, EventArgs e) { makeAndSendRequest(eachBarPrototypeEditor, cboEachBar); } private void btnRequestUniverse_Click(object sender, EventArgs e) { makeAndSendRequest(universePrototypeEditor, filterColumnComboBox); } private void makeAndSendRequest(PrototypeEditor editor, ComboBox combo) { object[] message = new object[] { "command", "flex_command", "subcommand", "two_stage_history", "prototype", editor.GetTcl(), // Not currently used. But we will probably add something like this. //"row_num", rowNumberRadioButton.Checked?rowNumberTextBox.Text:null, "start_time", rowTimeRadioButton.Checked?ServerFormats.ToTimeT(rowDateTimePicker.Value):0, "filter_col", combo.SelectedItem }; var formatted = TalkWithServer.CreateMessage(message); // TODO send a real message. I disabled this because the interface changed. //_connectionMaster.SendManager.SendMessage(out _messageId, formatted, GetDataResponse); } private void GetDataResponse(byte[] body, object clientId) { // This is a bit of a placeholder. We could do better. // Currently the server returns an integer. This is the number of stocks that you are looking at. if (null != body) // succeess! return; // try again this.InvokeIfRequired((MethodInvoker)delegate { _connectionMaster.SendManager.SendMessage((Dictionary)clientId, GetDataResponse, clientId: clientId); }); } private void StreamingResponse(Int64 id, XmlNode message) { this.InvokeIfRequired((MethodInvoker)delegate { if (id != _messageId) // An old request! return; string type = message.Property("type"); switch (type) { case "empty": _responseInProgress = new StringBuilder("no results"); ShowResult(); break; case "new": _responseInProgress = new StringBuilder(message.Text()); break; case "append": if (null == _responseInProgress) { // The server shouldn't do this to us. But to be on the safe safe, we make this a warning, // not an exception. _responseInProgress = new StringBuilder("warning: no header\r\n"); } _responseInProgress.Append(message.Text()); break; case "done": if (null == _responseInProgress) { // The server shouldn't do this to us. But to be on the safe safe, we make this a warning, // not an exception. _responseInProgress = new StringBuilder("warning: no header\r\n"); } ShowResult(); break; } }); } private void ShowResult() { CsvPreview preview = new CsvPreview(); preview.SuggestedFileName = "TwoStageHistoryResults.csv"; preview.SetContents(_responseInProgress.ToString()); preview.CreateAsWindow(); _responseInProgress = null; } private void saveButton_Click(object sender, EventArgs e) { LayoutManager layoutManager = LayoutManager.Instance(); SaveFileDialog dialog = new SaveFileDialog(); if (null != layoutManager.Directory) dialog.InitialDirectory = layoutManager.Directory; dialog.Filter = "TIQ Window files|*.WTI|All files|*.*"; dialog.DefaultExt = "*.WTI"; string fileName = ""; if (_fileNameSaved == null) { fileName = "My TIQ Two Stage History"; } else { fileName = _fileNameSaved; } dialog.FileName = FileNameMethod.QuoteFileName(fileName); if (dialog.ShowDialog() == DialogResult.OK) { layoutManager.SaveOne(this, dialog.FileName); _fileNameSaved = Path.GetFileName(dialog.FileName); } dialog.Dispose(); } private void duplicateButton_Click(object sender, EventArgs e) { LayoutManager.Instance().Duplicate(this); } } }