Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimizer/3.3/StartPage.cs @ 17021

Last change on this file since 17021 was 16992, checked in by gkronber, 6 years ago

#2520: Use ContentManager in Clipboard and StartPage. Show info when objects cannot be restored.

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