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

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

Added content of start page (#936).

File size: 4.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;
31
32namespace HeuristicLab.Optimizer {
33  [View("Start Page")]
34  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
35    private List<INamedItem> samples;
36
37    public StartPage() {
38      InitializeComponent();
39      Caption = "Start Page";
40    }
41
42    protected override void OnInitialized(EventArgs e) {
43      base.OnInitialized(e);
44      Assembly assembly = Assembly.GetExecutingAssembly();
45      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
46                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
47      titleLabel.Text = "HeuristicLab Optimizer";
48      if (version != null) titleLabel.Text += " " + version.Version;
49
50      try {
51        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
52          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
53      }
54      catch (Exception) { }
55
56      samples = new List<INamedItem>();
57      string path = Path.GetTempFileName();
58      foreach (string name in assembly.GetManifestResourceNames()) {
59        if (name.EndsWith(".hl")) {
60          try {
61            using (Stream stream = assembly.GetManifestResourceStream(name)) {
62              WriteStreamToTempFile(stream, path);
63              IItem item = XmlParser.Deserialize<IItem>(path);
64              if (item is INamedItem) samples.Add((INamedItem)item);
65            }
66          }
67          catch (Exception) { }
68        }
69      }
70      foreach (INamedItem sample in samples) {
71        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
72        item.ToolTipText = sample.ItemName + " (" + sample.ItemDescription + ")";
73        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
74        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
75        item.Tag = sample;
76        samplesListView.Items.Add(item);
77      }
78      samplesListView.Enabled = samplesListView.Items.Count > 0;
79      if (samplesListView.Items.Count > 0) {
80        for (int i = 0; i < samplesListView.Columns.Count; i++)
81          samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
82      }
83
84      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
85    }
86
87    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
88      System.Diagnostics.Process.Start(e.LinkText);
89    }
90
91    private void samplesListView_DoubleClick(object sender, EventArgs e) {
92      if (samplesListView.SelectedItems.Count == 1)
93        MainFormManager.CreateDefaultView(((IItem)samplesListView.SelectedItems[0].Tag).Clone()).Show();
94    }
95
96    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
97      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
98      Properties.Settings.Default.Save();
99    }
100
101    #region Helpers
102    private void WriteStreamToTempFile(Stream stream, string path) {
103      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
104        int cnt = 0;
105        byte[] buffer = new byte[32 * 1024];
106        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
107          output.Write(buffer, 0, cnt);
108      }
109    }
110    #endregion
111  }
112}
Note: See TracBrowser for help on using the repository browser.