Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3332 was 3299, checked in by swagner, 15 years ago

Enabled dragging and dropping of problems and algorithms in AlgorithmView and BatchRunView (#965).

File size: 11.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Threading;
27using System.Windows.Forms;
28using HeuristicLab.MainForm;
29using HeuristicLab.Persistence.Default.Xml;
30
31namespace HeuristicLab.Core.Views {
32  [View("Clipboard")]
33  public sealed partial class Clipboard<T> : HeuristicLab.MainForm.WindowsForms.View where T : class, IItem {
34    private TypeSelectorDialog typeSelectorDialog;
35    private Dictionary<T, ListViewItem> itemListViewItemTable;
36
37    private string itemsPath;
38    public string ItemsPath {
39      get { return itemsPath; }
40      private set {
41        if (string.IsNullOrEmpty(value)) throw new ArgumentException(string.Format("Invalid items path \"{0}\".", value));
42        itemsPath = value;
43        try {
44          if (!Directory.Exists(itemsPath)) {
45            Directory.CreateDirectory(itemsPath);
46            // directory creation might take some time -> wait until it is definitively created
47            while (!Directory.Exists(itemsPath)) {
48              Thread.Sleep(100);
49              Directory.CreateDirectory(itemsPath);
50            }
51          }
52        }
53        catch (Exception ex) {
54          throw new ArgumentException(string.Format("Invalid items path \"{0}\".", itemsPath), ex);
55        }
56      }
57    }
58
59    public Clipboard() {
60      InitializeComponent();
61      ItemsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
62                  Path.DirectorySeparatorChar + "HeuristicLab" + Path.DirectorySeparatorChar + "Clipboard";
63      itemListViewItemTable = new Dictionary<T, ListViewItem>();
64    }
65    public Clipboard(string itemsPath) {
66      InitializeComponent();
67      ItemsPath = itemsPath;
68      itemListViewItemTable = new Dictionary<T, ListViewItem>();
69    }
70
71    protected override void OnInitialized(EventArgs e) {
72      base.OnInitialized(e);
73      Enabled = false;
74      infoLabel.Text = "Loading ...";
75      progressBar.Value = 0;
76      infoPanel.Visible = true;
77      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadItems));
78    }
79    protected override void OnClosing(FormClosingEventArgs e) {
80      base.OnClosing(e);
81      if (e.CloseReason == CloseReason.UserClosing) {
82        e.Cancel = true;
83        this.Hide();
84      }
85    }
86
87    public void AddItem(T item) {
88      if (InvokeRequired)
89        Invoke(new Action<T>(AddItem), item);
90      else {
91        if (!itemListViewItemTable.ContainsKey(item)) {
92          if (!listView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
93            listView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
94
95          ListViewItem listViewItem = new ListViewItem(item.ToString());
96          listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
97          listViewItem.ImageIndex = listView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
98          listViewItem.Tag = item;
99          listView.Items.Add(listViewItem);
100          itemListViewItemTable.Add(item, listViewItem);
101          item.ToStringChanged += new EventHandler(Item_ToStringChanged);
102          sortAscendingButton.Enabled = sortDescendingButton.Enabled = listView.Items.Count > 1;
103          AdjustListViewColumnSizes();
104        }
105      }
106    }
107    private void RemoveItem(T item) {
108      if (InvokeRequired)
109        Invoke(new Action<T>(RemoveItem), item);
110      else {
111        if (itemListViewItemTable.ContainsKey(item)) {
112          item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
113          itemListViewItemTable[item].Remove();
114          itemListViewItemTable.Remove(item);
115          sortAscendingButton.Enabled = sortDescendingButton.Enabled = listView.Items.Count > 1;
116        }
117      }
118    }
119    private void Save() {
120      if (InvokeRequired)
121        Invoke(new Action(Save));
122      else {
123        Enabled = false;
124        infoLabel.Text = "Saving ...";
125        progressBar.Value = 0;
126        infoPanel.Visible = true;
127        ThreadPool.QueueUserWorkItem(new WaitCallback(SaveItems));
128      }
129    }
130
131    #region Loading/Saving Items
132    private void LoadItems(object state) {
133      string[] items = Directory.GetFiles(ItemsPath);
134      foreach (string filename in items) {
135        try {
136          T item = XmlParser.Deserialize<T>(filename);
137          OnItemLoaded(item, progressBar.Maximum / items.Length);
138        }
139        catch (Exception) { }
140      }
141      OnAllItemsLoaded();
142    }
143    private void OnItemLoaded(T item, int progress) {
144      if (InvokeRequired)
145        Invoke(new Action<T, int>(OnItemLoaded), item, progress);
146      else {
147        AddItem(item);
148        progressBar.Value += progress;
149      }
150    }
151    private void OnAllItemsLoaded() {
152      if (InvokeRequired)
153        Invoke(new Action(OnAllItemsLoaded));
154      else {
155        Enabled = true;
156        if (listView.Items.Count > 0) {
157          for (int i = 0; i < listView.Columns.Count; i++)
158            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
159        }
160        infoPanel.Visible = false;
161      }
162    }
163    private void SaveItems(object param) {
164      Directory.Delete(ItemsPath, true);
165      Directory.CreateDirectory(ItemsPath);
166      // directory creation might take some time -> wait until it is definitively created
167      while (!Directory.Exists(ItemsPath)) {
168        Thread.Sleep(100);
169        Directory.CreateDirectory(ItemsPath);
170      }
171
172      int i = 0;
173      T[] items = itemListViewItemTable.Keys.ToArray();
174      foreach (T item in items) {
175        try {
176          i++;
177          XmlGenerator.Serialize(item, ItemsPath + Path.DirectorySeparatorChar + i.ToString("00000000") + ".hl", 9);
178          OnItemSaved(item, progressBar.Maximum / listView.Items.Count);
179        }
180        catch (Exception) { }
181      }
182      OnAllItemsSaved();
183    }
184    private void OnItemSaved(T item, int progress) {
185      if (item != null) {
186        if (InvokeRequired)
187          Invoke(new Action<T, int>(OnItemLoaded), item, progress);
188        else
189          progressBar.Value += progress;
190      }
191    }
192    private void OnAllItemsSaved() {
193      if (InvokeRequired)
194        Invoke(new Action(OnAllItemsLoaded));
195      else {
196        Enabled = true;
197        infoPanel.Visible = false;
198      }
199    }
200    #endregion
201
202    #region ListView Events
203    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
204      removeButton.Enabled = listView.SelectedItems.Count > 0;
205    }
206    private void listView_KeyDown(object sender, KeyEventArgs e) {
207      if (e.KeyCode == Keys.Delete) {
208        if (listView.SelectedItems.Count > 0) {
209          foreach (ListViewItem item in listView.SelectedItems)
210            RemoveItem((T)item.Tag);
211        }
212      }
213    }
214    private void listView_DoubleClick(object sender, EventArgs e) {
215      if (listView.SelectedItems.Count == 1) {
216        T item = (T)listView.SelectedItems[0].Tag;
217        IView view = MainFormManager.CreateDefaultView(item);
218        if (view != null) view.Show();
219      }
220    }
221    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
222      ListViewItem listViewItem = (ListViewItem)e.Item;
223      T item = (T)listViewItem.Tag;
224      DataObject data = new DataObject();
225      data.SetData("Type", item.GetType());
226      data.SetData("Value", item);
227      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
228      if ((result & DragDropEffects.Move) == DragDropEffects.Move)
229        RemoveItem(item);
230    }
231    private void listView_DragEnterOver(object sender, DragEventArgs e) {
232      e.Effect = DragDropEffects.None;
233      Type type = e.Data.GetData("Type") as Type;
234      T item = e.Data.GetData("Value") as T;
235      if ((type != null) && (item != null)) {
236        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
237        else if (((e.KeyState & 4) == 4) && !itemListViewItemTable.ContainsKey(item)) e.Effect = DragDropEffects.Move;  // SHIFT key
238        else if (((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) && !itemListViewItemTable.ContainsKey(item)) e.Effect = DragDropEffects.Link;
239        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
240        else if (((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) && !itemListViewItemTable.ContainsKey(item)) e.Effect = DragDropEffects.Move;
241      }
242    }
243    private void listView_DragDrop(object sender, DragEventArgs e) {
244      if (e.Effect != DragDropEffects.None) {
245        T item = e.Data.GetData("Value") as T;
246        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
247        AddItem(item);
248      }
249    }
250    #endregion
251
252    #region Button Events
253    private void addButton_Click(object sender, EventArgs e) {
254      if (typeSelectorDialog == null) {
255        typeSelectorDialog = new TypeSelectorDialog();
256        typeSelectorDialog.Caption = "Select Item";
257        typeSelectorDialog.TypeSelector.Caption = "Available Items";
258        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, false);
259      }
260
261      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
262        AddItem((T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType());
263    }
264    private void sortAscendingButton_Click(object sender, EventArgs e) {
265      listView.Sorting = SortOrder.None;
266      listView.Sorting = SortOrder.Ascending;
267    }
268    private void sortDescendingButton_Click(object sender, EventArgs e) {
269      listView.Sorting = SortOrder.None;
270      listView.Sorting = SortOrder.Descending;
271    }
272    private void removeButton_Click(object sender, EventArgs e) {
273      if (listView.SelectedItems.Count > 0) {
274        foreach (ListViewItem item in listView.SelectedItems)
275          RemoveItem((T)item.Tag);
276      }
277    }
278    private void saveButton_Click(object sender, EventArgs e) {
279      Save();
280    }
281    #endregion
282
283    #region Item Events
284    private void Item_ToStringChanged(object sender, EventArgs e) {
285      if (InvokeRequired)
286        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
287      else {
288        T item = (T)sender;
289        itemListViewItemTable[item].Text = item.ToString();
290        listView.Sort();
291        AdjustListViewColumnSizes();
292      }
293    }
294    #endregion
295
296    #region Helpers
297    private void AdjustListViewColumnSizes() {
298      if (listView.Items.Count > 0) {
299        for (int i = 0; i < listView.Columns.Count; i++)
300          listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
301      }
302    }
303    #endregion
304  }
305}
Note: See TracBrowser for help on using the repository browser.