Memento 为一个对象提供存储和恢复的手段

ContractedBlock.gif ExpandedBlockStart.gif UI
  1None.gifusing System;
  2None.gifusing System.Drawing;
  3None.gifusing System.IO;
  4None.gifusing System.Reflection;
  5None.gifusing System.Windows.Forms;
  6None.gif
  7None.gifnamespace Gof.Test.Memento
  8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 10InBlock.gif    /// User interface utilities.
 11ExpandedSubBlockEnd.gif    /// </summary>

 12InBlock.gif    public class UI
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14InBlock.gif        protected Font _font = new Font("Book Antiqua", 18F);
 15InBlock.gif        public static readonly int STANDARD_PAD = 10;
 16InBlock.gif        public static readonly UI NORMAL = new UI();
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 18InBlock.gif        /// Set up a standard font that subclasses can override.
 19ExpandedSubBlockEnd.gif        /// </summary>

 20InBlock.gif        public virtual Font Font
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            get
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 24InBlock.gif                return _font;
 25ExpandedSubBlockEnd.gif            }

 26ExpandedSubBlockEnd.gif        }

 27ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 28InBlock.gif        /// Set up a standard pad amount that subclasses can override.
 29ExpandedSubBlockEnd.gif        /// </summary>

 30InBlock.gif        public virtual int Pad
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            get
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif                return STANDARD_PAD;
 35ExpandedSubBlockEnd.gif            }

 36ExpandedSubBlockEnd.gif        }

 37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 38InBlock.gif        /// Create a standard Ok! (or affirmation) button.
 39InBlock.gif        /// </summary>
 40ExpandedSubBlockEnd.gif        /// <returns>a standard Ok! (or affirmation) button</returns>

 41InBlock.gif        public virtual Button CreateButtonOk()
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43InBlock.gif            Button b = CreateButton();
 44InBlock.gif            b.Image = GetImage("rocket-large.gif");
 45InBlock.gif            b.Text = "Ok!";
 46InBlock.gif            return b;
 47ExpandedSubBlockEnd.gif        }

 48ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 49InBlock.gif        /// Create a standard Cancel! (or negation) button.
 50InBlock.gif        /// </summary>
 51ExpandedSubBlockEnd.gif        /// <returns>a standard Cancel! (or negation) button</returns>

 52InBlock.gif        public virtual Button CreateButtonCancel()
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 54InBlock.gif            Button b = CreateButton();
 55InBlock.gif            b.Image = GetImage("rocket-large-down.gif");
 56InBlock.gif            b.Text = "Cancel!";
 57InBlock.gif            return b;
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif        
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 61InBlock.gif        /// Create a standard button.
 62InBlock.gif        /// </summary>
 63ExpandedSubBlockEnd.gif        /// <returns>a standard button</returns>

 64InBlock.gif        public virtual Button CreateButton()
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            Button b = new Button();
 67InBlock.gif            b.Size = new Size(128128);
 68InBlock.gif            b.ImageAlign = ContentAlignment.TopCenter;
 69InBlock.gif            b.Font = Font;
 70InBlock.gif            b.TextAlign = ContentAlignment.BottomCenter;
 71InBlock.gif            return b;
 72ExpandedSubBlockEnd.gif        }

 73ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 74InBlock.gif        /// Create a panel that adds a standard amount of padding around
 75InBlock.gif        /// any controls that are added to it.
 76InBlock.gif        /// </summary>
 77ExpandedSubBlockEnd.gif        /// <returns>the panel</returns>

 78InBlock.gif        public virtual Panel CreatePaddedPanel()
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            Panel p = new Panel();
 81InBlock.gif            p.Dock = DockStyle.Fill;
 82InBlock.gif            p.DockPadding.All = Pad;
 83InBlock.gif            return p;
 84ExpandedSubBlockEnd.gif        }
 
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 86InBlock.gif        /// Create a panel that adds a standard amount of padding around
 87InBlock.gif        /// a given control.
 88InBlock.gif        /// </summary>
 89InBlock.gif        /// <param name="c">the control</param>
 90ExpandedSubBlockEnd.gif        /// <returns>the panel</returns>

 91InBlock.gif        public virtual Panel CreatePaddedPanel(Control c)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 93InBlock.gif            Panel p = CreatePaddedPanel(); 
 94InBlock.gif            p.Controls.Add(c);
 95InBlock.gif            return p;
 96ExpandedSubBlockEnd.gif        }
  
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 98InBlock.gif        /// Create a group box that wraps a titled border around a 
 99InBlock.gif        /// given component.
