KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > IRCSession


1 package rero.gui;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7 import javax.swing.event.*;
8
9 import java.util.*;
10
11 import rero.gui.windows.*;
12
13 import rero.client.*;
14 import rero.client.user.*;
15
16 import rero.ircfw.*;
17
18 import rero.bridges.menu.*;
19
20 import rero.gui.script.*;
21
22 import rero.gui.mdi.*;
23 import rero.gui.toolkit.*;
24 import rero.gui.sdi.*;
25
26 import rero.dcc.*;
27
28 import rero.config.*;
29
30 import rero.client.user.UserHandler;
31
32 public class IRCSession
33 {
34    protected WindowManager desktop;
35    protected InternetRelayChatClient client;
36
37    protected StatusWindow status; // status window isn't referred to by name, it is just the default
38
protected HashMap windows; // key=String value=StatusWindow
39

40    protected UserHandler userInput;
41    protected UIScriptBridge scriptBridge; // UI hooks for the scripting engine
42

43    protected ClientWindowStuff windowListener;
44    protected ClickableURLHandler urlListener;
45
46    public void cleanup()
47    {
48       getCapabilities().cleanup();
49    }
50
51    public Capabilities getCapabilities()
52    {
53       return client.getCapabilities();
54    }
55
56    public void executeCommand(String JavaDoc command)
57    {
58       ((UserHandler)getCapabilities().getDataStructure("commands")).processCommand(command);
59    }
60
61    public IRCSession()
62    {
63       client = new InternetRelayChatClient();
64       client.init(new UICapabilities(this)); // UI Capabilities provides interface for stable irc code to this lame gui code
65

66       userInput = (UserHandler)client.getCapabilities().getDataStructure("commands");
67
68       // init our gui stuff...
69

70       if (ClientState.getClientState().isOption("ui.sdi", ClientDefaults.ui_sdi))
71       {
72          desktop = new ClientPanel();
73       }
74       else
75       {
76          desktop = new ClientDesktop();
77          ((ClientDesktop)desktop).addMouseListener(new PopupManager());
78       }
79
80       windows = new HashMap();
81
82       urlListener = new ClickableURLHandler();
83       urlListener.installCapabilities(getCapabilities());
84
85       windowListener = new ClientWindowStuff();
86
87       scriptBridge = new UIScriptBridge(this);
88       status = createStatusWindow();
89       scriptBridge.windowCreated(status);
90       status.getDisplay().addClickListener(urlListener);
91    }
92
93    public boolean isWindow(String JavaDoc name)
94    {
95       return (windows.get(name.toUpperCase()) != null || name.equals(StatusWindow.STATUS_NAME));
96    }
97
98    public StatusWindow getWindow(String JavaDoc name)
99    {
100       if (name == null || windows.get(name.toUpperCase()) == null)
101       {
102          return status;
103       }
104       return (StatusWindow)windows.get(name.toUpperCase());
105    }
106
107    public Collection getAllWindows()
108    {
109       return desktop.getAllWindows();
110    }
111
112    public InternetRelayChatClient getClient()
113    {
114       return client;
115    }
116
117    public StatusWindow getStatusWindow()
118    {
119       return status;
120    }
121
122    public boolean isStatus(StatusWindow window)
123    {
124       return window == status;
125    }
126
127    public WindowManager getDesktop()
128    {
129       return desktop;
130    }
131
132    public StatusWindow getActiveWindow()
133    {
134       StatusWindow temp = desktop.getActiveWindow();
135       if (temp == null || !temp.isLegalWindow())
136       {
137          return getStatusWindow();
138       }
139       return temp;
140    }
141
142    public StatusWindow getRealActiveWindow()
143    {
144       return desktop.getActiveWindow();
145    }
146
147    public void createAboutWindow()
148    {
149       AboutWindow contents = new AboutWindow();
150       desktop.addWindow(contents, true);
151
152       Thread JavaDoc fred = new Thread JavaDoc(contents);
153       fred.start();
154    }
155
156    public void renameWindow(String JavaDoc name, String JavaDoc newname)
157    {
158       StatusWindow temp = (StatusWindow)windows.get(name.toUpperCase());
159       StatusWindow temp2 = (StatusWindow)windows.get(newname.toUpperCase());
160
161       if (temp != null && temp2 == null)
162       {
163          windows.remove(name.toUpperCase());
164          windows.put(newname.toUpperCase(), temp);
165          temp.setName(newname);
166       }
167    }
168
169    private DCCListDialog dccListDialog = null;
170   
171    public void createDCCWindow()
172    {
173       if (dccListDialog == null)
174       {
175          dccListDialog = new DCCListDialog();
176          dccListDialog.installCapabilities(client.getCapabilities());
177
178          desktop.addWindow(dccListDialog, true);
179
180          windows.put(dccListDialog.getName().toUpperCase(), dccListDialog);
181          scriptBridge.windowCreated(dccListDialog);
182          dccListDialog.getWindow().addWindowListener(windowListener);
183       }
184       else if (!dccListDialog.isOpen())
185       {
186          desktop.addWindow(dccListDialog, true);
187
188          windows.put(dccListDialog.getName().toUpperCase(), dccListDialog);
189          scriptBridge.windowCreated(dccListDialog);
190          dccListDialog.getWindow().addWindowListener(windowListener);
191       }
192    }
193
194    public ScriptedListDialog createSortedWindow(String JavaDoc title, String JavaDoc hook, Object JavaDoc data, LinkedList columns)
195    {
196       ScriptedListDialog contents = new ScriptedListDialog(title, hook, data, columns);
197       contents.installCapabilities(client.getCapabilities());
198
199       desktop.addWindow(contents, false);
200
201       windows.put(title.toUpperCase(), contents);
202       scriptBridge.windowCreated(contents);
203       contents.getWindow().addWindowListener(windowListener);
204
205       return contents;
206    }
207
208    public void createListWindow()
209    {
210       ChannelListDialog contents = new ChannelListDialog();
211       contents.installCapabilities(client.getCapabilities());
212
213       desktop.addWindow(contents, true);
214
215       windows.put(contents.getName().toUpperCase(), contents);
216       scriptBridge.windowCreated(contents);
217       contents.getWindow().addWindowListener(windowListener);
218    }
219
220    public StatusWindow createStatusWindow()
221    {
222       StatusWindow contents = new StatusWindow();
223
224       desktop.addWindow(contents, true);
225
226       contents.getInput().addInputListener(userInput); // we'll just do the input assignment ourselves thank you very much.
227
contents.installCapabilities(client.getCapabilities());
228
229       return contents;
230    }
231
232    public QueryWindow createQueryWindow(String JavaDoc user, boolean selected)
233    {
234       QueryWindow contents = new QueryWindow(user);
235
236       desktop.addWindow(contents, selected);
237
238       contents.getInput().addInputListener(userInput); // we'll just do the input assignment ourselves thank you very much.
239
contents.installCapabilities(client.getCapabilities());
240       windows.put(user.toUpperCase(), contents);
241
242       scriptBridge.windowCreated(contents);
243       contents.getDisplay().addClickListener(urlListener);
244
245       contents.getWindow().addWindowListener(windowListener); // must be added last, so this is the last event to fire when
246
// stuff happens
247

248       return contents;
249    }
250
251    public ChannelWindow createChannelWindow(rero.ircfw.Channel channel)
252    {
253       final ChannelWindow contents = new ChannelWindow(channel);
254
255       desktop.addWindow(contents, true);
256
257       contents.getInput().addInputListener(userInput); // we'll just do the input assignment ourselves thank you very much.
258
contents.installCapabilities(client.getCapabilities());
259       windows.put(channel.getName().toUpperCase(), contents);
260
261       scriptBridge.windowCreated(contents);
262       contents.getDisplay().addClickListener(urlListener);
263
264       contents.getWindow().addWindowListener(windowListener);
265
266       return contents;
267    }
268
269    public StatusWindow resolveClientWindow(ClientWindow temp)
270    {
271       Iterator i = windows.values().iterator();
272       while (i.hasNext())
273       {
274          StatusWindow x = (StatusWindow)i.next();
275          if (x.getWindow() == temp)
276          {
277             return x;
278          }
279       }
280
281       return status;
282    }
283
284    // perform actions related to the window closing...
285
public void postProcessWindow(StatusWindow window)
286    {
287       if (window instanceof ChannelWindow)
288       {
289          if (ClientState.getClientState().isOption("auto.part", ClientDefaults.auto_option))
290          {
291              InternalDataList ircData = (InternalDataList)getCapabilities().getDataStructure(DataStructures.InternalDataList);
292
293              if (ircData.getChannel(window.getName()) != null)
294              {
295                 getCapabilities().sendln("PART " + window.getName());
296              }
297          }
298       }
299       else if (window.getName().charAt(0) == '=' && window.isLegalWindow())
300       {
301          if (ClientState.getClientState().isOption("auto.chatclose", ClientDefaults.auto_option))
302          {
303              DataDCC dccData = (DataDCC)getCapabilities().getDataStructure("dcc");
304              dccData.closeChat(window.getName().substring(1, window.getName().length()));
305          }
306       }
307    }
308
309    protected class ClientWindowStuff implements ClientWindowListener
310    {
311
312       public void onActive(ClientWindowEvent ev) { }
313       public void onOpen(ClientWindowEvent ev) { }
314       public void onInactive(ClientWindowEvent ev) { }
315       public void onMinimize(ClientWindowEvent ev) { }
316       public void onClose(ClientWindowEvent ev)
317       {
318          ClientWindow temp = (ClientWindow)ev.getSource();
319          Iterator i = windows.values().iterator();
320          while (i.hasNext())
321          {
322             StatusWindow x = (StatusWindow)i.next();
323             if (x.getWindow() == temp)
324             {
325                postProcessWindow(x);
326                i.remove();
327                return;
328             }
329          }
330       }
331    }
332
333    protected class PopupManager extends MouseAdapter
334    {
335       public void maybeShowPopup(MouseEvent ev)
336       {
337          if (ev.isPopupTrigger())
338          {
339              MenuBridge bridge = (MenuBridge)client.getCapabilities().getDataStructure("menuBridge");
340              JPopupMenu menu = bridge.getPopupMenu("background", null);
341      
342              if (menu == null)
343              {
344                 return;
345              }
346
347              menu.show((JComponent)ev.getComponent(), ev.getX(), ev.getY());
348              ev.consume();
349          }
350       }
351
352       public void mouseClicked(MouseEvent e)
353       {
354          maybeShowPopup(e);
355       }
356
357       public void mousePressed(MouseEvent e)
358       {
359          maybeShowPopup(e);
360       }
361
362       public void mouseReleased(MouseEvent e)
363       {
364          maybeShowPopup(e);
365       }
366    }
367
368    protected void finalize()
369    {
370       System.out.println("FINALIZING IRC SESSION y0");
371    }
372 }
373
374
Popular Tags