Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.OptimizationFrontend/MainForm.cs @ 634

Last change on this file since 634 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 10.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Threading;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Core;
32
33namespace HeuristicLab.OptimizationFrontend {
34  public partial class MainForm : Form, IControlManager {
35    #region Inner Types
36    private class Task {
37      public string filename;
38      public IStorable storable;
39      public IEditor editor;
40
41      private Task() { }
42      public Task(string filename, IStorable storable, IEditor editor) {
43        this.filename = filename;
44        this.storable = storable;
45        this.editor = editor;
46      }
47    }
48    #endregion
49
50    private object locker;
51    private int runningTasks;
52
53    public MainForm() {
54      InitializeComponent();
55
56      locker = new object();
57      runningTasks = 0;
58
59      DiscoveryService discoveryService = new DiscoveryService();
60
61      // discover creatable items
62      Type[] creatables = discoveryService.GetTypes(typeof(IEditable));
63      string[] names = new string[creatables.Length];
64      for (int i = 0; i < creatables.Length; i++)
65        names[i] = creatables[i].Name;
66      Array.Sort(names, creatables);
67      foreach (Type type in creatables) {
68        if (!type.IsAbstract) {
69          ToolStripMenuItem item = new ToolStripMenuItem();
70          item.Tag = type;
71          item.Text = "&" + type.Name + "...";
72          item.Click += new EventHandler(newToolStripMenuItem_Click);
73          newToolStripMenuItem.DropDownItems.Add(item);
74
75          item = new ToolStripMenuItem();
76          item.Tag = type;
77          item.Text = "&" + type.Name + "...";
78          item.Click += new EventHandler(newToolStripMenuItem_Click);
79          newToolStripDropDownButton.DropDownItems.Add(item);
80        }
81      }
82    }
83
84    #region IControlManager Members
85    public void ShowControl(IControl control) {
86      if (control is IEditor) {
87        EditorForm form = new EditorForm((IEditor)control);
88        form.MdiParent = this;
89        form.Show();
90      } else if (control is IView) {
91        ViewForm form = new ViewForm((IView)control);
92        form.MdiParent = this;
93        form.Show();
94      } else {
95        throw new InvalidOperationException("Control is neither a view nor an editor.");
96      }
97    }
98    #endregion
99
100    private void EnableDisableItems() {
101      closeToolStripMenuItem.Enabled = false;
102      closeAllToolStripMenuItem.Enabled = false;
103      saveToolStripMenuItem.Enabled = false;
104      saveToolStripButton.Enabled = false;
105      saveAsToolStripMenuItem.Enabled = false;
106      saveAllToolStripMenuItem.Enabled = false;
107      saveAllToolStripButton.Enabled = false;
108      cascadeToolStripMenuItem.Enabled = false;
109      tileHorizontallyToolStripMenuItem.Enabled = false;
110      tileVerticallyToolStripMenuItem.Enabled = false;
111
112      if (ActiveMdiChild != null) {
113        closeToolStripMenuItem.Enabled = true;
114        closeAllToolStripMenuItem.Enabled = true;
115        saveAllToolStripMenuItem.Enabled = true;
116        saveAllToolStripButton.Enabled = true;
117        cascadeToolStripMenuItem.Enabled = true;
118        tileHorizontallyToolStripMenuItem.Enabled = true;
119        tileVerticallyToolStripMenuItem.Enabled = true;
120        EditorForm form = ActiveMdiChild as EditorForm;
121        if (form != null){
122          if (((Control)form.Editor).Enabled) {
123            saveToolStripMenuItem.Enabled = true;
124            saveToolStripButton.Enabled = true;
125            saveAsToolStripMenuItem.Enabled = true;
126          } else {
127            closeToolStripMenuItem.Enabled = false;
128            closeAllToolStripMenuItem.Enabled = false;
129          }
130        }
131      }
132    }
133
134    #region Open and Save Methods
135    private void Open() {
136      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
137        lock (locker) runningTasks++;
138        Cursor = Cursors.AppStarting;
139        Task task = new Task(openFileDialog.FileName, null, null);
140        ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousLoad), task);
141      }
142    }
143    private void AsynchronousLoad(object state) {
144      Task task = (Task)state;
145      task.storable = PersistenceManager.Load(task.filename);
146      LoadFinished(task);
147    }
148    private delegate void TaskFinishedDelegate(Task task);
149    private void LoadFinished(Task task) {
150      if (InvokeRequired)
151        Invoke(new TaskFinishedDelegate(LoadFinished), task);
152      else {
153        IEditor editor = null;
154        if (task.storable != null) {
155          IEditable editable = task.storable as IEditable;
156          if (editable != null)
157            editor = editable.CreateEditor();
158        }
159        if (editor == null)
160          MessageBox.Show("Could not open item. The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
161        else {
162          editor.Filename = task.filename;
163          PluginManager.ControlManager.ShowControl(editor);
164        }
165        lock (locker) {
166          runningTasks--;
167          if (runningTasks == 0)
168            Cursor = Cursors.Default;
169        }
170      }
171    }
172    private void Save(EditorForm form) {
173      if (form.Editor.Filename == null)
174        SaveAs(form);
175      else {
176        lock (locker) runningTasks++;
177        Cursor = Cursors.AppStarting;
178        ((Control)form.Editor).Enabled = false;
179        EnableDisableItems();
180        Task task = new Task(form.Editor.Filename, form.Editor.Item, form.Editor);
181        ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousSave), task);
182      }
183    }
184    private void SaveAs(EditorForm form) {
185      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
186        form.Editor.Filename = saveFileDialog.FileName;
187        Save(form);
188      }
189    }
190    private void AsynchronousSave(object state) {
191      Task task = (Task)state;
192      PersistenceManager.Save(task.storable, task.filename);
193      SaveFinished(task);
194    }
195    private void SaveFinished(Task task) {
196      if (InvokeRequired)
197        Invoke(new TaskFinishedDelegate(SaveFinished), task);
198      else {
199        ((Control)task.editor).Enabled = true;
200        EnableDisableItems();
201        lock (locker) {
202          runningTasks--;
203          if (runningTasks == 0)
204            Cursor = Cursors.Default;
205        }
206      }
207    }
208    #endregion
209
210    private void MainForm_MdiChildActivate(object sender, EventArgs e) {
211      EnableDisableItems();
212    }
213
214    #region Menu Events
215    private void newToolStripMenuItem_Click(object sender, EventArgs e) {
216      ToolStripItem item = (ToolStripItem)sender;
217      Type type = (Type)item.Tag;
218      IEditable editable = (IEditable)Activator.CreateInstance(type);
219      if (editable == null) {
220        MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
221      } else {
222        IEditor editor = editable.CreateEditor();
223        if (editor == null) {
224          MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
225        } else {
226          PluginManager.ControlManager.ShowControl(editor);
227          EnableDisableItems();
228        }
229      }
230    }
231    private void openToolStripMenuItem_Click(object sender, EventArgs e) {
232      Open();
233    }
234    private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
235      EditorForm form = ActiveMdiChild as EditorForm;
236      Save(form);
237    }
238    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
239      EditorForm form = ActiveMdiChild as EditorForm;
240      SaveAs(form);
241    }
242    private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
243      for (int i = 0; i < MdiChildren.Length; i++) {
244        EditorForm form = MdiChildren[i] as EditorForm;
245        if (((Control)form.Editor).Enabled) Save(form);
246      }
247    }
248    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
249      ActiveMdiChild.Close();
250      EnableDisableItems();
251    }
252    private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
253      while (MdiChildren.Length > 0)
254        MdiChildren[0].Close();
255      EnableDisableItems();
256    }
257    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
258      Application.Exit();
259    }
260    private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
261      AvailableOperatorsForm form = new AvailableOperatorsForm();
262      form.MdiParent = this;
263      form.Show();
264    }
265    private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
266      GC.Collect();
267    }
268    private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) {
269      LayoutMdi(MdiLayout.Cascade);
270    }
271    private void tileHorizontallyToolStripMenuItem_Click(object sender, EventArgs e) {
272      LayoutMdi(MdiLayout.TileHorizontal);
273    }
274    private void tileVerticallyToolStripMenuItem_Click(object sender, EventArgs e) {
275      LayoutMdi(MdiLayout.TileVertical);
276    }
277    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
278      AboutDialog dialog = new AboutDialog();
279      dialog.ShowDialog(this);
280      dialog.Dispose();
281    }
282    #endregion
283
284    #region ToolStrip Events
285    private void openToolStripButton_Click(object sender, EventArgs e) {
286      Open();
287    }
288    private void saveToolStripButton_Click(object sender, EventArgs e) {
289      EditorForm form = ActiveMdiChild as EditorForm;
290      Save(form);
291    }
292    private void saveAllToolStripButton_Click(object sender, EventArgs e) {
293      for (int i = 0; i < MdiChildren.Length; i++) {
294        EditorForm form = MdiChildren[i] as EditorForm;
295        if (((Control)form.Editor).Enabled) Save(form);
296      }
297    }
298    #endregion
299  }
300}
Note: See TracBrowser for help on using the repository browser.