Design Pattern Examples
Overview of object-oriented design patterns
MessageWindow Class Reference

Represents a rectangular region that can handle messages directed to that region. More...

Inheritance diagram for MessageWindow:
Inheritance graph
Collaboration diagram for MessageWindow:
Collaboration graph

Public Member Functions

bool ProcessMessage (Message message)
 Processes a message.
 
override string ToString ()
 Convert this handler to a string.
 
bool ProcessMessage (Message message)
 Called with a message on each window.
 
string ToString ()
 Convert the handler to a string.
 

Static Public Member Functions

static MessageWindow CreateWindow (string title, int x, int y, int width, int height, HandlerChain handlerChain)
 Creates an instance of the MessageWindow class with the specified attributes and adds the new instance to the given HandlerChain object.
 

Properties

int ID [get]
 Returns the ID of the message handler.
 
- Properties inherited from IMessageHandler
int ID [get]
 ID of the window. This is used to uniquely identify a window in the collection.
 

Private Member Functions

delegate bool MessageHandler (Message message)
 
bool _PointInWindow (MessagePosition position)
 Determine if the specified point is in this MessageWindow's region.
 
bool _PointInCloseBox (MessagePosition position)
 Determine if the specified point is in this MessageWindow's "close" region.
 
 MessageWindow (int windowId, string title, int x, int y, int width, int height, HandlerChain handlerChain)
 Constructor.
 
bool _HandleButtonDownMessage (Message message)
 Helper method to handle the ButtonDown message.
 
bool _HandleButtonUpMessage (Message message)
 Helper method to handle the ButtonUp message.
 
bool _HandleCloseMessage (Message message)
 Helper method to handle the Close message.
 

Private Attributes

Dictionary< MessageType, MessageHandler_messageHandlers = new Dictionary<MessageType, MessageHandler>()
 Maps a message type to a handler method of type MessageHandler.
 
int _windowId
 Unique ID of this window.
 
string _title
 Title/Name of this window.
 
WindowRectangle _windowBox
 Position of this window in global coordinates.
 
WindowRectangle _closeBox
 Position of the close window within the window box, although the coordinates are also global coordinates to eliminate the need to convert between window and global coordinates.
 
bool _selected
 Whether this window has been selected (a button click occurred within the window).
 
HandlerChain _handlerChain
 The HandlerChain to which this window belongs (as an IMessageHandler object).
 

Static Private Attributes

const int CLOSE_WIDTH = 2
 
const int CLOSE_HEIGHT = 2
 
static int _nextWindowId = 1
 Used for assigning a unique ID to each created window.
 

Detailed Description

Represents a rectangular region that can handle messages directed to that region.

Note: The IMessageHandler interface is an example of a Facade design pattern, where the complexity of the MessageWindow is exposed only through a few methods to a system that only needs to deal with those few methods.

Definition at line 97 of file HandlerChain_MessageWindow_Class.cs.

Constructor & Destructor Documentation

◆ MessageWindow()

MessageWindow ( int  windowId,
string  title,
int  x,
int  y,
int  width,
int  height,
HandlerChain  handlerChain 
)
inlineprivate

Constructor.

Parameters
windowIdUnique ID of the window to use.
titleTitle of the window.
xGlobal x position of the upper left corner of the window's region.
yGlobal y position of the upper left corner of the window's region.
widthWidth of the window's region.
heightHeight of the window's region.
handlerChainA HandlerChain object that will be used for removal of this window when it is closed.

Definition at line 211 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._closeBox, MessageWindow._HandleButtonDownMessage(), MessageWindow._HandleButtonUpMessage(), MessageWindow._HandleCloseMessage(), MessageWindow._handlerChain, MessageWindow._messageHandlers, MessageWindow._selected, MessageWindow._title, MessageWindow._windowBox, MessageWindow._windowId, MessageWindow.CLOSE_HEIGHT, MessageWindow.CLOSE_WIDTH, WindowRectangle.Right, and WindowRectangle.Top.

Member Function Documentation

◆ _HandleButtonDownMessage()

bool _HandleButtonDownMessage ( Message  message)
inlineprivate

Helper method to handle the ButtonDown message.

Parameters
messageA Message object describing the ButtonDown message.
Returns
Always returns false even if the message was handled (allows other windows to select/deselect themselves).

Definition at line 242 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._PointInWindow(), MessageWindow._selected, MessageWindow._title, and Message.Position.

Referenced by MessageWindow.MessageWindow().

◆ _HandleButtonUpMessage()

bool _HandleButtonUpMessage ( Message  message)
inlineprivate

Helper method to handle the ButtonUp message.

Parameters
messageA Message object describing the ButtonUp message.
Returns
Returns true if the message was handled; otherwise, returns false indicating the message was not handled.

Definition at line 275 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._handlerChain, MessageWindow._PointInCloseBox(), MessageWindow._PointInWindow(), MessageWindow._selected, MessageWindow._title, Message.Position, and HandlerChain.SendMessage().

Referenced by MessageWindow.MessageWindow().

◆ _HandleCloseMessage()

bool _HandleCloseMessage ( Message  message)
inlineprivate

