Minor style changes
[castle.git] / MonoRail / Castle.MonoRail.Framework / Flash.cs
blob34b9109d301b0f52ed9df62bdbf23098701eb5fa
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework
17 using System;
18 using System.Runtime.Serialization;
19 using System.Collections;
21 /// <summary>
22 /// Keeps data across a single request using the session
23 /// </summary>
24 [Serializable]
25 public class Flash : Hashtable
27 /// <summary>
28 /// Flash key
29 /// </summary>
30 public static readonly String FlashKey = "flash";
32 [NonSerialized]
33 private ArrayList keep = new ArrayList();
35 [NonSerialized]
36 private bool hasItemsToKeep;
38 [NonSerialized]
39 private bool hasSwept;
41 /// <summary>
42 /// Initializes a new instance of the <see cref="Flash"/> class.
43 /// </summary>
44 public Flash()
48 /// <summary>
49 /// Initializes a new instance of the <see cref="Flash"/> class.
50 /// </summary>
51 /// <param name="copy">The copy.</param>
52 public Flash(Flash copy)
54 if (copy == null) return;
56 foreach(DictionaryEntry entry in copy)
58 base.Add(entry.Key, entry.Value);
62 internal Flash(SerializationInfo info, StreamingContext context) : base(info, context)
66 /// <summary>
67 /// Remove any element thats not marked to be kept.
68 /// This method is automatically called by the framework after the controller is processed.
69 /// </summary>
70 public void Sweep()
72 if (hasSwept)
74 object[] keys = new object[Keys.Count];
76 Keys.CopyTo(keys, 0);
78 if (keys.Length != 0)
80 keep.AddRange(keys);
84 if (keep.Count == 0)
86 Clear();
88 else
90 object[] keys = new object[Keys.Count];
92 Keys.CopyTo(keys, 0);
94 for(int i = 0; i < keys.Length; i++)
96 if (!keep.Contains(keys[i]))
98 Remove(keys[i]);
100 else if (!hasItemsToKeep)
102 hasItemsToKeep = true;
106 keep.Clear();
109 hasSwept = true;
112 /// <summary>
113 /// Keeps the entire flash contents available for the next action
114 /// </summary>
115 public void Keep()
117 keep.Clear();
119 foreach(object key in base.Keys)
121 keep.Add(key);
125 /// <summary>
126 /// Keeps the Flash['key'] contents available for the next action
127 /// </summary>
128 public void Keep(object key)
130 if (!keep.Contains(key)) keep.Add(key);
133 /// <summary>
134 /// Marks the entire flash to be discarded by the end of the current action
135 /// </summary>
136 public void Discard()
138 keep.Clear();
141 /// <summary>
142 /// Marks Flash[key] to be discarded by the end of the current action
143 /// </summary>
144 public void Discard(object key)
146 keep.Remove(key);
149 /// <summary>
150 /// Sets a flash that will not be available to the next action, only to the current.
151 /// <code>
152 /// Flash.Now( key, "Hello current action" )
153 /// </code>
154 /// <para>
155 /// This method enables you to use the flash as a central messaging system in your app.
156 /// When you need to pass an object to the next action, you use the standard flash assign (<c>[]=</c>).
157 /// When you need to pass an object to the current action, you use <c>Now</c>, and your object will
158 /// vanish when the current action is done.
159 /// </para>
160 /// <para>
161 /// Entries set via <c>Now</c> are accessed the same way as standard entries: <c>Flash['my-key']</c>.
162 /// </para>
163 /// </summary>
164 public void Now(object key, object value)
166 base[key] = value;
169 /// <summary>
170 /// Adds an element with the specified key and value into the <see cref="T:System.Collections.Hashtable"></see>.
171 /// </summary>
172 /// <param name="key">The key of the element to add.</param>
173 /// <param name="value">The value of the element to add. The value can be null.</param>
174 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Hashtable"></see> is read-only.-or- The <see cref="T:System.Collections.Hashtable"></see> has a fixed size. </exception>
175 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Hashtable"></see>. </exception>
176 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
177 public override void Add(object key, object value)
179 InternalAdd(key, value);
182 /// <summary>
183 /// Gets or sets the <see cref="System.Object"/> with the specified key.
184 /// </summary>
185 /// <value></value>
186 public override object this[object key]
188 get { return base[key]; }
189 set { InternalAdd(key, value); }
192 /// <summary>
193 /// Gets a value indicating whether this instance has items to keep.
194 /// </summary>
195 /// <value>
196 /// <c>true</c> if this instance has items to keep; otherwise, <c>false</c>.
197 /// </value>
198 internal bool HasItemsToKeep
200 get { return hasItemsToKeep; }
203 /// <summary>
204 /// Making sure we keep any item added
205 /// to the flash directly for at least one more action.
206 /// </summary>
207 private void InternalAdd(object key, object value)
209 keep.Add(key);
210 base[key] = value;