ChatProviderPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.*;
import org.xnap.XNap;
import org.xnap.chat.ChannelInfo;
import org.xnap.chat.ChatManager;
import org.xnap.chat.ChatProvider;
import org.xnap.chat.ChatProviderEvent;
import org.xnap.chat.ChatProviderListener;
import org.xnap.chat.action.AbstractJoinAction;
import org.xnap.event.ListEvent;
import org.xnap.event.ListListener;
import org.xnap.gui.component.XNapButton;
import org.xnap.gui.component.XNapMenuItem;
import org.xnap.gui.event.DoubleClickListener;
import org.xnap.gui.event.PopupListener;
import org.xnap.gui.list.ChatProviderCellRenderer;
import org.xnap.gui.table.ChannelTableModel;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.util.Preferences;
public class ChatProviderPanel extends JPanel
implements ActionProvider, ChatProviderListener, ListListener,
ItemListener
{
//--- Constant(s) ---
// --- Data Field(s) ---
private static Preferences prefs = Preferences.getInstance();
private ConsolePane cpMessages;
private ChannelTableModel tmChannels;
private JComboBox jcbServers;
private JTable jta;
private JPanel jpActions;
private JSplitPane jspH;
private JoinChannelAction acJoinChannel = new JoinChannelAction();
//--- Constructor(s) ---
public ChatProviderPanel()
{
initialize();
ChatProvider[] providers = ChatManager.getInstance().getProviders();
for (int i = 0; i < providers.length; i++) {
jcbServers.addItem(providers[i]);
}
ChatManager.getInstance().addProviderListListener(this);
}
// --- Method(s) ---
private void initialize()
{
JPanel jpTop = new JPanel(new GridBagLayout());
jpTop.setBorder(new EmptyBorder(5, 5, 5, 5));
// server combo box
JLabel l = GridBagHelper.addLabel(jpTop, XNap.tr("Server"));
jcbServers = new JComboBox();
l.setLabelFor(jcbServers);
jcbServers.addItemListener(this);
jcbServers.setRenderer(new ChatProviderCellRenderer());
GridBagHelper.addComponent(jpTop, jcbServers);
// actions
jpActions = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
GridBagHelper.add(jpTop, jpActions);
// channel table
tmChannels = new ChannelTableModel();
jta = tmChannels.createTable(prefs, "channel");
jta.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DoubleClickListener.install(jta, acJoinChannel);
GUIHelper.bindEnterKeyLocally(jta, acJoinChannel);
JScrollPane jspChannels = new JScrollPane(jta);
// table context menu
JPopupMenu popup = new JPopupMenu();
popup.add(new XNapMenuItem(acJoinChannel));
jta.addMouseListener(new PopupListener(popup));
// console
cpMessages = new ConsolePane();
// split pane
jspH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jspH.add(jspChannels, JSplitPane.LEFT);
jspH.add(cpMessages, JSplitPane.RIGHT);
jspH.setDividerLocation(prefs.getChatGlobalVerticalDividerLocation());
jspH.setResizeWeight(0);
jspH.setOneTouchExpandable(true);
jspH.setBorder(GUIHelper.createEmptyBorder());
setLayout(new BorderLayout());
add(jpTop, BorderLayout.NORTH);
add(jspH, BorderLayout.CENTER);
GUIHelper.setMnemonics(this);
}
/**
* Invoked when the list of channels has been updated.
*/
public void channelsUpdated(final ChatProviderEvent event)
{
Runnable runner = new Runnable()
{
public void run()
{
ChatProvider p = getSelectedProvider();
if (p == event.getSource()) {
tmChannels.set(p);
}
}
};
SwingUtilities.invokeLater(runner);
}
/**
* Invoked when a message has been received.
*/
public void messageReceived(ChatProviderEvent event)
{
cpMessages.appendLater(event.getMessage());
}
public int getDividerLocation()
{
return jspH.getDividerLocation();
}
/**
* Invoked when a {@link ChatProvider} object is added to the
* {@link ChatManager}.
*/
public void itemAdded(ListEvent e)
{
final Object obj = e.getItem();
Runnable runner = new Runnable()
{
public void run()
{
jcbServers.addItem(obj);
}
};
SwingUtilities.invokeLater(runner);
}
/**
* Invoked when a {@link ChatProvider} object is removed from
* {@link ChatManager}.
*/
public void itemRemoved(ListEvent e)
{
final Object obj = e.getItem();
Runnable runner = new Runnable()
{
public void run()
{
jcbServers.removeItem(obj);
}
};
SwingUtilities.invokeLater(runner);
}
/**
* Invoked when a ChatProvider is selected from the ComboBox.
*/
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED) {
ChatProvider p = (ChatProvider)e.getItem();
p.addChatProviderListener(this);
tmChannels.set(p);
cpMessages.setLater(p.getMessages());
jpActions.removeAll();
Action[] actions = p.getActions();
if (actions != null) {
for (int i = 0; i < actions.length; i++) {
jpActions.add(new XNapButton(actions[i]));
}
}
// redraw
jpActions.updateUI();
}
else if (e.getStateChange() == ItemEvent.DESELECTED) {
((ChatProvider)e.getItem()).removeChatProviderListener(this);
tmChannels.clear();
cpMessages.setLater("");
jpActions.removeAll();
}
}
public ChannelInfo getSelectedChannel()
{
int i = jta.getSelectedRow();
return (i != -1) ? tmChannels.get(i) : null;
}
public ChatProvider getSelectedProvider()
{
return (ChatProvider)jcbServers.getSelectedItem();
}
public Action[] getActions()
{
return (new Action[] {
acJoinChannel
});
}
public void savePrefs()
{
prefs.setChatGlobalVerticalDividerLocation(jspH.getDividerLocation());
}
//--- Inner Class(es) ---
private class JoinChannelAction extends AbstractJoinAction {
public JoinChannelAction()
{
}
public void actionPerformed(ActionEvent event)
{
ChannelInfo c = getSelectedChannel();
if (c != null) {
getSelectedProvider().join(c.getName());
}
else {
StatusBar.setText(XNap.tr("Please select a channel"));
}
}
}
}
The table below shows all metrics for ChatProviderPanel.java.



