Changeset 14561 for trunk/sources
- Timestamp:
- 01/13/17 11:24:30 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs
r14299 r14561 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.ComponentModel; 24 25 using System.Drawing; … … 200 201 } 201 202 203 var elementNames = Content.ElementNames.ToList(); 202 204 for (int i = minRowIndex; i <= maxRowIndex; i++) { 203 205 DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible); 204 206 DataGridViewCell cell = dataGridView[column.Index, i]; 205 207 if (cell.Selected) { 206 s.Append(Content.ElementNames.ElementAt(i)); 207 s.Append("\t"); 208 if (i < elementNames.Count) { 209 s.Append(elementNames[i]); 210 s.Append("\t"); 211 } else if (elementNames.Count > 0) { 212 s.Append("Index " + i); 213 s.Append("\t"); 214 } 208 215 s.Append(Content.GetValue(i)); 209 216 s.Append(Environment.NewLine); … … 213 220 } 214 221 private void PasteValuesToDataGridView() { 215 string[] values = SplitClipboardString(Clipboard.GetText()); 222 Tuple<string, string>[] values = null; 223 try { 224 values = SplitClipboardString(Clipboard.GetText()).ToArray(); 225 } catch (ArgumentException ex) { 226 MessageBox.Show(this, ex.Message, "Error while parsing clipboard text.", MessageBoxButtons.OK, MessageBoxIcon.Error); 227 return; 228 } 216 229 int rowIndex = 0; 217 230 if (dataGridView.CurrentCell != null) … … 219 232 220 233 if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length; 221 for (int row = 0; row < values.Length; row++) 222 Content.SetValue(values[row], row + rowIndex); 223 } 224 private string[] SplitClipboardString(string clipboardText) { 234 for (int row = 0; row < values.Length; row++) { 235 Content.SetValue(values[row].Item2, row + rowIndex); 236 } 237 if (values.Any(x => !string.IsNullOrEmpty(x.Item1))) { 238 var elementNames = Content.ElementNames.ToList(); 239 for (int row = 0; row < values.Length; row++) { 240 if (row + rowIndex < elementNames.Count) 241 elementNames[row + rowIndex] = values[row].Item1; 242 else elementNames.Add(values[row].Item1); 243 } 244 Content.ElementNames = elementNames; 245 } 246 } 247 private IEnumerable<Tuple<string, string>> SplitClipboardString(string clipboardText) { 225 248 if (clipboardText.EndsWith(Environment.NewLine)) 226 249 clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length); //remove last newline constant 227 return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None); 250 251 var lines = clipboardText.Split(new [] { Environment.NewLine }, StringSplitOptions.None); 252 var tabSep = new[] { '\t' }; 253 if (lines.Length > 2) { 254 // Case 1: Each line contains either "elementName \t value" or just "value" (one or two vertical vectors) 255 foreach (var l in lines) { 256 var row = l.Split(tabSep, StringSplitOptions.RemoveEmptyEntries); 257 if (row.Length > 2) throw new ArgumentException("Clipboard may have either at most two rows or at most two columns."); 258 if (row.Length == 2) yield return Tuple.Create(row[0], row[1]); 259 else if (row.Length == 1) yield return Tuple.Create(string.Empty, row[0]); 260 else yield return Tuple.Create(string.Empty, string.Empty); 261 } 262 } else if (lines.Length == 2) { 263 var firstLine = lines[0].Split(tabSep, StringSplitOptions.None); 264 var secondLine = lines[1].Split(tabSep, StringSplitOptions.None); 265 if (firstLine.Length <= 2 && secondLine.Length <= 2) { 266 // Case 2a: The two lines contain either "elementName \t value" or just "value" (one or two vertical vectors) 267 yield return firstLine.Length >= 2 ? Tuple.Create(firstLine[0], firstLine[1]) : Tuple.Create(string.Empty, firstLine[0]); 268 yield return secondLine.Length >= 2 ? Tuple.Create(secondLine[0], secondLine[1]) : Tuple.Create(string.Empty, secondLine[0]); 269 } else { 270 // Case 2b: The first line contains the elementNames, the second line contains the values (two horizontal vectors) 271 var max = Math.Max(firstLine.Length, secondLine.Length); 272 for (var i = 0; i < max; i++) { 273 var elemName = i < firstLine.Length ? firstLine[i] : string.Empty; 274 var value = i < secondLine.Length ? secondLine[i] : string.Empty; 275 yield return Tuple.Create(elemName, value); 276 } 277 } 278 } else if (lines.Length == 1) { 279 // Case 3: The line contains the values (one horizontal vector) 280 var entries = lines[0].Split(tabSep, StringSplitOptions.None); 281 foreach (var e in entries) { 282 yield return Tuple.Create(string.Empty, e); 283 } 284 } 228 285 } 229 286 #endregion
Note: See TracChangeset
for help on using the changeset viewer.