Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4040 was 3764, checked in by mkommend, 15 years ago

adapted view captions (ticket #893)

File size: 5.6 KB
RevLine 
[3163]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;
[3202]23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Windows.Forms;
[3376]28using HeuristicLab.Common;
[3202]29using HeuristicLab.Core;
[3163]30using HeuristicLab.MainForm;
[3202]31using HeuristicLab.Persistence.Default.Xml;
[3291]32using System.Threading;
[3163]33
34namespace HeuristicLab.Optimizer {
35  [View("Start Page")]
[3202]36  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
[3163]37    public StartPage() {
38      InitializeComponent();
39    }
40
41    protected override void OnInitialized(EventArgs e) {
42      base.OnInitialized(e);
[3202]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
[3291]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"));
[3298]72      int count = samples.Count();
[3202]73      string path = Path.GetTempFileName();
[3291]74
75      foreach (string name in samples) {
76        try {
77          using (Stream stream = assembly.GetManifestResourceStream(name)) {
78            WriteStreamToTempFile(stream, path);
[3298]79            INamedItem item = XmlParser.Deserialize<INamedItem>(path);
80            OnSampleLoaded(item, loadingProgressBar.Maximum / count);
[3202]81          }
82        }
[3291]83        catch (Exception) { }
[3202]84      }
[3291]85      OnAllSamplesLoaded();
86    }
87    private void OnSampleLoaded(INamedItem sample, int progress) {
[3298]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;
[3202]98      }
[3291]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;
[3202]110      }
[3163]111    }
112
[3202]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)
[3557]119        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
[3202]120    }
[3298]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    }
[3202]129
[3163]130    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
131      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
132      Properties.Settings.Default.Save();
133    }
[3202]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
[3163]145  }
146}
Note: See TracBrowser for help on using the repository browser.