Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Optimizer/3.3/StartPage.cs @ 6004

Last change on this file since 6004 was 5744, checked in by swagner, 14 years ago

Implemented dragging of multiple items in all collection views and refactored drag & drop code (#1112)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.IO;
24using System.Linq;
25using System.Reflection;
26using System.Threading;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Optimizer {
34  [View("Start Page")]
35  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
36    public StartPage() {
37      InitializeComponent();
38    }
39
40    protected override void OnInitialized(EventArgs e) {
41      base.OnInitialized(e);
42      Assembly assembly = Assembly.GetExecutingAssembly();
43      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
44                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
45      titleLabel.Text = "HeuristicLab Optimizer";
46      if (version != null) titleLabel.Text += " " + version.Version;
47
48      try {
49        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
50          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
51      }
52      catch (Exception) { }
53
54      samplesListView.Enabled = false;
55      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
56
57      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
58    }
59
60    protected override void OnClosing(FormClosingEventArgs e) {
61      base.OnClosing(e);
62      if (e.CloseReason == CloseReason.UserClosing) {
63        e.Cancel = true;
64        this.Hide();
65      }
66    }
67
68    private void LoadSamples(object state) {
69      Assembly assembly = Assembly.GetExecutingAssembly();
70      var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hl"));
71      int count = samples.Count();
72      string path = Path.GetTempFileName();
73
74      foreach (string name in samples) {
75        try {
76          using (Stream stream = assembly.GetManifestResourceStream(name)) {
77            WriteStreamToTempFile(stream, path);
78            INamedItem item = XmlParser.Deserialize<INamedItem>(path);
79            OnSampleLoaded(item, loadingProgressBar.Maximum / count);
80          }
81        }
82        catch (Exception) { }
83      }
84      OnAllSamplesLoaded();
85    }
86    private void OnSampleLoaded(INamedItem sample, int progress) {
87      if (InvokeRequired)
88        Invoke(new Action<INamedItem, int>(OnSampleLoaded), sample, progress);
89      else {
90        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
91        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
92        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
93        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
94        item.Tag = sample;
95        samplesListView.Items.Add(item);
96        loadingProgressBar.Value += progress;
97      }
98    }
99    private void OnAllSamplesLoaded() {
100      if (InvokeRequired)
101        Invoke(new Action(OnAllSamplesLoaded));
102      else {
103        samplesListView.Enabled = samplesListView.Items.Count > 0;
104        if (samplesListView.Items.Count > 0) {
105          for (int i = 0; i < samplesListView.Columns.Count; i++)
106            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
107        }
108        loadingPanel.Visible = false;
109      }
110    }
111
112    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
113      System.Diagnostics.Process.Start(e.LinkText);
114    }
115
116    private void samplesListView_DoubleClick(object sender, EventArgs e) {
117      if (samplesListView.SelectedItems.Count == 1)
118        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
119    }
120    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
121      ListViewItem listViewItem = (ListViewItem)e.Item;
122      IItem item = (IItem)listViewItem.Tag;
123      DataObject data = new DataObject();
124      data.SetData("HeuristicLab", item);
125      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
126    }
127
128    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
129      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
130      Properties.Settings.Default.Save();
131    }
132
133    #region Helpers
134    private void WriteStreamToTempFile(Stream stream, string path) {
135      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
136        int cnt = 0;
137        byte[] buffer = new byte[32 * 1024];
138        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
139          output.Write(buffer, 0, cnt);
140      }
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.