Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10923 was 10488, checked in by gkronber, 11 years ago

#2155: remove the progress bar only after the last sample has been loaded (otherwise an exception occurs if at least two samples cannot be loaded)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.IO;
24using System.Linq;
25using System.Reflection;
26using System.Threading;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Optimizer {
34  [View("Start Page")]
35  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
36    private IProgress progress;
37
38    public StartPage() {
39      InitializeComponent();
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      samplesListView.Enabled = false;
57      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
58
59      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
60    }
61
62    protected override void OnClosing(FormClosingEventArgs e) {
63      base.OnClosing(e);
64      if (e.CloseReason == CloseReason.UserClosing) {
65        e.Cancel = true;
66        this.Hide();
67      }
68    }
69
70    private void LoadSamples(object state) {
71      progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(samplesListView, "Loading...");
72      try {
73        Assembly assembly = Assembly.GetExecutingAssembly();
74        var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hl"));
75        int count = samples.Count();
76        string path = Path.GetTempFileName();
77
78        foreach (string name in samples) {
79          try {
80            using (Stream stream = assembly.GetManifestResourceStream(name)) {
81              WriteStreamToTempFile(stream, path);
82              INamedItem item = XmlParser.Deserialize<INamedItem>(path);
83              OnSampleLoaded(item, 1.0 / count);
84            }
85          }
86          catch (Exception) {
87          }
88        }
89        OnAllSamplesLoaded();
90      }
91      finally {
92        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
93      }
94    }
95    private void OnSampleLoaded(INamedItem sample, double progress) {
96      if (InvokeRequired)
97        Invoke(new Action<INamedItem, double>(OnSampleLoaded), sample, progress);
98      else {
99        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
100        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
101        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
102        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
103        item.Tag = sample;
104        samplesListView.Items.Add(item);
105        this.progress.ProgressValue += progress;
106      }
107    }
108    private void OnAllSamplesLoaded() {
109      if (InvokeRequired)
110        Invoke(new Action(OnAllSamplesLoaded));
111      else {
112        samplesListView.Enabled = samplesListView.Items.Count > 0;
113        if (samplesListView.Items.Count > 0) {
114          for (int i = 0; i < samplesListView.Columns.Count; i++)
115            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
116        }
117      }
118    }
119
120    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
121      System.Diagnostics.Process.Start(e.LinkText);
122    }
123
124    private void samplesListView_DoubleClick(object sender, EventArgs e) {
125      if (samplesListView.SelectedItems.Count == 1)
126        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
127    }
128    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
129      ListViewItem listViewItem = (ListViewItem)e.Item;
130      IItem item = (IItem)listViewItem.Tag;
131      DataObject data = new DataObject();
132      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
133      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
134    }
135
136    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
137      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
138      Properties.Settings.Default.Save();
139    }
140
141    #region Helpers
142    private void WriteStreamToTempFile(Stream stream, string path) {
143      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
144        int cnt = 0;
145        byte[] buffer = new byte[32 * 1024];
146        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
147          output.Write(buffer, 0, cnt);
148      }
149    }
150    #endregion
151  }
152}
Note: See TracBrowser for help on using the repository browser.