jTracer  1.03
Stack trace visualization tool
SessionManager.java
Go to the documentation of this file.
1 package org.libcsdbg.jtracer;
2 
3 import java.beans.PropertyChangeListener;
4 
5 import java.util.Vector;
6 //import java.util.Hashtable;
7 
8 import java.io.IOException;
9 
10 import java.net.Socket;
11 
12 import java.awt.event.WindowEvent;
13 import java.awt.event.WindowListener;
14 
15 import javax.swing.JFrame;
16 import java.awt.Component;
17 
18 public class SessionManager extends Component implements
19  WindowListener
20 {
22  private static final long serialVersionUID = 0x0200;
23 
24  private JFrame owner = null;
25 
26  private Vector<Session> sessions = null;
27 
29  private int current = -1;
30 
31 
32  SessionManager(JFrame parent)
33  {
34  super();
35  owner = parent;
36 
37  addPropertyChangeListener("sessionCount", (PropertyChangeListener) owner);
38  addPropertyChangeListener("currentSession", (PropertyChangeListener) owner);
39 
40  sessions = new Vector<Session>();
41  }
42 
43 
44  public void register(Socket sock) throws IOException
45  {
46  Session s = new Session(owner, sock);
47  s.addWindowListener(this);
48 
49  sessions.add(s);
50  current = sessions.size() - 1;
51 
52  setClientPosition(current);
53  s.setVisible(true);
54  s.toFront();
55  s.setAlwaysOnTop(owner.isAlwaysOnTop());
56  }
57 
58 
59  public void disposeCurrent()
60  {
61  sessions.get(current).quit();
62  sessions.get(current).dispose();
63  }
64 
65 
66  public void disposeAll()
67  {
68  while (sessions.size() > 0) {
69  Session cur = sessions.remove(0);
70  cur.removeWindowListener(this);
71  cur.dispose();
72  }
73 
74  current = -1;
75  firePropertyChange("sessionCount", null, 0);
76  }
77 
78 
79  public void setAlwaysOnTop(int i, boolean how)
80  {
81  int cnt = sessions.size();
82  if (i >= 0 && i < cnt) {
83  sessions.get(i).setAlwaysOnTop(how);
84  return;
85  }
86 
87  for (i = 0; i < cnt; i++)
88  sessions.get(i).setAlwaysOnTop(how);
89  }
90 
91 
97  public void setIconified(final boolean how)
98  {
99  /* Defer execution to another thread, with a Worker object */
100  class Worker implements Runnable {
101  public void run()
102  {
103  int prev = current;
104  for (int i = 0, cnt = sessions.size(); i < cnt; i++) {
105  sessions.get(i).setIconified(how);
106 
107  try {
108  Thread.sleep(50);
109  }
110 
111  catch (Throwable t) {
112  }
113  }
114 
115  /* When a window is restored, it's activated by the OS window manager */
116  sessions.get(prev).toFront();
117  }
118  }
119 
120  Thread t = new Thread(new Worker());
121  t.start();
122  }
123 
124 
132  public void shiftSelection(int step)
133  {
134  step = (step < 0) ? -1 : 1;
135  Session prev = sessions.get(current), cur = null;
136 
137  /*
138  This loop stops when either a non-iconified window is selected or (if all
139  windows are iconified) when a full circle is completed
140  */
141  do {
142  current += step;
143  if (current < 0)
144  current = sessions.size() - 1;
145 
146  else if (current > sessions.size() - 1)
147  current = 0;
148 
149  cur = sessions.get(current);
150  if (cur.isIconified())
151  continue;
152 
153  cur.toFront();
154  break;
155  }
156  while (!prev.equals(cur));
157  }
158 
159 
160  public Vector<Session> getSessions()
161  {
162  return sessions;
163  }
164 
165 
166  public int getCurrent()
167  {
168  return current;
169  }
170 
171 
172  public void setCurrent(int sel)
173  {
174  Session cur = sessions.get(sel);
175  cur.setIconified(false);
176  cur.toFront();
177  }
178 
179 
180  public int getSessionCount()
181  {
182  return sessions.size();
183  }
184 
185 
191  public int getTraceCount()
192  {
193  int cnt = 0;
194  for (int i = 0, sz = sessions.size(); i < sz; i++)
195  cnt += sessions.get(i).getTraceCount();
196 
197  return cnt;
198  }
199 
200 
210  public void setClientPosition(int index)
211  {
213  int rowsz = 6;
214  int step = 40;
215  int basex = 100;
216  int basey = 50;
217 
218  int row = (index + rowsz) / rowsz;
219  int mod = index % rowsz;
220  int x = basex + step * mod;
221  int y = basey + step * (row + mod);
222 
223  sessions.get(index).setLocation(x, y);
224  }
225 
226 
227  public void cascade()
228  {
229  for (int i = 0, cnt = sessions.size(); i < cnt; i++)
231  }
232 
233 
239  public void windowClosing(WindowEvent event)
240  {
241  try {
242  JFrame f = (JFrame) event.getWindow();
243  if (f.equals(owner))
244  System.exit(0);
245 
246  int i, cnt;
247  for (i = 0, cnt = sessions.size(); i < cnt; i++)
248  if (f.equals(sessions.get(i)))
249  break;
250 
251  if (i == cnt)
252  return;
253 
254  Session cur = sessions.remove(i);
255  cur.removeWindowListener(this);
256  cur.quit();
257  cur.dispose();
258 
259  cnt--;
260  if (cnt == 0) {
261  current = -1;
262  firePropertyChange("sessionCount", null, cnt);
263  return;
264  }
265 
266  if (i < current || current == cnt)
267  current--;
268 
269  firePropertyChange("sessionCount", null, cnt);
270  cur = sessions.get(current);
271  if (cur.isIconified())
272  ;//shiftSelection(1);
273  else
274  cur.toFront();
275  }
276 
277  catch (Throwable t) {
278  Registry.debug(t);
279  }
280  }
281 
282 
288  public void windowClosed(WindowEvent event)
289  {
290  try {
291  JFrame f = (JFrame) event.getWindow();
292  if (f.equals(owner))
293  return;
294 
295  Session cur = sessions.remove(current);
296  cur.quit();
297  int cnt = sessions.size();
298  if (cnt == 0) {
299  current = -1;
300  firePropertyChange("sessionCount", null, cnt);
301  return;
302  }
303 
304  if (current == cnt)
305  current--;
306 
307  firePropertyChange("sessionCount", null, cnt);
308  cur = sessions.get(current);
309  if (cur.isIconified())
310  ;//shiftSelection(1);
311  else
312  cur.toFront();
313 
314  //setUiState("Client list changed");*/
315  }
316 
317  catch (Throwable t) {
318  Registry.debug(t);
319  }
320  }
321 
322 
328  public void windowOpened(WindowEvent event)
329  {
330  try {
331  JFrame f = (JFrame) event.getWindow();
332  if (f.equals(owner))
333  return;
334 
335  firePropertyChange("sessionCount", null, sessions.size());
336  }
337 
338  catch (Throwable t) {
339  Registry.debug(t);
340  }
341  }
342 
343 
349  public void windowIconified(WindowEvent event)
350  {
351  try {
352  JFrame f = (JFrame) event.getWindow();
353  if (f.equals(owner))
354  setIconified(true);
355  }
356 
357  catch (Throwable t) {
358  Registry.debug(t);
359  }
360  }
361 
362 
368  public void windowDeiconified(WindowEvent event)
369  {
370  try {
371  JFrame f = (JFrame) event.getWindow();
372  if (f.equals(owner))
373  setIconified(false);
374  }
375 
376  catch (Throwable t) {
377  Registry.debug(t);
378  }
379  }
380 
381 
387  public void windowActivated(WindowEvent event)
388  {
389  try {
390  JFrame f = (JFrame) event.getWindow();
391  if (f.equals(owner))
392  return;
393 
394  for (int i = 0, cnt = sessions.size(); i < cnt; i++) {
395  Session cur = sessions.get(i);
396 
397  if (f.equals(cur)) {
398  current = i;
399  firePropertyChange("currentSession", null, current);
400  break;
401  }
402  }
403  }
404 
405  catch (Throwable t) {
406  Registry.debug(t);
407  }
408  }
409 
410 
416  public void windowDeactivated(WindowEvent event)
417  {
418  }
419 }
420 
void windowDeiconified(WindowEvent event)
Handler for events fired for restored windows.
void windowActivated(WindowEvent event)
Handler for events fired for activated (focused) windows.
void windowClosed(WindowEvent event)
Handler for events fired for closed (disposed) windows.
void windowIconified(WindowEvent event)
Handler for events fired for iconified windows.
void windowDeactivated(WindowEvent event)
Handler for events fired for deactivated (unfocused) windows.
void windowOpened(WindowEvent event)
Handler for events fired for opened windows.
void setAlwaysOnTop(int i, boolean how)
void setIconified(final boolean how)
Minimize or restore all client windows.
void shiftSelection(int step)
Select the previous/next not iconified client window.
void setClientPosition(int index)
Position a client frame to its default coordinates.
int current
Currently selected (focused) client.
int getTraceCount()
Get the total number of traces.
static final long serialVersionUID
Class version.
boolean isIconified()
Check frame state.
Definition: Session.java:191
void windowClosing(WindowEvent event)
Handler for events fired for closing windows.