Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.AdvancedOptimizationFrontend/MainForm.cs @ 241

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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