using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Xml; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using TradeIdeas.TIProData; using System.Text.RegularExpressions; using TradeIdeas.TIProData.Configuration; using TradeIdeas.XML; namespace TradeIdeas.TIProGUI { public partial class MultiStrategyConfig : Form, ICultureListener { MultiStrategy _owner; private List _phrases; private readonly ConnectionMaster _connectionMaster; //Start location of MultiStrategyConfig window when first brought up... private int _multiConfigX = 50; private int _multiConfigY = 50; //for saving height private int _multiConfigHeight = -1; private int _multiConfigWidth = -1; //This static signifies whether or not the specially modified ConfigWindow.cs will be used... public static bool isMultiStrategyInUse = false; //The below bool signifies when to remove just the "Columns" tab from the unmodified //ConfigWindow.cs public static bool configuringStrategies = false; private string _fileNameSaved; public MultiStrategyConfig(MultiStrategy owner,ConnectionMaster connectionMaster) { _owner = owner; _connectionMaster = connectionMaster; // Notice the blank at the end. That is needed to terminate the list. In particular, // if the setting already included 10 columns, and we didn't have that, we'd overwrite // the first 5 columns with our own, and leave the remaining 5. Columns = "&show0=Price&show1=TV&show2=RV&show3=RD&show4="; InitializeComponent(); Icon = GuiEnvironment.Icon; _phrases = GuiEnvironment.XmlConfig.Node("MULTI_STRATEGY_CONFIG").Node("PHRASES"); if (_multiConfigWidth == -1 && _multiConfigHeight == -1) { _multiConfigWidth = Width; _multiConfigHeight = Height; } CultureChanged(); } public void CultureChanged() { Text = _phrases.Node("TITLE").PropertyForCulture("TEXT", "***"); lblColor.Text = _phrases.Node("SAMPLE_COLORS").PropertyForCulture("TEXT", "***"); // actionsContextMenuStrip.Text = _phrases.Node("ACTIONS").PropertyForCulture("TEXT", "***"); addActionStripMenuItem.Text = _phrases.Node("ADD").PropertyForCulture("TEXT", "***"); addStripMenuItem1.Text = _phrases.Node("ADD").PropertyForCulture("TEXT", "***"); deleteActionStripMenuItem.Text = _phrases.Node("DELETE").PropertyForCulture("TEXT", "***"); deleteStripMenuItem1.Text = _phrases.Node("DELETE").PropertyForCulture("TEXT", "***"); copyActionStripMenuItem.Text = _phrases.Node("COPY").PropertyForCulture("TEXT", "***"); copyStripMenuItem1.Text = _phrases.Node("COPY").PropertyForCulture("TEXT", "***"); configureActionStripMenuItem.Text = _phrases.Node("CONFIGURE").PropertyForCulture("TEXT", "***"); configureStripMenuItem1.Text = _phrases.Node("CONFIGURE").PropertyForCulture("TEXT", "***"); collaborateActionStripMenuItem.Text = _phrases.Node("COLLABORATE").PropertyForCulture("TEXT", "***"); collaborateStripMenuItem1.Text = _phrases.Node("COLLABORATE").PropertyForCulture("TEXT", "***"); deleteAllActionStripMenuItem.Text = _phrases.Node("DELETE_ALL").PropertyForCulture("TEXT", "***"); saveActionStripMenuItem.Text = _phrases.Node("SAVE_AS").PropertyForCulture("TEXT", "***"); closeActionStripMenuItem.Text = _phrases.Node("CLOSE").PropertyForCulture("TEXT", "***"); columnsActionStripMenuItem.Text = _phrases.Node("CHANGECOLUMN").PropertyForCulture("TEXT", "***"); colorsActionStripMenuItem.Text = _phrases.Node("SAMPLE").PropertyForCulture("TEXT", "***"); colorsStripMenuItem1.Text = _phrases.Node("SAMPLE").PropertyForCulture("TEXT", "***"); nameActionStripMenuItem.Text = _phrases.Node("WINDOW_NAME").PropertyForCulture("TEXT", "***"); } public void RedrawList() { // It seems like there must be a more effecient way to tod this. BeginUpdate() and EndUpdate() // do nothing on their own. Invalidate() and Refresh() do nothing. RefreshItems() and // RefreshItem look like what I want, according to the documentation, but they are protected. listBox1.BeginUpdate(); for (int i = listBox1.Items.Count - 1; i >= 0; i--) listBox1.Items[i] = listBox1.Items[i]; listBox1.EndUpdate(); } public ListBox.ObjectCollection Strategies { get { return listBox1.Items; } } // This should be appended to the end of the config string. //public String Columns { get; private set; } //needed setter public (handy for duplicating entire //MultiStrategy Window. public String Columns { get; set; } public void RemoveAllStrategies() { foreach (Object obj in listBox1.Items) { MultiStrategy.DataWatcher dataWatcher = (MultiStrategy.DataWatcher)obj; dataWatcher.Stop(); } listBox1.Items.Clear(); } private void Remove(MultiStrategy.DataWatcher dataWatcher) { dataWatcher.Stop(); listBox1.Items.Remove(dataWatcher); } private void strategyColorToolStripMenuItem_Click(object sender, EventArgs e) { MultiStrategy.DataWatcher data = (MultiStrategy.DataWatcher)listBox1.SelectedItem; StratgyColors form = new StratgyColors(data); form.Text = _phrases.Node("SET_STRATEGY_COLORS").PropertyForCulture("TEXT", "***"); form.ShowDialog(); if (form.DialogResult == System.Windows.Forms.DialogResult.OK) { Label dummy = form.returnColors(); data.Style.ForeColor = dummy.ForeColor; data.Style.BackColor = dummy.BackColor; lblColor.ForeColor = dummy.ForeColor; lblColor.BackColor = dummy.BackColor; dummy.Dispose(); _owner.RedrawTable(); } form.Dispose(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { /*This method will color the "Sample Colors" label according to the * DataGridViewCellStyle item that's selected in the listBox*/ if (listBox1.SelectedIndex >= 0) { setWindowSampleLabel(); lblColor.ForeColor = ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Style.ForeColor; lblColor.BackColor = ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Style.BackColor; } } private void btnOK_Click(object sender, EventArgs e) { this.Hide(); } private void bbtnActions_MouseClick(object sender, MouseEventArgs e) { actionContextMenuStrip.Show(listBox1, new System.Drawing.Point(listBox1.Width, 0)); } private bool areNonWhiteSpacesPresent(string testString) { Regex objNonWhiteSpaces = new Regex("\\S"); return objNonWhiteSpaces.IsMatch(testString); } private void UpdateColumns() { // This part is real. Call it after updating the columns variable. foreach (Object obj in listBox1.Items) { MultiStrategy.DataWatcher dataWatcher = (MultiStrategy.DataWatcher)obj; dataWatcher.Config = dataWatcher.Config; } } private void setWindowSampleLabel() { MultiStrategy.DataWatcher data = (MultiStrategy.DataWatcher)listBox1.SelectedItem; if (data.Name == null) { lblColor.Text = lblColor.Text = _phrases.Node("SAMPLE_COLORS").PropertyForCulture("TEXT", "***"); } else { lblColor.Text = data.Name; } } private void addToolStripMenuItem_Click(object sender, EventArgs e) { listBox1.Items.Add(new MultiStrategy.DataWatcher(_owner)); listBox1.SelectedIndex = listBox1.Items.Count - 1; checkListBoxStatus(); } private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { // I don't think we can do a foreach on listbox1.SelectedItems. I think // as soon as we delete the first item, the foreach loop will fail. /*Eliminated the loop. If a person had two(or more) of the exact same strategy entered in the window, the loop will remove all of them. In the original MultiStrategy window, a person can have a few duplicates and can eliminate one, with the delete button. The other entry he can keep, or configure it to another strategy*/ Remove((MultiStrategy.DataWatcher)listBox1.SelectedItems[0]); listBox1.SelectedIndex = listBox1.Items.Count - 1; checkListBoxStatus(); } private void copyToolStripMenuItem_Click(object sender, EventArgs e) { //Get config string of original selected item... string newConfig = ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config; //Now make a new DataWatcher object and add to the list.. listBox1.Items.Add(new MultiStrategy.DataWatcher(_owner)); //Make this new object now the new selected index(and hence selected item) listBox1.SelectedIndex = listBox1.Items.Count - 1; //Set this new object's config string to that of the original ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config = newConfig; /* This 4-line operation seems a bit "kludgey" and can be done in one line-however * problems had arisen with the one-liner. What happened was that if I changed one copy to * a particular background the other copy would also get that same colored background. If * I attempted to configure one copy to another strategy, the other copy would also * automatically get changed to that strategy as well. Similar to a pass-by-reference * scenario...*/ } private void configureToolStripMenuItem_Click(object sender, EventArgs e) { configuringStrategies = true; string initialString = ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config; ConfigWindow form = new ConfigWindow(ConfigurationType.Alerts, _connectionMaster, initialString); form.ShowDialog(); if (form.DialogResult == System.Windows.Forms.DialogResult.OK) { ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config = form.AlertStrategy.MakeConfigString(); } form.Dispose(); setWindowSampleLabel(); } private void collaborateToolStripMenuItem_Click(object sender, EventArgs e) { if (listBox1.SelectedItems.Count != 1) { // It doesn't make much sense to reconfigure a lot of strategies at once. // The GUI should disable the button or something, so we enver get here // in the first place. System.Media.SystemSounds.Beep.Play(); return; } CollaborateForm form = new CollaborateForm(); // The user should have an option to bring up a // config window. Another option should bring up a collaborate dialog box. form.ConfigString = GuiEnvironment.XmlConfig.Node("MULTI_STRATEGY_CONFIG").Node("CONFIG_PREFIX").PropertyForCulture("BASE", "***") + ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config; form.Text = _phrases.Node("COLLABORATE").PropertyForCulture("TEXT", "***"); form.ShowDialog(); if (form.DialogResult == System.Windows.Forms.DialogResult.OK) { ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config = form.ConfigString; } form.Dispose(); } private void deleteAllToolStripMenuItem_Click(object sender, EventArgs e) { RemoveAllStrategies(); checkListBoxStatus(); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { LayoutManager layoutManager = LayoutManager.Instance(); SaveFileDialog dialog = new SaveFileDialog(); if (null != layoutManager.Directory) dialog.InitialDirectory = layoutManager.Directory; dialog.Filter = "Window files|*.WTI|All files|*.*"; dialog.DefaultExt = "*.WTI"; string fileName = ""; if (_fileNameSaved == null) { fileName = this.Text; } else { fileName = _fileNameSaved; } dialog.FileName = FileNameMethod.QuoteFileName(fileName); if (dialog.ShowDialog() == DialogResult.OK) { layoutManager.SaveOne(_owner, dialog.FileName); _fileNameSaved = Path.GetFileName(dialog.FileName); } dialog.Dispose(); } private void columsToolStripMenuItem_Click(object sender, EventArgs e) { // The real value should come from the config window, with // a few special modifications. isMultiStrategyInUse = true; string initialString = ""; if (listBox1.Items.Count == 0) { /*Since the listbox is devoid of strategies, we need *some* sort of "dummy strategy" just for the purposes of populating the modified configwindow * as well as retaining the column display should one wish to adjust the columns indefinitely when the listbox is empty.*/ initialString = "O=8000000000000000000000000_41D_0&WN=My+Alerts" + Columns; } else { initialString = ((MultiStrategy.DataWatcher)listBox1.SelectedItem).Config + Columns; } ConfigWindow form = new ConfigWindow(ConfigurationType.Alerts, _connectionMaster, initialString); form.ShowDialog(); if (form.DialogResult == System.Windows.Forms.DialogResult.OK) { Columns = form.AlertStrategy.GetColumnConfig(); } form.Dispose(); UpdateColumns(); } private void windowNameToolStripMenuItem_Click(object sender, EventArgs e) { MultiStrategyWindowName form = new MultiStrategyWindowName(_owner.BaseName); form.Text = _phrases.Node("WINDOW_NAME").PropertyForCulture("TEXT", "***"); form.ShowDialog(); if (form.DialogResult == System.Windows.Forms.DialogResult.OK) { if (areNonWhiteSpacesPresent(form.NewName)) { _owner.BaseName = form.NewName; } else { //If user only enters whitespace for a //window name, the window name will default // to Multi-Strategy Window _owner.BaseName = "Multi-Strategy Window"; } _owner.checkDemoStatus(); } form.Dispose(); } private void checkListBoxStatus() { /*This method checks whether the list box is empty or not. This determines when some of the buttons are enabled or disabled*/ if (listBox1.Items.Count == 0) { lblColor.ForeColor = Color.Black; lblColor.BackColor = Color.White; deleteActionStripMenuItem.Enabled = false; deleteStripMenuItem1.Enabled = false; copyActionStripMenuItem.Enabled = false; copyStripMenuItem1.Enabled = false; configureActionStripMenuItem.Enabled = false; configureStripMenuItem1.Enabled = false; collaborateActionStripMenuItem.Enabled = false; collaborateStripMenuItem1.Enabled = false; deleteAllActionStripMenuItem.Enabled = false; saveActionStripMenuItem.Enabled = false; colorsActionStripMenuItem.Enabled = false; colorsStripMenuItem1.Enabled = false; lblColor.Text = _phrases.Node("SAMPLE_COLORS").PropertyForCulture("TEXT", "***"); } else { deleteActionStripMenuItem.Enabled = true; deleteStripMenuItem1.Enabled = true; copyActionStripMenuItem.Enabled = true; copyStripMenuItem1.Enabled = true; configureActionStripMenuItem.Enabled = true; configureStripMenuItem1.Enabled = true; collaborateActionStripMenuItem.Enabled = true; collaborateStripMenuItem1.Enabled = true; deleteAllActionStripMenuItem.Enabled = true; saveActionStripMenuItem.Enabled = true; colorsActionStripMenuItem.Enabled = true; colorsStripMenuItem1.Enabled = true; if (listBox1.SelectedIndex < 0) { /*This one-liner comes in handy when a MultiStrategy window is duplicated its corresponding config-window is first brought into view for the very first time. If there are associated strategies, * they will be seen in the listBox. However none of them will be selected.*/ listBox1.SelectedIndex = listBox1.Items.Count - 1; } setWindowSampleLabel(); } } private void MultiStrategyConfig_VisibleChanged(object sender, EventArgs e) { if (!this.Visible) { //Save the location of this window on the screen. //When made visible, this window will assume the position _multiConfigX = Location.X; _multiConfigY = Location.Y; //save the size of this window...should user resize. _multiConfigHeight = Height; _multiConfigWidth = Width; } else { Location = new Point(_multiConfigX, _multiConfigY); Size = new Size(_multiConfigWidth, _multiConfigHeight); CultureChanged(); checkListBoxStatus(); } } private void closeToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide(); } private void listBox1_DoubleClick(object sender, EventArgs e) { if (listBox1.SelectedIndex >= 0) { configureToolStripMenuItem_Click(sender, e); } } } }