- Timestamp:
- 11/27/14 11:23:37 (10 years ago)
- Location:
- branches/Breadcrumbs
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/Breadcrumbs
- Property svn:ignore
-
old new 8 8 FxCopResults.txt 9 9 Google.ProtocolBuffers-0.9.1.dll 10 Google.ProtocolBuffers-2.4.1.473.dll 10 11 HeuristicLab 3.3.5.1.ReSharper.user 11 12 HeuristicLab 3.3.6.0.ReSharper.user 12 13 HeuristicLab.4.5.resharper.user 13 14 HeuristicLab.ExtLibs.6.0.ReSharper.user 15 HeuristicLab.Scripting.Development 14 16 HeuristicLab.resharper.user 15 17 ProtoGen.exe … … 17 19 _ReSharper.HeuristicLab 18 20 _ReSharper.HeuristicLab 3.3 21 _ReSharper.HeuristicLab 3.3 Tests 19 22 _ReSharper.HeuristicLab.ExtLibs 20 23 bin 21 24 protoc.exe 22 _ReSharper.HeuristicLab 3.3 Tests23 Google.ProtocolBuffers-2.4.1.473.dll
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/Breadcrumbs/HeuristicLab.Scripting.Views/3.3/VariableStoreView.cs
r11014 r11594 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 { 42 protected Dictionary<string, ListViewItem> itemListViewItemMapping; 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 49 protected readonly Dictionary<string, ListViewItem> itemListViewItemMapping; 50 protected readonly Dictionary<Type, bool> serializableLookup; 43 51 protected TypeSelectorDialog typeSelectorDialog; 44 52 protected bool validDragOperation; … … 56 64 InitializeComponent(); 57 65 itemListViewItemMapping = new Dictionary<string, ListViewItem>(); 58 variableListView.SmallImageList.Images.AddRange(new Image[] { 59 HeuristicLab.Common.Resources.VSImageLibrary.Error, 60 HeuristicLab.Common.Resources.VSImageLibrary.Object, 61 HeuristicLab.Common.Resources.VSImageLibrary.Nothing 62 }); 66 serializableLookup = new Dictionary<Type, bool>(); 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); 63 74 } 64 75 … … 90 101 variableListView.Items.Clear(); 91 102 itemListViewItemMapping.Clear(); 92 RebuildImageList();93 103 if (Content != null) { 94 104 Caption += " (" + Content.GetType().Name + ")"; … … 109 119 variableListView.Enabled = false; 110 120 } else { 111 addButton.Enabled = !Locked && !ReadOnly; 121 bool enabled = !Locked && !ReadOnly; 122 addButton.Enabled = enabled; 112 123 sortAscendingButton.Enabled = variableListView.Items.Count > 1; 113 124 sortDescendingButton.Enabled = variableListView.Items.Count > 1; 114 removeButton.Enabled = !Locked && !ReadOnly&& variableListView.SelectedItems.Count > 0;115 variableListView.Enabled = true;116 variableListView.LabelEdit = !Locked && !ReadOnly;125 removeButton.Enabled = enabled && variableListView.SelectedItems.Count > 0; 126 variableListView.Enabled = enabled; 127 variableListView.LabelEdit = enabled; 117 128 } 118 129 } … … 137 148 protected virtual void AddVariable(KeyValuePair<string, object> variable) { 138 149 if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable"); 139 string value = (variable.Value ?? "null").ToString();140 string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();141 150 bool serializable = IsSerializable(variable); 142 var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { ToolTipText = GetToolTipText(variable, serializable), Tag = variable }; 143 if (serializable) { 144 listViewItem.ImageIndex = variable.Value == null ? 2 : 1; 145 } else listViewItem.ImageIndex = 0; 151 152 var listViewItem = new ListViewItem(); 153 AssignVariableToListViewItem(listViewItem, variable); 154 SetImageKey(listViewItem, serializable); 155 SetToolTipText(listViewItem, serializable); 146 156 variableListView.Items.Add(listViewItem); 157 147 158 itemListViewItemMapping[variable.Key] = listViewItem; 148 159 sortAscendingButton.Enabled = variableListView.Items.Count > 1; … … 154 165 protected virtual void RemoveVariable(KeyValuePair<string, object> variable) { 155 166 if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable"); 167 156 168 ListViewItem listViewItem; 157 if ( itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) {158 itemListViewItemMapping.Remove(variable.Key); 159 variableListView.Items.Remove(listViewItem);160 sortAscendingButton.Enabled = variableListView.Items.Count > 1;161 sortDescendingButton.Enabled = variableListView.Items.Count > 1;162 var item = variable.Value as IItem;163 if (item != null) item.ToStringChanged -= item_ToStringChanged;164 }169 if (!itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) return; 170 171 itemListViewItemMapping.Remove(variable.Key); 172 variableListView.Items.Remove(listViewItem); 173 sortAscendingButton.Enabled = variableListView.Items.Count > 1; 174 sortDescendingButton.Enabled = variableListView.Items.Count > 1; 175 var item = variable.Value as IItem; 176 if (item != null) item.ToStringChanged -= item_ToStringChanged; 165 177 } 166 178 167 179 protected virtual void UpdateVariable(KeyValuePair<string, object> variable) { 168 180 if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable"); 181 169 182 ListViewItem listViewItem; 170 if (itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) { 171 string value = (variable.Value ?? "null").ToString(); 172 string type = variable.Value == null ? "null" : variable.Value.GetType().ToString(); 173 bool serializable = IsSerializable(variable); 174 if (serializable) { 175 listViewItem.ImageIndex = variable.Value == null ? 2 : 1; 176 } else listViewItem.ImageIndex = 0; 177 listViewItem.SubItems[1].Text = value; 178 listViewItem.SubItems[2].Text = type; 179 listViewItem.ToolTipText = GetToolTipText(variable, serializable); 180 listViewItem.Tag = variable; 181 } else throw new ArgumentException("A variable with the specified name does not exist.", "variable"); 183 if (!itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) 184 throw new ArgumentException("A variable with the specified name does not exist.", "variable"); 185 186 bool serializable = IsSerializable(variable); 187 AssignVariableToListViewItem(listViewItem, variable); 188 SetImageKey(listViewItem, serializable); 189 SetToolTipText(listViewItem, serializable); 190 182 191 } 183 192 … … 188 197 } 189 198 protected virtual void variableListView_KeyDown(object sender, KeyEventArgs e) { 190 if (e.KeyCode == Keys.Delete) { 191 if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) { 192 foreach (ListViewItem item in variableListView.SelectedItems) 193 Content.Remove(item.Text); 194 } 199 switch (e.KeyCode) { 200 case Keys.Delete: 201 if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) { 202 foreach (ListViewItem item in variableListView.SelectedItems) 203 Content.Remove(item.Text); 204 } 205 break; 206 case Keys.F2: 207 if (variableListView.SelectedItems.Count != 1) return; 208 var selectedItem = variableListView.SelectedItems[0]; 209 if (variableListView.LabelEdit) 210 selectedItem.BeginEdit(); 211 break; 212 case Keys.A: 213 if (e.Modifiers.HasFlag(Keys.Control)) { 214 foreach (ListViewItem item in variableListView.Items) 215 item.Selected = true; 216 } 217 break; 195 218 } 196 219 } 197 220 protected virtual void variableListView_DoubleClick(object sender, EventArgs e) { 198 if (variableListView.SelectedItems.Count == 1) { 199 var item = variableListView.SelectedItems[0].Tag as KeyValuePair<string, object>?; 200 if (item != null) { 201 var value = item.Value.Value as IContent; 202 if (value != null) { 203 IContentView view = MainFormManager.MainForm.ShowContent(value); 204 if (view != null) { 205 view.ReadOnly = ReadOnly; 206 view.Locked = Locked; 207 } 208 } 209 } 210 } 221 if (variableListView.SelectedItems.Count != 1) return; 222 var item = variableListView.SelectedItems[0].Tag as KeyValuePair<string, object>?; 223 if (item == null) return; 224 225 var value = item.Value.Value as IContent; 226 if (value == null) return; 227 228 IContentView view = MainFormManager.MainForm.ShowContent(value); 229 if (view == null) return; 230 231 view.ReadOnly = ReadOnly; 232 view.Locked = Locked; 211 233 } 212 234 protected virtual void variableListView_ItemDrag(object sender, ItemDragEventArgs e) { 213 if (!Locked) { 214 var items = new List<object>(); 215 foreach (ListViewItem listViewItem in variableListView.SelectedItems) { 216 var item = (KeyValuePair<string, object>)listViewItem.Tag as KeyValuePair<string, object>?; 217 if (item != null) items.Add(item.Value.Value); 218 } 219 220 if (items.Count > 0) { 221 var data = new DataObject(); 222 if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]); 223 else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items); 224 if (ReadOnly) { 225 DoDragDrop(data, DragDropEffects.Copy); 226 } else { 227 var result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move); 228 if ((result & DragDropEffects.Move) == DragDropEffects.Move) { 229 foreach (string item in items) Content.Remove(item); 230 } 231 } 232 } 233 } 235 if (Locked || variableListView.SelectedItems.Count != 1) return; 236 237 var listViewItem = variableListView.SelectedItems[0]; 238 var item = (KeyValuePair<string, object>)listViewItem.Tag; 239 if (!(item.Value is IDeepCloneable)) return; 240 var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item); 241 DoDragDrop(data, DragDropEffects.Copy); 234 242 } 235 243 protected virtual void variableListView_DragEnter(object sender, DragEventArgs e) { … … 239 247 e.Effect = DragDropEffects.None; 240 248 if (validDragOperation) { 241 if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key 242 else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key 243 else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy; 244 else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move; 245 else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link; 249 if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy; 246 250 } 247 251 } 248 252 protected virtual void variableListView_DragDrop(object sender, DragEventArgs e) { 249 if (e.Effect != DragDropEffects.None) { 250 object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat); 251 if (e.Effect.HasFlag(DragDropEffects.Copy)) { 252 var cloner = new Cloner(); 253 var dc = item as IDeepCloneable; 254 if (dc != null) item = cloner.Clone(dc); 255 } 256 string name = GenerateNewVariableName(); 257 Content.Add(name, item); 258 var listViewItem = variableListView.FindItemWithText(name); 259 listViewItem.BeginEdit(); 260 } 253 if (e.Effect != DragDropEffects.Copy) return; 254 object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat); 255 256 string variableName; 257 bool editLabel; 258 259 if (item is KeyValuePair<string, object>) { 260 var variable = (KeyValuePair<string, object>)item; 261 variableName = GenerateNewVariableName(out editLabel, variable.Key, false); 262 item = variable.Value; 263 } else { 264 var namedItem = item as INamedItem; 265 if (namedItem != null) 266 variableName = GenerateNewVariableName(out editLabel, namedItem.Name, false); 267 else 268 variableName = GenerateNewVariableName(out editLabel); 269 } 270 271 var cloneable = item as IDeepCloneable; 272 if (cloneable != null) item = cloneable.Clone(); 273 274 Content.Add(variableName, item); 275 276 var listViewItem = variableListView.FindItemWithText(variableName); 277 variableListView.SelectedItems.Clear(); 278 if (editLabel) listViewItem.BeginEdit(); 261 279 } 262 280 263 281 private readonly Regex SafeVariableNameRegex = new Regex("^[@]?[_a-zA-Z][_a-zA-Z0-9]*$"); 282 private const string DefaultVariableName = "enter_name"; 283 264 284 private void variableListView_AfterLabelEdit(object sender, LabelEditEventArgs e) { 265 285 string name = e.Label; 266 if (!string.IsNullOrEmpty(name) && SafeVariableNameRegex.IsMatch(name)) {286 if (!string.IsNullOrEmpty(name)) { 267 287 var variable = (KeyValuePair<string, object>)variableListView.Items[e.Item].Tag; 268 288 if (!Content.ContainsKey(name)) { … … 277 297 #region Button Events 278 298 protected virtual void addButton_Click(object sender, EventArgs e) { 279 object newVar = CreateItem(); 280 if (newVar != null) { 281 string name = GenerateNewVariableName(); 282 Content.Add(name, newVar); 283 var item = variableListView.FindItemWithText(name); 284 item.BeginEdit(); 285 } 299 object variableValue = CreateItem(); 300 if (variableValue == null) return; 301 302 string variableName; 303 var namedItem = variableValue as INamedItem; 304 if (namedItem != null) 305 variableName = GenerateNewVariableName(namedItem.Name, false); 306 else 307 variableName = GenerateNewVariableName(); 308 309 Content.Add(variableName, variableValue); 310 311 var item = variableListView.FindItemWithText(variableName); 312 variableListView.SelectedItems.Clear(); 313 item.BeginEdit(); 286 314 } 287 315 protected virtual void sortAscendingButton_Click(object sender, EventArgs e) { … … 327 355 foreach (var item in e.Items) 328 356 RemoveVariable(item); 329 RebuildImageList();330 357 AdjustListViewColumnSizes(); 331 358 } … … 337 364 foreach (var item in e.OldItems) 338 365 RemoveVariable(item); 339 RebuildImageList();340 366 foreach (var item in e.Items) 341 367 AddVariable(item); … … 347 373 foreach (ListViewItem item in variableListView.Items) { 348 374 var variable = item.Tag as KeyValuePair<string, object>?; 349 if (variable != null && variable.Value.Value == sender) { 350 string value = (variable.Value.Value ?? "null").ToString(); 351 item.SubItems[1].Text = value; 352 item.SubItems[2].Text = variable.Value.Value.GetType().ToString(); 353 item.ToolTipText = GetToolTipText(variable.Value, item.ImageIndex != 0); 354 return; 355 } 375 if (variable == null || variable.Value.Value != sender) continue; 376 377 string value = (variable.Value.Value ?? "null").ToString(); 378 item.SubItems[1].Text = value; 379 item.SubItems[2].Text = variable.Value.Value.GetType().ToString(); 380 SetToolTipText(item, item.ImageIndex != 0); 356 381 } 357 382 } … … 368 393 ch.Width = -2; 369 394 } 370 protected virtual void RebuildImageList() { 371 variableListView.SmallImageList.Images.Clear(); 372 variableListView.SmallImageList.Images.AddRange(new Image[] { 373 HeuristicLab.Common.Resources.VSImageLibrary.Error, 374 HeuristicLab.Common.Resources.VSImageLibrary.Object, 375 HeuristicLab.Common.Resources.VSImageLibrary.Nothing 376 }); 377 foreach (ListViewItem listViewItem in variableListView.Items) { 378 var variable = (KeyValuePair<string, object>)listViewItem.Tag; 379 bool serializable = IsSerializable(variable); 380 if (serializable) { 381 listViewItem.ImageIndex = variable.Value == null ? 2 : 1; 382 } else listViewItem.ImageIndex = 0; 383 } 384 } 385 386 private string GetToolTipText(KeyValuePair<string, object> variable, bool serializable) { 387 if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable"); 395 396 protected virtual void AssignVariableToListViewItem(ListViewItem listViewItem, KeyValuePair<string, object> variable) { 388 397 string value = (variable.Value ?? "null").ToString(); 389 398 string type = variable.Value == null ? "null" : variable.Value.GetType().ToString(); 399 400 listViewItem.Tag = variable; 401 402 var subItems = listViewItem.SubItems; 403 subItems[0].Text = variable.Key; 404 if (subItems.Count == 1) { // variable information is added; subitems do not exist yet 405 subItems.AddRange(new[] { value, type }); 406 } else { // variable information is updated; subitems are changed 407 subItems[1].Text = value; 408 subItems[2].Text = type; 409 } 410 } 411 412 protected virtual void SetImageKey(ListViewItem listViewItem, bool serializable) { 413 var variable = (KeyValuePair<string, object>)listViewItem.Tag; 414 if (!serializable) listViewItem.ImageKey = ErrorImageName; 415 else if (!SafeVariableNameRegex.IsMatch(variable.Key)) listViewItem.ImageKey = WarningImageName; 416 else if (variable.Value is IItem) listViewItem.ImageKey = HeuristicLabObjectImageName; 417 else if (variable.Value != null) listViewItem.ImageKey = ObjectImageName; 418 else listViewItem.ImageKey = NothingImageName; 419 } 420 421 protected virtual void SetToolTipText(ListViewItem listViewItem, bool serializable) { 422 var variable = (KeyValuePair<string, object>)listViewItem.Tag; 423 if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable"); 424 string value = listViewItem.SubItems[1].Text; 425 string type = listViewItem.SubItems[2].Text; 426 390 427 string[] lines = { 391 428 "Name: " + variable.Key, … … 393 430 "Type: " + type 394 431 }; 432 395 433 string toolTipText = string.Join(Environment.NewLine, lines); 434 if (!SafeVariableNameRegex.IsMatch(variable.Key)) 435 toolTipText = "Caution: Identifier is no valid C# identifier!" + Environment.NewLine + toolTipText; 396 436 if (!serializable) 397 437 toolTipText = "Caution: Type is not serializable!" + Environment.NewLine + toolTipText; 398 return toolTipText; 399 } 400 401 private string GenerateNewVariableName(string defaultName = "enter_name") { 438 listViewItem.ToolTipText = toolTipText; 439 } 440 441 private string GenerateNewVariableName(string defaultName = DefaultVariableName, bool generateValidIdentifier = true) { 442 bool editLabel; 443 return GenerateNewVariableName(out editLabel, defaultName, generateValidIdentifier); 444 } 445 446 private string GenerateNewVariableName(out bool defaultNameExists, string defaultName = DefaultVariableName, bool generateValidIdentifier = true) { 447 if (string.IsNullOrEmpty(defaultName) || generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName)) 448 defaultName = DefaultVariableName; 402 449 if (Content.ContainsKey(defaultName)) { 403 450 int i = 1; 451 string formatString = generateValidIdentifier ? "{0}{1}" : "{0} ({1})"; 404 452 string newName; 405 453 do { 406 newName = defaultName + i++;454 newName = string.Format(formatString, defaultName, i++); 407 455 } while (Content.ContainsKey(newName)); 456 defaultNameExists = true; 408 457 return newName; 409 458 } 459 defaultNameExists = false; 410 460 return defaultName; 411 461 } 412 462 413 463 private bool IsSerializable(KeyValuePair<string, object> variable) { 464 Type type = null; 465 bool serializable; 466 467 if (variable.Value != null) { 468 type = variable.Value.GetType(); 469 if (serializableLookup.TryGetValue(type, out serializable)) 470 return serializable; 471 } 472 414 473 var ser = new Serializer(variable, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()), "ROOT", true); 415 474 try { 416 return ser.Count() > 0;475 serializable = ser.Count() > 0; // try to create all serialization tokens 417 476 } catch (PersistenceException) { 418 return false; 419 } 477 serializable = false; 478 } 479 480 if (type != null) 481 serializableLookup[type] = serializable; 482 return serializable; 420 483 } 421 484 #endregion
Note: See TracChangeset
for help on using the changeset viewer.