Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/LabelProviderBase.cs @ 13229

Last change on this file since 13229 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 6.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
7using Microsoft.Research.DynamicDataDisplay.Common;
8using System.ComponentModel;
9
10namespace Microsoft.Research.DynamicDataDisplay.Charts.Axes
11{
12  /// <summary>
13  /// Contains data for custom generation of tick's label.
14  /// </summary>
15  /// <typeparam name="T">Type of ticks</typeparam>
16  public sealed class LabelTickInfo<T>
17  {
18    internal LabelTickInfo() { }
19
20    /// <summary>
21    /// Gets or sets the tick.
22    /// </summary>
23    /// <value>The tick.</value>
24    public T Tick { get; internal set; }
25    /// <summary>
26    /// Gets or sets additional info about ticks range.
27    /// </summary>
28    /// <value>The info.</value>
29    public object Info { get; internal set; }
30    /// <summary>
31    /// Gets or sets the index of tick in ticks array.
32    /// </summary>
33    /// <value>The index.</value>
34    public int Index { get; internal set; }
35  }
36
37  /// <summary>
38  /// Base class for all label providers.
39  /// Contains a number of properties that can be used to adjust generated labels.
40  /// </summary>
41  /// <typeparam name="T">Type of ticks, which labels are generated for</typeparam>
42  /// <remarks>
43  /// Order of apllication of custom label string properties:
44  /// If CustomFormatter is not null, it is called first.
45  /// Then, if it was null or if it returned null string,
46  /// virtual GetStringCore method is called. It can be overloaded in subclasses. GetStringCore should not return null.
47  /// Then if LabelStringFormat is not null, it is applied.
48  /// After label's UI was created, you can change it by setting CustomView delegate - it allows you to adjust
49  /// UI properties of label. Note: not all labelProviders takes CustomView into account.
50  /// </remarks>
51  public abstract class LabelProviderBase<T>
52  {
53
54    #region Private
55
56    private string labelStringFormat = null;
57    private Func<LabelTickInfo<T>, string> customFormatter = null;
58    private Action<LabelTickInfo<T>, UIElement> customView = null;
59
60    #endregion
61
62    private static readonly UIElement[] emptyLabelsArray = new UIElement[0];
63    protected static UIElement[] EmptyLabelsArray
64    {
65      get { return emptyLabelsArray; }
66    }
67
68    /// <summary>
69    /// Creates labels by given ticks info.
70    /// Is not intended to be called from your code.
71    /// </summary>
72    /// <param name="ticksInfo">The ticks info.</param>
73    /// <returns>Array of <see cref="UIElement"/>s, which are axis labels for specified axis ticks.</returns>
74    [EditorBrowsable(EditorBrowsableState.Never)]
75    public abstract UIElement[] CreateLabels(ITicksInfo<T> ticksInfo);
76
77    /// <summary>
78    /// Gets or sets the label string format.
79    /// </summary>
80    /// <value>The label string format.</value>
81    public string LabelStringFormat
82    {
83      get { return labelStringFormat; }
84      set
85      {
86        if (labelStringFormat != value)
87        {
88          labelStringFormat = value;
89          RaiseChanged();
90        }
91      }
92    }
93
94    /// <summary>
95    /// Gets or sets the custom formatter - delegate that can be called to create custom string representation of tick.
96    /// </summary>
97    /// <value>The custom formatter.</value>
98    public Func<LabelTickInfo<T>, string> CustomFormatter
99    {
100      get { return customFormatter; }
101      set
102      {
103        if (customFormatter != value)
104        {
105          customFormatter = value;
106          RaiseChanged();
107        }
108      }
109    }
110
111    /// <summary>
112    /// Gets or sets the custom view - delegate that is used to create a custom, non-default look of axis label.
113    /// Can be used to adjust some UI properties of generated label.
114    /// </summary>
115    /// <value>The custom view.</value>
116    public Action<LabelTickInfo<T>, UIElement> CustomView
117    {
118      get { return customView; }
119      set
120      {
121        if (customView != value)
122        {
123          customView = value;
124          RaiseChanged();
125        }
126      }
127    }
128
129    /// <summary>
130    /// Sets the custom formatter.
131    /// This is alternative to CustomFormatter property setter, the only difference is that Visual Studio shows
132    /// more convenient tooltip for methods rather than for properties' setters.
133    /// </summary>
134    /// <param name="formatter">The formatter.</param>
135    public void SetCustomFormatter(Func<LabelTickInfo<T>, string> formatter)
136    {
137      CustomFormatter = formatter;
138    }
139
140    /// <summary>
141    /// Sets the custom view.
142    /// This is alternative to CustomView property setter, the only difference is that Visual Studio shows
143    /// more convenient tooltip for methods rather than for properties' setters.
144    /// </summary>
145    /// <param name="view">The view.</param>
146    public void SetCustomView(Action<LabelTickInfo<T>, UIElement> view)
147    {
148      CustomView = view;
149    }
150
151    protected virtual string GetString(LabelTickInfo<T> tickInfo)
152    {
153      string text = null;
154      if (CustomFormatter != null)
155      {
156        text = CustomFormatter(tickInfo);
157      }
158      if (text == null)
159      {
160        text = GetStringCore(tickInfo);
161
162        if (text == null)
163          throw new ArgumentNullException(Strings.Exceptions.TextOfTickShouldNotBeNull);
164      }
165      if (LabelStringFormat != null)
166      {
167        text = String.Format(LabelStringFormat, text);
168      }
169
170      return text;
171    }
172
173    protected virtual string GetStringCore(LabelTickInfo<T> tickInfo)
174    {
175      return tickInfo.Tick.ToString();
176    }
177
178    protected void ApplyCustomView(LabelTickInfo<T> info, UIElement label)
179    {
180      if (CustomView != null)
181      {
182        CustomView(info, label);
183      }
184    }
185
186    /// <summary>
187    /// Occurs when label provider is changed.
188    /// Notifies axis to update its view.
189    /// </summary>
190    public event EventHandler Changed;
191    protected void RaiseChanged()
192    {
193      Changed.Raise(this);
194    }
195
196    private readonly ResourcePool<UIElement> pool = new ResourcePool<UIElement>();
197    internal void ReleaseLabel(UIElement label)
198    {
199      if (ReleaseCore(label))
200      {
201        pool.Put(label);
202      }
203    }
204
205    protected virtual bool ReleaseCore(UIElement label) { return false; }
206
207    protected UIElement GetResourceFromPool()
208    {
209      return pool.Get();
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.