Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Stylesheets/MediaList.cs @ 13777

Last change on this file since 13777 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 4.4 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>75</completed>
3
4using System;
5using System.Collections.Generic;
6using System.Text.RegularExpressions;
7
8namespace SharpVectors.Dom.Stylesheets
9{
10  /// <summary>
11  /// The MediaList interface provides the abstraction of an ordered collection of media, without defining or constraining how this collection is implemented. An empty list is the same as a list that contains the medium "all".
12  /// The items in the MediaList are accessible via an integral index, starting from 0.
13  /// </summary>
14  public sealed class MediaList : IMediaList
15  {
16    private static Regex splitter = new Regex(@"\s*,\s*");
17
18    #region Constructors
19    public MediaList() : this(String.Empty)
20    {
21    }
22
23    public MediaList(string val)
24    {
25      _parseString(val);
26    }
27    #endregion
28
29    private void _parseString(string mediaText)
30    {
31      mediaText = mediaText.Trim();
32      if(mediaText.Length > 0)
33      {
34        string[] ms = splitter.Split(mediaText);
35        foreach(string m in ms)
36        {
37          AppendMedium(m);
38        }
39      }
40    }
41
42    private void _clear()
43    {
44      medias.Clear();
45    }
46
47    #region Public methods
48    /// <summary>
49    /// Compares this MediaList with another and see if the second fits this
50    /// </summary>
51    /// <param name="inMedia">The MediaList to compare</param>
52    /// <returns>True if this list fits the specified</returns>
53    public bool Matches(MediaList inMedia)
54    {
55      if(inMedia.Length == 0) return false;
56      else if(Length == 0 || containsAll)
57      {
58        // is empty or this list contains "all"
59        return true;
60      }
61      else
62      {
63        for(ulong i = 0; i<inMedia.Length; i++)
64        {
65          if (medias.Contains(inMedia[i]))
66                        return true;
67        }
68      }
69      return false;
70    }
71
72    #endregion
73
74    #region Private fields
75    private bool containsAll = false; //speeds up matching if the list contains "all"
76        private List<string> medias = new List<string>();
77    #endregion
78
79    #region Implementation of IMediaList
80    /// <summary>
81    /// Adds the medium newMedium to the end of the list. If the newMedium is already used, it is first removed.
82    /// </summary>
83    /// <param name="newMedium">The new medium to add.</param>
84    /// <exception cref="DomException">INVALID_CHARACTER_ERR: If the medium contains characters that are invalid in the underlying style language.</exception>
85    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this media list is readonly.</exception>
86    public void AppendMedium(string newMedium)
87    {
88      if (newMedium.Length > 0)
89      {
90        medias.Remove(newMedium);
91        medias.Add(newMedium);
92        if (newMedium.Equals("all"))
93                    containsAll = true;
94      }
95    }
96
97    /// <summary>
98    /// Deletes the medium indicated by oldMedium from the list.
99    /// </summary>
100    /// <param name="oldMedium">The medium to delete in the media list.</param>
101    /// <exception cref="DomException">NOT_FOUND_ERR: Raised if oldMedium is not in the list.</exception>
102    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this media list is readonly.</exception>
103    public void DeleteMedium(string oldMedium)
104    {
105      if(oldMedium == "all") containsAll = false;
106      medias.Remove(oldMedium);
107    }
108
109    /// <summary>
110    /// The number of media in the list. The range of valid media is 0 to length-1 inclusive.
111    /// </summary>
112    public ulong Length
113    {
114      get
115      {
116        return (ulong)medias.Count;
117      }
118    }
119
120    /// <summary>
121    /// The parsable textual representation of the media list. This is a comma-separated list of media.
122    /// </summary>
123    /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified string value has a syntax error and is unparsable.</exception>
124    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this media list is readonly.</exception>
125    public string MediaText
126    {
127      get
128      {
129        string result = String.Empty;
130        foreach(string media in medias)
131        {
132          result += media + ",";
133        }
134        if(result.Length > 0)
135        {
136          result = result.Substring(0, result.Length-1);
137        }
138        return result;
139      }
140      set
141      {
142        _clear();
143        _parseString(value);
144      }
145    }
146
147    /// <summary>
148    /// Returns the indexth in the list. If index is greater than or equal to the number of media in the list, this returns null.
149    /// </summary>
150    public string this[ulong index]
151    {
152      get
153      {
154        return (index < Length) ? medias[(int)index] : null;
155      }
156    }
157
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.