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