Changeset 11479
- Timestamp:
- 10/17/14 12:52:15 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Scripting.Views/3.3/VariableStoreView.cs
r11474 r11479 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Linq; 26 25 using System.Text.RegularExpressions; … … 40 39 [Content(typeof(VariableStore), true)] 41 40 public partial class VariableStoreView : AsynchronousContentView { 41 #region Image Names 42 private const string ErrorImageName = "Error"; 43 private const string WarningImageName = "Warning"; 44 private const string HeuristicLabObjectImageName = "HeuristicLabObject"; 45 private const string ObjectImageName = "Object"; 46 private const string NothingImageName = "Nothing"; 47 #endregion 48 42 49 protected readonly Dictionary<string, ListViewItem> itemListViewItemMapping; 43 50 protected readonly Dictionary<Type, bool> serializableLookup; … … 58 65 itemListViewItemMapping = new Dictionary<string, ListViewItem>(); 59 66 serializableLookup = new Dictionary<Type, bool>(); 60 variableListView.SmallImageList.Images.AddRange(new Image[] { 61 HeuristicLab.Common.Resources.VSImageLibrary.Error, 62 HeuristicLab.Common.Resources.VSImageLibrary.Warning, 63 HeuristicLab.Common.Resources.VSImageLibrary.Object, 64 HeuristicLab.Common.Resources.VSImageLibrary.Nothing 65 }); 67 68 var images = variableListView.SmallImageList.Images; 69 images.Add(ErrorImageName, Common.Resources.VSImageLibrary.Error); 70 images.Add(WarningImageName, Common.Resources.VSImageLibrary.Warning); 71 images.Add(HeuristicLabObjectImageName, Common.Resources.HeuristicLab.Icon.ToBitmap()); 72 images.Add(ObjectImageName, Common.Resources.VSImageLibrary.Object); 73 images.Add(NothingImageName, Common.Resources.VSImageLibrary.Nothing); 66 74 } 67 75 … … 142 150 string type = variable.Value == null ? "null" : variable.Value.GetType().ToString(); 143 151 bool serializable = IsSerializable(variable); 144 var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { ToolTipText = GetToolTipText(variable, serializable), Tag = variable }; 145 bool validIdentifier = SafeVariableNameRegex.IsMatch(variable.Key); 146 if (!serializable) listViewItem.ImageIndex = 0; 147 else if (!validIdentifier) listViewItem.ImageIndex = 1; 148 else if (variable.Value != null) listViewItem.ImageIndex = 2; 149 else listViewItem.ImageIndex = 3; 152 153 var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { Tag = variable }; 154 SetImageKey(listViewItem, serializable); 155 SetToolTipText(listViewItem, serializable); 150 156 variableListView.Items.Add(listViewItem); 157 151 158 itemListViewItemMapping[variable.Key] = listViewItem; 152 159 sortAscendingButton.Enabled = variableListView.Items.Count > 1; … … 177 184 bool serializable = IsSerializable(variable); 178 185 bool validIdentifier = SafeVariableNameRegex.IsMatch(variable.Key); 179 if (!serializable) listViewItem.ImageIndex = 0; 180 else if (!validIdentifier) listViewItem.ImageIndex = 1; 181 else if (variable.Value != null) listViewItem.ImageIndex = 2; 182 else listViewItem.ImageIndex = 3; 186 187 listViewItem.Tag = variable; 183 188 listViewItem.SubItems[1].Text = value; 184 189 listViewItem.SubItems[2].Text = type; 185 listViewItem.ToolTipText = GetToolTipText(variable, serializable);186 listViewItem.Tag = variable;190 SetImageKey(listViewItem, serializable); 191 SetToolTipText(listViewItem, serializable); 187 192 } else throw new ArgumentException("A variable with the specified name does not exist.", "variable"); 188 193 } … … 234 239 var listViewItem = variableListView.SelectedItems[0]; 235 240 var item = (KeyValuePair<string, object>)listViewItem.Tag; 236 var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item.Value); 241 if (!(item.Value is IDeepCloneable)) return; 242 var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item); 237 243 DoDragDrop(data, DragDropEffects.Copy); 238 244 } … … 250 256 if (e.Effect == DragDropEffects.Copy) { 251 257 object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat); 252 var dc = item as IDeepCloneable; 253 if (dc != null) item = dc.Clone(); 254 255 string name; 258 259 string variableName; 260 object variableValue; 256 261 bool editLabel; 257 if (sender == variableListView) { 258 name = GenerateNewVariableName(variableListView.FocusedItem.Text, false); 259 editLabel = true; 262 if (item is KeyValuePair<string, object>) { 263 var variable = (KeyValuePair<string, object>)item; 264 variableName = GenerateNewVariableName(out editLabel, variable.Key, false); 265 variableValue = variable.Value; 260 266 } else { 267 var cloneable = item as IDeepCloneable; 268 variableValue = cloneable != null ? cloneable.Clone() : item; 269 261 270 var namedItem = item as INamedItem; 262 editLabel = namedItem == null || string.IsNullOrEmpty(namedItem.Name); 263 name = editLabel ? GenerateNewVariableName() : GenerateNewVariableName(namedItem.Name, false); 271 if (namedItem != null) 272 variableName = GenerateNewVariableName(out editLabel, namedItem.Name, false); 273 else 274 variableName = GenerateNewVariableName(out editLabel); 264 275 } 265 276 266 Content.Add(name, item); 267 var listViewItem = variableListView.FindItemWithText(name); 277 Content.Add(variableName, variableValue); 278 279 var listViewItem = variableListView.FindItemWithText(variableName); 268 280 variableListView.SelectedItems.Clear(); 269 281 if (editLabel) listViewItem.BeginEdit(); … … 289 301 #region Button Events 290 302 protected virtual void addButton_Click(object sender, EventArgs e) { 291 object newVar = CreateItem(); 292 if (newVar == null) return; 293 294 var namedItem = newVar as INamedItem; 295 bool editLabel = namedItem == null || string.IsNullOrEmpty(namedItem.Name); 296 string name = editLabel ? GenerateNewVariableName() : GenerateNewVariableName(namedItem.Name, false); 297 Content.Add(name, newVar); 298 299 var item = variableListView.FindItemWithText(name); 303 object variableValue = CreateItem(); 304 if (variableValue == null) return; 305 306 string variableName; 307 var namedItem = variableValue as INamedItem; 308 if (namedItem != null) 309 variableName = GenerateNewVariableName(namedItem.Name, false); 310 else 311 variableName = GenerateNewVariableName(); 312 313 Content.Add(variableName, variableValue); 314 315 var item = variableListView.FindItemWithText(variableName); 316 variableListView.SelectedItems.Clear(); 300 317 item.BeginEdit(); 301 318 } … … 364 381 item.SubItems[1].Text = value; 365 382 item.SubItems[2].Text = variable.Value.Value.GetType().ToString(); 366 item.ToolTipText = GetToolTipText(variable.Value, item.ImageIndex != 0);383 SetToolTipText(item, item.ImageIndex != 0); 367 384 } 368 385 } … … 381 398 } 382 399 383 private string GetToolTipText(KeyValuePair<string, object> variable, bool serializable) { 400 protected virtual void SetImageKey(ListViewItem listViewItem, bool serializable) { 401 var variable = (KeyValuePair<string, object>)listViewItem.Tag; 402 if (!serializable) listViewItem.ImageKey = ErrorImageName; 403 else if (!SafeVariableNameRegex.IsMatch(variable.Key)) listViewItem.ImageKey = WarningImageName; 404 else if (variable.Value is IItem) listViewItem.ImageKey = HeuristicLabObjectImageName; 405 else if (variable.Value != null) listViewItem.ImageKey = ObjectImageName; 406 else listViewItem.ImageKey = NothingImageName; 407 } 408 409 protected virtual void SetToolTipText(ListViewItem listViewItem, bool serializable) { 410 var variable = (KeyValuePair<string, object>)listViewItem.Tag; 384 411 if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable"); 385 412 string value = (variable.Value ?? "null").ToString(); … … 395 422 if (!serializable) 396 423 toolTipText = "Caution: Type is not serializable!" + Environment.NewLine + toolTipText; 397 returntoolTipText;424 listViewItem.ToolTipText = toolTipText; 398 425 } 399 426 400 427 private string GenerateNewVariableName(string defaultName = DefaultVariableName, bool generateValidIdentifier = true) { 401 if (generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName)) 428 bool editLabel; 429 return GenerateNewVariableName(out editLabel, defaultName, generateValidIdentifier); 430 } 431 432 private string GenerateNewVariableName(out bool defaultNameExists, string defaultName = DefaultVariableName, bool generateValidIdentifier = true) { 433 if (string.IsNullOrEmpty(defaultName) || generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName)) 402 434 defaultName = DefaultVariableName; 403 435 if (Content.ContainsKey(defaultName)) { … … 408 440 newName = string.Format(formatString, defaultName, i++); 409 441 } while (Content.ContainsKey(newName)); 442 defaultNameExists = true; 410 443 return newName; 411 444 } 445 defaultNameExists = false; 412 446 return defaultName; 413 447 }
Note: See TracChangeset
for help on using the changeset viewer.