Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/MovieView.cs @ 4641

Last change on this file since 4641 was 4641, checked in by swagner, 14 years ago

Worked on allele frequency analysis (#1234)

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
24using System.Threading;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Core.Views {
31  [View("Movie View")]
32  public partial class MovieView<T> : ItemView where T : class, IItem {
33    public new IItemCollection<T> Content {
34      get { return (IItemCollection<T>)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public MovieView() {
39      InitializeComponent();
40    }
41
42    protected override void DeregisterContentEvents() {
43      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
44      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
45      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
46      base.DeregisterContentEvents();
47    }
48    protected override void RegisterContentEvents() {
49      base.RegisterContentEvents();
50      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
51      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
52      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
53    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      if (backgroundWorker.IsBusy) backgroundWorker.CancelAsync();
58      if (Content == null) {
59        trackBar.Maximum = 10;
60        viewHost.Content = null;
61      } else {
62        Caption += " (" + Content.GetType().Name + ")";
63        trackBar.Maximum = Content.Count - 1;
64        viewHost.Content = Content.FirstOrDefault();
65      }
66      trackBar.Value = 0;
67      UpdateLables();
68    }
69
70    protected override void SetEnabledStateOfControls() {
71      base.SetEnabledStateOfControls();
72      firstButton.Enabled = (Content != null) && (Content.Count > 0) && (!backgroundWorker.IsBusy);
73      trackBar.Enabled = (Content != null) && (Content.Count > 0) && (!backgroundWorker.IsBusy);
74      lastButton.Enabled = (Content != null) && (Content.Count > 0) && (!backgroundWorker.IsBusy);
75      playButton.Enabled = (Content != null) && (Content.Count > 0) && (!backgroundWorker.IsBusy);
76      stopButton.Enabled = (Content != null) && (backgroundWorker.IsBusy);
77    }
78
79    protected override void OnClosed(FormClosedEventArgs e) {
80      base.OnClosed(e);
81      if (backgroundWorker.IsBusy) backgroundWorker.CancelAsync();
82    }
83
84    #region Content Events
85    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
86      if (InvokeRequired)
87        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
88      else {
89        trackBar.Maximum = Content.Count - 1;
90        maximumLabel.Text = trackBar.Maximum.ToString();
91        if (viewHost.Content == null) {
92          viewHost.Content = Content.FirstOrDefault();
93          SetEnabledStateOfControls();
94        }
95      }
96    }
97    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
98      if (InvokeRequired)
99        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
100      else {
101        trackBar.Maximum = Content.Count - 1;
102        maximumLabel.Text = trackBar.Maximum.ToString();
103        if (e.Items.Contains((T)viewHost.Content)) {
104          trackBar.Value = 0;
105          viewHost.Content = Content.FirstOrDefault();
106          UpdateLables();
107          SetEnabledStateOfControls();
108        }
109      }
110    }
111    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
112      if (InvokeRequired)
113        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
114      else {
115        trackBar.Maximum = Content.Count - 1;
116        trackBar.Value = 0;
117        viewHost.Content = Content.FirstOrDefault();
118        UpdateLables();
119        SetEnabledStateOfControls();
120      }
121    }
122    #endregion
123
124    #region Control Events
125    protected virtual void trackBar_ValueChanged(object sender, EventArgs e) {
126      indexLabel.Text = trackBar.Value.ToString();
127      viewHost.Content = Content == null ? null : Content.ElementAtOrDefault(trackBar.Value);
128    }
129    protected virtual void firstButton_Click(object sender, EventArgs e) {
130      trackBar.Value = trackBar.Minimum;
131    }
132    protected virtual void lastButton_Click(object sender, EventArgs e) {
133      trackBar.Value = trackBar.Maximum;
134    }
135    protected virtual void playButton_Click(object sender, EventArgs e) {
136      firstButton.Enabled = false;
137      trackBar.Enabled = false;
138      lastButton.Enabled = false;
139      playButton.Enabled = false;
140      stopButton.Enabled = true;
141      backgroundWorker.RunWorkerAsync();
142    }
143    protected virtual void stopButton_Click(object sender, EventArgs e) {
144      backgroundWorker.CancelAsync();
145    }
146    #endregion
147
148    #region BackgroundWorker Events
149    protected virtual void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
150      bool terminate = false;
151      while (!backgroundWorker.CancellationPending && !terminate) {
152        Invoke((Action)delegate() {
153          if (trackBar.Value < trackBar.Maximum) trackBar.Value++;
154          terminate = trackBar.Value == trackBar.Maximum;
155        });
156        Thread.Sleep(100);
157      }
158    }
159    protected virtual void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) {
160      firstButton.Enabled = true;
161      trackBar.Enabled = true;
162      lastButton.Enabled = true;
163      playButton.Enabled = true;
164      stopButton.Enabled = false;
165    }
166    #endregion
167
168    #region Helpers
169    protected virtual void UpdateLables() {
170      minimumLabel.Text = trackBar.Minimum.ToString();
171      indexLabel.Text = trackBar.Value.ToString();
172      maximumLabel.Text = trackBar.Maximum.ToString();
173    }
174    #endregion
175  }
176}
Note: See TracBrowser for help on using the repository browser.