Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/UserDefinedAlgorithmView.cs @ 3225

Last change on this file since 3225 was 3177, checked in by swagner, 14 years ago

Moved extension methods of Control from HeuristicLab.Core.Views to HeuristicLab.MainForm.WindowsForms and suspended/resumed repainting in some controls to improve UI response times (#887).

File size: 6.5 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.Windows.Forms;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.Persistence.Default.Xml;
28
29namespace HeuristicLab.Optimization.Views {
30  /// <summary>
31  /// The base class for visual representations of items.
32  /// </summary>
33  [View("UserDefinedAlgorithm View")]
34  [Content(typeof(UserDefinedAlgorithm), true)]
35  public sealed partial class UserDefinedAlgorithmView : EngineAlgorithmView {
36    public new UserDefinedAlgorithm Content {
37      get { return (UserDefinedAlgorithm)base.Content; }
38      set { base.Content = value; }
39    }
40
41    /// <summary>
42    /// Initializes a new instance of <see cref="ItemBaseView"/>.
43    /// </summary>
44    public UserDefinedAlgorithmView() {
45      InitializeComponent();
46    }
47    /// <summary>
48    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
49    /// </summary>
50    /// <param name="item">The item that should be displayed.</param>
51    public UserDefinedAlgorithmView(UserDefinedAlgorithm content)
52      : this() {
53      Content = content;
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.OperatorGraphChanged -= new EventHandler(Content_OperatorGraphChanged);
58      base.DeregisterContentEvents();
59    }
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.OperatorGraphChanged += new EventHandler(Content_OperatorGraphChanged);
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content == null) {
68        operatorGraphViewHost.Content = null;
69        globalScopeView.Content = null;
70      } else {
71        operatorGraphViewHost.ViewType = null;
72        operatorGraphViewHost.Content = Content.OperatorGraph;
73        globalScopeView.Content = Content.GlobalScope;
74      }
75    }
76
77    protected override void Content_Started(object sender, EventArgs e) {
78      if (InvokeRequired)
79        Invoke(new EventHandler(Content_Started), sender, e);
80      else {
81        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
82        operatorGraphViewHost.Enabled = false;
83        globalScopeView.Enabled = false;
84        base.Content_Started(sender, e);
85      }
86    }
87    protected override void Content_Stopped(object sender, EventArgs e) {
88      if (InvokeRequired)
89        Invoke(new EventHandler(Content_Stopped), sender, e);
90      else {
91        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
92        operatorGraphViewHost.Enabled = true;
93        globalScopeView.Enabled = true;
94        base.Content_Stopped(sender, e);
95      }
96    }
97    private void Content_OperatorGraphChanged(object sender, EventArgs e) {
98      if (InvokeRequired)
99        Invoke(new EventHandler(Content_OperatorGraphChanged), sender, e);
100      else {
101        operatorGraphViewHost.ViewType = null;
102        operatorGraphViewHost.Content = Content.OperatorGraph;
103      }
104    }
105
106    private void newOperatorGraphButton_Click(object sender, EventArgs e) {
107      Content.OperatorGraph = new OperatorGraph();
108    }
109    private void openOperatorGraphButton_Click(object sender, EventArgs e) {
110      openFileDialog.Title = "Open Operator Graph";
111      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
112        this.Cursor = Cursors.AppStarting;
113        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
114        operatorGraphViewHost.Enabled = false;
115
116        var call = new Func<string, object>(XmlParser.Deserialize);
117        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
118          OperatorGraph operatorGraph = null;
119          try {
120            operatorGraph = call.EndInvoke(a) as OperatorGraph;
121          }
122          catch (Exception ex) {
123            Auxiliary.ShowErrorMessageBox(ex);
124          }
125          Invoke(new Action(delegate() {
126            if (operatorGraph == null)
127              MessageBox.Show(this, "The selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
128            else
129              Content.OperatorGraph = operatorGraph;
130            operatorGraphViewHost.Enabled = true;
131            newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
132            this.Cursor = Cursors.Default;
133          }));
134        }, null);
135      }
136    }
137    private void saveOperatorGraphButton_Click(object sender, EventArgs e) {
138      saveFileDialog.Title = "Save Operator Graph";
139      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
140        this.Cursor = Cursors.AppStarting;
141        newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
142        operatorGraphViewHost.Enabled = false;
143
144        var call = new Action<OperatorGraph, string, int>(XmlGenerator.Serialize);
145        int compression = 9;
146        if (saveFileDialog.FilterIndex == 1) compression = 0;
147        call.BeginInvoke(Content.OperatorGraph, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
148          try {
149            call.EndInvoke(a);
150          }
151          catch (Exception ex) {
152            Auxiliary.ShowErrorMessageBox(ex);
153          }
154          Invoke(new Action(delegate() {
155            operatorGraphViewHost.Enabled = true;
156            newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
157            this.Cursor = Cursors.Default;
158          }));
159        }, null);
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.