KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > windows > ChannelListData


1 package rero.gui.windows;
2
3 import rero.ircfw.*;
4 import rero.ircfw.data.*;
5 import rero.ircfw.interfaces.*;
6
7 import text.list.*;
8 import text.*;
9 import rero.client.*;
10 import java.util.*;
11
12 import rero.gui.*;
13
14 /** I did not enjoy writing this code... really */
15 public class ChannelListData extends ListData
16 {
17    protected HashMap userInfo;
18    protected Channel channel;
19
20    protected Capabilities capabilities;
21
22    protected HashMap event;
23    protected Iterator tempIter;
24
25    protected int iterValue;
26
27    public void dirty()
28    {
29       userInfo.clear();
30    }
31
32    public void removeUser(User u)
33    {
34       userInfo.remove(u);
35    }
36
37    public void installCapabilities(Capabilities c)
38    {
39       capabilities = c;
40    }
41
42    public void updateChannel(Channel _channel)
43    {
44       channel = _channel;
45
46       event = new HashMap();
47       event.put("$channel", channel.getName());
48
49       userInfo = new HashMap();
50    }
51
52    public ChannelListData(Channel _channel)
53    {
54       updateChannel(_channel);
55    }
56
57    public int getSize()
58    {
59       if (getChannel() == null) { return 0; }
60       return getChannel().getAllUsers().size();
61    }
62
63    public Channel getChannel()
64    {
65       return channel;
66    }
67
68    public ListElement head()
69    {
70       if (getChannel() == null)
71       {
72          return null;
73       }
74
75       iterValue = getValue();
76       tempIter = getChannel().getAllUsers().iterator();
77       for (int x = 0; x < iterValue; x++)
78       {
79          tempIter.next();
80       }
81       return next();
82    }
83   
84    public ListElement next()
85    {
86       if (getChannel() != null && tempIter.hasNext())
87       {
88          return getElementForUser((User)tempIter.next());
89       }
90       return null;
91    }
92
93    // in the listbox painting code we ensure we have the script variable lock first before doing any painting...
94
public Object JavaDoc getSynchronizationKeyOuter()
95    {
96       if (capabilities != null)
97       {
98          // lock this tree by the script variables so no other thread can touch it...
99
return capabilities.getDataStructure(DataStructures.ScriptVariables);
100       }
101       else
102       {
103          return getChannel().getAllUsers(); // prevent a null pointer exception when capabilities haven't been installed yet
104
}
105    }
106
107    public Object JavaDoc getSynchronizationKeyInner()
108    {
109       return getChannel().getAllUsers();
110    }
111
112    protected ListElement getElementForUser(User u)
113    {
114       UserElement temp = (UserElement)userInfo.get(u);
115       if (temp == null)
116       {
117          temp = new UserElement(u);
118          userInfo.put(u, temp);
119       }
120       temp.touch();
121       return temp;
122    }
123
124    public ListElement getElementAt(int number)
125    {
126       if (getChannel() == null)
127       {
128          return null;
129       }
130
131       Iterator i = getChannel().getAllUsers().iterator();
132       for (int x = 0; x < number && i.hasNext(); x++)
133       {
134          i.next();
135       }
136
137       if (i.hasNext())
138       {
139          User t = (User)i.next();
140          return getElementForUser(t);
141       }
142       else
143       {
144          return null;
145       }
146    }
147
148     public Iterator dataIterator()
149     {
150        return new MyIterator();
151     }
152
153     private class MyIterator implements Iterator
154     {
155        protected Iterator i;
156   
157        public MyIterator()
158        {
159           i = getChannel().getAllUsers().iterator();
160        }
161
162        public boolean hasNext()
163        {
164           return i.hasNext();
165        }
166
167        public Object JavaDoc next()
168        {
169           return getElementForUser((User)i.next());
170        }
171
172        public void remove() { }
173     }
174
175     private class UserElement extends ListElement
176     {
177        protected int oldState;
178        protected User user;
179        protected AttributedString idle = null;
180        protected AttributedString normal = null;
181        protected int oldNick;
182
183        public UserElement(User _user)
184        {
185           user = _user;
186           oldState = _user.getModeFor(getChannel());
187           oldNick = _user.getNick().hashCode();
188
189           setSource(user);
190        }
191
192        public void touch()
193        {
194           if (oldState != user.getModeFor(getChannel()) || oldNick != user.getNick().hashCode())
195           {
196              idle = null;
197              normal = null;
198              oldNick = user.getNick().hashCode();
199              oldState = user.getModeFor(getChannel());
200              setSelected(false);
201           }
202        }
203
204        protected AttributedString buildString(String JavaDoc prepend)
205        {
206 // try
207
// {
208
if (capabilities != null) // some sort of weird race condition causes this to be null occasionally
209
{
210                 event.put("$nick", user.getNick());
211   
212                 AttributedString temp = AttributedString.CreateAttributedString(prepend + capabilities.getOutputCapabilities().parseSet(event, "NICKLIST_FORMAT"));
213                 temp.assignWidths();
214                 return temp;
215              }
216   /* }
217           catch (NullPointerException ex)
218           {
219              System.out.println("User: " + user);
220              System.out.println("prepend: " + prepend);
221              System.out.println("capabilities: " + capabilities);
222              System.out.println("event: " + event);
223              System.out.println("output: " + capabilities.getOutputCapabilities());
224              System.out.println("getNick(): " + user.getNick());
225           } */

226
227           return null;
228        }
229  
230        protected AttributedString getData()
231        {
232           if (user.getIdleTime() > (5 * 60))
233           {
234              if (idle == null)
235              {
236                 idle = buildString("" + AttributedString.reverse);
237              }
238              return idle;
239           }
240           else
241           {
242              if (normal == null)
243              {
244                 normal = buildString("");
245              }
246              return normal;
247           }
248        }
249
250        public AttributedText getAttributedText()
251        {
252           AttributedString data = getData();
253
254           if (data != null)
255              return getData().getAttributedText();
256
257           AttributedString temp = AttributedString.CreateAttributedString("");
258           temp.assignWidths();
259
260           return temp.getAttributedText();
261        }
262
263        public String JavaDoc getText()
264        {
265           return getData().getText();
266        }
267     }
268 }
269
270
Popular Tags