1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Core.Views;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Views;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
36 |
|
---|
37 | [View("DataAnalysisSolution View")]
|
---|
38 | [Content(typeof(DataAnalysisSolution), false)]
|
---|
39 | public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
|
---|
40 | public DataAnalysisSolutionView() {
|
---|
41 | InitializeComponent();
|
---|
42 | viewHost.ViewsLabelVisible = false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public new DataAnalysisSolution Content {
|
---|
46 | get { return (DataAnalysisSolution)base.Content; }
|
---|
47 | set { base.Content = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void SetEnabledStateOfControls() {
|
---|
51 | base.SetEnabledStateOfControls();
|
---|
52 | addButton.Enabled = false;
|
---|
53 | removeButton.Enabled = false;
|
---|
54 | if (Content == null) {
|
---|
55 | exportButton.Enabled = false;
|
---|
56 | } else {
|
---|
57 | exportButton.Enabled = !Locked;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override void RegisterContentEvents() {
|
---|
62 | base.RegisterContentEvents();
|
---|
63 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
64 | }
|
---|
65 | protected override void DeregisterContentEvents() {
|
---|
66 | base.DeregisterContentEvents();
|
---|
67 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
68 | }
|
---|
69 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
70 | OnContentChanged();
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected override void OnContentChanged() {
|
---|
74 | string selectedName = null;
|
---|
75 | if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null && itemsListView.SelectedItems[0].Tag is Type))
|
---|
76 | selectedName = itemsListView.SelectedItems[0].Text;
|
---|
77 |
|
---|
78 | base.OnContentChanged();
|
---|
79 | AddEvaluationViewTypes();
|
---|
80 |
|
---|
81 | //recover selection
|
---|
82 | if (selectedName != null) {
|
---|
83 | foreach (ListViewItem item in itemsListView.Items) {
|
---|
84 | if (item.Tag != null && item.Tag is Type && item.Text == selectedName)
|
---|
85 | item.Selected = true;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override IResult CreateItem() {
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected virtual void AddEvaluationViewTypes() {
|
---|
95 | if (Content != null && !Content.ProblemData.IsEmpty) {
|
---|
96 | var viewTypes = MainFormManager.GetViewTypes(Content.GetType(), true)
|
---|
97 | .Where(t => typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(t));
|
---|
98 | foreach (var viewType in viewTypes)
|
---|
99 | AddViewListViewItem(viewType, ((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected override void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
104 | if (itemsListView.SelectedItems.Count != 1) return;
|
---|
105 |
|
---|
106 | IResult result = itemsListView.SelectedItems[0].Tag as IResult;
|
---|
107 | Type viewType = itemsListView.SelectedItems[0].Tag as Type;
|
---|
108 | if (result != null) {
|
---|
109 | IContentView view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
|
---|
110 | if (view != null) {
|
---|
111 | view.ReadOnly = ReadOnly;
|
---|
112 | view.Locked = Locked;
|
---|
113 | }
|
---|
114 | } else if (viewType != null) {
|
---|
115 | MainFormManager.MainForm.ShowContent(Content, viewType);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
120 | if (itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
|
---|
121 | detailsGroupBox.Enabled = true;
|
---|
122 | Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
|
---|
123 | viewHost.ViewType = viewType;
|
---|
124 | viewHost.Content = Content;
|
---|
125 | } else
|
---|
126 | base.itemsListView_SelectedIndexChanged(sender, e);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | private void exportButton_Click(object sender, EventArgs e) {
|
---|
131 | var exporters = ApplicationManager.Manager.GetInstances<IDataAnalysisSolutionExporter>()
|
---|
132 | .Where(exporter => exporter.Supports(Content)).ToArray();
|
---|
133 | exportFileDialog.Filter = exporters.Skip(1)
|
---|
134 | .Aggregate(exporters.First().FileTypeFilter, (s, exporter) => s + "|" + exporter.FileTypeFilter);
|
---|
135 | var result = exportFileDialog.ShowDialog();
|
---|
136 | if (result == DialogResult.OK) {
|
---|
137 |
|
---|
138 | var name = exportFileDialog.FileName;
|
---|
139 | var selectedExporter = exporters.Single(exporter => exporter.FileTypeFilter == exportFileDialog.Filter);
|
---|
140 |
|
---|
141 | using (BackgroundWorker bg = new BackgroundWorker()) {
|
---|
142 | MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Exportion solution to " + name + ".");
|
---|
143 | bg.DoWork += (_, __) => selectedExporter.Export(Content, name);
|
---|
144 | bg.RunWorkerCompleted += (_, __) => MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
|
---|
145 | bg.RunWorkerAsync();
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected void AddViewListViewItem(Type viewType, Image image) {
|
---|
151 | ListViewItem listViewItem = new ListViewItem();
|
---|
152 | listViewItem.Text = ViewAttribute.GetViewName(viewType);
|
---|
153 | itemsListView.SmallImageList.Images.Add(image);
|
---|
154 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
155 | listViewItem.Tag = viewType;
|
---|
156 | itemsListView.Items.Add(listViewItem);
|
---|
157 |
|
---|
158 | AdjustListViewColumnSizes();
|
---|
159 | }
|
---|
160 |
|
---|
161 | protected void RemoveViewListViewItem(Type viewType) {
|
---|
162 | List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
|
---|
163 |
|
---|
164 | foreach (ListViewItem item in itemsToRemove)
|
---|
165 | itemsListView.Items.Remove(item);
|
---|
166 | }
|
---|
167 |
|
---|
168 | protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
169 | if (showDetailsCheckBox.Checked && itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
|
---|
170 | Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
|
---|
171 | viewHost.ViewType = viewType;
|
---|
172 | viewHost.Content = Content;
|
---|
173 | splitContainer.Panel2Collapsed = false;
|
---|
174 | detailsGroupBox.Enabled = true;
|
---|
175 | } else base.showDetailsCheckBox_CheckedChanged(sender, e);
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected override void RebuildImageList() {
|
---|
179 | itemsListView.SmallImageList.Images.Clear();
|
---|
180 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
181 | IResult result = listViewItem.Tag as IResult;
|
---|
182 | Type viewType = listViewItem.Tag as Type;
|
---|
183 | if (result != null) itemsListView.SmallImageList.Images.Add(result.ItemImage);
|
---|
184 | else if (viewType != null && typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
|
---|
185 | itemsListView.SmallImageList.Images.Add(((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
|
---|
186 | else itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
|
---|
187 |
|
---|
188 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | #region drag and drop
|
---|
193 | protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
|
---|
194 | validDragOperation = false;
|
---|
195 | if (ReadOnly) return;
|
---|
196 |
|
---|
197 | var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
198 | if (dropData is DataAnalysisProblemData) validDragOperation = true;
|
---|
199 | else if (dropData is IValueParameter) {
|
---|
200 | var param = (IValueParameter)dropData;
|
---|
201 | if (param.Value is DataAnalysisProblemData) validDragOperation = true;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
206 | if (e.Effect != DragDropEffects.None) {
|
---|
207 | var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
208 | if (dropData is DataAnalysisProblemData) {
|
---|
209 | DataAnalysisProblemData problemData = (DataAnalysisProblemData)dropData;
|
---|
210 | Content.ProblemData = (DataAnalysisProblemData)problemData.Clone();
|
---|
211 | } else if (dropData is IValueParameter) {
|
---|
212 | var param = (IValueParameter)dropData;
|
---|
213 | DataAnalysisProblemData problemData = param.Value as DataAnalysisProblemData;
|
---|
214 | if (problemData != null)
|
---|
215 | Content.ProblemData = (DataAnalysisProblemData)problemData.Clone();
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | #endregion
|
---|
220 | }
|
---|
221 | }
|
---|