]> code.citadel.org Git - citadel.git/blob - shaggy/whoOnlineWindow.java
Fixed a broken tag in siteconfig.c
[citadel.git] / shaggy / whoOnlineWindow.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.util.*;
5
6 public class whoOnlineWindow extends JFrame {
7     DefaultListModel    users;
8     JList               theList;
9     boolean             refresh=false;
10
11     public whoOnlineWindow() {
12         setTitle( "Who is online" );
13
14         JPanel  p = new JPanel();
15         p.setLayout( new BorderLayout() );
16         p.setBorder( BorderFactory.createTitledBorder( 
17                                                       BorderFactory.createEtchedBorder(), "Users" ) );
18
19         theList = new JList();
20         users = new DefaultListModel();
21         theList.setModel( users );
22
23         theList.addMouseListener( new MouseAdapter() {
24             public void mouseClicked( MouseEvent e ) {
25                 if( refresh) return;
26                 int     i = theList.getSelectedIndex();
27                 if( i >= 0 )
28                     new pageUserWindow( unpad( (String)users.elementAt( i )) );
29             } } );
30
31         p.add( "Center", new JScrollPane( theList ) );
32         
33         getContentPane().setLayout( new BorderLayout() );
34         getContentPane().add( "Center", p );
35
36         p = new JPanel();
37         JButton b;
38         p.add( b = new JButton( "Refresh" ) );
39         b.addActionListener( new ActionListener() {
40             public void actionPerformed( ActionEvent e ) {
41                 refresh();
42             } } );
43
44         p.add( b = new JButton( "Close" ) );
45         b.addActionListener( new ActionListener() {
46             public void actionPerformed( ActionEvent e ) {
47                 closeWin();
48             } } );
49
50         getContentPane().add( "South", p );
51
52         addWindowListener( new WindowAdapter() {
53             public void windowClosing( WindowEvent e ) {
54                 closeWin();
55             } } );
56
57         citadel.me.registerWindow( this );
58         citadel.me.wo = this;
59         refresh();
60         pack();
61         show();
62     }
63
64     public void closeWin() {
65         citadel.me.removeWindow( this );
66         citadel.me.wo = null;
67         dispose();
68     }
69
70     public void refresh() {
71         citadel.me.networkEvent( "RWHO", new CallBack() {
72             public void run( citReply r ) {
73                 refresh = true;
74
75                 users.removeAllElements();
76
77                 int             i = 0;
78                 String  s;
79                 while( (s = r.getLine( i++ )) != null ) {
80                     int j = s.indexOf( '|' ) + 1;
81                     int k = s.indexOf( '|', j );
82                     int l = s.indexOf( '|', k + 1 );
83                     String      user = s.substring( j, k );
84                     String      room = s.substring( k+1, l );
85                     users.addElement( pad( user, room ) );
86                 }
87             refresh=false;
88             }
89         } );
90     }
91
92     public String pad( String u, String r ) {
93         StringBuffer    s = new StringBuffer( u );
94         while( s.length() < 30 )
95             s.append( ' ' );
96         s.append( r );
97         return s.toString();
98     }
99
100     public String unpad( String p ) {
101         StringBuffer    s = new StringBuffer( p.substring( 0, Math.min( 29, p.length() ) ) );
102         while( s.charAt( s.length() - 1) == ' ' )
103             s.setLength( s.length() -1 );
104         return s.toString();
105     }
106         
107
108 }
109
110 /*
111  class MyCellRenderer extends JLabel implements ListCellRenderer {
112      final static ImageIcon longIcon = new ImageIcon("long.gif");
113      final static ImageIcon shortIcon = new ImageIcon("short.gif");
114
115      // This is the only method defined by ListCellRenderer.  We just
116      // reconfigure the Jlabel each time we're called.
117
118      public Component getListCellRendererComponent(
119        JList list,
120        Object value,            // value to display
121        int index,               // cell index
122        boolean isSelected,      // is the cell selected
123        boolean cellHasFocus)    // the list and the cell have the focus
124      {
125          String s = value.toString();
126          setText(s);
127          setIcon((s.length() > 10) ? longIcon : shortIcon);
128            if (isSelected) {
129              setBackground(list.getSelectionBackground());
130                setForeground(list.getSelectionForeground());
131            }
132          else {
133                setBackground(list.getBackground());
134                setForeground(list.getForeground());
135            }
136            setEnabled(list.isEnabled());
137            setFont(list.getFont());
138          return this;
139      }
140  }
141
142  String[] data = {"one", "two", "free", "four"};
143  JList dataList = new JList(data);
144  dataList.setCellRenderer(new MyCellRenderer());
145 */