[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Data;
|
---|
| 26 | using System.Drawing;
|
---|
| 27 | using System.Text;
|
---|
[2591] | 28 | using System.Linq;
|
---|
[2] | 29 | using System.Threading;
|
---|
| 30 | using System.Windows.Forms;
|
---|
| 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 | using HeuristicLab.Core;
|
---|
[660] | 33 | using System.IO;
|
---|
[2] | 34 |
|
---|
| 35 | namespace HeuristicLab.OptimizationFrontend {
|
---|
| 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 | // discover creatable items
|
---|
[2591] | 62 | IEnumerable<Type> creatables = from x in ApplicationManager.Manager.GetTypes(typeof(IEditable))
|
---|
| 63 | orderby x.Name
|
---|
| 64 | select x;
|
---|
[2576] | 65 | foreach (Type type in creatables) {
|
---|
| 66 | if (!type.IsAbstract) {
|
---|
[2] | 67 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
| 68 | item.Tag = type;
|
---|
| 69 | item.Text = "&" + type.Name + "...";
|
---|
| 70 | item.Click += new EventHandler(newToolStripMenuItem_Click);
|
---|
| 71 | newToolStripMenuItem.DropDownItems.Add(item);
|
---|
| 72 |
|
---|
| 73 | item = new ToolStripMenuItem();
|
---|
| 74 | item.Tag = type;
|
---|
| 75 | item.Text = "&" + type.Name + "...";
|
---|
| 76 | item.Click += new EventHandler(newToolStripMenuItem_Click);
|
---|
| 77 | newToolStripDropDownButton.DropDownItems.Add(item);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | #region IControlManager Members
|
---|
[2591] | 83 | public void ShowControl(object control) {
|
---|
| 84 | if (InvokeRequired) Invoke((Action<object>)ShowControl, control);
|
---|
[2576] | 85 | else {
|
---|
| 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 | }
|
---|
[2] | 97 | }
|
---|
| 98 | }
|
---|
| 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | private void EnableDisableItems() {
|
---|
| 102 | closeToolStripMenuItem.Enabled = false;
|
---|
| 103 | closeAllToolStripMenuItem.Enabled = false;
|
---|
| 104 | saveToolStripMenuItem.Enabled = false;
|
---|
| 105 | saveToolStripButton.Enabled = false;
|
---|
| 106 | saveAsToolStripMenuItem.Enabled = false;
|
---|
| 107 | saveAllToolStripMenuItem.Enabled = false;
|
---|
| 108 | saveAllToolStripButton.Enabled = false;
|
---|
| 109 | cascadeToolStripMenuItem.Enabled = false;
|
---|
| 110 | tileHorizontallyToolStripMenuItem.Enabled = false;
|
---|
| 111 | tileVerticallyToolStripMenuItem.Enabled = false;
|
---|
| 112 |
|
---|
[2576] | 113 | if (ActiveMdiChild != null) {
|
---|
[2] | 114 | closeToolStripMenuItem.Enabled = true;
|
---|
| 115 | closeAllToolStripMenuItem.Enabled = true;
|
---|
| 116 | saveAllToolStripMenuItem.Enabled = true;
|
---|
| 117 | saveAllToolStripButton.Enabled = true;
|
---|
| 118 | cascadeToolStripMenuItem.Enabled = true;
|
---|
| 119 | tileHorizontallyToolStripMenuItem.Enabled = true;
|
---|
| 120 | tileVerticallyToolStripMenuItem.Enabled = true;
|
---|
| 121 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
[2576] | 122 | if (form != null) {
|
---|
| 123 | if (((Control)form.Editor).Enabled) {
|
---|
[2] | 124 | saveToolStripMenuItem.Enabled = true;
|
---|
| 125 | saveToolStripButton.Enabled = true;
|
---|
| 126 | saveAsToolStripMenuItem.Enabled = true;
|
---|
| 127 | } else {
|
---|
| 128 | closeToolStripMenuItem.Enabled = false;
|
---|
| 129 | closeAllToolStripMenuItem.Enabled = false;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | #region Open and Save Methods
|
---|
| 136 | private void Open() {
|
---|
[2576] | 137 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 138 | lock (locker) runningTasks++;
|
---|
[2] | 139 | Cursor = Cursors.AppStarting;
|
---|
| 140 | Task task = new Task(openFileDialog.FileName, null, null);
|
---|
| 141 | ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousLoad), task);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | private void AsynchronousLoad(object state) {
|
---|
| 145 | Task task = (Task)state;
|
---|
[660] | 146 | try {
|
---|
| 147 | task.storable = PersistenceManager.Load(task.filename);
|
---|
[2576] | 148 | }
|
---|
| 149 | catch (FileNotFoundException ex) {
|
---|
[660] | 150 | MessageBox.Show("Sorry couldn't open file \"" + task.filename + "\".\nThe file or plugin \"" + ex.FileName + "\" is not available.\nPlease make sure you have all necessary plugins installed.",
|
---|
| 151 | "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
[2576] | 152 | }
|
---|
| 153 | catch (TypeLoadException typeLoadEx) {
|
---|
[675] | 154 | 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.",
|
---|
| 155 | "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
[660] | 156 | }
|
---|
[675] | 157 |
|
---|
[2] | 158 | LoadFinished(task);
|
---|
| 159 | }
|
---|
| 160 | private delegate void TaskFinishedDelegate(Task task);
|
---|
| 161 | private void LoadFinished(Task task) {
|
---|
[2576] | 162 | if (InvokeRequired)
|
---|
[2] | 163 | Invoke(new TaskFinishedDelegate(LoadFinished), task);
|
---|
| 164 | else {
|
---|
| 165 | IEditor editor = null;
|
---|
[2576] | 166 | if (task.storable != null) {
|
---|
[2] | 167 | IEditable editable = task.storable as IEditable;
|
---|
[2576] | 168 | if (editable != null)
|
---|
[2] | 169 | editor = editable.CreateEditor();
|
---|
| 170 | }
|
---|
[2576] | 171 | if (editor == null)
|
---|
[2] | 172 | MessageBox.Show("Could not open item. The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 173 | else {
|
---|
| 174 | editor.Filename = task.filename;
|
---|
[2591] | 175 | ControlManager.Manager.ShowControl(editor);
|
---|
[2] | 176 | }
|
---|
[2576] | 177 | lock (locker) {
|
---|
[2] | 178 | runningTasks--;
|
---|
[2576] | 179 | if (runningTasks == 0)
|
---|
[2] | 180 | Cursor = Cursors.Default;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | private void Save(EditorForm form) {
|
---|
[2576] | 185 | if (form.Editor.Filename == null)
|
---|
[2] | 186 | SaveAs(form);
|
---|
| 187 | else {
|
---|
[2576] | 188 | lock (locker) runningTasks++;
|
---|
[2] | 189 | Cursor = Cursors.AppStarting;
|
---|
| 190 | ((Control)form.Editor).Enabled = false;
|
---|
| 191 | EnableDisableItems();
|
---|
| 192 | Task task = new Task(form.Editor.Filename, form.Editor.Item, form.Editor);
|
---|
| 193 | ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousSave), task);
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | private void SaveAs(EditorForm form) {
|
---|
[2576] | 197 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[2] | 198 | form.Editor.Filename = saveFileDialog.FileName;
|
---|
| 199 | Save(form);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | private void AsynchronousSave(object state) {
|
---|
| 203 | Task task = (Task)state;
|
---|
| 204 | PersistenceManager.Save(task.storable, task.filename);
|
---|
| 205 | SaveFinished(task);
|
---|
| 206 | }
|
---|
| 207 | private void SaveFinished(Task task) {
|
---|
[2576] | 208 | if (InvokeRequired)
|
---|
[2] | 209 | Invoke(new TaskFinishedDelegate(SaveFinished), task);
|
---|
| 210 | else {
|
---|
| 211 | ((Control)task.editor).Enabled = true;
|
---|
| 212 | EnableDisableItems();
|
---|
[2576] | 213 | lock (locker) {
|
---|
[2] | 214 | runningTasks--;
|
---|
[2576] | 215 | if (runningTasks == 0)
|
---|
[2] | 216 | Cursor = Cursors.Default;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | #endregion
|
---|
| 221 |
|
---|
| 222 | private void MainForm_MdiChildActivate(object sender, EventArgs e) {
|
---|
| 223 | EnableDisableItems();
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | #region Menu Events
|
---|
| 227 | private void newToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 228 | ToolStripItem item = (ToolStripItem)sender;
|
---|
| 229 | Type type = (Type)item.Tag;
|
---|
| 230 | IEditable editable = (IEditable)Activator.CreateInstance(type);
|
---|
[2576] | 231 | if (editable == null) {
|
---|
[2] | 232 | MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 233 | } else {
|
---|
| 234 | IEditor editor = editable.CreateEditor();
|
---|
[2576] | 235 | if (editor == null) {
|
---|
[2] | 236 | MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 237 | } else {
|
---|
[2591] | 238 | ControlManager.Manager.ShowControl(editor);
|
---|
[2] | 239 | EnableDisableItems();
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | private void openToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 244 | Open();
|
---|
| 245 | }
|
---|
| 246 | private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 247 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
| 248 | Save(form);
|
---|
| 249 | }
|
---|
| 250 | private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 251 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
| 252 | SaveAs(form);
|
---|
| 253 | }
|
---|
| 254 | private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2576] | 255 | for (int i = 0; i < MdiChildren.Length; i++) {
|
---|
[2] | 256 | EditorForm form = MdiChildren[i] as EditorForm;
|
---|
[2576] | 257 | if (((Control)form.Editor).Enabled) Save(form);
|
---|
[2] | 258 | }
|
---|
| 259 | }
|
---|
| 260 | private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 261 | ActiveMdiChild.Close();
|
---|
| 262 | EnableDisableItems();
|
---|
| 263 | }
|
---|
| 264 | private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2576] | 265 | while (MdiChildren.Length > 0)
|
---|
[2] | 266 | MdiChildren[0].Close();
|
---|
| 267 | EnableDisableItems();
|
---|
| 268 | }
|
---|
| 269 | private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 270 | Application.Exit();
|
---|
| 271 | }
|
---|
| 272 | private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 273 | AvailableOperatorsForm form = new AvailableOperatorsForm();
|
---|
| 274 | form.MdiParent = this;
|
---|
| 275 | form.Show();
|
---|
| 276 | }
|
---|
| 277 | private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 278 | GC.Collect();
|
---|
| 279 | }
|
---|
| 280 | private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 281 | LayoutMdi(MdiLayout.Cascade);
|
---|
| 282 | }
|
---|
| 283 | private void tileHorizontallyToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 284 | LayoutMdi(MdiLayout.TileHorizontal);
|
---|
| 285 | }
|
---|
| 286 | private void tileVerticallyToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 287 | LayoutMdi(MdiLayout.TileVertical);
|
---|
| 288 | }
|
---|
| 289 | private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 290 | AboutDialog dialog = new AboutDialog();
|
---|
| 291 | dialog.ShowDialog(this);
|
---|
| 292 | dialog.Dispose();
|
---|
| 293 | }
|
---|
| 294 | #endregion
|
---|
| 295 |
|
---|
| 296 | #region ToolStrip Events
|
---|
| 297 | private void openToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 298 | Open();
|
---|
| 299 | }
|
---|
| 300 | private void saveToolStripButton_Click(object sender, EventArgs e) {
|
---|
| 301 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
| 302 | Save(form);
|
---|
| 303 | }
|
---|
| 304 | private void saveAllToolStripButton_Click(object sender, EventArgs e) {
|
---|
[2576] | 305 | for (int i = 0; i < MdiChildren.Length; i++) {
|
---|
[2] | 306 | EditorForm form = MdiChildren[i] as EditorForm;
|
---|
[2576] | 307 | if (form != null && ((Control)form.Editor).Enabled) Save(form);
|
---|
[2] | 308 | }
|
---|
| 309 | }
|
---|
| 310 | #endregion
|
---|
| 311 | }
|
---|
| 312 | }
|
---|