[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9615] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[7868] | 23 | using System.Collections;
|
---|
[6133] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.ComponentModel;
|
---|
| 26 | using System.Drawing;
|
---|
[11653] | 27 | using System.IO.Compression;
|
---|
[6133] | 28 | using System.Linq;
|
---|
| 29 | using System.Windows.Forms;
|
---|
[7868] | 30 | using System.Xml;
|
---|
[6133] | 31 | using HeuristicLab.DataImporter.Data;
|
---|
| 32 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 33 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 34 | using HeuristicLab.DataImporter.Data.View;
|
---|
| 35 | using HeuristicLab.DataImporter.DataProcessor.Command;
|
---|
[7868] | 36 | using HeuristicLab.DataImporter.DbExplorer.Interfaces;
|
---|
[6133] | 37 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[7868] | 38 | using HeuristicLab.PluginInfrastructure;
|
---|
| 39 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[6133] | 40 |
|
---|
| 41 | namespace HeuristicLab.DataImporter.DataProcessor {
|
---|
| 42 | public partial class DataProcessorView : Form {
|
---|
| 43 | private DataSetView dataSetView;
|
---|
| 44 | private BackgroundWorker backgroundWorker;
|
---|
| 45 | private string lastSavedFile;
|
---|
| 46 | private ICommand lastSavedCommand;
|
---|
| 47 | private ProgressDialog progressDlg;
|
---|
| 48 |
|
---|
| 49 | private DataProcessorView() {
|
---|
| 50 | InitializeComponent();
|
---|
| 51 | this.HScroll = false;
|
---|
| 52 | this.HorizontalScroll.Visible = false;
|
---|
| 53 | backgroundWorker = new BackgroundWorker();
|
---|
| 54 | backgroundWorker.WorkerReportsProgress = true;
|
---|
| 55 | backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
|
---|
| 56 | backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
|
---|
| 57 | backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public DataProcessorView(DataProcessor dataProcessor)
|
---|
| 61 | : this() {
|
---|
| 62 | this.dataProcessor = dataProcessor;
|
---|
| 63 | this.dataProcessor.CommandChain.Changed += this.CommandChainChanged;
|
---|
| 64 | this.dataSetView = new DataSetView(this.dataProcessor.DataSet, this.dataProcessor.CommandChain);
|
---|
| 65 | this.dataSetView.StateChanged += new EventHandler(DataSetStateChanged);
|
---|
| 66 | this.dataSetView.Dock = DockStyle.Fill;
|
---|
| 67 | this.dataSetView.ColumnGroupMaxWidth = this.splitContainer1.Panel2.Width;
|
---|
| 68 | this.splitContainer1.Panel2.Controls.Add(this.dataSetView);
|
---|
| 69 | this.splitContainer1.Panel2.Resize += new EventHandler(Panel2_Resize);
|
---|
| 70 | FillItemList();
|
---|
| 71 |
|
---|
| 72 | IDbExplorer[] explorers = ApplicationManager.Manager.GetInstances<IDbExplorer>().ToArray();
|
---|
| 73 | ToolStripItem item;
|
---|
| 74 | foreach (IDbExplorer exp in explorers) {
|
---|
| 75 | item = new ToolStripMenuItem();
|
---|
| 76 | item.Text = exp.MenuItemDescription;
|
---|
| 77 | item.Tag = exp;
|
---|
| 78 | item.Click += new EventHandler(setGenericDBConnectionToolStripMenuItem_Click);
|
---|
| 79 | databaseToolStripMenuItem.DropDownItems.Add(item);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private DataProcessor dataProcessor;
|
---|
| 84 | public DataProcessor DataProcessor {
|
---|
| 85 | get { return this.dataProcessor; }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private bool UndoEnabled {
|
---|
| 89 | get { return this.undoToolStripButton.Enabled; }
|
---|
| 90 | set { this.undoToolStripButton.Enabled = value; this.undoToolStripMenuItem.Enabled = value; }
|
---|
| 91 | }
|
---|
| 92 | private bool RedoEnabled {
|
---|
| 93 | get { return this.redoToolStripButton.Enabled; }
|
---|
| 94 | set { this.redoToolStripButton.Enabled = value; this.redoToolStripMenuItem.Enabled = value; }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | private bool SaveCommandChainEnabled {
|
---|
| 98 | get { return this.saveCommandChainToolStripButton.Enabled; }
|
---|
| 99 | set { this.saveCommandChainToolStripButton.Enabled = value; }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | #region CommandList
|
---|
| 103 |
|
---|
| 104 | private void listCommands_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
| 105 | ListViewItem item = listCommands.HitTest(e.Location).Item;
|
---|
| 106 | if (item != null) {
|
---|
| 107 | ICommandGenerator generator = (ICommandGenerator)listCommands.FocusedItem;
|
---|
| 108 | if (generator.Enabled(dataSetView.State)) {
|
---|
| 109 | ICommand cmd = ((ICommandGenerator)generator).GenerateCommand(dataProcessor.DataSet);
|
---|
| 110 | if (cmd != null) {
|
---|
| 111 | try {
|
---|
| 112 | dataProcessor.CommandChain.Add(cmd);
|
---|
| 113 | }
|
---|
| 114 | catch (CommandExecutionException cee) {
|
---|
| 115 | MessageBox.Show(cee.Message, cee.Command.Description);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | item.Selected = false;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private void FillItemList() {
|
---|
| 124 | //sort after ViewableCommandInfoAttribute
|
---|
| 125 | IEnumerable<Type> commands = ApplicationManager.Manager.GetTypes(typeof(ICommand)).Select(t => new {
|
---|
| 126 | Command = t,
|
---|
| 127 | Attribute = (ViewableCommandInfoAttribute)Attribute.GetCustomAttribute(t, typeof(ViewableCommandInfoAttribute))
|
---|
| 128 | })
|
---|
| 129 | .Where(t => t.Attribute != null && !t.Command.IsAbstract)
|
---|
| 130 | .OrderBy(t => t.Attribute, new AttributeComparer())
|
---|
| 131 | .Select(t => t.Command);
|
---|
| 132 | ListViewItem item = null;
|
---|
| 133 | bool addGroup;
|
---|
| 134 | foreach (Type t in commands) {
|
---|
| 135 | item = (ListViewItem)Activator.CreateInstance(typeof(CommandListViewItem<>).MakeGenericType(t));
|
---|
| 136 | addGroup = true;
|
---|
| 137 | foreach (ListViewGroup grp in listCommands.Groups)
|
---|
| 138 | if (grp.Name == item.Group.Name) {
|
---|
| 139 | item.Group = grp;
|
---|
| 140 | addGroup = false;
|
---|
| 141 | break;
|
---|
| 142 | }
|
---|
| 143 | if (addGroup) {
|
---|
| 144 | listCommands.Groups.Add(item.Group);
|
---|
| 145 | }
|
---|
| 146 | listCommands.Items.Add(item);
|
---|
| 147 | }
|
---|
| 148 | UpdateListViewItems();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public void DataSetStateChanged(object sender, EventArgs e) {
|
---|
| 152 | UpdateListViewItems();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void UpdateListViewItems() {
|
---|
| 156 | foreach (ICommandGenerator generator in listCommands.Items) {
|
---|
| 157 | if (generator.Enabled(dataSetView.State))
|
---|
| 158 | ((ListViewItem)generator).ForeColor = Color.Black;
|
---|
| 159 | else
|
---|
| 160 | ((ListViewItem)generator).ForeColor = Color.LightGray;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | #endregion
|
---|
| 164 |
|
---|
| 165 | private void CommandChainChanged(object sender, EventArgs e) {
|
---|
| 166 | #region undo & redo updates
|
---|
| 167 | this.UndoEnabled = !this.dataProcessor.CommandChain.FirstCommandReached;
|
---|
| 168 | this.RedoEnabled = !this.dataProcessor.CommandChain.LastCommandReached;
|
---|
| 169 | this.SaveCommandChainEnabled = !this.dataProcessor.CommandChain.FirstCommandReached;
|
---|
| 170 | this.undoToolStripButton.ToolTipText = this.dataProcessor.CommandChain.PreviousCommandDescription;
|
---|
| 171 | this.redoToolStripButton.ToolTipText = this.dataProcessor.CommandChain.NextCommandDescription;
|
---|
| 172 |
|
---|
| 173 | //needed to update tooltip if it is currently being shown - taken from
|
---|
| 174 | //http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=167320
|
---|
| 175 | System.Reflection.FieldInfo f = this.toolStrip.GetType().GetField("currentlyActiveTooltipItem", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
---|
| 176 | if (object.ReferenceEquals(f.GetValue(this.toolStrip), this.undoToolStripButton)) {
|
---|
| 177 | // Need to set currentlyActiveTooltipItem to null, so the call to UpdateToolTip actually does the update
|
---|
| 178 | f.SetValue(this.toolStrip, null);
|
---|
| 179 | System.Reflection.MethodInfo m = this.toolStrip.GetType().GetMethod("UpdateToolTip", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
---|
| 180 | m.Invoke(this.toolStrip, new object[] { this.undoToolStripButton });
|
---|
| 181 | }
|
---|
| 182 | f = this.toolStrip.GetType().GetField("currentlyActiveTooltipItem", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
---|
| 183 | if (object.ReferenceEquals(f.GetValue(this.toolStrip), this.redoToolStripButton)) {
|
---|
| 184 | // Need to set currentlyActiveTooltipItem to null, so the call to UpdateToolTip actually does the update
|
---|
| 185 | f.SetValue(this.toolStrip, null);
|
---|
| 186 | System.Reflection.MethodInfo m = this.toolStrip.GetType().GetMethod("UpdateToolTip", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
---|
| 187 | m.Invoke(this.toolStrip, new object[] { this.redoToolStripButton });
|
---|
| 188 | }
|
---|
| 189 | #endregion
|
---|
| 190 |
|
---|
| 191 | #region macro recording
|
---|
| 192 | Bitmap image;
|
---|
| 193 | string toolTipText;
|
---|
| 194 | if (dataProcessor.CommandChain.IsMacroRecording) {
|
---|
| 195 | image = Properties.Resources.StopHS;
|
---|
| 196 | toolTipText = "Stop Macro recording";
|
---|
| 197 | } else {
|
---|
| 198 | image = Properties.Resources.RecordHS;
|
---|
| 199 | toolTipText = "Start Macro recording";
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | recordMacroToolStripButton.Image = image;
|
---|
| 203 | recordMacroToolStripButton.ToolTipText = toolTipText;
|
---|
| 204 | f = this.toolStrip.GetType().GetField("currentlyActiveTooltipItem", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
---|
| 205 | if (object.ReferenceEquals(f.GetValue(this.toolStrip), this.recordMacroToolStripButton)) {
|
---|
| 206 | // Need to set currentlyActiveTooltipItem to null, so the call to UpdateToolTip actually does the update
|
---|
| 207 | f.SetValue(this.toolStrip, null);
|
---|
| 208 | System.Reflection.MethodInfo m = this.toolStrip.GetType().GetMethod("UpdateToolTip", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
---|
| 209 | m.Invoke(this.toolStrip, new object[] { this.recordMacroToolStripButton });
|
---|
| 210 | }
|
---|
| 211 | #endregion
|
---|
| 212 |
|
---|
| 213 | if (!string.IsNullOrEmpty(lastSavedFile)) {
|
---|
| 214 | if (lastSavedCommand != this.dataProcessor.CommandChain.LastExecutedCommand && !lastSavedFile.Contains('*'))
|
---|
| 215 | this.Text = "Data Importer - " + lastSavedFile + "*";
|
---|
| 216 | else
|
---|
| 217 | this.Text = "Data Importer - " + lastSavedFile;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | private void chbReorderColumns_CheckedChanged(object sender, EventArgs e) {
|
---|
| 222 | this.dataSetView.AllowReorderColumns = chbReorder.CheckBoxControl.Checked;
|
---|
| 223 |
|
---|
| 224 | if (chbReorder.CheckBoxControl.Checked) {
|
---|
| 225 | foreach (ListViewItem item in listCommands.Items)
|
---|
| 226 | item.ForeColor = Color.LightGray;
|
---|
| 227 | listCommands.Enabled = false;
|
---|
| 228 | } else {
|
---|
| 229 | Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>();
|
---|
| 230 | int[] displayIndices;
|
---|
| 231 | for (int i = this.dataSetView.Controls.Count - 1; i >= 0; i--) {
|
---|
| 232 | displayIndices = ((ColumnGroupView)this.dataSetView.Controls[i]).DisplayIndexes;
|
---|
| 233 | //check if displayIndices are ordered, otherwise the array is added to the list
|
---|
| 234 | for (int j = 0; j < displayIndices.Length; j++) {
|
---|
| 235 | if (j != 0 && ((displayIndices[j] - displayIndices[j - 1]) != 1)) {
|
---|
| 236 | ColumnGroup columnGroup = this.dataSetView.DataSet.ColumnGroups.ElementAt(this.dataSetView.Controls.Count - 1 - i);
|
---|
| 237 | dictionary[columnGroup.Name] = displayIndices;
|
---|
| 238 | break;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | if (dictionary.Count != 0)
|
---|
| 243 | this.dataProcessor.CommandChain.Add(new ReorderColumnsCommand(this.dataProcessor.DataSet, dictionary));
|
---|
| 244 |
|
---|
| 245 | listCommands.Enabled = true;
|
---|
| 246 | UpdateListViewItems();
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | private void UpdateDataSetView() {
|
---|
| 251 | this.dataSetView.Dispose();
|
---|
| 252 | this.dataSetView = new DataSetView(this.dataProcessor.DataSet, this.dataProcessor.CommandChain);
|
---|
| 253 | this.dataSetView.StateChanged += new EventHandler(DataSetStateChanged);
|
---|
| 254 | UpdateListViewItems();
|
---|
| 255 |
|
---|
| 256 | this.dataSetView.Dock = DockStyle.Fill;
|
---|
| 257 | this.splitContainer1.Panel2.Controls.Clear();
|
---|
| 258 | this.splitContainer1.Panel2.Controls.Add(this.dataSetView);
|
---|
| 259 | this.dataSetView.ColumnGroupMaxWidth = this.splitContainer1.Panel2.Width;
|
---|
| 260 | this.dataProcessor.CommandChain.Changed += this.CommandChainChanged;
|
---|
| 261 | GC.Collect();
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | private void Panel2_Resize(object sender, EventArgs e) {
|
---|
| 265 | this.dataSetView.ColumnGroupMaxWidth = this.splitContainer1.Panel2.Width;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | #region BackgroundWorker
|
---|
| 270 | private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
| 271 | CommandChain temp = (CommandChain)e.Argument;
|
---|
| 272 | int i = 0;
|
---|
| 273 | int max = temp.CommandsCount;
|
---|
| 274 | foreach (DataSetCommandBase cmd in temp) {
|
---|
| 275 | //update description
|
---|
| 276 | backgroundWorker.ReportProgress(i * 100 / max, cmd.Description);
|
---|
| 277 | cmd.DataSet = dataProcessor.DataSet;
|
---|
| 278 | if (cmd is DatabaseCommandBase)
|
---|
| 279 | ((DatabaseCommandBase)cmd).DbExplorer = this.dataProcessor.DatabaseExplorer;
|
---|
| 280 | this.dataProcessor.CommandChain.Add(cmd, false);
|
---|
| 281 | i++;
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
| 286 | progressDlg.ProgressBar.Value = 100;
|
---|
| 287 | progressDlg.Refresh();
|
---|
| 288 | this.Enabled = true;
|
---|
| 289 | UpdateDataSetView();
|
---|
| 290 | progressDlg.Close();
|
---|
| 291 | if (e.Error != null) {
|
---|
| 292 | string message = e.Error.Message;
|
---|
| 293 | if (e.Error is CommandExecutionException)
|
---|
| 294 | message += Environment.NewLine + "Command: " +
|
---|
| 295 | ((CommandExecutionException)e.Error).Command.GetType().ToString();
|
---|
| 296 | MessageBox.Show(message, "Error occured during loading of command chain");
|
---|
| 297 | }
|
---|
| 298 | this.dataProcessor.CommandChain.FireChanged();
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
|
---|
| 302 | progressDlg.Label.Text = (string)e.UserState;
|
---|
| 303 | progressDlg.ProgressBar.Value = e.ProgressPercentage;
|
---|
| 304 | progressDlg.Refresh();
|
---|
| 305 | }
|
---|
| 306 | #endregion
|
---|
| 307 |
|
---|
| 308 | #region GUI actions
|
---|
| 309 | private void NewFile() {
|
---|
| 310 | if (!CheckUnsavedChanges())
|
---|
| 311 | return;
|
---|
| 312 | this.DataProcessor.Reset();
|
---|
| 313 | this.lastSavedFile = "";
|
---|
| 314 | this.Text = "Data Importer";
|
---|
| 315 | UpdateDataSetView();
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | private void Open() {
|
---|
| 319 | if (!CheckUnsavedChanges())
|
---|
| 320 | return;
|
---|
| 321 | OpenFileDialog dlg = new OpenFileDialog();
|
---|
| 322 | dlg.Filter = "DataImporter file (*.dhl)|*.dhl|All files (*.*)|*.*";
|
---|
| 323 | dlg.RestoreDirectory = true;
|
---|
| 324 | if (dlg.ShowDialog() == DialogResult.OK) {
|
---|
| 325 | try {
|
---|
| 326 | DataProcessor dp = (DataProcessor)XmlParser.Deserialize(dlg.FileName);
|
---|
| 327 | dp.DatabaseExplorer = this.dataProcessor.DatabaseExplorer;
|
---|
| 328 | this.lastSavedFile = dlg.FileName;
|
---|
| 329 | this.Text = "Data Importer - " + lastSavedFile;
|
---|
| 330 | this.dataProcessor.Reset();
|
---|
| 331 | this.dataProcessor = dp;
|
---|
| 332 | }
|
---|
| 333 | catch (XmlException e) {
|
---|
| 334 | MessageBox.Show(e.Message, "Error occured during file open.");
|
---|
| 335 | }
|
---|
| 336 | finally {
|
---|
| 337 | this.lastSavedCommand = null;
|
---|
| 338 | UpdateDataSetView();
|
---|
| 339 | this.dataProcessor.CommandChain.FireChanged();
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | private bool CheckUnsavedChanges() {
|
---|
| 345 | if (this.lastSavedCommand != this.dataProcessor.CommandChain.LastExecutedCommand) {
|
---|
[9627] | 346 | DialogResult result = new SaveDialog().ShowDialog(this);
|
---|
[6133] | 347 | if (result == DialogResult.Yes)
|
---|
[9627] | 348 | return this.Save();
|
---|
[6133] | 349 | else if (result == DialogResult.Cancel)
|
---|
| 350 | return false;
|
---|
| 351 | }
|
---|
| 352 | return true;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[9627] | 355 | private bool Save() {
|
---|
[6133] | 356 | if (string.IsNullOrEmpty(this.lastSavedFile))
|
---|
[9627] | 357 | return this.SaveAs();
|
---|
| 358 |
|
---|
| 359 | this.Save(dataProcessor, this.lastSavedFile);
|
---|
| 360 | return true;
|
---|
[6133] | 361 | }
|
---|
| 362 |
|
---|
[7868] | 363 | private void Save(object serialize, string filename) {
|
---|
[6133] | 364 | try {
|
---|
| 365 | this.Cursor = Cursors.WaitCursor;
|
---|
[11653] | 366 | XmlGenerator.Serialize(serialize, filename, CompressionLevel.Optimal);
|
---|
[6133] | 367 | this.Cursor = Cursors.Default;
|
---|
| 368 | this.Text = "Data Importer - " + lastSavedFile;
|
---|
| 369 | this.lastSavedCommand = this.dataProcessor.CommandChain.LastExecutedCommand;
|
---|
| 370 | }
|
---|
| 371 | finally {
|
---|
| 372 | this.Cursor = Cursors.Default;
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[9627] | 376 | private bool SaveAs() {
|
---|
[6133] | 377 | SaveFileDialog saveFileDialog = new SaveFileDialog();
|
---|
| 378 | saveFileDialog.AddExtension = true;
|
---|
| 379 | saveFileDialog.RestoreDirectory = true;
|
---|
[7868] | 380 | saveFileDialog.Filter = "Regression Problem file (*.hl)|*.hl|Classification Problem file (*.hl)|*.hl|DataImporter file (*.dhl)|*.dhl|All files (*.*)|*.*";
|
---|
[6133] | 381 | saveFileDialog.DefaultExt = "dhl";
|
---|
[7868] | 382 | saveFileDialog.FilterIndex = 3;
|
---|
[6133] | 383 |
|
---|
| 384 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 385 | this.lastSavedFile = saveFileDialog.FileName;
|
---|
[7868] | 386 | if (saveFileDialog.FilterIndex <= 2) {
|
---|
| 387 | Dataset dataset = ConvertDataset(dataProcessor.DataSet);
|
---|
| 388 | DataAnalysisProblemData problemData = null;
|
---|
| 389 | if (saveFileDialog.FilterIndex == 1) {
|
---|
[8532] | 390 | problemData = new RegressionProblemData(dataset, dataset.DoubleVariables, dataset.DoubleVariables.First());
|
---|
[7868] | 391 | } else if (saveFileDialog.FilterIndex == 2) {
|
---|
[8532] | 392 | problemData = new ClassificationProblemData(dataset, dataset.DoubleVariables, dataset.DoubleVariables.First());
|
---|
[7868] | 393 | } else {
|
---|
| 394 | throw new ArgumentException("File extension doesn't exist");
|
---|
| 395 | }
|
---|
| 396 | Save(problemData, lastSavedFile);
|
---|
| 397 | } else {
|
---|
| 398 | Save(dataProcessor, lastSavedFile);
|
---|
| 399 | }
|
---|
[9627] | 400 | return true;
|
---|
[6133] | 401 | }
|
---|
[9627] | 402 | return false;
|
---|
[6133] | 403 | }
|
---|
| 404 |
|
---|
[7868] | 405 | private Dataset ConvertDataset(DataSet dataSet) {
|
---|
| 406 | ColumnGroup cg;
|
---|
[8532] | 407 | if (dataSet.ActiveColumnGroups.Any()) {
|
---|
[7868] | 408 | cg = dataSet.ActiveColumnGroups.First();
|
---|
[8532] | 409 | } else if (dataSet.ColumnGroups.Any()) {
|
---|
[7868] | 410 | cg = dataSet.ColumnGroups.First();
|
---|
| 411 | } else {
|
---|
| 412 | throw new ArgumentException("There exists no ColumnGroup which could be saved!");
|
---|
| 413 | }
|
---|
| 414 | List<IList> values = new List<IList>();
|
---|
| 415 | foreach (var column in cg.Columns) {
|
---|
[8532] | 416 | if (column.DataType == typeof(double?)) {
|
---|
| 417 | values.Add(column.ValuesEnumerable.Cast<double?>().Select(x => x ?? double.NaN).ToList());
|
---|
| 418 | } else if (column.DataType == typeof(string)) {
|
---|
| 419 | values.Add(column.ValuesEnumerable.Cast<string>().Select(x => x ?? string.Empty).ToList());
|
---|
| 420 | } else if (column.DataType == typeof(DateTime?)) {
|
---|
| 421 | values.Add(column.ValuesEnumerable.Cast<DateTime?>().Select(x => x ?? default(DateTime)).ToList());
|
---|
[7868] | 422 | } else {
|
---|
| 423 | throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 | IEnumerable<string> variableNames = cg.Columns.Select(x => x.Name);
|
---|
| 427 | return new Dataset(variableNames, values);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
[6133] | 430 | private void RecordMacro() {
|
---|
| 431 | if (dataProcessor.CommandChain.IsMacroRecording)
|
---|
| 432 | this.SaveCommandChain();
|
---|
| 433 | this.dataProcessor.CommandChain.ToggleMacroRecording();
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | private void OpenCommandChain() {
|
---|
| 437 | OpenFileDialog dlg = new OpenFileDialog();
|
---|
| 438 | dlg.Filter = "DataImporter Command file (*.dhlcmd)|*.dhlcmd|All files (*.*)|*.*";
|
---|
| 439 | dlg.RestoreDirectory = true;
|
---|
| 440 | if (dlg.ShowDialog() == DialogResult.OK) {
|
---|
| 441 | this.dataSetView.Dispose();
|
---|
| 442 | this.Cursor = Cursors.WaitCursor;
|
---|
| 443 | this.Enabled = false;
|
---|
| 444 | try {
|
---|
| 445 | CommandChain temp = (CommandChain)XmlParser.Deserialize(dlg.FileName);
|
---|
| 446 | progressDlg = new ProgressDialog();
|
---|
| 447 | progressDlg.DesktopLocation = new Point(this.Location.X + this.Width / 2 - progressDlg.Width / 2, this.Location.Y + this.Height / 2 - progressDlg.Height / 2);
|
---|
| 448 | backgroundWorker.RunWorkerAsync(temp);
|
---|
| 449 | progressDlg.ShowDialog();
|
---|
| 450 | this.Refresh();
|
---|
| 451 | }
|
---|
| 452 | finally {
|
---|
| 453 | this.Enabled = true;
|
---|
| 454 | this.Cursor = Cursors.Default;
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | private void SaveCommandChain() {
|
---|
| 460 | SaveFileDialog saveFileDialog = new SaveFileDialog();
|
---|
| 461 | saveFileDialog.AddExtension = true;
|
---|
| 462 | saveFileDialog.RestoreDirectory = true;
|
---|
| 463 | saveFileDialog.DefaultExt = "dhlcmd";
|
---|
| 464 | saveFileDialog.Filter = "DataImporter Command file (*.dhlcmd)|*.dhlcmd|All files (*.*)|*.*";
|
---|
| 465 |
|
---|
| 466 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 467 | try {
|
---|
| 468 | this.Cursor = Cursors.WaitCursor;
|
---|
[11653] | 469 | XmlGenerator.Serialize(this.dataProcessor.CommandChain, saveFileDialog.FileName, CompressionLevel.Optimal);
|
---|
[6133] | 470 | }
|
---|
| 471 | finally {
|
---|
| 472 | this.Cursor = Cursors.Default;
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | private void Export() {
|
---|
| 478 | ExportDialog exportDlg = new ExportDialog(this.dataProcessor.DataSet);
|
---|
| 479 | exportDlg.StartPosition = FormStartPosition.CenterParent;
|
---|
| 480 | exportDlg.ShowDialog(this);
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | private void Import() {
|
---|
| 484 | ImportDialog importDlg = new ImportDialog(this.dataProcessor.DataSet, this.dataProcessor.CommandChain);
|
---|
| 485 | importDlg.StartPosition = FormStartPosition.CenterParent;
|
---|
| 486 | importDlg.ShowDialog(this);
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | private void Undo() {
|
---|
| 490 | this.dataProcessor.CommandChain.Undo();
|
---|
| 491 | this.Focus();
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | private void Redo() {
|
---|
| 495 | this.dataProcessor.CommandChain.Redo();
|
---|
| 496 | this.Focus();
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | private void AddTables() {
|
---|
| 500 | if (dataProcessor.DatabaseExplorer == null || !dataProcessor.DatabaseExplorer.IsConnected) {
|
---|
| 501 | MessageBox.Show("Not connected to database!");
|
---|
| 502 | return;
|
---|
| 503 | }
|
---|
| 504 | DbTablesView tablesForm = new DbTablesView();
|
---|
| 505 | tablesForm.DbExplorer = dataProcessor.DatabaseExplorer;
|
---|
| 506 | tablesForm.SetTables(dataProcessor.DatabaseExplorer.AllTables);
|
---|
| 507 | tablesForm.StartPosition = FormStartPosition.CenterParent;
|
---|
| 508 | if (tablesForm.ShowDialog(this) == DialogResult.OK) {
|
---|
| 509 | progressDlg = new ProgressDialog();
|
---|
| 510 | progressDlg.Label.Text = "Loading data from database";
|
---|
| 511 | progressDlg.Show(this);
|
---|
| 512 | progressDlg.DesktopLocation = new Point(this.Location.X + this.Width / 2 - progressDlg.Width / 2, this.Location.Y + this.Height / 2 - progressDlg.Height / 2);
|
---|
| 513 | this.Refresh();
|
---|
| 514 | this.dataProcessor.CommandChain.Add(new LoadColumnGroupsFromDBCommand(this.dataProcessor.DataSet, this.dataProcessor.DatabaseExplorer,
|
---|
| 515 | tablesForm.SelectedTables));
|
---|
| 516 | progressDlg.Close();
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | private void DatabaseExplorer_NewRowLoaded(object sender, ProgressChangedEventArgs e) {
|
---|
| 521 | progressDlg.ProgressBar.Value = e.ProgressPercentage;
|
---|
| 522 | progressDlg.Label.Text = (string)e.UserState;
|
---|
| 523 | progressDlg.Refresh();
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | private void AddTablesWithSQLCommand() {
|
---|
| 527 | CommandViewDialog dlg = new CommandViewDialog();
|
---|
| 528 | dlg.StartPosition = FormStartPosition.CenterParent;
|
---|
| 529 | SqlCommandView cmdView = new SqlCommandView();
|
---|
| 530 | dlg.Panel.Controls.Add(cmdView);
|
---|
| 531 | dlg.Panel.Size = cmdView.Size;
|
---|
| 532 | dlg.ShowDialog(this);
|
---|
| 533 | if (dlg.DialogResult == DialogResult.OK) {
|
---|
| 534 | this.dataProcessor.CommandChain.Add(new LoadColumnGroupWithSqlStringFromDBCommand(this.dataProcessor.DataSet, this.dataProcessor.DatabaseExplorer, cmdView.SqlCommand));
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | private void SetDbConnection() {
|
---|
| 539 | dataProcessor.DatabaseExplorer.ShowDbConnectionWizard();
|
---|
| 540 | if (dataProcessor.DatabaseExplorer.IsConnected) {
|
---|
| 541 | this.lblConnectionStatus.Text = "Successfully connected to database!";
|
---|
| 542 | this.lblConnectionStatus.BackColor = Color.LightGreen;
|
---|
| 543 | } else {
|
---|
| 544 | this.lblConnectionStatus.Text = "Not connected to database!";
|
---|
| 545 | this.lblConnectionStatus.BackColor = Color.FromArgb(255, 128, 128); //light red
|
---|
| 546 | }
|
---|
| 547 | }
|
---|
| 548 | #endregion
|
---|
| 549 |
|
---|
| 550 | #region GUI ClickEvents - menuitems, toolbarbuttons
|
---|
| 551 |
|
---|
| 552 | private void DataProcessorView_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
| 553 | if (!CheckUnsavedChanges())
|
---|
| 554 | e.Cancel = true;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | #region MenuItems
|
---|
| 558 | private void newToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 559 | this.NewFile();
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | private void openToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 563 | this.Open();
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 567 | this.Save();
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 571 | this.SaveAs();
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | private void importToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 575 | this.Import();
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 579 | this.Export();
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 583 | this.Close();
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 587 | this.Undo();
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | private void redoToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 591 | this.Redo();
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | private void addTablesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 595 | this.AddTables();
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | private void addTablesWithSqlStringToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 599 | this.AddTablesWithSQLCommand();
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | private void setGenericDBConnectionToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 603 | this.dataProcessor.DatabaseExplorer = (IDbExplorer)((ToolStripMenuItem)sender).Tag;
|
---|
| 604 | this.dataProcessor.DatabaseExplorer.NewRowLoaded += new ProgressChangedEventHandler(DatabaseExplorer_NewRowLoaded);
|
---|
| 605 | this.SetDbConnection();
|
---|
| 606 | }
|
---|
| 607 | #endregion
|
---|
| 608 |
|
---|
| 609 | #region ToolbarButtons
|
---|
| 610 | private void newToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 611 | this.NewFile();
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | private void openToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 615 | this.Open();
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | private void saveToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 619 | this.Save();
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | private void recordMacroToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 623 | this.RecordMacro();
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | private void openCommandChainToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 627 | this.OpenCommandChain();
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | private void saveCommandChaingToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 631 | this.SaveCommandChain();
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | private void undoToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 635 | this.Undo();
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | private void redoToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 639 | this.Redo();
|
---|
| 640 | }
|
---|
| 641 | #endregion
|
---|
| 642 | #endregion
|
---|
| 643 |
|
---|
| 644 | private class AttributeComparer : IComparer<ViewableCommandInfoAttribute> {
|
---|
| 645 | public int Compare(ViewableCommandInfoAttribute x, ViewableCommandInfoAttribute y) {
|
---|
| 646 | int cmpResult = x.GroupName.CompareTo(y.GroupName);
|
---|
| 647 | if (cmpResult != 0)
|
---|
| 648 | return cmpResult;
|
---|
| 649 | cmpResult = x.Position.CompareTo(y.Position);
|
---|
| 650 | if (cmpResult != 0)
|
---|
| 651 | return cmpResult;
|
---|
| 652 | return x.Name.CompareTo(y.Name);
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 | }
|
---|
| 656 | }
|
---|