Changeset 3483 for trunk/sources/HeuristicLab.Optimizer
- Timestamp:
- 04/22/10 05:14:39 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimizer/3.3
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs
r3416 r3483 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.IO;25 23 using System.Linq; 26 using System.Threading;27 24 using System.Windows.Forms; 25 using HeuristicLab.Common; 28 26 using HeuristicLab.Core.Views; 29 27 using HeuristicLab.MainForm; 30 using HeuristicLab.MainForm.WindowsForms;31 using HeuristicLab.Persistence.Default.Xml;32 28 33 29 namespace HeuristicLab.Optimizer { 34 30 internal static class FileManager { 35 36 #region Private Class FileInfo37 private class FileInfo {38 public string Filename { get; set; }39 public bool Compressed { get; set; }40 41 public FileInfo(string filename, bool compressed) {42 Filename = filename;43 Compressed = compressed;44 }45 public FileInfo(string filename)46 : this(filename, true) {47 }48 public FileInfo()49 : this(string.Empty, true) {50 }51 }52 #endregion53 54 private static Dictionary<IContentView, FileInfo> files;55 31 private static NewItemDialog newItemDialog; 56 32 private static OpenFileDialog openFileDialog; 57 33 private static SaveFileDialog saveFileDialog; 58 private static int waitingCursors;59 private static int newDocumentsCounter;60 34 61 35 static FileManager() { 62 files = new Dictionary<IContentView, FileInfo>();63 36 newItemDialog = null; 64 37 openFileDialog = null; 65 38 saveFileDialog = null; 66 waitingCursors = 0;67 newDocumentsCounter = 1;68 // NOTE: Events fired by the main form are registered in HeuristicLabOptimizerApplication.69 39 } 70 40 … … 73 43 if (newItemDialog.ShowDialog() == DialogResult.OK) { 74 44 IView view = MainFormManager.CreateDefaultView(newItemDialog.Item); 75 if (view == null) { 76 MessageBox.Show("There is no view for the new item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error); 77 } else { 78 if (view is IContentView) { 79 view.Caption = "Item" + newDocumentsCounter.ToString() + ".hl"; 80 newDocumentsCounter++; 81 } 82 view.Show(); 83 } 45 if (view == null) MessageBox.Show("There is no view for the new item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error); 46 else view.Show(); 84 47 } 85 48 } … … 96 59 97 60 if (openFileDialog.ShowDialog() == DialogResult.OK) { 98 foreach (string filename in openFileDialog.FileNames) 99 LoadItemAsync(filename); 61 foreach (string filename in openFileDialog.FileNames) { 62 ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor(); 63 ContentManager.LoadAsync(filename, LoadingCompleted); 64 } 65 } 66 } 67 private static void LoadingCompleted(IStorableContent content, Exception error) { 68 try { 69 if (error != null) throw error; 70 Invoke(delegate() { 71 IView view = MainFormManager.CreateDefaultView(content); 72 if (view == null) MessageBox.Show("There is no view for the loaded item. It cannot be displayed.", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error); 73 else view.Show(); 74 }); 75 } 76 catch (Exception ex) { 77 Auxiliary.ShowErrorMessageBox(ex); 78 } 79 finally { 80 ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor(); 100 81 } 101 82 } … … 108 89 } 109 90 private static void Save(IContentView view) { 110 if (!view.Locked) { 111 if ((!files.ContainsKey(view)) || (!File.Exists(files[view].Filename))) { 91 IStorableContent content = view.Content as IStorableContent; 92 if (!view.Locked && (content != null)) { 93 if (string.IsNullOrEmpty(content.Filename)) 112 94 SaveAs(view); 113 } else { 114 if (files[view].Compressed) 115 SaveItemAsync(view, files[view].Filename, 9); 116 else 117 SaveItemAsync(view, files[view].Filename, 0); 95 else { 96 ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor(); 97 ((Form)MainFormManager.MainForm).Enabled = false; 98 ContentManager.SaveAsync(content, content.Filename, true, SavingCompleted); 118 99 } 119 100 } 120 101 } 121 122 102 public static void SaveAs() { 123 103 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; … … 127 107 } 128 108 public static void SaveAs(IContentView view) { 129 if (!view.Locked) { 109 IStorableContent content = view.Content as IStorableContent; 110 if (!view.Locked && (content != null)) { 130 111 if (saveFileDialog == null) { 131 112 saveFileDialog = new SaveFileDialog(); … … 135 116 saveFileDialog.FilterIndex = 2; 136 117 } 137 138 if (!files.ContainsKey(view)) { 139 files.Add(view, new FileInfo()); 140 saveFileDialog.FileName = view.Caption; 141 } else { 142 saveFileDialog.FileName = files[view].Filename; 143 } 144 if (!files[view].Compressed) 145 saveFileDialog.FilterIndex = 1; 146 else 147 saveFileDialog.FilterIndex = 2; 118 saveFileDialog.FileName = string.IsNullOrEmpty(content.Filename) ? "Item" : content.Filename; 148 119 149 120 if (saveFileDialog.ShowDialog() == DialogResult.OK) { 121 ((OptimizerMainForm)MainFormManager.MainForm).SetAppStartingCursor(); 122 ((Form)MainFormManager.MainForm).Enabled = false; 150 123 if (saveFileDialog.FilterIndex == 1) { 151 SaveItemAsync(view, saveFileDialog.FileName, 0);124 ContentManager.SaveAsync(content, saveFileDialog.FileName, false, SavingCompleted); 152 125 } else { 153 SaveItemAsync(view, saveFileDialog.FileName, 9);126 ContentManager.SaveAsync(content, saveFileDialog.FileName, true, SavingCompleted); 154 127 } 155 128 } 156 129 } 157 130 } 158 159 131 public static void SaveAll() { 160 132 foreach (IContentView view in MainFormManager.MainForm.Views.OfType<IContentView>()) 161 133 Save(view); 162 134 } 163 164 // NOTE: This event is fired by the main form. It is registered in HeuristicLabOptimizerApplication. 165 internal static void ViewClosed(object sender, ViewEventArgs e) { 166 IContentView view = e.View as IContentView; 167 if (view != null) files.Remove(view); 135 private static void SavingCompleted(IStorableContent content, Exception error) { 136 try { 137 if (error != null) throw error; 138 Invoke(delegate() { 139 ((OptimizerMainForm)MainFormManager.MainForm).UpdateTitle(); 140 ((Form)MainFormManager.MainForm).Enabled = true; 141 }); 142 } 143 catch (Exception ex) { 144 Auxiliary.ShowErrorMessageBox(ex); 145 } 146 finally { 147 ((OptimizerMainForm)MainFormManager.MainForm).ResetAppStartingCursor(); 148 } 168 149 } 169 150 170 #region Asynchronous Save/Load Operations171 151 private static void Invoke(Action a) { 172 152 Form form = MainFormManager.MainForm as Form; … … 176 156 a.Invoke(); 177 157 } 178 179 private static void SaveItemAsync(IContentView view, string filename, int compression) {180 ThreadPool.QueueUserWorkItem(181 new WaitCallback(182 delegate(object arg) {183 try {184 DisableView(view);185 SetWaitingCursor();186 XmlGenerator.Serialize(view.Content, filename, compression);187 Invoke(delegate() {188 view.Caption = Path.GetFileName(filename);189 files[view].Filename = filename;190 files[view].Compressed = compression > 0;191 });192 }193 catch (Exception ex) {194 Auxiliary.ShowErrorMessageBox(ex);195 } finally {196 ResetWaitingCursor();197 EnableView(view);198 }199 }200 )201 );202 }203 private static void LoadItemAsync(string filename) {204 ThreadPool.QueueUserWorkItem(205 new WaitCallback(206 delegate(object arg) {207 try {208 SetWaitingCursor();209 object content = XmlParser.Deserialize(filename);210 Invoke(delegate() {211 IContentView view = MainFormManager.CreateDefaultView(content) as IContentView;212 if (view == null) {213 MessageBox.Show("There is no view for the loaded item. It cannot be displayed. ", "No View Available", MessageBoxButtons.OK, MessageBoxIcon.Error);214 } else {215 view.Caption = Path.GetFileName(filename);216 files.Add(view, new FileInfo(filename));217 view.Show();218 }219 });220 } catch (Exception ex) {221 Auxiliary.ShowErrorMessageBox(ex);222 } finally {223 ResetWaitingCursor();224 }225 }226 )227 );228 }229 230 private static void SetWaitingCursor() {231 Invoke(delegate() {232 waitingCursors++;233 ((Form)MainFormManager.MainForm).Cursor = Cursors.AppStarting;234 });235 }236 private static void ResetWaitingCursor() {237 Invoke(delegate() {238 waitingCursors--;239 if (waitingCursors == 0) ((Form)MainFormManager.MainForm).Cursor = Cursors.Default;240 });241 }242 private static void DisableView(IView view) {243 Invoke(delegate() {244 ((Control)view).SuspendRepaint();245 ((Control)view).Enabled = false;246 ((Control)view).ResumeRepaint(true);247 });248 }249 private static void EnableView(IView view) {250 Invoke(delegate() {251 ((Control)view).SuspendRepaint();252 ((Control)view).Enabled = true;253 ((Control)view).ResumeRepaint(true);254 });255 }256 #endregion257 158 } 258 159 } -
trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/CopyToClipboardMenuItem.cs
r3416 r3483 39 39 get { return 2100; } 40 40 } 41 public override Keys ShortCutKeys {42 get { return Keys.Control | Keys.C; }43 }44 41 45 42 protected override void OnToolStripItemSet(EventArgs e) { … … 48 45 protected override void OnActiveViewChanged(object sender, EventArgs e) { 49 46 ItemView activeView = MainFormManager.MainForm.ActiveView as ItemView; 50 ToolStripItem.Enabled = (activeView != null) && ( !activeView.Locked);47 ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && !activeView.Locked; 51 48 } 52 49 53 50 public override void Execute() { 54 51 ItemView activeView = MainFormManager.MainForm.ActiveView as ItemView; 55 if ((activeView != null) && ( !activeView.Locked)) {52 if ((activeView != null) && (activeView.Content != null) && !activeView.Locked) { 56 53 Clipboard<IItem> clipboard = ((OptimizerMainForm)MainFormManager.MainForm).Clipboard; 57 54 clipboard.AddItem((IItem)activeView.Content.Clone()); -
trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/SaveAllMenuItem.cs
r3416 r3483 25 25 using System.Linq; 26 26 using System.Windows.Forms; 27 using HeuristicLab.Common; 27 28 using HeuristicLab.MainForm; 28 29 … … 47 48 protected override void OnActiveViewChanged(object sender, EventArgs e) { 48 49 var views = from v in MainFormManager.MainForm.Views.OfType<IContentView>() 50 where v.Content != null 51 where v.Content is IStorableContent 49 52 where !v.Locked 50 53 select v; -
trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/SaveAsMenuItem.cs
r3416 r3483 23 23 using System.Collections.Generic; 24 24 using System.Windows.Forms; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.MainForm; 26 27 … … 45 46 protected override void OnActiveViewChanged(object sender, EventArgs e) { 46 47 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 47 ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked); 48 ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && 49 (activeView.Content is IStorableContent) && !activeView.Locked; 48 50 } 49 51 -
trunk/sources/HeuristicLab.Optimizer/3.3/MenuItems/SaveMenuItem.cs
r3416 r3483 24 24 using System.Drawing; 25 25 using System.Windows.Forms; 26 using HeuristicLab.Common; 26 27 using HeuristicLab.MainForm; 27 28 … … 49 50 protected override void OnActiveViewChanged(object sender, EventArgs e) { 50 51 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 51 ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked); 52 ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && 53 (activeView.Content is IStorableContent) && !activeView.Locked; 52 54 } 53 55 -
trunk/sources/HeuristicLab.Optimizer/3.3/OptimizerMainForm.cs
r3395 r3483 32 32 namespace HeuristicLab.Optimizer { 33 33 internal partial class OptimizerMainForm : DockingMainForm { 34 private string title; 35 private int appStartingCursors; 36 private int waitingCursors; 37 34 38 private Clipboard<IItem> clipboard; 35 39 public Clipboard<IItem> Clipboard { … … 40 44 : base() { 41 45 InitializeComponent(); 46 appStartingCursors = 0; 47 waitingCursors = 0; 42 48 } 43 49 public OptimizerMainForm(Type userInterfaceItemType) 44 50 : base(userInterfaceItemType) { 45 51 InitializeComponent(); 52 appStartingCursors = 0; 53 waitingCursors = 0; 46 54 } 47 55 public OptimizerMainForm(Type userInterfaceItemType, bool showViewsInViewHost) … … 54 62 AssemblyFileVersionAttribute version = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true). 55 63 Cast<AssemblyFileVersionAttribute>().FirstOrDefault(); 56 Title = "HeuristicLab Optimizer"; 57 if (version != null) Title += " " + version.Version; 58 ViewClosed += new EventHandler<ViewEventArgs>(FileManager.ViewClosed); 64 title = "HeuristicLab Optimizer"; 65 if (version != null) title += " " + version.Version; 66 Title = title; 67 68 ContentManager.Initialize(new PersistenceContentManager()); 59 69 60 70 clipboard = new Clipboard<IItem>(); … … 73 83 } 74 84 } 85 86 protected override void OnActiveViewChanged() { 87 base.OnActiveViewChanged(); 88 UpdateTitle(); 89 } 90 91 public void UpdateTitle() { 92 if (InvokeRequired) 93 Invoke(new Action(UpdateTitle)); 94 else { 95 IContentView activeView = ActiveView as IContentView; 96 if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IStorableContent)) { 97 IStorableContent content = (IStorableContent)activeView.Content; 98 Title = title + " [" + (string.IsNullOrEmpty(content.Filename) ? "Unsaved" : content.Filename) + "]"; 99 } else { 100 Title = title; 101 } 102 } 103 } 104 105 #region Cursor Handling 106 public void SetAppStartingCursor() { 107 if (InvokeRequired) 108 Invoke(new Action(SetAppStartingCursor)); 109 else { 110 appStartingCursors++; 111 SetCursor(); 112 } 113 } 114 public void ResetAppStartingCursor() { 115 if (InvokeRequired) 116 Invoke(new Action(ResetAppStartingCursor)); 117 else { 118 appStartingCursors--; 119 SetCursor(); 120 } 121 } 122 public void SetWaitCursor() { 123 if (InvokeRequired) 124 Invoke(new Action(SetWaitCursor)); 125 else { 126 waitingCursors++; 127 SetCursor(); 128 } 129 } 130 public void ResetWaitCursor() { 131 if (InvokeRequired) 132 Invoke(new Action(ResetWaitCursor)); 133 else { 134 waitingCursors--; 135 SetCursor(); 136 } 137 } 138 private void SetCursor() { 139 if (waitingCursors > 0) Cursor = Cursors.WaitCursor; 140 else if (appStartingCursors > 0) Cursor = Cursors.AppStarting; 141 else Cursor = Cursors.Default; 142 } 143 #endregion 75 144 } 76 145 } -
trunk/sources/HeuristicLab.Optimizer/3.3/ToolBarItems/SaveAllToolBarItem.cs
r3416 r3483 24 24 using System.Linq; 25 25 using System.Windows.Forms; 26 using HeuristicLab.Common; 26 27 using HeuristicLab.MainForm; 27 28 … … 46 47 protected override void OnActiveViewChanged(object sender, EventArgs e) { 47 48 var views = from v in MainFormManager.MainForm.Views.OfType<IContentView>() 49 where v.Content != null 50 where v.Content is IStorableContent 48 51 where !v.Locked 49 52 select v; -
trunk/sources/HeuristicLab.Optimizer/3.3/ToolBarItems/SaveToolBarItem.cs
r3416 r3483 23 23 using System.Drawing; 24 24 using System.Windows.Forms; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.MainForm; 26 27 … … 45 46 protected override void OnActiveViewChanged(object sender, EventArgs e) { 46 47 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 47 ToolStripItem.Enabled = (activeView != null) && (!activeView.Locked); 48 ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && 49 (activeView.Content is IStorableContent) && !activeView.Locked; 48 50 } 49 51
Note: See TracChangeset
for help on using the changeset viewer.