KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > SessionManager


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.net.interfaces.*;
12 import rero.net.*;
13
14 import rero.gui.windows.*;
15
16 import rero.client.*;
17 import rero.dialogs.server.*;
18
19 import rero.test.QuickConnect;
20
21 import rero.bridges.menu.*;
22
23 import rero.config.*;
24 import rero.gui.toolkit.*;
25
26 public class SessionManager extends JTabbedPane implements ClientWindowListener, SocketStatusListener, ChangeListener, ClientStateListener
27 {
28    protected HashMap sessions = new HashMap();
29    protected IRCSession active = null;
30    protected JFrame frame = null;
31    protected JMenuBar menu = null;
32    protected MenuBridge bridge = null;
33    protected PopupManager popups = null;
34    protected KeyBindings keyb = null;
35
36    protected static GlobalCapabilities global;
37
38    public static GlobalCapabilities getGlobalCapabilities()
39    {
40       return global;
41    }
42
43    public void propertyChanged(String JavaDoc property, String JavaDoc parameter)
44    {
45       bridge = (MenuBridge)getActiveSession().getCapabilities().getDataStructure("menuBridge");
46       
47       SwingUtilities.invokeLater(new Runnable JavaDoc()
48       {
49          public void run()
50          {
51             if (ClientState.getClientState().isOption("ui.showbar", ClientDefaults.ui_showbar))
52             {
53                if (frame.getJMenuBar() == null)
54                   frame.setJMenuBar(menu);
55             }
56             else
57             {
58                frame.setJMenuBar(null);
59             }
60
61             bridge.installMenubar(menu);
62             menu.repaint();
63             frame.validate();
64          }
65       });
66    }
67
68    public void stateChanged(ChangeEvent ev)
69    {
70       active = getSession(getSelectedComponent());
71
72       if (getActiveSession() != null)
73       {
74          MenuBridge temp = (MenuBridge)getActiveSession().getCapabilities().getDataStructure("menuBridge");
75          temp.installMenubar(menu);
76          GraphicalToolbar.stateChanged(); // CHEATING!!!!
77

78          getActiveSession().getCapabilities().dispatchEvent(switchEventHashMap);
79       }
80    }
81
82    protected HashMap switchEventHashMap = new HashMap();
83
84    public SessionManager(JFrame _frame)
85    {
86       //
87
// make the tabs scrollable, show them on the bottom.
88
//
89
// super(JTabbedPane.BOTTOM, JTabbedPane.SCROLL_TAB_LAYOUT);
90
super();
91
92       switchEventHashMap.put("$event", "session");
93
94       setTabPlacement(JTabbedPane.BOTTOM);
95 // setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); - swing bug
96

97       global = new GlobalCapabilities(this);
98
99       keyb = new KeyBindings(this);
100       KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyb);
101  
102       addChangeListener(this);
103
104       SwingUtilities.invokeLater(new Runnable JavaDoc()
105       {
106          public void run()
107          {
108             // do this before doing anything else... dig?
109
if (!ClientState.getClientState().isOption("ui.showtabs", ClientDefaults.ui_showtabs))
110             {
111                setUI(new MinimalTabUI());
112             }
113
114             StringList temp = ClientState.getClientState().getStringList("auto.connect");
115
116             if (QuickConnect.IsQuickConnect())
117             {
118                addSession();
119                getSession(getComponentAt(0)).executeCommand(QuickConnect.GetInformation().getConnectCommand());
120             }
121             else if (temp.getList().size() == 0)
122             {
123                addSession();
124             }
125             else
126             {
127                ServerData sdata = ServerData.getServerData();
128
129                int y = 0;
130                Iterator i = temp.getList().iterator();
131                while (i.hasNext())
132                {
133                   Server stemp = sdata.getServerByName(i.next().toString());
134
135                   if (stemp != null)
136                   {
137                      addSession();
138                      getSession(getComponentAt(y)).executeCommand(stemp.getCommand());
139                      y++;
140                   }
141                }
142             }
143
144             GraphicalToolbar.startup(); // the toolbar knows how to set itself up using the global stuff available
145
}
146       });
147
148       menu = new JMenuBar();
149
150       frame = _frame;
151
152       if (ClientState.getClientState().isOption("ui.showbar", ClientDefaults.ui_showbar))
153       {
154          frame.setJMenuBar(menu);
155       }
156
157       if (getMouseListeners().length > 0)
158          removeMouseListener(getMouseListeners()[0]);
159
160       popups = new PopupManager();
161       addMouseListener(popups);
162
163       ClientState.getClientState().addClientStateListener("loaded.scripts", this);
164       ClientState.getClientState().addClientStateListener("ui.showbar", this);
165    }
166
167    
168
169    /** returns the currently active irc session */
170    public IRCSession getActiveSession()
171    {
172       return active;
173    }
174
175    /** returns the specified session */
176    public IRCSession getSpecificSession(int index)
177    {
178       return getSession(getComponentAt(index));
179    }
180
181    /** all calls to add session should be done on the java event thread... period! Or else bad things will happen. */
182    public void addSession()
183    {
184       IRCSession session = new IRCSession();
185
186       active = session; // we'll assume that a new session is always "active"
187

188       sessions.put(session.getStatusWindow().getWindow(), session); // store the session by its status window
189
sessions.put(session.getCapabilities().getSocketConnection(), session); // store the session by its socket
190
sessions.put(session.getCapabilities(), session); // store the session by its capabilities
191

192       sessions.put(session.getDesktop(), session); // store the session by its component
193

194       session.getStatusWindow().getWindow().addWindowListener(this);
195       session.getCapabilities().getSocketConnection().addSocketStatusListener(this);
196
197       setSelectedComponent(add("not connected", session.getDesktop()));
198
199       session.getClient().post(); // tells the irc client code to do the default stuff once everything is initialized
200

201 /* for (int x = 0; x < getComponents().length; x++)
202       {
203          System.out.println("Adding listener: " + getComponents()[x]);
204          getComponents()[x].addMouseListener(popups);
205       } */

206
207       revalidate();
208
209       if (ClientState.getClientState().getString("user.nick", null) == null && !QuickConnect.IsQuickConnect())
210       {
211           getGlobalCapabilities().showOptionDialog(null);
212           session.getStatusWindow().getInput().requestFocus(); // give status window focus after first time setup...
213
}
214    }
215
216    public IRCSession getSession(Object JavaDoc key)
217    {
218       return (IRCSession)sessions.get(key);
219    }
220
221    public int getIndexFor(Object JavaDoc key)
222    {
223       IRCSession session = getSession(key);
224
225       if (session == null)
226       {
227          return -1;
228       }
229
230       int n = indexOfComponent(session.getDesktop());
231
232       if (n > getTabCount())
233           return -1;
234
235       return n;
236    }
237
238    public void removeSession(IRCSession session)
239    {
240       Iterator i = sessions.values().iterator();
241       while (i.hasNext())
242       {
243          Object JavaDoc temp = i.next();
244          if (temp == session)
245          {
246             i.remove();
247          }
248       }
249
250       long freememory = Runtime.getRuntime().freeMemory();
251       remove(session.getDesktop());
252
253       keyb.removeSession(session);
254
255       session.cleanup();
256
257       revalidate();
258
259       System.gc();
260
261       freememory = Runtime.getRuntime().freeMemory() - freememory;
262       System.out.println("Freed a total of " + rero.util.ClientUtils.formatBytes(freememory));
263
264       if (getTabCount() <= 0)
265       {
266          addSession(); // bad things happen if we don't always have a session "open"
267
}
268       
269       if (active == session)
270       {
271          stateChanged(null);
272       }
273    }
274
275    public void onClose(ClientWindowEvent ev)
276    {
277       IRCSession source = getSession(ev.getSource());
278
279       removeSession(source); // remove the session first so the irc connection is told to clean up and not "auto reconnect"
280

281       if (source.getCapabilities().isConnected())
282       {
283          source.getCapabilities().sendln("QUIT :Hey! Where'd my controlling terminal go?");
284          source.getCapabilities().getSocketConnection().disconnect();
285       }
286    }
287
288    public void onActive(ClientWindowEvent ev)
289    {
290
291    }
292
293    public void onInactive(ClientWindowEvent ev)
294    {
295
296    }
297
298    public void onMinimize(ClientWindowEvent ev)
299    {
300
301    }
302
303    public void onOpen(ClientWindowEvent ev)
304    {
305  
306    }
307
308    public void setTabTitle(Object JavaDoc key, String JavaDoc text)
309    {
310       setTitleAt(getIndexFor(key), text);
311    }
312
313    public void socketStatusChanged(SocketEvent ev)
314    {
315       if (ev.data.isConnected)
316       {
317          if (getIndexFor(ev.socket) > -1)
318             setTitleAt(getIndexFor(ev.socket), ev.data.hostname);
319       }
320       else
321       {
322          if (getIndexFor(ev.socket) > -1)
323             setTitleAt(getIndexFor(ev.socket), "disconnected");
324       }
325
326       GraphicalToolbar.stateChanged(); // CHEATING!!!!
327
}
328
329    public IRCSession getSessionAt(Point location)
330    {
331       if (indexAtLocation((int)location.getX(), (int)location.getY()) < 0)
332       {
333          return null;
334       }
335       return getSession(getComponentAt(indexAtLocation((int)location.getX(), (int)location.getY())));
336    }
337
338    protected class PopupManager extends MouseAdapter
339    {
340    public void maybeShowPopup(MouseEvent ev)
341    {
342       if (ev.isPopupTrigger())
343       {
344          IRCSession temp = getSessionAt(ev.getPoint());
345
346          if (temp == null)
347          {
348             return;
349          }
350
351          bridge = (MenuBridge)temp.getCapabilities().getDataStructure("menuBridge");
352          JPopupMenu menu = bridge.getPopupMenu("tab", null);
353
354          if (menu == null)
355          {
356             return;
357          }
358
359          menu.show((JComponent)ev.getComponent(), ev.getX(), ev.getY());
360          ev.consume();
361
362          return;
363       }
364
365       return;
366    }
367
368    public void mouseClicked(MouseEvent e)
369    {
370       maybeShowPopup(e);
371    }
372
373    public void mousePressed(MouseEvent e)
374    {
375       maybeShowPopup(e);
376
377       if (e.getButton() == MouseEvent.BUTTON1)
378       {
379           int idx = indexAtLocation(e.getX(), e.getY());
380
381           if (idx > -1)
382           {
383              setSelectedIndex(idx);
384           }
385       }
386    }
387
388    public void mouseReleased(MouseEvent e)
389    {
390       maybeShowPopup(e);
391    }
392
393    public void mouseEntered(MouseEvent e) { }
394    public void mouseExited(MouseEvent e) { }
395    }
396 }
397
Popular Tags