Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StackingProblems/HeuristicLab.Data.MoveVectorData.Views/3.3/StackView.cs @ 14278

Last change on this file since 14278 was 14278, checked in by mzehetho, 8 years ago

Initial Commit for ticket #2605

Implemented:

  • Encoding
  • Moves
  • Views
  • Blocks World Problem
  • Blocks Relocation Problem
  • Stacking Problem
File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Data.MoveVectorData;
30using System.Linq;
31
32namespace HeuristicLab.Data.Views
33{
34    [View("Stack View")]
35    [Content(typeof(Stack), true)]
36    public partial class StackView : AsynchronousContentView
37    {
38        protected int[] virtualRowIndices;
39
40        public new IStringConvertibleArray Content
41        {
42            get { return (IStringConvertibleArray)base.Content; }
43            set { base.Content = value; }
44        }
45
46        public override bool ReadOnly
47        {
48            get
49            {
50                if ((Content != null) && Content.ReadOnly) return true;
51                return base.ReadOnly;
52            }
53            set { base.ReadOnly = value; }
54        }
55
56        public StackView()
57        {
58            InitializeComponent();
59        }
60
61        protected override void DeregisterContentEvents()
62        {
63            Content.ElementNamesChanged -= new EventHandler(Content_ElementNamesChanged);
64            Content.ItemChanged -= new EventHandler<EventArgs<int>>(Content_ItemChanged);
65            Content.Reset -= new EventHandler(Content_Reset);
66            base.DeregisterContentEvents();
67        }
68
69        protected override void RegisterContentEvents()
70        {
71            base.RegisterContentEvents();
72            Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
73            Content.Reset += new EventHandler(Content_Reset);
74            Content.ElementNamesChanged += new EventHandler(Content_ElementNamesChanged);
75        }
76
77        protected override void OnContentChanged()
78        {
79            base.OnContentChanged();
80            if (Content == null)
81            {
82                dataGridView.Rows.Clear();
83                dataGridView.Columns.Clear();
84                virtualRowIndices = new int[0];
85            }
86            else
87                UpdateData();
88        }
89
90        protected override void SetEnabledStateOfControls()
91        {
92            base.SetEnabledStateOfControls();
93            dataGridView.Enabled = Content != null;
94            dataGridView.ReadOnly = ReadOnly;
95        }
96
97        private void UpdateData()
98        {
99            virtualRowIndices = Enumerable.Range(0, Content.Length).Reverse().ToArray();
100
101            dataGridView.Rows.Clear();
102            dataGridView.Columns.Clear();
103            if (Content.Length > 0)
104            {
105                dataGridView.ColumnCount++;
106                dataGridView.Columns[0].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
107                dataGridView.RowCount = Content.Length;
108                for (int i = 0; i < Content.Length; i++)
109                {
110                    int rowIndex = this.virtualRowIndices[i];
111                    dataGridView.Rows[i].Cells[0].Value = Content.GetValue(rowIndex);
112                }
113                dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
114            }
115            UpdateRowHeaders();
116            dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
117            dataGridView.Enabled = true;
118        }
119
120        public virtual void UpdateRowHeaders()
121        {
122            int i = 0;
123            int count = dataGridView.RowCount;
124            foreach (string elementName in Content.ElementNames)
125            {
126                dataGridView.Rows[i].HeaderCell.Value = elementName;
127                i++;
128            }
129            for (; i < dataGridView.RowCount; i++)
130            {
131                dataGridView.Rows[i].HeaderCell.Value = "Tier " + (count - i - 1);
132            }
133        }
134
135        private void Content_ElementNamesChanged(object sender, EventArgs e)
136        {
137            if (InvokeRequired)
138                Invoke(new EventHandler(Content_ElementNamesChanged), sender, e);
139            else
140                UpdateRowHeaders();
141        }
142
143        private void Content_ItemChanged(object sender, EventArgs<int> e)
144        {
145            if (InvokeRequired)
146                Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
147            else {
148                // if a resize of the array occurs and some other class handles the event and provides default values
149                //then the itemChanged will occur before the reset event. hence the check was added
150                if (dataGridView.RowCount <= e.Value) return;
151                int rowIndex = this.virtualRowIndices[e.Value];
152                dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(rowIndex);
153                Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
154                dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
155            }
156        }
157        private void Content_Reset(object sender, EventArgs e)
158        {
159            if (InvokeRequired)
160                Invoke(new EventHandler(Content_Reset), sender, e);
161            else
162                UpdateData();
163        }
164
165        #region DataGridView Events
166        private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
167        {
168            string errorMessage;
169            if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage))
170            {
171                e.Cancel = true;
172                dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
173            }
174        }
175        private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
176        {
177            string value = e.Value.ToString();
178            int rowIndex = virtualRowIndices[e.RowIndex];
179            e.ParsingApplied = Content.SetValue(value, rowIndex);
180            if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex);
181        }
182        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
183        {
184            dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
185        }
186        private void dataGridView_KeyDown(object sender, KeyEventArgs e)
187        {
188            if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
189                PasteValuesToDataGridView();
190            else if (e.Control && e.KeyCode == Keys.C)
191                CopyValuesFromDataGridView();
192            else if (e.Control && e.KeyCode == Keys.A)
193                dataGridView.SelectAll();
194        }
195        private void CopyValuesFromDataGridView()
196        {
197            if (dataGridView.SelectedCells.Count == 0) return;
198            StringBuilder s = new StringBuilder();
199            int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
200            int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
201
202            if (minRowIndex > maxRowIndex)
203            {
204                int temp = minRowIndex;
205                minRowIndex = maxRowIndex;
206                maxRowIndex = temp;
207            }
208
209            for (int i = minRowIndex; i <= maxRowIndex; i++)
210            {
211                int rowIndex = this.virtualRowIndices[i];
212
213                DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
214                DataGridViewCell cell = dataGridView[column.Index, i];
215                if (cell.Selected)
216                {
217                    s.Append(Content.GetValue(rowIndex));
218                    s.Append(Environment.NewLine);
219                }
220            }
221            Clipboard.SetText(s.ToString());
222        }
223        private void PasteValuesToDataGridView()
224        {
225            string[] values = SplitClipboardString(Clipboard.GetText());
226            int rowIndex = 0;
227            if (dataGridView.CurrentCell != null)
228                rowIndex = dataGridView.CurrentCell.RowIndex;
229
230            if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length;
231            for (int row = 0; row < values.Length; row++)
232                Content.SetValue(values[row], row + rowIndex);
233        }
234        private string[] SplitClipboardString(string clipboardText)
235        {
236            if (clipboardText.EndsWith(Environment.NewLine))
237                clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
238            return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
239        }
240        #endregion
241    }
242}
Note: See TracBrowser for help on using the repository browser.