Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3607 was 3557, checked in by mkommend, 15 years ago

changed logic of showing new views (ticket #972)

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