Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5645 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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