Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added empty clipboard control (#965).

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