An application works with documents in some way. Though
some content is not document-specific, the term document is used to mean any
content that appears in the center of the window. For instance, in Visual
Studio, a document is code, help content, or URL content that appears in the
tabbed area in the center. Even though this content varies, a user interface
represents it in a specific and customized way.
Each document has a common set of attributes associated with
it. Each object has a name, which is a unique key, and a title, which is the
display text. This can be easily abstracted into the architecture described
above. Document windows go beyond it because it requires a user interface.
This user interface could be a Windows forms user control, a WPF user control,
a form, or some other object that can render a UI.
To start, think of the functionality that is involved with
document windows. Document windows are the main area content windows, which
can be added, inserted in the middle of the collection, removed when clicked on
the "X", selected by clicking on the tab, and moved within the
collection. The first three items are normal collection-based behavior. The
last two are specialized behavior, but aren't hard to do.
All windows use the BaseWindow object, since they have
similar behavior. You first saw this in the previous section. All BaseWindow
objects have a BaseWindowCollection, which inherits from UIElementCollection,
which utilizes all of these features as shown below:
Listing 3
public abstract class UIElementCollection < T > : IEnumerable < T > where T:
UIElement
{
public virtual void Add(T item)
{
//Typical add
}
public bool Contains(string name)
{
//Typical contains
}
public int IndexOf(string name)
{
//Typical index of
}
public virtual void Insert(int index, T item)
{
//Typical insert
}
}
public virtual bool Remove(T item)
{
//Typical remove
}
public bool Remove(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
T item = this[name];
if (item == null)
return false;
return this.Remove(item);
}
}
These are the base features of a collection class. Windows
need other features, which the BaseWindowCollection class adds to this, as
shown below:
Listing 4
public abstract class BaseWindowCollection < T > : UIElementCollection < T >
where T: BaseWindow
{
public event DataEventHandler < T > SelectedIndexChanged;
public T Selected
{
get
{
return _selected;
}
set
{
if (_selected != value)
{
_selected = value;
this.OnSelectedIndexChanged(new DataEventArgs < T > (value));
}
}
}
}
With windows, such as a document window, the window can be
selected by clicking on the related tab. This property represents that by
storing a reference to the selected item, and upon changing the item, an event
is fired for that selection. Not only can this property be used, but the BaseWindow
class has a select method. This select method is shown below:
Listing 5
protected virtual void OnSelecting(EventArgs e)
{
if (Selecting != null)
Selecting(this, e);
}
public void Select()
{
this.OnSelecting(EventArgs.Empty);
}
This event is caught by the collection class and upon doing
so this value is assigned to the Selected collection property.