1 // Copyright 2004-2007 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();
36 private bool hasItemsToKeep
;
39 private bool hasSwept
;
42 /// Initializes a new instance of the <see cref="Flash"/> class.
49 /// Initializes a new instance of the <see cref="Flash"/> class.
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
)
67 /// Remove any element thats not marked to be kept.
68 /// This method is automatically called by the framework after the controller is processed.
74 object[] keys
= new object[Keys
.Count
];
90 object[] keys
= new object[Keys
.Count
];
94 for(int i
= 0; i
< keys
.Length
; i
++)
96 if (!keep
.Contains(keys
[i
]))
100 else if (!hasItemsToKeep
)
102 hasItemsToKeep
= true;
113 /// Keeps the entire flash contents available for the next action
119 foreach(object key
in base.Keys
)
126 /// Keeps the Flash['key'] contents available for the next action
128 public void Keep(object key
)
130 if (!keep
.Contains(key
)) keep
.Add(key
);
134 /// Marks the entire flash to be discarded by the end of the current action
136 public void Discard()
142 /// Marks Flash[key] to be discarded by the end of the current action
144 public void Discard(object key
)
150 /// Sets a flash that will not be available to the next action, only to the current.
152 /// Flash.Now( key, "Hello current action" )
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.
161 /// Entries set via <c>Now</c> are accessed the same way as standard entries: <c>Flash['my-key']</c>.
164 public void Now(object key
, object value)
170 /// Adds an element with the specified key and value into the <see cref="T:System.Collections.Hashtable"></see>.
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);
183 /// Gets or sets the <see cref="System.Object"/> with the specified key.
186 public override object this[object key
]
188 get { return base[key]; }
189 set { InternalAdd(key, value); }
193 /// Gets a value indicating whether this instance has items to keep.
196 /// <c>true</c> if this instance has items to keep; otherwise, <c>false</c>.
198 internal bool HasItemsToKeep
200 get { return hasItemsToKeep; }
204 /// Making sure we keep any item added
205 /// to the flash directly for at least one more action.
207 private void InternalAdd(object key
, object value)