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