Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10914 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
RevLine 
[3163]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3163]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;
[3202]23using System.IO;
24using System.Linq;
25using System.Reflection;
[4068]26using System.Threading;
[3202]27using System.Windows.Forms;
[3376]28using HeuristicLab.Common;
[3202]29using HeuristicLab.Core;
[3163]30using HeuristicLab.MainForm;
[3202]31using HeuristicLab.Persistence.Default.Xml;
[3163]32
33namespace HeuristicLab.Optimizer {
34  [View("Start Page")]
[3202]35  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
[9907]36    private IProgress progress;
37
[3163]38    public StartPage() {
39      InitializeComponent();
40    }
41
42    protected override void OnInitialized(EventArgs e) {
43      base.OnInitialized(e);
[3202]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
[3291]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) {
[9907]71      progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(samplesListView, "Loading...");
[10488]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();
[3291]77
[10488]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            }
[3202]85          }
[10488]86          catch (Exception) {
87          }
[3202]88        }
[10488]89        OnAllSamplesLoaded();
[3202]90      }
[10488]91      finally {
92        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
93      }
[3291]94    }
[9907]95    private void OnSampleLoaded(INamedItem sample, double progress) {
[3298]96      if (InvokeRequired)
[9907]97        Invoke(new Action<INamedItem, double>(OnSampleLoaded), sample, progress);
[3298]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);
[9907]105        this.progress.ProgressValue += progress;
[3202]106      }
[3291]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        }
[3202]117      }
[3163]118    }
119
[3202]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)
[3557]126        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
[3202]127    }
[3298]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();
[5837]132      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
[3298]133      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
134    }
[3202]135
[3163]136    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
137      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
138      Properties.Settings.Default.Save();
139    }
[3202]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
[3163]151  }
152}
Note: See TracBrowser for help on using the repository browser.