Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.DataProcessor/DataProcessorView.cs @ 10879

Last change on this file since 10879 was 9627, checked in by mkommend, 12 years ago

#1734: Improved data importer usability:

  • new closing message
  • rename column reacts on lost focus
  • updated copyright info
File size: 26.2 KB
Line 
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Drawing;
27using System.Linq;
28using System.Windows.Forms;
29using System.Xml;
30using HeuristicLab.DataImporter.Data;
31using HeuristicLab.DataImporter.Data.CommandBase;
32using HeuristicLab.DataImporter.Data.Model;
33using HeuristicLab.DataImporter.Data.View;
34using HeuristicLab.DataImporter.DataProcessor.Command;
35using HeuristicLab.DataImporter.DbExplorer.Interfaces;
36using HeuristicLab.Persistence.Default.Xml;
37using HeuristicLab.PluginInfrastructure;
38using HeuristicLab.Problems.DataAnalysis;
39
40namespace 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 = new SaveDialog().ShowDialog(this);
346        if (result == DialogResult.Yes)
347          return this.Save();
348        else if (result == DialogResult.Cancel)
349          return false;
350      }
351      return true;
352    }
353
354    private bool Save() {
355      if (string.IsNullOrEmpty(this.lastSavedFile))
356        return this.SaveAs();
357
358      this.Save(dataProcessor, this.lastSavedFile);
359      return true;
360    }
361
362    private void Save(object serialize, string filename) {
363      try {
364        this.Cursor = Cursors.WaitCursor;
365        XmlGenerator.Serialize(serialize, filename, 9);
366        this.Cursor = Cursors.Default;
367        this.Text = "Data Importer - " + lastSavedFile;
368        this.lastSavedCommand = this.dataProcessor.CommandChain.LastExecutedCommand;
369      }
370      finally {
371        this.Cursor = Cursors.Default;
372      }
373    }
374
375    private bool SaveAs() {
376      SaveFileDialog saveFileDialog = new SaveFileDialog();
377      saveFileDialog.AddExtension = true;
378      saveFileDialog.RestoreDirectory = true;
379      saveFileDialog.Filter = "Regression Problem file (*.hl)|*.hl|Classification Problem file (*.hl)|*.hl|DataImporter file (*.dhl)|*.dhl|All files (*.*)|*.*";
380      saveFileDialog.DefaultExt = "dhl";
381      saveFileDialog.FilterIndex = 3;
382
383      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
384        this.lastSavedFile = saveFileDialog.FileName;
385        if (saveFileDialog.FilterIndex <= 2) {
386          Dataset dataset = ConvertDataset(dataProcessor.DataSet);
387          DataAnalysisProblemData problemData = null;
388          if (saveFileDialog.FilterIndex == 1) {
389            problemData = new RegressionProblemData(dataset, dataset.DoubleVariables, dataset.DoubleVariables.First());
390          } else if (saveFileDialog.FilterIndex == 2) {
391            problemData = new ClassificationProblemData(dataset, dataset.DoubleVariables, dataset.DoubleVariables.First());
392          } else {
393            throw new ArgumentException("File extension doesn't exist");
394          }
395          Save(problemData, lastSavedFile);
396        } else {
397          Save(dataProcessor, lastSavedFile);
398        }
399        return true;
400      }
401      return false;
402    }
403
404    private Dataset ConvertDataset(DataSet dataSet) {
405      ColumnGroup cg;
406      if (dataSet.ActiveColumnGroups.Any()) {
407        cg = dataSet.ActiveColumnGroups.First();
408      } else if (dataSet.ColumnGroups.Any()) {
409        cg = dataSet.ColumnGroups.First();
410      } else {
411        throw new ArgumentException("There exists no ColumnGroup which could be saved!");
412      }
413      List<IList> values = new List<IList>();
414      foreach (var column in cg.Columns) {
415        if (column.DataType == typeof(double?)) {
416          values.Add(column.ValuesEnumerable.Cast<double?>().Select(x => x ?? double.NaN).ToList());
417        } else if (column.DataType == typeof(string)) {
418          values.Add(column.ValuesEnumerable.Cast<string>().Select(x => x ?? string.Empty).ToList());
419        } else if (column.DataType == typeof(DateTime?)) {
420          values.Add(column.ValuesEnumerable.Cast<DateTime?>().Select(x => x ?? default(DateTime)).ToList());
421        } else {
422          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
423        }
424      }
425      IEnumerable<string> variableNames = cg.Columns.Select(x => x.Name);
426      return new Dataset(variableNames, values);
427    }
428
429    private void RecordMacro() {
430      if (dataProcessor.CommandChain.IsMacroRecording)
431        this.SaveCommandChain();
432      this.dataProcessor.CommandChain.ToggleMacroRecording();
433    }
434
435    private void OpenCommandChain() {
436      OpenFileDialog dlg = new OpenFileDialog();
437      dlg.Filter = "DataImporter Command file (*.dhlcmd)|*.dhlcmd|All files (*.*)|*.*";
438      dlg.RestoreDirectory = true;
439      if (dlg.ShowDialog() == DialogResult.OK) {
440        this.dataSetView.Dispose();
441        this.Cursor = Cursors.WaitCursor;
442        this.Enabled = false;
443        try {
444          CommandChain temp = (CommandChain)XmlParser.Deserialize(dlg.FileName);
445          progressDlg = new ProgressDialog();
446          progressDlg.DesktopLocation = new Point(this.Location.X + this.Width / 2 - progressDlg.Width / 2, this.Location.Y + this.Height / 2 - progressDlg.Height / 2);
447          backgroundWorker.RunWorkerAsync(temp);
448          progressDlg.ShowDialog();
449          this.Refresh();
450        }
451        finally {
452          this.Enabled = true;
453          this.Cursor = Cursors.Default;
454        }
455      }
456    }
457
458    private void SaveCommandChain() {
459      SaveFileDialog saveFileDialog = new SaveFileDialog();
460      saveFileDialog.AddExtension = true;
461      saveFileDialog.RestoreDirectory = true;
462      saveFileDialog.DefaultExt = "dhlcmd";
463      saveFileDialog.Filter = "DataImporter Command file (*.dhlcmd)|*.dhlcmd|All files (*.*)|*.*";
464
465      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
466        try {
467          this.Cursor = Cursors.WaitCursor;
468          XmlGenerator.Serialize(this.dataProcessor.CommandChain, saveFileDialog.FileName, 9);
469        }
470        finally {
471          this.Cursor = Cursors.Default;
472        }
473      }
474    }
475
476    private void Export() {
477      ExportDialog exportDlg = new ExportDialog(this.dataProcessor.DataSet);
478      exportDlg.StartPosition = FormStartPosition.CenterParent;
479      exportDlg.ShowDialog(this);
480    }
481
482    private void Import() {
483      ImportDialog importDlg = new ImportDialog(this.dataProcessor.DataSet, this.dataProcessor.CommandChain);
484      importDlg.StartPosition = FormStartPosition.CenterParent;
485      importDlg.ShowDialog(this);
486    }
487
488    private void Undo() {
489      this.dataProcessor.CommandChain.Undo();
490      this.Focus();
491    }
492
493    private void Redo() {
494      this.dataProcessor.CommandChain.Redo();
495      this.Focus();
496    }
497
498    private void AddTables() {
499      if (dataProcessor.DatabaseExplorer == null || !dataProcessor.DatabaseExplorer.IsConnected) {
500        MessageBox.Show("Not connected to database!");
501        return;
502      }
503      DbTablesView tablesForm = new DbTablesView();
504      tablesForm.DbExplorer = dataProcessor.DatabaseExplorer;
505      tablesForm.SetTables(dataProcessor.DatabaseExplorer.AllTables);
506      tablesForm.StartPosition = FormStartPosition.CenterParent;
507      if (tablesForm.ShowDialog(this) == DialogResult.OK) {
508        progressDlg = new ProgressDialog();
509        progressDlg.Label.Text = "Loading data from database";
510        progressDlg.Show(this);
511        progressDlg.DesktopLocation = new Point(this.Location.X + this.Width / 2 - progressDlg.Width / 2, this.Location.Y + this.Height / 2 - progressDlg.Height / 2);
512        this.Refresh();
513        this.dataProcessor.CommandChain.Add(new LoadColumnGroupsFromDBCommand(this.dataProcessor.DataSet, this.dataProcessor.DatabaseExplorer,
514          tablesForm.SelectedTables));
515        progressDlg.Close();
516      }
517    }
518
519    private void DatabaseExplorer_NewRowLoaded(object sender, ProgressChangedEventArgs e) {
520      progressDlg.ProgressBar.Value = e.ProgressPercentage;
521      progressDlg.Label.Text = (string)e.UserState;
522      progressDlg.Refresh();
523    }
524
525    private void AddTablesWithSQLCommand() {
526      CommandViewDialog dlg = new CommandViewDialog();
527      dlg.StartPosition = FormStartPosition.CenterParent;
528      SqlCommandView cmdView = new SqlCommandView();
529      dlg.Panel.Controls.Add(cmdView);
530      dlg.Panel.Size = cmdView.Size;
531      dlg.ShowDialog(this);
532      if (dlg.DialogResult == DialogResult.OK) {
533        this.dataProcessor.CommandChain.Add(new LoadColumnGroupWithSqlStringFromDBCommand(this.dataProcessor.DataSet, this.dataProcessor.DatabaseExplorer, cmdView.SqlCommand));
534      }
535    }
536
537    private void SetDbConnection() {
538      dataProcessor.DatabaseExplorer.ShowDbConnectionWizard();
539      if (dataProcessor.DatabaseExplorer.IsConnected) {
540        this.lblConnectionStatus.Text = "Successfully connected to database!";
541        this.lblConnectionStatus.BackColor = Color.LightGreen;
542      } else {
543        this.lblConnectionStatus.Text = "Not connected to database!";
544        this.lblConnectionStatus.BackColor = Color.FromArgb(255, 128, 128); //light red
545      }
546    }
547    #endregion
548
549    #region GUI ClickEvents - menuitems, toolbarbuttons
550
551    private void DataProcessorView_FormClosing(object sender, FormClosingEventArgs e) {
552      if (!CheckUnsavedChanges())
553        e.Cancel = true;
554    }
555
556    #region MenuItems
557    private void newToolStripMenuItem_Click(object sender, EventArgs e) {
558      this.NewFile();
559    }
560
561    private void openToolStripMenuItem_Click(object sender, EventArgs e) {
562      this.Open();
563    }
564
565    private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
566      this.Save();
567    }
568
569    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
570      this.SaveAs();
571    }
572
573    private void importToolStripMenuItem_Click(object sender, EventArgs e) {
574      this.Import();
575    }
576
577    private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
578      this.Export();
579    }
580
581    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
582      this.Close();
583    }
584
585    private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
586      this.Undo();
587    }
588
589    private void redoToolStripMenuItem_Click(object sender, EventArgs e) {
590      this.Redo();
591    }
592
593    private void addTablesToolStripMenuItem_Click(object sender, EventArgs e) {
594      this.AddTables();
595    }
596
597    private void addTablesWithSqlStringToolStripMenuItem_Click(object sender, EventArgs e) {
598      this.AddTablesWithSQLCommand();
599    }
600
601    private void setGenericDBConnectionToolStripMenuItem_Click(object sender, EventArgs e) {
602      this.dataProcessor.DatabaseExplorer = (IDbExplorer)((ToolStripMenuItem)sender).Tag;
603      this.dataProcessor.DatabaseExplorer.NewRowLoaded += new ProgressChangedEventHandler(DatabaseExplorer_NewRowLoaded);
604      this.SetDbConnection();
605    }
606    #endregion
607
608    #region ToolbarButtons
609    private void newToolStripButton_Click(object sender, EventArgs e) {
610      this.NewFile();
611    }
612
613    private void openToolStripButton_Click(object sender, EventArgs e) {
614      this.Open();
615    }
616
617    private void saveToolStripButton_Click(object sender, EventArgs e) {
618      this.Save();
619    }
620
621    private void recordMacroToolStripButton_Click(object sender, EventArgs e) {
622      this.RecordMacro();
623    }
624
625    private void openCommandChainToolStripButton_Click(object sender, EventArgs e) {
626      this.OpenCommandChain();
627    }
628
629    private void saveCommandChaingToolStripButton_Click(object sender, EventArgs e) {
630      this.SaveCommandChain();
631    }
632
633    private void undoToolStripButton_Click(object sender, EventArgs e) {
634      this.Undo();
635    }
636
637    private void redoToolStripButton_Click(object sender, EventArgs e) {
638      this.Redo();
639    }
640    #endregion
641    #endregion
642
643    private class AttributeComparer : IComparer<ViewableCommandInfoAttribute> {
644      public int Compare(ViewableCommandInfoAttribute x, ViewableCommandInfoAttribute y) {
645        int cmpResult = x.GroupName.CompareTo(y.GroupName);
646        if (cmpResult != 0)
647          return cmpResult;
648        cmpResult = x.Position.CompareTo(y.Position);
649        if (cmpResult != 0)
650          return cmpResult;
651        return x.Name.CompareTo(y.Name);
652      }
653    }
654  }
655}
Note: See TracBrowser for help on using the repository browser.