1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text.RegularExpressions;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
33 | using HeuristicLab.Persistence.Core;
|
---|
34 | using HeuristicLab.Persistence.Default.Xml;
|
---|
35 | using HeuristicLab.PluginInfrastructure;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Scripting.Views {
|
---|
38 | [View("ItemCollection View")]
|
---|
39 | [Content(typeof(VariableStore), true)]
|
---|
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 |
|
---|
49 | protected readonly Dictionary<string, ListViewItem> itemListViewItemMapping;
|
---|
50 | protected readonly Dictionary<Type, bool> serializableLookup;
|
---|
51 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
52 | protected bool validDragOperation;
|
---|
53 |
|
---|
54 | public new VariableStore Content {
|
---|
55 | get { return (VariableStore)base.Content; }
|
---|
56 | set { base.Content = value; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public ListView ItemsListView {
|
---|
60 | get { return variableListView; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public VariableStoreView() {
|
---|
64 | InitializeComponent();
|
---|
65 | itemListViewItemMapping = new Dictionary<string, ListViewItem>();
|
---|
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);
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected override void Dispose(bool disposing) {
|
---|
77 | if (disposing) {
|
---|
78 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
79 | if (components != null) components.Dispose();
|
---|
80 | }
|
---|
81 | base.Dispose(disposing);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void DeregisterContentEvents() {
|
---|
85 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
86 | Content.ItemsReplaced -= Content_ItemsReplaced;
|
---|
87 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
88 | Content.CollectionReset -= Content_CollectionReset;
|
---|
89 | base.DeregisterContentEvents();
|
---|
90 | }
|
---|
91 | protected override void RegisterContentEvents() {
|
---|
92 | base.RegisterContentEvents();
|
---|
93 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
94 | Content.ItemsReplaced += Content_ItemsReplaced;
|
---|
95 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
96 | Content.CollectionReset += Content_CollectionReset;
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected override void OnContentChanged() {
|
---|
100 | base.OnContentChanged();
|
---|
101 | variableListView.Items.Clear();
|
---|
102 | itemListViewItemMapping.Clear();
|
---|
103 | if (Content != null) {
|
---|
104 | Caption += " (" + Content.GetType().Name + ")";
|
---|
105 | foreach (var item in Content)
|
---|
106 | AddVariable(item);
|
---|
107 | AdjustListViewColumnSizes();
|
---|
108 | SortItemsListView(SortOrder.Ascending);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected override void SetEnabledStateOfControls() {
|
---|
113 | base.SetEnabledStateOfControls();
|
---|
114 | if (Content == null) {
|
---|
115 | addButton.Enabled = false;
|
---|
116 | sortAscendingButton.Enabled = false;
|
---|
117 | sortDescendingButton.Enabled = false;
|
---|
118 | removeButton.Enabled = false;
|
---|
119 | variableListView.LabelEdit = false;
|
---|
120 | } else {
|
---|
121 | bool enabled = !Locked && !ReadOnly;
|
---|
122 | addButton.Enabled = enabled;
|
---|
123 | sortAscendingButton.Enabled = variableListView.Items.Count > 1;
|
---|
124 | sortDescendingButton.Enabled = variableListView.Items.Count > 1;
|
---|
125 | removeButton.Enabled = enabled && variableListView.SelectedItems.Count > 0;
|
---|
126 | variableListView.LabelEdit = enabled;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected virtual object CreateItem() {
|
---|
131 | if (typeSelectorDialog == null) {
|
---|
132 | typeSelectorDialog = new TypeSelectorDialog { Caption = "Select Item" };
|
---|
133 | typeSelectorDialog.TypeSelector.Caption = "Available Items";
|
---|
134 | typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
138 | try {
|
---|
139 | return (object)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
140 | } catch (Exception ex) {
|
---|
141 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return null;
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected virtual void AddVariable(KeyValuePair<string, object> variable) {
|
---|
148 | if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
|
---|
149 | bool serializable = IsSerializable(variable);
|
---|
150 |
|
---|
151 | var listViewItem = new ListViewItem();
|
---|
152 | AssignVariableToListViewItem(listViewItem, variable);
|
---|
153 | SetImageKey(listViewItem, serializable);
|
---|
154 | SetToolTipText(listViewItem, serializable);
|
---|
155 | variableListView.Items.Add(listViewItem);
|
---|
156 |
|
---|
157 | itemListViewItemMapping[variable.Key] = listViewItem;
|
---|
158 | sortAscendingButton.Enabled = variableListView.Items.Count > 1;
|
---|
159 | sortDescendingButton.Enabled = variableListView.Items.Count > 1;
|
---|
160 | var item = variable.Value as IItem;
|
---|
161 | if (item != null) item.ToStringChanged += item_ToStringChanged;
|
---|
162 | }
|
---|
163 |
|
---|
164 | protected virtual void RemoveVariable(KeyValuePair<string, object> variable) {
|
---|
165 | if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
|
---|
166 |
|
---|
167 | ListViewItem listViewItem;
|
---|
168 | if (!itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) return;
|
---|
169 |
|
---|
170 | itemListViewItemMapping.Remove(variable.Key);
|
---|
171 | variableListView.Items.Remove(listViewItem);
|
---|
172 | sortAscendingButton.Enabled = variableListView.Items.Count > 1;
|
---|
173 | sortDescendingButton.Enabled = variableListView.Items.Count > 1;
|
---|
174 | var item = variable.Value as IItem;
|
---|
175 | if (item != null) item.ToStringChanged -= item_ToStringChanged;
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected virtual void UpdateVariable(KeyValuePair<string, object> variable) {
|
---|
179 | if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
|
---|
180 |
|
---|
181 | ListViewItem listViewItem;
|
---|
182 | if (!itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem))
|
---|
183 | throw new ArgumentException("A variable with the specified name does not exist.", "variable");
|
---|
184 |
|
---|
185 | bool serializable = IsSerializable(variable);
|
---|
186 | AssignVariableToListViewItem(listViewItem, variable);
|
---|
187 | SetImageKey(listViewItem, serializable);
|
---|
188 | SetToolTipText(listViewItem, serializable);
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | #region ListView Events
|
---|
193 | protected virtual void variableListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
194 | removeButton.Enabled = (Content != null) && !Locked && !ReadOnly && variableListView.SelectedItems.Count > 0;
|
---|
195 | AdjustListViewColumnSizes();
|
---|
196 | }
|
---|
197 | protected virtual void variableListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
198 | switch (e.KeyCode) {
|
---|
199 | case Keys.Delete:
|
---|
200 | if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) {
|
---|
201 | foreach (ListViewItem item in variableListView.SelectedItems)
|
---|
202 | Content.Remove(item.Text);
|
---|
203 | }
|
---|
204 | break;
|
---|
205 | case Keys.F2:
|
---|
206 | if (variableListView.SelectedItems.Count != 1) return;
|
---|
207 | var selectedItem = variableListView.SelectedItems[0];
|
---|
208 | if (variableListView.LabelEdit)
|
---|
209 | selectedItem.BeginEdit();
|
---|
210 | break;
|
---|
211 | case Keys.A:
|
---|
212 | if (e.Modifiers.HasFlag(Keys.Control)) {
|
---|
213 | foreach (ListViewItem item in variableListView.Items)
|
---|
214 | item.Selected = true;
|
---|
215 | }
|
---|
216 | break;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | protected virtual void variableListView_DoubleClick(object sender, EventArgs e) {
|
---|
220 | if (variableListView.SelectedItems.Count != 1) return;
|
---|
221 | var item = variableListView.SelectedItems[0].Tag as KeyValuePair<string, object>?;
|
---|
222 | if (item == null) return;
|
---|
223 |
|
---|
224 | var value = item.Value.Value as IContent;
|
---|
225 | if (value == null) return;
|
---|
226 |
|
---|
227 | IContentView view = MainFormManager.MainForm.ShowContent(value);
|
---|
228 | if (view == null) return;
|
---|
229 |
|
---|
230 | view.ReadOnly = ReadOnly;
|
---|
231 | view.Locked = Locked;
|
---|
232 | }
|
---|
233 | protected virtual void variableListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
234 | if (Locked || variableListView.SelectedItems.Count != 1) return;
|
---|
235 |
|
---|
236 | var listViewItem = variableListView.SelectedItems[0];
|
---|
237 | var item = (KeyValuePair<string, object>)listViewItem.Tag;
|
---|
238 | if (!(item.Value is IDeepCloneable)) return;
|
---|
239 | var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item);
|
---|
240 | DoDragDrop(data, DragDropEffects.Copy);
|
---|
241 | }
|
---|
242 | protected virtual void variableListView_DragEnter(object sender, DragEventArgs e) {
|
---|
243 | validDragOperation = !Locked && !ReadOnly && e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null;
|
---|
244 | }
|
---|
245 | protected virtual void variableListView_DragOver(object sender, DragEventArgs e) {
|
---|
246 | e.Effect = DragDropEffects.None;
|
---|
247 | if (validDragOperation) {
|
---|
248 | if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | protected virtual void variableListView_DragDrop(object sender, DragEventArgs e) {
|
---|
252 | if (e.Effect != DragDropEffects.Copy) return;
|
---|
253 | object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
254 |
|
---|
255 | string variableName;
|
---|
256 | bool editLabel;
|
---|
257 |
|
---|
258 | if (item is KeyValuePair<string, object>) {
|
---|
259 | var variable = (KeyValuePair<string, object>)item;
|
---|
260 | variableName = GenerateNewVariableName(out editLabel, variable.Key, false);
|
---|
261 | item = variable.Value;
|
---|
262 | } else {
|
---|
263 | var namedItem = item as INamedItem;
|
---|
264 | if (namedItem != null)
|
---|
265 | variableName = GenerateNewVariableName(out editLabel, namedItem.Name, false);
|
---|
266 | else
|
---|
267 | variableName = GenerateNewVariableName(out editLabel);
|
---|
268 | }
|
---|
269 |
|
---|
270 | var cloneable = item as IDeepCloneable;
|
---|
271 | if (cloneable != null) item = cloneable.Clone();
|
---|
272 |
|
---|
273 | Content.Add(variableName, item);
|
---|
274 |
|
---|
275 | var listViewItem = variableListView.FindItemWithText(variableName);
|
---|
276 | variableListView.SelectedItems.Clear();
|
---|
277 | if (editLabel) listViewItem.BeginEdit();
|
---|
278 | }
|
---|
279 |
|
---|
280 | private readonly Regex SafeVariableNameRegex = new Regex("^[@]?[_a-zA-Z][_a-zA-Z0-9]*$");
|
---|
281 | private const string DefaultVariableName = "enter_name";
|
---|
282 |
|
---|
283 | private void variableListView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
|
---|
284 | string name = e.Label;
|
---|
285 | if (!string.IsNullOrEmpty(name)) {
|
---|
286 | var variable = (KeyValuePair<string, object>)variableListView.Items[e.Item].Tag;
|
---|
287 | if (!Content.ContainsKey(name)) {
|
---|
288 | Content.Remove(variable.Key);
|
---|
289 | Content.Add(name, variable.Value);
|
---|
290 | }
|
---|
291 | }
|
---|
292 | e.CancelEdit = true;
|
---|
293 | }
|
---|
294 | #endregion
|
---|
295 |
|
---|
296 | #region Button Events
|
---|
297 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
298 | object variableValue = CreateItem();
|
---|
299 | if (variableValue == null) return;
|
---|
300 |
|
---|
301 | string variableName;
|
---|
302 | var namedItem = variableValue as INamedItem;
|
---|
303 | if (namedItem != null)
|
---|
304 | variableName = GenerateNewVariableName(namedItem.Name, false);
|
---|
305 | else
|
---|
306 | variableName = GenerateNewVariableName();
|
---|
307 |
|
---|
308 | Content.Add(variableName, variableValue);
|
---|
309 |
|
---|
310 | var item = variableListView.FindItemWithText(variableName);
|
---|
311 | variableListView.SelectedItems.Clear();
|
---|
312 | item.BeginEdit();
|
---|
313 | }
|
---|
314 | protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
315 | SortItemsListView(SortOrder.Ascending);
|
---|
316 | }
|
---|
317 | protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
318 | SortItemsListView(SortOrder.Descending);
|
---|
319 | }
|
---|
320 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
321 | if (variableListView.SelectedItems.Count > 0) {
|
---|
322 | foreach (ListViewItem item in variableListView.SelectedItems)
|
---|
323 | Content.Remove(item.Text);
|
---|
324 | variableListView.SelectedItems.Clear();
|
---|
325 | }
|
---|
326 | }
|
---|
327 | #endregion
|
---|
328 |
|
---|
329 | #region Content Events
|
---|
330 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
|
---|
331 | if (InvokeRequired)
|
---|
332 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsAdded), sender, e);
|
---|
333 | else {
|
---|
334 | foreach (var item in e.Items)
|
---|
335 | AddVariable(item);
|
---|
336 | AdjustListViewColumnSizes();
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
|
---|
341 | if (InvokeRequired)
|
---|
342 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsReplaced), sender, e);
|
---|
343 | else {
|
---|
344 | foreach (var item in e.Items)
|
---|
345 | UpdateVariable(item);
|
---|
346 | AdjustListViewColumnSizes();
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
|
---|
351 | if (InvokeRequired)
|
---|
352 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsRemoved), sender, e);
|
---|
353 | else {
|
---|
354 | foreach (var item in e.Items)
|
---|
355 | RemoveVariable(item);
|
---|
356 | AdjustListViewColumnSizes();
|
---|
357 | }
|
---|
358 | }
|
---|
359 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
|
---|
360 | if (InvokeRequired)
|
---|
361 | Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_CollectionReset), sender, e);
|
---|
362 | else {
|
---|
363 | foreach (var item in e.OldItems)
|
---|
364 | RemoveVariable(item);
|
---|
365 | foreach (var item in e.Items)
|
---|
366 | AddVariable(item);
|
---|
367 | AdjustListViewColumnSizes();
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | private void item_ToStringChanged(object sender, EventArgs e) {
|
---|
372 | foreach (ListViewItem item in variableListView.Items) {
|
---|
373 | var variable = item.Tag as KeyValuePair<string, object>?;
|
---|
374 | if (variable == null || variable.Value.Value != sender) continue;
|
---|
375 |
|
---|
376 | string value = (variable.Value.Value ?? "null").ToString();
|
---|
377 | item.SubItems[1].Text = value;
|
---|
378 | item.SubItems[2].Text = variable.Value.Value.GetType().ToString();
|
---|
379 | SetToolTipText(item, item.ImageIndex != 0);
|
---|
380 | }
|
---|
381 | }
|
---|
382 | #endregion
|
---|
383 |
|
---|
384 | #region Helpers
|
---|
385 | protected virtual void SortItemsListView(SortOrder sortOrder) {
|
---|
386 | variableListView.Sorting = SortOrder.None;
|
---|
387 | variableListView.Sorting = sortOrder;
|
---|
388 | variableListView.Sorting = SortOrder.None;
|
---|
389 | }
|
---|
390 | protected virtual void AdjustListViewColumnSizes() {
|
---|
391 | foreach (ColumnHeader ch in variableListView.Columns)
|
---|
392 | ch.Width = -2;
|
---|
393 | }
|
---|
394 |
|
---|
395 | protected virtual void AssignVariableToListViewItem(ListViewItem listViewItem, KeyValuePair<string, object> variable) {
|
---|
396 | string value = (variable.Value ?? "null").ToString();
|
---|
397 | string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
|
---|
398 |
|
---|
399 | listViewItem.Tag = variable;
|
---|
400 |
|
---|
401 | var subItems = listViewItem.SubItems;
|
---|
402 | subItems[0].Text = variable.Key;
|
---|
403 | if (subItems.Count == 1) { // variable information is added; subitems do not exist yet
|
---|
404 | subItems.AddRange(new[] { value, type });
|
---|
405 | } else { // variable information is updated; subitems are changed
|
---|
406 | subItems[1].Text = value;
|
---|
407 | subItems[2].Text = type;
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | protected virtual void SetImageKey(ListViewItem listViewItem, bool serializable) {
|
---|
412 | var variable = (KeyValuePair<string, object>)listViewItem.Tag;
|
---|
413 | if (!serializable) listViewItem.ImageKey = ErrorImageName;
|
---|
414 | else if (!SafeVariableNameRegex.IsMatch(variable.Key)) listViewItem.ImageKey = WarningImageName;
|
---|
415 | else if (variable.Value is IItem) listViewItem.ImageKey = HeuristicLabObjectImageName;
|
---|
416 | else if (variable.Value != null) listViewItem.ImageKey = ObjectImageName;
|
---|
417 | else listViewItem.ImageKey = NothingImageName;
|
---|
418 | }
|
---|
419 |
|
---|
420 | protected virtual void SetToolTipText(ListViewItem listViewItem, bool serializable) {
|
---|
421 | var variable = (KeyValuePair<string, object>)listViewItem.Tag;
|
---|
422 | if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
|
---|
423 | string value = listViewItem.SubItems[1].Text;
|
---|
424 | string type = listViewItem.SubItems[2].Text;
|
---|
425 |
|
---|
426 | string[] lines = {
|
---|
427 | "Name: " + variable.Key,
|
---|
428 | "Value: " + value,
|
---|
429 | "Type: " + type
|
---|
430 | };
|
---|
431 |
|
---|
432 | string toolTipText = string.Join(Environment.NewLine, lines);
|
---|
433 | if (!SafeVariableNameRegex.IsMatch(variable.Key))
|
---|
434 | toolTipText = "Caution: Identifier is no valid C# identifier!" + Environment.NewLine + toolTipText;
|
---|
435 | if (!serializable)
|
---|
436 | toolTipText = "Caution: Type is not serializable!" + Environment.NewLine + toolTipText;
|
---|
437 | listViewItem.ToolTipText = toolTipText;
|
---|
438 | }
|
---|
439 |
|
---|
440 | private string GenerateNewVariableName(string defaultName = DefaultVariableName, bool generateValidIdentifier = true) {
|
---|
441 | bool editLabel;
|
---|
442 | return GenerateNewVariableName(out editLabel, defaultName, generateValidIdentifier);
|
---|
443 | }
|
---|
444 |
|
---|
445 | private string GenerateNewVariableName(out bool defaultNameExists, string defaultName = DefaultVariableName, bool generateValidIdentifier = true) {
|
---|
446 | if (string.IsNullOrEmpty(defaultName) || generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName))
|
---|
447 | defaultName = DefaultVariableName;
|
---|
448 | if (Content.ContainsKey(defaultName)) {
|
---|
449 | int i = 1;
|
---|
450 | string formatString = generateValidIdentifier ? "{0}{1}" : "{0} ({1})";
|
---|
451 | string newName;
|
---|
452 | do {
|
---|
453 | newName = string.Format(formatString, defaultName, i++);
|
---|
454 | } while (Content.ContainsKey(newName));
|
---|
455 | defaultNameExists = true;
|
---|
456 | return newName;
|
---|
457 | }
|
---|
458 | defaultNameExists = false;
|
---|
459 | return defaultName;
|
---|
460 | }
|
---|
461 |
|
---|
462 | private bool IsSerializable(KeyValuePair<string, object> variable) {
|
---|
463 | Type type = null;
|
---|
464 | bool serializable;
|
---|
465 |
|
---|
466 | if (variable.Value != null) {
|
---|
467 | type = variable.Value.GetType();
|
---|
468 | if (serializableLookup.TryGetValue(type, out serializable))
|
---|
469 | return serializable;
|
---|
470 | }
|
---|
471 |
|
---|
472 | var ser = new Serializer(variable, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()), "ROOT", true);
|
---|
473 | try {
|
---|
474 | serializable = ser.Count() > 0; // try to create all serialization tokens
|
---|
475 | } catch (PersistenceException) {
|
---|
476 | serializable = false;
|
---|
477 | }
|
---|
478 |
|
---|
479 | if (type != null)
|
---|
480 | serializableLookup[type] = serializable;
|
---|
481 | return serializable;
|
---|
482 | }
|
---|
483 | #endregion
|
---|
484 | }
|
---|
485 | }
|
---|