View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.gui;
21  
22  import java.awt.BorderLayout;
23  import java.awt.FlowLayout;
24  import java.awt.event.ActionEvent;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.JPanel;
29  import javax.swing.JScrollPane;
30  import javax.swing.JTable;
31  
32  import org.xnap.XNap;
33  import org.xnap.gui.ActionProvider;
34  import org.xnap.gui.PeerProvider;
35  import org.xnap.gui.component.XNapButton;
36  import org.xnap.gui.event.PopupListener;
37  import org.xnap.gui.menu.PeerMenu;
38  import org.xnap.gui.util.IconHelper;
39  import org.xnap.peer.Peer;
40  import org.xnap.plugin.opennap.OpenNapPlugin;
41  
42  public class OpenNapWhoisPanel extends JPanel 
43      implements ActionProvider, PeerProvider {
44  
45      // --- Data Field(s) ---
46  
47      protected JTable jtaWhois;
48      protected OpenNapWhoisTableModel tmWhois;
49  
50      protected ClearAction acClear = new ClearAction();
51      protected UpdateAction acUpdate = new UpdateAction();
52  
53      //--- Constructor(s) ---
54  	
55      public OpenNapWhoisPanel()
56      {
57  		initialize();
58      }
59  
60      // --- Method(s) ---
61  
62      private void initialize()
63      {
64  		setLayout(new BorderLayout(5, 5));
65  
66  		JPanel jpUsers = new JPanel(new BorderLayout());
67  
68  		// user popup
69  		PeerMenu jm = new PeerMenu(this);
70  
71  		// user table
72  		tmWhois = new OpenNapWhoisTableModel();
73  		jtaWhois = tmWhois.createTable(OpenNapPlugin.getPreferences(), "whois");
74  		jtaWhois.addMouseListener(new PopupListener(jm));
75  		jtaWhois.setShowGrid(false);
76  
77  		jpUsers.add(new JScrollPane(jtaWhois), BorderLayout.CENTER);
78  
79  		// buttons
80          JPanel jpButtons = new JPanel(new FlowLayout(FlowLayout.LEFT));
81  		jpButtons.add(new XNapButton(acUpdate));
82  		jpButtons.add(new XNapButton(acClear));
83  		jpUsers.add(jpButtons, BorderLayout.SOUTH);
84  
85  		add(jpUsers, BorderLayout.CENTER);
86      }
87  
88      public Action[] getActions()
89      {
90  		return new AbstractAction[] { acUpdate, acClear	};
91      }
92  
93      public Peer[] getPeers()
94      {
95  		int[] rows = jtaWhois.getSelectedRows();
96  		Peer[] users = new Peer[rows.length];
97  		for (int i = 0; i < rows.length; i++) {
98  			users[i] = tmWhois.get(rows[i]);
99  		}
100 		return users;
101     }
102 
103     /***
104      * Clears whois table.
105      */
106     private class ClearAction extends AbstractAction {
107 
108         public ClearAction() 
109 		{
110             putValue(Action.NAME, XNap.tr("Clear"));
111             putValue(Action.SHORT_DESCRIPTION, XNap.tr("Clear table."));
112 			putValue(IconHelper.XNAP_ICON, "edittrash.png");
113         }
114 
115         public void actionPerformed(ActionEvent event) 
116 		{
117 			tmWhois.clear();
118         }
119 
120     }
121 
122     /***
123      * Updates user table.
124      */
125     private class UpdateAction extends AbstractAction {
126 
127         public UpdateAction() 
128 		{
129             putValue(Action.NAME,XNap.tr("Update"));
130             putValue(Action.SHORT_DESCRIPTION,XNap.tr("Update table."));
131 			putValue(IconHelper.XNAP_ICON, "reload.png");
132         }
133 
134         public void actionPerformed(ActionEvent event) 
135 		{
136 			tmWhois.reload();
137         }
138 
139     }
140 
141 }
142 
143