[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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.Windows.Forms;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[234] | 25 | using HeuristicLab.PluginInfrastructure;
|
---|
[2] | 26 |
|
---|
| 27 | namespace HeuristicLab.DataAnalysis {
|
---|
[232] | 28 | public partial class DatasetView : EditorBase {
|
---|
[2] | 29 | public Dataset Dataset {
|
---|
| 30 | get { return (Dataset)Item; }
|
---|
| 31 | set {
|
---|
| 32 | Item = value;
|
---|
[168] | 33 | Refresh();
|
---|
[2] | 34 | }
|
---|
| 35 | }
|
---|
[168] | 36 | public DatasetView()
|
---|
| 37 | : base() {
|
---|
[2] | 38 | InitializeComponent();
|
---|
[234] | 39 | DiscoveryService discovery = new DiscoveryService();
|
---|
| 40 | IDatasetManipulator[] manipuators = discovery.GetInstances<IDatasetManipulator>();
|
---|
| 41 | contextMenuStrip.Items.Add(new ToolStripSeparator());
|
---|
| 42 | foreach(IDatasetManipulator manipulator in manipuators) {
|
---|
| 43 | contextMenuStrip.Items.Add(new ToolStripButton(manipulator.Action,null , delegate(object source, EventArgs args)
|
---|
| 44 | { manipulator.Execute(Dataset); }));
|
---|
| 45 | }
|
---|
[2] | 46 | }
|
---|
| 47 |
|
---|
[168] | 48 | public DatasetView(Dataset dataset)
|
---|
| 49 | : this() {
|
---|
[2] | 50 | this.Dataset = dataset;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | protected override void UpdateControls() {
|
---|
| 54 | base.UpdateControls();
|
---|
[168] | 55 | if (Dataset != null) {
|
---|
[2] | 56 | int rows = Dataset.Rows;
|
---|
| 57 | int columns = Dataset.Columns;
|
---|
| 58 | nameTextBox.Text = Dataset.Name;
|
---|
| 59 | rowsTextBox.Text = rows + "";
|
---|
| 60 | columnsTextBox.Text = columns + "";
|
---|
| 61 | dataGridView.ColumnCount = columns;
|
---|
| 62 | dataGridView.RowCount = rows;
|
---|
[168] | 63 | for (int i = 0; i < rows; i++) {
|
---|
| 64 | for (int j = 0; j < columns; j++) {
|
---|
[2] | 65 | dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
[232] | 68 | for (int i = 0; i < columns; i++) {
|
---|
| 69 | dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
|
---|
| 70 | dataGridView.Columns[i].Name = GetColumnName(i);
|
---|
[2] | 71 | }
|
---|
[232] | 72 | dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
|
---|
[2] | 73 | } else {
|
---|
| 74 | rowsTextBox.Text = "1";
|
---|
| 75 | columnsTextBox.Text = "1";
|
---|
| 76 | dataGridView.ColumnCount = 1;
|
---|
| 77 | dataGridView.RowCount = 1;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[168] | 82 | if (ValidateData((string)e.FormattedValue)) {
|
---|
[2] | 83 | SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
|
---|
| 84 | e.Cancel = false;
|
---|
| 85 | } else {
|
---|
| 86 | e.Cancel = true;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | private void SetArrayElement(int row, int column, string element) {
|
---|
| 92 | double result;
|
---|
| 93 | double.TryParse(element, out result);
|
---|
[168] | 94 | if (result != Dataset.GetValue(row, column)) {
|
---|
[2] | 95 | Dataset.SetValue(row, column, result);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private bool ValidateData(string element) {
|
---|
| 100 | double result;
|
---|
| 101 | return element != null && double.TryParse(element, out result);
|
---|
| 102 | }
|
---|
[232] | 103 |
|
---|
| 104 | private void scaleValuesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 105 | foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
|
---|
[237] | 106 | Dataset.ScaleVariable(column.Index);
|
---|
| 107 | column.Name = GetColumnName(column.Index) + " [scaled]";
|
---|
[232] | 108 | }
|
---|
| 109 | Refresh();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | private void originalValuesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 113 | foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
|
---|
[237] | 114 | Dataset.UnscaleVariable(column.Index);
|
---|
| 115 | column.Name = GetColumnName(column.Index);
|
---|
[232] | 116 | }
|
---|
| 117 | Refresh();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private string GetColumnName(int index) {
|
---|
| 121 | if(Dataset.VariableNames.Length == dataGridView.Columns.Count) {
|
---|
| 122 | return Dataset.VariableNames[index];
|
---|
| 123 | } else {
|
---|
| 124 | return "Var " + index;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[2] | 127 | }
|
---|
[232] | 128 | } |
---|