Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Optimizer/3.3/StartPage.cs

Last change on this file was 3764, checked in by mkommend, 14 years ago

adapted view captions (ticket #893)

File size: 5.6 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.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.Persistence.Default.Xml;
32using System.Threading;
33
34namespace HeuristicLab.Optimizer {
35  [View("Start Page")]
36  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
37    public StartPage() {
38      InitializeComponent();
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      int count = samples.Count();
73      string path = Path.GetTempFileName();
74
75      foreach (string name in samples) {
76        try {
77          using (Stream stream = assembly.GetManifestResourceStream(name)) {
78            WriteStreamToTempFile(stream, path);
79            INamedItem item = XmlParser.Deserialize<INamedItem>(path);
80            OnSampleLoaded(item, loadingProgressBar.Maximum / count);
81          }
82        }
83        catch (Exception) { }
84      }
85      OnAllSamplesLoaded();
86    }
87    private void OnSampleLoaded(INamedItem sample, int progress) {
88      if (InvokeRequired)
89        Invoke(new Action<INamedItem, int>(OnSampleLoaded), sample, progress);
90      else {
91        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
92        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
93        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
94        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
95        item.Tag = sample;
96        samplesListView.Items.Add(item);
97        loadingProgressBar.Value += progress;
98      }
99    }
100    private void OnAllSamplesLoaded() {
101      if (InvokeRequired)
102        Invoke(new Action(OnAllSamplesLoaded));
103      else {
104        samplesListView.Enabled = samplesListView.Items.Count > 0;
105        if (samplesListView.Items.Count > 0) {
106          for (int i = 0; i < samplesListView.Columns.Count; i++)
107            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
108        }
109        loadingPanel.Visible = false;
110      }
111    }
112
113    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
114      System.Diagnostics.Process.Start(e.LinkText);
115    }
116
117    private void samplesListView_DoubleClick(object sender, EventArgs e) {
118      if (samplesListView.SelectedItems.Count == 1)
119        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
120    }
121    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
122      ListViewItem listViewItem = (ListViewItem)e.Item;
123      IItem item = (IItem)listViewItem.Tag;
124      DataObject data = new DataObject();
125      data.SetData("Type", item.GetType());
126      data.SetData("Value", item);
127      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
128    }
129
130    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
131      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
132      Properties.Settings.Default.Save();
133    }
134
135    #region Helpers
136    private void WriteStreamToTempFile(Stream stream, string path) {
137      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
138        int cnt = 0;
139        byte[] buffer = new byte[32 * 1024];
140        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
141          output.Write(buffer, 0, cnt);
142      }
143    }
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.