Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 781 was 676, checked in by gkronber, 16 years ago

fixed #303 (again)

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