1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Runtime
.Serialization
;
19 using System
.Collections
;
22 /// Keeps data across a single request using the session
25 public class Flash
: Hashtable
30 public static readonly String FlashKey
= "flash";
33 private ArrayList keep
= new ArrayList();
35 private bool hasItemsToKeep
;
37 private bool hasSwept
;
40 /// Initializes a new instance of the <see cref="Flash"/> class.
42 public Flash() : base(StringComparer
.InvariantCultureIgnoreCase
)
47 /// Initializes a new instance of the <see cref="Flash"/> class.
49 /// <param name="copy">The copy.</param>
50 public Flash(Flash copy
) : base(StringComparer
.InvariantCultureIgnoreCase
)
52 if (copy
== null) return;
54 foreach(DictionaryEntry entry
in copy
)
56 base.Add(entry
.Key
, entry
.Value
);
61 /// Initializes a new instance of the <see cref="Flash"/> class.
63 /// <param name="info">A <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object containing the information required to serialize the <see cref="T:System.Collections.Hashtable"/> object.</param>
64 /// <param name="context">A <see cref="T:System.Runtime.Serialization.StreamingContext"/> object containing the source and destination of the serialized stream associated with the <see cref="T:System.Collections.Hashtable"/>.</param>
65 /// <exception cref="T:System.ArgumentNullException">
66 /// <paramref name="info"/> is null. </exception>
67 public Flash(SerializationInfo info
, StreamingContext context
) : base(info
, context
)
72 /// Remove any element thats not marked to be kept.
73 /// This method is automatically called by the framework after the controller is processed.
79 object[] keys
= new object[Keys
.Count
];
95 object[] keys
= new object[Keys
.Count
];
99 for(int i
= 0; i
< keys
.Length
; i
++)
101 if (!keep
.Contains(keys
[i
]))
105 else if (!hasItemsToKeep
)
107 hasItemsToKeep
= true;
118 /// Keeps the entire flash contents available for the next action
124 foreach(object key
in base.Keys
)
131 /// Keeps the Flash['key'] contents available for the next action
133 public void Keep(object key
)
135 if (!keep
.Contains(key
)) keep
.Add(key
);
139 /// Marks the entire flash to be discarded by the end of the current action
141 public void Discard()
147 /// Marks Flash[key] to be discarded by the end of the current action
149 public void Discard(object key
)
155 /// Sets a flash that will not be available to the next action, only to the current.
157 /// Flash.Now( key, "Hello current action" )
160 /// This method enables you to use the flash as a central messaging system in your app.
161 /// When you need to pass an object to the next action, you use the standard flash assign (<c>[]=</c>).
162 /// When you need to pass an object to the current action, you use <c>Now</c>, and your object will
163 /// vanish when the current action is done.
166 /// Entries set via <c>Now</c> are accessed the same way as standard entries: <c>Flash['my-key']</c>.
169 public void Now(object key
, object value)
175 /// Adds an element with the specified key and value into the <see cref="T:System.Collections.Hashtable"></see>.
177 /// <param name="key">The key of the element to add.</param>
178 /// <param name="value">The value of the element to add. The value can be null.</param>
179 /// <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>
180 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Hashtable"></see>. </exception>
181 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
182 public override void Add(object key
, object value)
184 InternalAdd(key
, value);
188 /// Gets or sets the <see cref="System.Object"/> with the specified key.
191 public override object this[object key
]
193 get { return base[key]; }
194 set { InternalAdd(key, value); }
198 /// Gets a value indicating whether this instance has items to keep.
201 /// <c>true</c> if this instance has items to keep; otherwise, <c>false</c>.
203 public bool HasItemsToKeep
205 get { return hasItemsToKeep; }
209 /// Making sure we keep any item added
210 /// to the flash directly for at least one more action.
212 private void InternalAdd(object key
, object value)