Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/Clipboard.cs @ 15304

Last change on this file since 15304 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 16.9 KB
RevLine 
[3292]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3292]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;
[5744]23using System.Collections;
[3298]24using System.Collections.Generic;
25using System.IO;
[11650]26using System.IO.Compression;
[3298]27using System.Linq;
28using System.Threading;
[3292]29using System.Windows.Forms;
[6527]30using HeuristicLab.Common;
[3292]31using HeuristicLab.MainForm;
[3298]32using HeuristicLab.Persistence.Default.Xml;
[3758]33using HeuristicLab.PluginInfrastructure;
[3292]34
35namespace HeuristicLab.Core.Views {
36  [View("Clipboard")]
[3571]37  public sealed partial class Clipboard<T> : HeuristicLab.MainForm.WindowsForms.Sidebar where T : class, IItem {
[3298]38    private TypeSelectorDialog typeSelectorDialog;
[5237]39    private Dictionary<T, ListViewItem> itemListViewItemMapping;
[5744]40    private bool validDragOperation;
41    private bool draggedItemsAlreadyContained;
[3298]42
43    private string itemsPath;
44    public string ItemsPath {
45      get { return itemsPath; }
46      private set {
47        if (string.IsNullOrEmpty(value)) throw new ArgumentException(string.Format("Invalid items path \"{0}\".", value));
48        itemsPath = value;
49        try {
50          if (!Directory.Exists(itemsPath)) {
51            Directory.CreateDirectory(itemsPath);
52            // directory creation might take some time -> wait until it is definitively created
53            while (!Directory.Exists(itemsPath)) {
54              Thread.Sleep(100);
55              Directory.CreateDirectory(itemsPath);
56            }
57          }
58        }
59        catch (Exception ex) {
60          throw new ArgumentException(string.Format("Invalid items path \"{0}\".", itemsPath), ex);
61        }
62      }
63    }
64
[3292]65    public Clipboard() {
66      InitializeComponent();
[3298]67      ItemsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
68                  Path.DirectorySeparatorChar + "HeuristicLab" + Path.DirectorySeparatorChar + "Clipboard";
[5237]69      itemListViewItemMapping = new Dictionary<T, ListViewItem>();
[3292]70    }
[3298]71    public Clipboard(string itemsPath) {
72      InitializeComponent();
73      ItemsPath = itemsPath;
[5237]74      itemListViewItemMapping = new Dictionary<T, ListViewItem>();
[3298]75    }
[3292]76
[5237]77    protected override void Dispose(bool disposing) {
78      if (disposing) {
79        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
80        foreach (T item in itemListViewItemMapping.Keys) {
81          item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
82          item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
83        }
84        if (components != null) components.Dispose();
85      }
86      base.Dispose(disposing);
87    }
88
[3292]89    protected override void OnInitialized(EventArgs e) {
90      base.OnInitialized(e);
[3362]91      SetEnabledStateOfControls();
[3298]92      Enabled = false;
93      infoLabel.Text = "Loading ...";
94      progressBar.Value = 0;
95      infoPanel.Visible = true;
96      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadItems));
[3292]97    }
[3298]98
[3904]99    protected override void SetEnabledStateOfControls() {
100      base.SetEnabledStateOfControls();
[3362]101      addButton.Enabled = !ReadOnly;
102      removeButton.Enabled = !ReadOnly && listView.SelectedItems.Count > 0;
103      saveButton.Enabled = !ReadOnly;
104    }
105
[3298]106    public void AddItem(T item) {
107      if (InvokeRequired)
108        Invoke(new Action<T>(AddItem), item);
109      else {
[5237]110        if (item == null) throw new ArgumentNullException("item", "Cannot add null item to clipboard.");
111        if (!itemListViewItemMapping.ContainsKey(item)) {
[3298]112          ListViewItem listViewItem = new ListViewItem(item.ToString());
113          listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
[3341]114          listView.SmallImageList.Images.Add(item.ItemImage);
115          listViewItem.ImageIndex = listView.SmallImageList.Images.Count - 1;
[3298]116          listViewItem.Tag = item;
117          listView.Items.Add(listViewItem);
[5237]118          itemListViewItemMapping.Add(item, listViewItem);
[3341]119          item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
[3298]120          item.ToStringChanged += new EventHandler(Item_ToStringChanged);
121          sortAscendingButton.Enabled = sortDescendingButton.Enabled = listView.Items.Count > 1;
[3299]122          AdjustListViewColumnSizes();
[3298]123        }
124      }
125    }
[3341]126
[3298]127    private void RemoveItem(T item) {
128      if (InvokeRequired)
129        Invoke(new Action<T>(RemoveItem), item);
130      else {
[5237]131        if (itemListViewItemMapping.ContainsKey(item)) {
[3341]132          item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
[3298]133          item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
[5237]134          ListViewItem listViewItem = itemListViewItemMapping[item];
[3341]135          listViewItem.Remove();
[5237]136          itemListViewItemMapping.Remove(item);
[3298]137          sortAscendingButton.Enabled = sortDescendingButton.Enabled = listView.Items.Count > 1;
138        }
139      }
140    }
141    private void Save() {
142      if (InvokeRequired)
143        Invoke(new Action(Save));
144      else {
145        Enabled = false;
146        infoLabel.Text = "Saving ...";
147        progressBar.Value = 0;
148        infoPanel.Visible = true;
149        ThreadPool.QueueUserWorkItem(new WaitCallback(SaveItems));
150      }
151    }
152
153    #region Loading/Saving Items
154    private void LoadItems(object state) {
155      string[] items = Directory.GetFiles(ItemsPath);
156      foreach (string filename in items) {
157        try {
158          T item = XmlParser.Deserialize<T>(filename);
159          OnItemLoaded(item, progressBar.Maximum / items.Length);
160        }
161        catch (Exception) { }
162      }
163      OnAllItemsLoaded();
164    }
165    private void OnItemLoaded(T item, int progress) {
166      if (InvokeRequired)
167        Invoke(new Action<T, int>(OnItemLoaded), item, progress);
168      else {
169        AddItem(item);
170        progressBar.Value += progress;
171      }
172    }
173    private void OnAllItemsLoaded() {
174      if (InvokeRequired)
175        Invoke(new Action(OnAllItemsLoaded));
176      else {
177        Enabled = true;
178        if (listView.Items.Count > 0) {
179          for (int i = 0; i < listView.Columns.Count; i++)
180            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
181        }
182        infoPanel.Visible = false;
183      }
184    }
185    private void SaveItems(object param) {
186      Directory.Delete(ItemsPath, true);
187      Directory.CreateDirectory(ItemsPath);
188      // directory creation might take some time -> wait until it is definitively created
189      while (!Directory.Exists(ItemsPath)) {
190        Thread.Sleep(100);
191        Directory.CreateDirectory(ItemsPath);
192      }
193
194      int i = 0;
[5237]195      T[] items = GetStorableItems(itemListViewItemMapping.Keys);
[4447]196
[3298]197      foreach (T item in items) {
198        try {
199          i++;
[4435]200          SetEnabledStateOfContentViews(item, false);
[11650]201          XmlGenerator.Serialize(item, ItemsPath + Path.DirectorySeparatorChar + i.ToString("00000000") + ".hl", CompressionLevel.Optimal);
[3298]202          OnItemSaved(item, progressBar.Maximum / listView.Items.Count);
203        }
204        catch (Exception) { }
[4447]205        finally {
206          SetEnabledStateOfContentViews(item, true);
207        }
[3298]208      }
209      OnAllItemsSaved();
210    }
[4453]211
[3298]212    private void OnItemSaved(T item, int progress) {
213      if (item != null) {
214        if (InvokeRequired)
[4435]215          Invoke(new Action<T, int>(OnItemSaved), item, progress);
216        else {
[3298]217          progressBar.Value += progress;
[4435]218        }
[3298]219      }
220    }
221    private void OnAllItemsSaved() {
222      if (InvokeRequired)
223        Invoke(new Action(OnAllItemsLoaded));
224      else {
225        Enabled = true;
226        infoPanel.Visible = false;
227      }
228    }
[4435]229
230    private void SetEnabledStateOfContentViews(IItem item, bool enabled) {
231      if (InvokeRequired)
232        Invoke((Action<IItem, bool>)SetEnabledStateOfContentViews, item, enabled);
233      else {
234        var views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == item).ToList();
235        views.ForEach(v => v.Enabled = enabled);
236      }
237    }
[4453]238
239    private static T[] GetStorableItems(IEnumerable<T> items) {
240      var query = from item in items
241                  let executeable = item as IExecutable
242                  let views = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v.Content == item)
243                  where executeable == null || executeable.ExecutionState != ExecutionState.Started
244                  where !views.Any(v => v.Locked)
245                  select item;
246      T[] itemArray = query.ToArray();
247      return itemArray;
248    }
[3298]249    #endregion
250
251    #region ListView Events
252    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
[3362]253      removeButton.Enabled = !ReadOnly && listView.SelectedItems.Count > 0;
[3298]254    }
255    private void listView_KeyDown(object sender, KeyEventArgs e) {
256      if (e.KeyCode == Keys.Delete) {
[3362]257        if (!ReadOnly && (listView.SelectedItems.Count > 0)) {
[3298]258          foreach (ListViewItem item in listView.SelectedItems)
259            RemoveItem((T)item.Tag);
[5237]260          RebuildImageList();
[3298]261        }
262      }
263    }
264    private void listView_DoubleClick(object sender, EventArgs e) {
265      if (listView.SelectedItems.Count == 1) {
266        T item = (T)listView.SelectedItems[0].Tag;
[5270]267        IContentView view = MainFormManager.MainForm.ShowContent(item, true);
[3298]268      }
269    }
270    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
[5744]271      List<T> items = new List<T>();
272      foreach (ListViewItem listViewItem in listView.SelectedItems) {
273        T item = listViewItem.Tag as T;
274        if (item != null) items.Add(item);
275      }
276
277      if (items.Count > 0) {
278        DataObject data = new DataObject();
[5837]279        if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
280        else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
[5744]281        if (ReadOnly) {
282          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
283        } else {
284          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
285          if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
286            foreach (T item in items) RemoveItem(item);
287            RebuildImageList();
288          }
[5237]289        }
[3362]290      }
[3298]291    }
[5744]292    private void listView_DragEnter(object sender, DragEventArgs e) {
293      validDragOperation = false;
294      draggedItemsAlreadyContained = false;
[5837]295      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
[5744]296        validDragOperation = true;
[5837]297        draggedItemsAlreadyContained = itemListViewItemMapping.ContainsKey((T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat));
298      } else if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
[5744]299        validDragOperation = true;
[5837]300        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]301        foreach (object item in items) {
302          validDragOperation = validDragOperation && (item is T);
303          draggedItemsAlreadyContained = draggedItemsAlreadyContained || itemListViewItemMapping.ContainsKey((T)item);
304        }
305      }
306    }
307    private void listView_DragOver(object sender, DragEventArgs e) {
[3298]308      e.Effect = DragDropEffects.None;
[5744]309      if (validDragOperation) {
310        if (((e.KeyState & 32) == 32) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Link;  // ALT key
311        else if (((e.KeyState & 4) == 4) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Move;  // SHIFT key
312        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
313        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Move;
314        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link) && !draggedItemsAlreadyContained) e.Effect = DragDropEffects.Link;
[3298]315      }
316    }
317    private void listView_DragDrop(object sender, DragEventArgs e) {
318      if (e.Effect != DragDropEffects.None) {
[5237]319        try {
[5837]320          if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
321            T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[5744]322            AddItem(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
[5837]323          } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
324            IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
[6527]325            if (e.Effect.HasFlag(DragDropEffects.Copy)) {
326              Cloner cloner = new Cloner();
327              items = items.Select(x => cloner.Clone(x));
328            }
[5744]329            foreach (T item in items)
[6527]330              AddItem(item);
[5744]331          }
[5237]332        }
333        catch (Exception ex) {
334          ErrorHandling.ShowErrorDialog(this, ex);
335        }
[3298]336      }
337    }
338    #endregion
339
340    #region Button Events
341    private void addButton_Click(object sender, EventArgs e) {
342      if (typeSelectorDialog == null) {
343        typeSelectorDialog = new TypeSelectorDialog();
344        typeSelectorDialog.Caption = "Select Item";
345        typeSelectorDialog.TypeSelector.Caption = "Available Items";
[3588]346        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
[3298]347      }
348
[3407]349      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
350        try {
351          AddItem((T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType());
352        }
353        catch (Exception ex) {
[3758]354          ErrorHandling.ShowErrorDialog(this, ex);
[3407]355        }
356      }
[3298]357    }
358    private void sortAscendingButton_Click(object sender, EventArgs e) {
359      listView.Sorting = SortOrder.None;
360      listView.Sorting = SortOrder.Ascending;
361    }
362    private void sortDescendingButton_Click(object sender, EventArgs e) {
363      listView.Sorting = SortOrder.None;
364      listView.Sorting = SortOrder.Descending;
365    }
366    private void removeButton_Click(object sender, EventArgs e) {
367      if (listView.SelectedItems.Count > 0) {
368        foreach (ListViewItem item in listView.SelectedItems)
369          RemoveItem((T)item.Tag);
[5237]370        RebuildImageList();
[3298]371      }
372    }
373    private void saveButton_Click(object sender, EventArgs e) {
[5237]374      IEnumerable<T> items = itemListViewItemMapping.Keys.Except(GetStorableItems(itemListViewItemMapping.Keys));
[4453]375      if (items.Any()) {
376        string itemNames = string.Join(Environment.NewLine, items.Select(item => item.ToString()).ToArray());
[4500]377        MessageBox.Show("The following items are not saved, because they are locked (e.g. used in a running algorithm):" + Environment.NewLine + Environment.NewLine +
378          itemNames + Environment.NewLine + Environment.NewLine + "All other items will be saved.", "Cannot save all items", MessageBoxButtons.OK, MessageBoxIcon.Warning);
[4447]379      }
[3298]380      Save();
381    }
382    #endregion
383
384    #region Item Events
[3341]385    private void Item_ItemImageChanged(object sender, EventArgs e) {
386      if (InvokeRequired)
387        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
388      else {
389        T item = (T)sender;
[5237]390        ListViewItem listViewItem = itemListViewItemMapping[item];
[3341]391        int i = listViewItem.ImageIndex;
[5839]392        listView.SmallImageList.Images[i] = item.ItemImage;
[3341]393        listViewItem.ImageIndex = -1;
394        listViewItem.ImageIndex = i;
395      }
396    }
[3298]397    private void Item_ToStringChanged(object sender, EventArgs e) {
398      if (InvokeRequired)
399        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
400      else {
401        T item = (T)sender;
[5237]402        itemListViewItemMapping[item].Text = item.ToString();
[3298]403        listView.Sort();
[3299]404        AdjustListViewColumnSizes();
[3298]405      }
406    }
407    #endregion
[3299]408
409    #region Helpers
410    private void AdjustListViewColumnSizes() {
411      if (listView.Items.Count > 0) {
412        for (int i = 0; i < listView.Columns.Count; i++)
413          listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
414      }
415    }
[5237]416    private void RebuildImageList() {
417      listView.SmallImageList.Images.Clear();
418      foreach (ListViewItem item in listView.Items) {
419        listView.SmallImageList.Images.Add(((T)item.Tag).ItemImage);
420        item.ImageIndex = listView.SmallImageList.Images.Count - 1;
421      }
422    }
[3299]423    #endregion
[3292]424  }
425}
Note: See TracBrowser for help on using the repository browser.