100InBlock.gif        /// </summary>
101InBlock.gif        /// <param name="title">The words to show in the title border tab</param>
102InBlock.gif        /// <param name="control">The control that the border goes around</param>
103InBlock.gif        /// <returns>A group box panel with a title, wrapped around the 
104ExpandedSubBlockEnd.gif        /// supplied control</returns>

105InBlock.gif        public virtual GroupBox CreateGroupBox(
106InBlock.gif            String title, Control control)
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            GroupBox gb = new GroupBox();
109InBlock.gif            gb.Text = title;
110InBlock.gif            gb.Dock = DockStyle.Fill;
111InBlock.gif            gb.Controls.Add(control);
112InBlock.gif            return gb;
113ExpandedSubBlockEnd.gif        }
        
114ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
115InBlock.gif        /// Create a standard grid for displaying, in particular,
116InBlock.gif        /// database tables.
117InBlock.gif        /// </summary>
118ExpandedSubBlockEnd.gif        /// <returns>A standard data grid</returns>

119InBlock.gif        public virtual DataGrid CreateGrid()
120ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
121InBlock.gif            DataGrid g = new DataGrid();
122InBlock.gif            g.Dock = DockStyle.Fill;
123InBlock.gif            g.CaptionVisible = false;
124InBlock.gif            return g;
125ExpandedSubBlockEnd.gif        }

126ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
127InBlock.gif        /// Create a standard list, where each list item has an accompanying
128InBlock.gif        /// image/icon.
129InBlock.gif        /// </summary>
130InBlock.gif        /// <param name="size">the size for images</param>
131InBlock.gif        /// <param name="images">the images</param>
132ExpandedSubBlockEnd.gif        /// <returns>a standard list view</returns>

133InBlock.gif        public virtual ListView CreateListView(Size size, params Image[] images)
134ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
135InBlock.gif            ListView lv = new ListView();
136InBlock.gif            lv.Font = Font;
137InBlock.gif            lv.View = View.Details;
138InBlock.gif            lv.Columns.Add(new ColumnHeader());
139InBlock.gif            lv.Columns[0].Width = -2// autosize
140InBlock.gif            lv.HeaderStyle = ColumnHeaderStyle.None;
141InBlock.gif            lv.SmallImageList = CreateImageList(size, images);
142InBlock.gif            return lv;
143ExpandedSubBlockEnd.gif        }

144InBlock.gif        // Create an ImageList object given the images and the desired
145InBlock.gif        // size for them.
146InBlock.gif        protected virtual ImageList CreateImageList (Size size, params Image[] images) 
147ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
148InBlock.gif            ImageList il = new ImageList();
149InBlock.gif            il.ColorDepth = ColorDepth.Depth32Bit;
150InBlock.gif            il.ImageSize = size;
151InBlock.gif            foreach (Image i in images) 
152ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
153InBlock.gif                il.Images.Add(i);
154ExpandedSubBlockEnd.gif            }

155InBlock.gif            return il; 
156ExpandedSubBlockEnd.gif        }
 
157ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
158InBlock.gif        /// This method looks up an image file and returns the image
159InBlock.gif        /// contained therein. This method expects all images to be
160InBlock.gif        /// in a path relative to the directory that this assembly
161InBlock.gif        /// is in, namely ..\images.
162InBlock.gif        /// </summary>
163InBlock.gif        /// <param name="imageName">the image to look up</param>
164ExpandedSubBlockEnd.gif        /// <returns>the image</returns>

165InBlock.gif        public static Image GetImage(String imageName) 
166ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
167InBlock.gif            return Image.FromFile(Gof.Test.Proxy.DataService.GetFileName("images", imageName));
168ExpandedSubBlockEnd.gif        }

169ExpandedSubBlockEnd.gif    }

170ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif FactoryDelegate
1None.gifusing System;
2None.gifusing System.Drawing;
3None.gif
4None.gifnamespace Gof.Test.Memento
5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
6InBlock.gif    public delegate void AddHandler(Point p);
7InBlock.gif    public delegate void RebuildHandler();
8InBlock.gif    public delegate void DragHandler(Point oldP,Point newP);
9ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif FacotryModel
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Drawing;
  4None.gif
  5None.gifnamespace Gof.Test.Memento
  6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  7InBlock.gif    public class FactoryModel
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        private Stack _mementos;
 10InBlock.gif        public event AddHandler AddEvent;
 11InBlock.gif        public event DragHandler DragEvent;
 12InBlock.gif        public event RebuildHandler RebuildEvent;
 13InBlock.gif        public static readonly Point DEFAULT_LOCATION = new Point(10,10); 
 14InBlock.gif        public FactoryModel()
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 16InBlock.gif            _mementos = new Stack();
 17InBlock.gif            _mementos.Push(new ArrayList());
 18ExpandedSubBlockEnd.gif        }
 
 19InBlock.gif        public void AddMachine()
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 21InBlock.gif            IList newLocs = new ArrayList();
 22InBlock.gif            Point newP = DEFAULT_LOCATION;
 23InBlock.gif            newLocs.Add(newP);
 24InBlock.gif            foreach(Point p in (IList)_mementos.Peek())
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 26InBlock.gif                newLocs.Add(new Point(p.X,p.Y));
 27ExpandedSubBlockEnd.gif            }

 28InBlock.gif            _mementos.Push(newLocs);
 29InBlock.gif
 30InBlock.gif            if(AddEvent != null)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 32InBlock.gif                AddEvent(newP);
 33ExpandedSubBlockEnd.gif            }

 34ExpandedSubBlockEnd.gif        }
 
 35InBlock.gif        public IList Locations 
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            get
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 39InBlock.gif                return (IList)_mementos.Peek();
 40ExpandedSubBlockEnd.gif            }

 41ExpandedSubBlockEnd.gif        }

 42InBlock.gif        public void Pop()
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            if(_mementos.Count > 1)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 46InBlock.gif                _mementos.Pop();
 47ExpandedSubBlockEnd.gif            }

 48InBlock.gif            if(RebuildEvent != null)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 50InBlock.gif                RebuildEvent();
 51ExpandedSubBlockEnd.gif            }

 52ExpandedSubBlockEnd.gif        }

 53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 54InBlock.gif        /// Move a machine from an old location to a new one. A side
 55InBlock.gif        /// effect is that the new location will be the first in this
 56InBlock.gif        /// model's list of locations. This helps the GUI handle Z order.
 57InBlock.gif        /// In particular, just clicking a machine will move it the the
 58InBlock.gif        /// head of the list and will thus bring it to the "front" of
 59InBlock.gif        /// the display.
 60InBlock.gif        /// </summary>
 61InBlock.gif        /// <param name="oldP">where the machine was</param>
 62ExpandedSubBlockEnd.gif        /// <param name="newP">the new spot for the machine</param>

 63InBlock.gif        public void Drag(Point oldP, Point newP) 
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            // put the new location up front so its Z order is on top
 66InBlock.gif            IList newLocs = new ArrayList(); 
 67InBlock.gif            newLocs.Add(newP);
 68InBlock.gif            // create a new list, copying in all except the dragee
 69InBlock.gif            bool foundDragee = false;
 70InBlock.gif            foreach (Point p in (IList)_mementos.Peek())
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72InBlock.gif                if ( !foundDragee && p.Equals(oldP)) 
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 74InBlock.gif                    foundDragee = true;
 75ExpandedSubBlockEnd.gif                }

 76InBlock.gif                else
 77ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 78InBlock.gif                    newLocs.Add(new Point(p.X, p.Y)); 
 79ExpandedSubBlockEnd.gif                }

 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif            _mementos.Push(newLocs);
 82InBlock.gif            if (DragEvent != null) DragEvent(oldP, newP);
 83ExpandedSubBlockEnd.gif        }

 84ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 85InBlock.gif        /// Add a new configuration.
 86InBlock.gif        /// </summary>
 87ExpandedSubBlockEnd.gif        /// <param name="list">A list of Point objects representing machine locations</param>

 88InBlock.gif        public void Push(IList list)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 90InBlock.gif            _mementos.Push(list);
 91InBlock.gif            if (RebuildEvent != null) RebuildEvent();
 92ExpandedSubBlockEnd.gif        }

 93ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 94InBlock.gif        /// Return the number of saved configurations. This helps the GUI
 95InBlock.gif        /// decide whether to offer "undo".
 96ExpandedSubBlockEnd.gif        /// </summary>

 97InBlock.gif        public int MementoCount
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99InBlock.gif            get 
100ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
101InBlock.gif                return _mementos.Count;
102ExpandedSubBlockEnd.gif            }

103ExpandedSubBlockEnd.gif        }

104ExpandedSubBlockEnd.gif    }

105ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif VisMediator
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Drawing;
  4None.gifusing System.IO;
  5None.gifusing System.Runtime.Serialization.Formatters.Soap; 
  6None.gifusing System.Windows.Forms;
  7None.gif
  8None.gifnamespace Gof.Test.Memento
  9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11InBlock.gif    /// This class handles the UI events for the Visualization class
 12ExpandedSubBlockEnd.gif    /// </summary>

 13InBlock.gif    public class VisMediator 
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        protected int initX;
 16InBlock.gif        protected int initY;
 17InBlock.gif        protected Point initLocation;
 18InBlock.gif        protected bool isMouseDown = false;
 19InBlock.gif         
 20InBlock.gif        protected FactoryModel _factoryModel;
 21InBlock.gif
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 23InBlock.gif        /// Create a new mediator for a visualization that uses the provided
 24InBlock.gif        /// factory model.
 25InBlock.gif        /// </summary>
 26ExpandedSubBlockEnd.gif        /// <param name="m">The model that tracks equipment locations</param>

 27InBlock.gif        public VisMediator(FactoryModel m)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            _factoryModel = m; 
 30ExpandedSubBlockEnd.gif        }
 
 31InBlock.gif
 32InBlock.gif        // The user clicked "Add"
 33InBlock.gif        internal void Add(object sender, System.EventArgs e)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35InBlock.gif            _factoryModel.AddMachine();
 36ExpandedSubBlockEnd.gif        }

 37InBlock.gif
 38InBlock.gif        // The user has clicked "Undo"
 39InBlock.gif        internal void Undo(object sender, System.EventArgs e)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 41InBlock.gif            _factoryModel.Pop();
 42ExpandedSubBlockEnd.gif        }

 43InBlock.gif
 44InBlock.gif        // A click on a picture box
 45InBlock.gif        internal void MouseDown(object sender, MouseEventArgs e)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            if (e.Button == MouseButtons.Left) 
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
 49InBlock.gif                PictureBox pb = (PictureBox) sender;
 50InBlock.gif                initLocation = pb.Location;
 51InBlock.gif                initX = Control.MousePosition.X;
 52InBlock.gif                initY = Control.MousePosition.Y;             
 53InBlock.gif                isMouseDown = true;
 54ExpandedSubBlockEnd.gif            }
    
 55ExpandedSubBlockEnd.gif        }

 56InBlock.gif
 57InBlock.gif        // A drag while a picture box is clicked
 58InBlock.gif        internal void MouseMove(object sender, MouseEventArgs e)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            if (isMouseDown) 
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif             
 63InBlock.gif                PictureBox pb = (PictureBox) sender;
 64InBlock.gif                pb.Location = new Point(initLocation.X + Control.MousePosition.X - initX,
 65InBlock.gif                    initLocation.Y + Control.MousePosition.Y - initY);
 66ExpandedSubBlockEnd.gif            }

 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif
 69InBlock.gif        // leggo of a picture box. Let the factory model know about this change
 70InBlock.gif        internal void MouseUp(object sender, MouseEventArgs e)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            if (e.Button == MouseButtons.Left) 
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 74InBlock.gif                isMouseDown = false;
 75InBlock.gif                PictureBox pb = (PictureBox) sender;
 76InBlock.gif                _factoryModel.Drag(initLocation, pb.Location); 
 77ExpandedSubBlockEnd.gif            }

 78ExpandedSubBlockEnd.gif        }

 79InBlock.gif
 80InBlock.gif        // User clicked "Save Asdot.gif" menu item
 81InBlock.gif        internal void Save(object sender, System.EventArgs e)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 83InBlock.gif            SaveFileDialog d = new SaveFileDialog();
 84InBlock.gif            if (d.ShowDialog() == DialogResult.OK)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{   
 86InBlock.gif                using (FileStream fs = File.Create(d.FileName))
 87ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 88InBlock.gif                    new SoapFormatter().Serialize(fs, _factoryModel.Locations);
 89ExpandedSubBlockEnd.gif                }
             
 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif        // User clicked "Restore fromdot.gif" menu item
 94InBlock.gif        internal void Restore(object sender, System.EventArgs e)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            OpenFileDialog d = new OpenFileDialog();
 97InBlock.gif            if (d.ShowDialog() == DialogResult.OK)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 99InBlock.gif                using (FileStream fs = File.Open(d.FileName, FileMode.Open))
100ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
101InBlock.gif                    IList list = (IList)(new SoapFormatter().Deserialize(fs));    
102InBlock.gif                    _factoryModel.Push(list);             
103ExpandedSubBlockEnd.gif                }
 
104ExpandedSubBlockEnd.gif            }

105ExpandedSubBlockEnd.gif        }

106ExpandedSubBlockEnd.gif    }

107ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Visualization
  1None.gifusing System;
  2None.gifusing System.Drawing;
  3None.gifusing System.Windows.Forms;
  4None.gif
  5None.gifnamespace Gof.Test.Memento
  6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// This class provides a visualization of a factory that contains
  9InBlock.gif    /// machines and through which material flows. At present the only
 10InBlock.gif    /// functionality is the ability to create and drag machines. In the
 11InBlock.gif    /// future we'll add operational modeling functions.
 12ExpandedSubBlockEnd.gif    /// </summary>

 13InBlock.gif    public class Visualization : Form
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        protected UI _ui;
 16InBlock.gif        protected Panel _machinePanel;
 17InBlock.gif        protected Panel _buttonPanel;
 18InBlock.gif        protected Button _addButton;
 19InBlock.gif        protected Button _undoButton;         
 20InBlock.gif        protected Image _image = UI.GetImage("machine.png"); 
 21InBlock.gif        protected FactoryModel _factoryModel = new FactoryModel();
 22InBlock.gif        protected VisMediator _mediator; 
 23InBlock.gif
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 25InBlock.gif        /// Create a new visualization of a simulated factory.
 26ExpandedSubBlockEnd.gif        /// </summary>

 27InBlock.gif        public Visualization(UI ui)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
 29InBlock.gif            _ui = ui;
 30InBlock.gif            _factoryModel.AddEvent += new AddHandler(HandleAdd);
 31InBlock.gif            _factoryModel.DragEvent += new DragHandler(HandleDrag);
 32InBlock.gif            _factoryModel.RebuildEvent += new RebuildHandler(HandleUndo);
 33InBlock.gif            _mediator = new VisMediator(_factoryModel);
 34InBlock.gif            Controls.Add(MachinePanel());
 35InBlock.gif            Controls.Add(ButtonPanel());
 36InBlock.gif            Text = "Operational Model";
 37ExpandedSubBlockEnd.gif        }
 
 38InBlock.gif
 39InBlock.gif        // the panel on which we'll drag around machines
 40InBlock.gif        protected Panel MachinePanel()
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            if (_machinePanel == null)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                _machinePanel = new Panel();
 45InBlock.gif                _machinePanel.BackColor = Color.White;            
 46InBlock.gif                _machinePanel.Dock = DockStyle.Fill;
 47ExpandedSubBlockEnd.gif            }

 48InBlock.gif            return _machinePanel;
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif     
 51InBlock.gif        // the panel that holds the app's buttons
 52InBlock.gif        protected Panel ButtonPanel()
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 54InBlock.gif            if (_buttonPanel == null
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 56InBlock.gif                _buttonPanel = new Panel();
 57InBlock.gif                _buttonPanel.Controls.Add(AddButton());
 58InBlock.gif                _buttonPanel.Controls.Add(UndoButton());
 59InBlock.gif                _buttonPanel.Dock = DockStyle.Bottom;
 60InBlock.gif                _buttonPanel.Height = (int)(AddButton().Height * 1.10);
 61ExpandedSubBlockEnd.gif            }

 62InBlock.gif            return _buttonPanel;
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65InBlock.gif        // the "Add" button
 66InBlock.gif        protected Button AddButton()
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            if (_addButton == null)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                _addButton = _ui.CreateButtonOk();
 71InBlock.gif                _addButton.Dock = DockStyle.Left;
 72InBlock.gif                _addButton.Text = "Add";
 73InBlock.gif                _addButton.Click += new System.EventHandler(_mediator.Add);
 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif            return _addButton;
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78InBlock.gif        // the "Undo" button
 79InBlock.gif        protected Button UndoButton()
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            if (_undoButton == null)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                _undoButton = _ui.CreateButtonCancel();
 84InBlock.gif                _undoButton.Dock = DockStyle.Right;
 85InBlock.gif                _undoButton.Text = "Undo";
 86InBlock.gif                _undoButton.Click += new System.EventHandler(_mediator.Undo);
 87InBlock.gif                _undoButton.Enabled = false;
 88ExpandedSubBlockEnd.gif            }

 89InBlock.gif            return _undoButton;
 90ExpandedSubBlockEnd.gif        }

 91InBlock.gif
 92InBlock.gif
 93InBlock.gif        // Add a new machine at the given location
 94InBlock.gif        protected void HandleAdd(Point p)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            PictureBox pb = CreatePictureBox(p);
 97InBlock.gif            MachinePanel().Controls.Add(pb);
 98InBlock.gif            pb.BringToFront();
 99InBlock.gif            UndoButton().Enabled = true;
100ExpandedSubBlockEnd.gif        }

101InBlock.gif
102InBlock.gif        // Dragging a machine brings it to the front of the controls
103InBlock.gif        // in the machine panel, and updates the machine's location         
104InBlock.gif        protected void HandleDrag(Point oldP, Point newP)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
106InBlock.gif            foreach (PictureBox pb in MachinePanel().Controls)
107ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
108InBlock.gif                if (pb.Location.Equals(newP)) 
109ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
110InBlock.gif                    pb.BringToFront();
111InBlock.gif                    return;
112ExpandedSubBlockEnd.gif                }
 
113ExpandedSubBlockEnd.gif            }

114ExpandedSubBlockEnd.gif        }

115InBlock.gif
116InBlock.gif        // When the user clicks Undo, we rebuild the factory
117InBlock.gif        // from the model.
118InBlock.gif        protected void HandleUndo()
119ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
120InBlock.gif            MachinePanel().Controls.Clear();      
121InBlock.gif            foreach (Point p in _factoryModel.Locations)
122ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{   
123InBlock.gif                MachinePanel().Controls.Add(CreatePictureBox(p));
124ExpandedSubBlockEnd.gif            }
       
125InBlock.gif            UndoButton().Enabled = _factoryModel.MementoCount > 1;
126ExpandedSubBlockEnd.gif        }

127InBlock.gif
128InBlock.gif        // Create a standard picture of a machine
129InBlock.gif        protected PictureBox CreatePictureBox(Point p)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
131InBlock.gif            PictureBox pb = new PictureBox();
132InBlock.gif            pb.Image = _image;
133InBlock.gif            pb.Size = _image.Size; 
134InBlock.gif            pb.MouseDown += new MouseEventHandler(_mediator.MouseDown); 
135InBlock.gif            pb.MouseMove += new MouseEventHandler(_mediator.MouseMove);
136InBlock.gif            pb.MouseUp   += new MouseEventHandler(_mediator.MouseUp);
137InBlock.gif            pb.Location = p; 
138InBlock.gif            return pb;
139ExpandedSubBlockEnd.gif        }

140ExpandedSubBlockEnd.gif    }

141ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
1None.gif            System.Windows.Forms.Application.Run(new Gof.Test.Memento.Visualization(Gof.Test.Memento.UI.NORMAL));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值