Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/StartPage.cs @ 3291

Last change on this file since 3291 was 3291, checked in by swagner, 12 years ago

Loaded samples asynchronously in start page (#964).

File size: 5.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.Reflection;
27using System.Windows.Forms;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.Persistence.Default.Xml;
31using System.Threading;
32
33namespace HeuristicLab.Optimizer {
34  [View("Start Page")]
35  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
36    private List<INamedItem> samples;
37
38    public StartPage() {
39      InitializeComponent();
40      Caption = "Start Page";
41    }
42
43    protected override void OnInitialized(EventArgs e) {
44      base.OnInitialized(e);
45      Assembly assembly = Assembly.GetExecutingAssembly();
46      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
47                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
48      titleLabel.Text = "HeuristicLab Optimizer";
49      if (version != null) titleLabel.Text += " " + version.Version;
50
51      try {
52        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
53          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
54      }
55      catch (Exception) { }
56
57      samplesListView.Enabled = false;
58      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
59      Cursor = Cursors.AppStarting;
60
61      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
62    }
63
64    protected override void OnClosing(FormClosingEventArgs e) {
65      base.OnClosing(e);
66      if (e.CloseReason == CloseReason.UserClosing) {
67        e.Cancel = true;
68        this.Hide();
69      }
70    }
71
72    private void LoadSamples(object state) {
73      Assembly assembly = Assembly.GetExecutingAssembly();
74      var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hl"));
75      string path = Path.GetTempFileName();
76      int progress = loadingProgressBar.Maximum / samples.Count();
77
78      foreach (string name in samples) {
79        try {
80          using (Stream stream = assembly.GetManifestResourceStream(name)) {
81            WriteStreamToTempFile(stream, path);
82            IItem item = XmlParser.Deserialize<IItem>(path);
83            OnSampleLoaded(item as INamedItem, progress);
84          }
85        }
86        catch (Exception) { }
87      }
88      OnAllSamplesLoaded();
89    }
90    private void OnSampleLoaded(INamedItem sample, int progress) {
91      if (sample != null) {
92        if (InvokeRequired)
93          Invoke(new Action<INamedItem, int>(OnSampleLoaded), sample, progress);
94        else {
95          ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
96          item.ToolTipText = sample.ItemName + " (" + sample.ItemDescription + ")";
97          samplesListView.SmallImageList.Images.Add(sample.ItemImage);
98          item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
99          item.Tag = sample;
100          samplesListView.Items.Add(item);
101          loadingProgressBar.Value += progress;
102        }
103      }
104    }
105    private void OnAllSamplesLoaded() {
106      if (InvokeRequired)
107        Invoke(new Action(OnAllSamplesLoaded));
108      else {
109        samplesListView.Enabled = samplesListView.Items.Count > 0;
110        if (samplesListView.Items.Count > 0) {
111          for (int i = 0; i < samplesListView.Columns.Count; i++)
112            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
113        }
114        loadingPanel.Visible = false;
115        Cursor = Cursors.Default;
116      }
117    }
118
119    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
120      System.Diagnostics.Process.Start(e.LinkText);
121    }
122
123    private void samplesListView_DoubleClick(object sender, EventArgs e) {
124      if (samplesListView.SelectedItems.Count == 1)
125        MainFormManager.CreateDefaultView(((IItem)samplesListView.SelectedItems[0].Tag).Clone()).Show();
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.