Helper method to handle the Close message.

Parameters
messageA Message object describing the Close message.
Returns
Returns true if the message was handled; otherwise, returns false indicating the message was not handled.

Definition at line 307 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._handlerChain, MessageWindow._selected, MessageWindow._title, and HandlerChain.RemoveHandler().

Referenced by MessageWindow.MessageWindow().

◆ _PointInCloseBox()

bool _PointInCloseBox ( MessagePosition  position)
inlineprivate

Determine if the specified point is in this MessageWindow's "close" region.

Parameters
positionThe point to examine.
Returns
Returns true if the point is contained within the MessageWindow's "close" region.

Definition at line 193 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._closeBox, and WindowRectangle.PointInside().

Referenced by MessageWindow._HandleButtonUpMessage().

◆ _PointInWindow()

bool _PointInWindow ( MessagePosition  position)
inlineprivate

Determine if the specified point is in this MessageWindow's region.

Parameters
positionThe global coordinate to examine.
Returns
Returns true if the point is contained within the MessageWindow's region.

Definition at line 180 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._windowBox, and WindowRectangle.PointInside().

Referenced by MessageWindow._HandleButtonDownMessage(), and MessageWindow._HandleButtonUpMessage().

◆ CreateWindow()

static MessageWindow CreateWindow ( string  title,
int  x,
int  y,
int  width,
int  height,
HandlerChain  handlerChain 
)
inlinestatic

Creates an instance of the MessageWindow class with the specified attributes and adds the new instance to the given HandlerChain object.

Parameters
titleTitle of the MessageWindow.
xX position of the upper left corner of the window's region.
yY position of the upper left corner of hte window's region.
widthWidth of the window's region.
heightHeight of the window's region.
handlerChainA HandlerChain object that will be given the window.
Returns
Returns the newly created MessageWindow.

Each MessageWindow instance is assigned a unique ID, which is required by the HandlerChain object.

Definition at line 165 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._nextWindowId, and HandlerChain.AddHandler().

Referenced by HandlerChain_Exercise._HandlerChain_ConstructWindowChain().

◆ MessageHandler()

delegate bool MessageHandler ( Message  message)
private

◆ ProcessMessage()

bool ProcessMessage ( Message  message)
inline

Processes a message.

Parameters
messageThe Message object to process.
Returns
Returns true if the message was handled and no further windows should be notified; otherwise return false to allow the message to be passed to subsequent windows.

The message types are mapped to handlers in the MessageWindow constructor and stored in the _messageHandlers dictionary.

Implements IMessageHandler.

Definition at line 354 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._messageHandlers, MessageWindow.MessageHandler(), and Message.MessageType.

◆ ToString()

override string ToString ( )
inline

Convert this handler to a string.

Returns
Returns a string representation of the message handler.

Implements IMessageHandler.

Definition at line 372 of file HandlerChain_MessageWindow_Class.cs.

References MessageWindow._selected, MessageWindow._title, MessageWindow._windowBox, and MessageWindow.ID.

Member Data Documentation

◆ _closeBox

WindowRectangle _closeBox
private

Position of the close window within the window box, although the coordinates are also global coordinates to eliminate the need to convert between window and global coordinates.

Definition at line 130 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow._PointInCloseBox(), and MessageWindow.MessageWindow().

◆ _handlerChain

HandlerChain _handlerChain
private

The HandlerChain to which this window belongs (as an IMessageHandler object).

Definition at line 141 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow._HandleButtonUpMessage(), MessageWindow._HandleCloseMessage(), and MessageWindow.MessageWindow().

◆ _messageHandlers

Dictionary<MessageType, MessageHandler> _messageHandlers = new Dictionary<MessageType, MessageHandler>()
private

◆ _nextWindowId

int _nextWindowId = 1
staticprivate

Used for assigning a unique ID to each created window.

Definition at line 147 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow.CreateWindow().

◆ _selected

bool _selected
private

Whether this window has been selected (a button click occurred within the window).

Definition at line 136 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow._HandleButtonDownMessage(), MessageWindow._HandleButtonUpMessage(), MessageWindow._HandleCloseMessage(), MessageWindow.MessageWindow(), and MessageWindow.ToString().

◆ _title

◆ _windowBox

WindowRectangle _windowBox
private

Position of this window in global coordinates.

Definition at line 123 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow._PointInWindow(), MessageWindow.MessageWindow(), and MessageWindow.ToString().

◆ _windowId

int _windowId
private

Unique ID of this window.

Definition at line 113 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow.ID(), and MessageWindow.MessageWindow().

◆ CLOSE_HEIGHT

const int CLOSE_HEIGHT = 2
staticprivate

Definition at line 101 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow.MessageWindow().

◆ CLOSE_WIDTH

const int CLOSE_WIDTH = 2
staticprivate

Definition at line 100 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow.MessageWindow().

Property Documentation

◆ ID

int ID
get

Returns the ID of the message handler.

Implements IMessageHandler.

Definition at line 334 of file HandlerChain_MessageWindow_Class.cs.

Referenced by MessageWindow.ToString().


The documentation for this class was generated from the following file: