OSDN Git Service

More event arg changes
[radegast/radegast.git] / Radegast / Core / Types / AgentNameTextBox.cs
1 // \r
2 // Radegast Metaverse Client\r
3 // Copyright (c) 2009, Radegast Development Team\r
4 // All rights reserved.\r
5 // \r
6 // Redistribution and use in source and binary forms, with or without\r
7 // modification, are permitted provided that the following conditions are met:\r
8 // \r
9 //     * Redistributions of source code must retain the above copyright notice,\r
10 //       this list of conditions and the following disclaimer.\r
11 //     * Redistributions in binary form must reproduce the above copyright\r
12 //       notice, this list of conditions and the following disclaimer in the\r
13 //       documentation and/or other materials provided with the distribution.\r
14 //     * Neither the name of the application "Radegast", nor the names of its\r
15 //       contributors may be used to endorse or promote products derived from\r
16 //       this software without specific prior written permission.\r
17 // \r
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 //\r
29 // $Id$\r
30 //\r
31 using System;\r
32 using System.Collections.Generic;\r
33 using System.Windows.Forms;\r
34 using OpenMetaverse;\r
35 \r
36 namespace Radegast\r
37 {\r
38     public class AgentNameTextBox : System.Windows.Forms.TextBox\r
39     {\r
40         private UUID agentID;\r
41         private GridClient client { get { return RadegastInstance.GlobalInstance.Client; } }\r
42         private RadegastInstance instance { get { return RadegastInstance.GlobalInstance; } }\r
43 \r
44         [System.ComponentModel.Browsable(false)]\r
45         public UUID AgentID\r
46         {\r
47             get { return agentID; }\r
48 \r
49             set\r
50             {\r
51                 if (agentID == value) return;\r
52 \r
53                 agentID = value;\r
54 \r
55                 if (agentID == UUID.Zero)\r
56                 {\r
57                     SetName(string.Empty);\r
58                 }\r
59                 else\r
60                 {\r
61                     SetupHandlers();\r
62                     string name = instance.getAvatarName(agentID);\r
63                     SetName(name);\r
64 \r
65                     // We got cached response, unhook event handlers\r
66                     if (name != RadegastInstance.INCOMPLETE_NAME)\r
67                     {\r
68                         CleanupHandlers(null, null);\r
69                     }\r
70                 }\r
71             }\r
72         }\r
73 \r
74         public AgentNameTextBox()\r
75             : base()\r
76         {\r
77             SetStyle(ControlStyles.SupportsTransparentBackColor, true);\r
78             Disposed += new EventHandler(CleanupHandlers);\r
79         }\r
80 \r
81         void SetupHandlers()\r
82         {\r
83             if (client != null)\r
84             {\r
85                 client.Avatars.UUIDNameReply += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);\r
86             }\r
87         }\r
88 \r
89         void CleanupHandlers(object sender, EventArgs e)\r
90         {\r
91             if (client != null)\r
92             {\r
93                 client.Avatars.UUIDNameReply -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);\r
94             }\r
95         }\r
96 \r
97         void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e)\r
98         {\r
99             if (e.Names.ContainsKey(agentID))\r
100             {\r
101                 CleanupHandlers(null, null);\r
102                 SetName(e.Names[agentID]);\r
103             }\r
104         }\r
105 \r
106         void SetName(string name)\r
107         {\r
108             if (InvokeRequired)\r
109             {\r
110                 BeginInvoke(new MethodInvoker(delegate() { SetName(name); }));\r
111                 return;\r
112             }\r
113 \r
114             base.Text = name;\r
115         }\r
116     }\r
117 }\r