Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3296 was 3296, checked in by swagner, 13 years ago

Fixed some warnings (#915).

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