Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimizer/3.3/StartPage.cs @ 12009

Last change on this file since 12009 was 12009, checked in by ascheibe, 9 years ago

#2212 updated copyright year

File size: 9.1 KB
RevLine 
[3163]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 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;
[11122]23using System.Collections.Generic;
[3202]24using System.IO;
25using System.Linq;
26using System.Reflection;
[4068]27using System.Threading;
[3202]28using System.Windows.Forms;
[3376]29using HeuristicLab.Common;
[3202]30using HeuristicLab.Core;
[3163]31using HeuristicLab.MainForm;
[3202]32using HeuristicLab.Persistence.Default.Xml;
[3163]33
34namespace HeuristicLab.Optimizer {
35  [View("Start Page")]
[3202]36  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
[11122]37    private const string StandardProblemsGroupName = "Standard Problems";
38    private const string DataAnalysisGroupName = "Data Analysis";
39    private const string ScriptsGroupName = "Scripts";
40    private const string UncategorizedGroupName = "Uncategorized";
41    private const string SampleNamePrefix = "HeuristicLab.Optimizer.Documents.";
42    private const string SampleNameSuffix = ".hl";
43
44    private readonly Dictionary<ListViewGroup, List<string>> groupLookup = new Dictionary<ListViewGroup, List<string>>();
45    private readonly ListViewGroup standardProblemsGroup = new ListViewGroup(StandardProblemsGroupName);
46    private readonly ListViewGroup dataAnalysisGroup = new ListViewGroup(DataAnalysisGroupName);
47    private readonly ListViewGroup scriptsGroup = new ListViewGroup(ScriptsGroupName);
48    private readonly ListViewGroup uncategorizedGroup = new ListViewGroup(UncategorizedGroupName);
49
[9933]50    private IProgress progress;
51
[3163]52    public StartPage() {
53      InitializeComponent();
54    }
55
56    protected override void OnInitialized(EventArgs e) {
57      base.OnInitialized(e);
[3202]58      Assembly assembly = Assembly.GetExecutingAssembly();
59      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
60                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
61      titleLabel.Text = "HeuristicLab Optimizer";
62      if (version != null) titleLabel.Text += " " + version.Version;
63
64      try {
65        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
66          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
[11901]67      }
68      catch (Exception) { }
[3202]69
[3291]70      samplesListView.Enabled = false;
[11122]71      samplesListView.Groups.Add(standardProblemsGroup);
72      samplesListView.Groups.Add(dataAnalysisGroup);
73      samplesListView.Groups.Add(scriptsGroup);
74      samplesListView.Groups.Add(uncategorizedGroup);
75      FillGroupLookup();
76
[3291]77      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
78
79      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
80    }
81
82    protected override void OnClosing(FormClosingEventArgs e) {
83      base.OnClosing(e);
84      if (e.CloseReason == CloseReason.UserClosing) {
85        e.Cancel = true;
86        this.Hide();
87      }
88    }
89
90    private void LoadSamples(object state) {
[9933]91      progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(samplesListView, "Loading...");
[11105]92      try {
[11122]93        var assembly = Assembly.GetExecutingAssembly();
94        var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(SampleNameSuffix));
[11105]95        int count = samples.Count();
[3291]96
[11122]97        foreach (var entry in groupLookup) {
98          var group = entry.Key;
99          var sampleList = entry.Value;
100          foreach (var sampleName in sampleList) {
101            string resourceName = SampleNamePrefix + sampleName + SampleNameSuffix;
102            LoadSample(resourceName, assembly, group, count);
[3202]103          }
104        }
[11122]105
106        var categorizedSamples = groupLookup.Select(x => x.Value).SelectMany(x => x).Select(x => SampleNamePrefix + x + SampleNameSuffix);
107        var uncategorizedSamples = samples.Except(categorizedSamples);
108
109        foreach (var resourceName in uncategorizedSamples) {
110          LoadSample(resourceName, assembly, uncategorizedGroup, count);
111        }
112
[11105]113        OnAllSamplesLoaded();
[11901]114      }
115      finally {
[11105]116        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
117      }
[3291]118    }
[11122]119
120    private void LoadSample(string name, Assembly assembly, ListViewGroup group, int count) {
121      string path = Path.GetTempFileName();
122      try {
123        using (var stream = assembly.GetManifestResourceStream(name)) {
124          WriteStreamToTempFile(stream, path); // create a file in a temporary folder (persistence cannot load these files directly from the stream)
125          var item = XmlParser.Deserialize<INamedItem>(path);
126          OnSampleLoaded(item, group, 1.0 / count);
127        }
[11901]128      }
129      catch (Exception) {
130      }
131      finally {
[11122]132        if (File.Exists(path)) {
133          File.Delete(path); // make sure we remove the temporary file
134        }
135      }
136    }
137
138    private void FillGroupLookup() {
139      var standardProblems = new List<string> { "ES_Griewank", "GA_TSP", "GA_VRP", "GE_ArtificialAnt",
140                "IslandGA_TSP", "LS_Knapsack", "PSO_Schwefel", "RAPGA_JSSP",
141                "SA_Rastrigin", "SGP_SantaFe","GP_Multiplexer", "SS_VRP", "TS_TSP", "TS_VRP", "VNS_TSP"
142        };
143      groupLookup[standardProblemsGroup] = standardProblems;
144      var dataAnalysisProblems = new List<string> { "SGP_SymbClass", "SGP_SymbReg", "OSGP_TimeSeries", "GE_SymbReg", "GPR" };
145      groupLookup[dataAnalysisGroup] = dataAnalysisProblems;
[11901]146      var scripts = new List<string> { "GA_QAP_Script", "GUI_Automation_Script", "OSGA_Rastrigin_Script",
[11907]147                                       "GridSearch_RF_Classification_Script", "GridSearch_RF_Regression_Script",
148                                       "GridSearch_SVM_Classification_Script", "GridSearch_SVM_Regression_Script" };
[11122]149      groupLookup[scriptsGroup] = scripts;
150    }
151
152    private void OnSampleLoaded(INamedItem sample, ListViewGroup group, double progress) {
[3298]153      if (InvokeRequired)
[11122]154        Invoke(new Action<INamedItem, ListViewGroup, double>(OnSampleLoaded), sample, group, progress);
[3298]155      else {
[11122]156        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description }, group);
[3298]157        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
158        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
159        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
160        item.Tag = sample;
161        samplesListView.Items.Add(item);
[9933]162        this.progress.ProgressValue += progress;
[3202]163      }
[3291]164    }
165    private void OnAllSamplesLoaded() {
166      if (InvokeRequired)
167        Invoke(new Action(OnAllSamplesLoaded));
168      else {
169        samplesListView.Enabled = samplesListView.Items.Count > 0;
170        if (samplesListView.Items.Count > 0) {
171          for (int i = 0; i < samplesListView.Columns.Count; i++)
172            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
173        }
[3202]174      }
[3163]175    }
176
[3202]177    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
178      System.Diagnostics.Process.Start(e.LinkText);
179    }
180
181    private void samplesListView_DoubleClick(object sender, EventArgs e) {
[11122]182      if (samplesListView.SelectedItems.Count == 1) {
183        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
184        try {
185          mainForm.SetWaitCursor();
186          mainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
[11901]187        }
188        finally {
[11122]189          mainForm.ResetWaitCursor();
190        }
191
192      }
[3202]193    }
[3298]194    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
195      ListViewItem listViewItem = (ListViewItem)e.Item;
196      IItem item = (IItem)listViewItem.Tag;
197      DataObject data = new DataObject();
[5837]198      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
[3298]199      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
200    }
[3202]201
[3163]202    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
203      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
204      Properties.Settings.Default.Save();
205    }
[3202]206
207    #region Helpers
208    private void WriteStreamToTempFile(Stream stream, string path) {
209      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
[11122]210        stream.CopyTo(output);
[3202]211      }
212    }
213    #endregion
[3163]214  }
215}
Note: See TracBrowser for help on using the repository browser.