📄 chatroompanel.java~
字号:
/* Copyright (C) 2003 Adam Olsen 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; either version 1, or (at your option) any later version. 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., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother.groupchat;import java.awt.*;import java.awt.event.*;import java.beans.*;import java.io.File;import java.util.*;import java.util.regex.*;import javax.swing.*;import javax.swing.text.JTextComponent;import javax.swing.text.html.HTMLDocument;import org.jivesoftware.smack.*;import org.jivesoftware.smack.packet.*;import org.jivesoftware.smackx.Form;import org.jivesoftware.smackx.muc.DiscussionHistory;import org.jivesoftware.smackx.muc.MultiUserChat;import org.jivesoftware.smackx.packet.MUCUser;import com.valhalla.gui.*;import com.valhalla.jbother.*;import com.valhalla.jbother.jabber.BuddyStatus;import com.valhalla.jbother.jabber.MUCBuddyStatus;import com.valhalla.jbother.jabber.smack.InvitationRejectionPacketListener;import com.valhalla.jbother.plugins.events.MUCEvent;import com.valhalla.pluginmanager.PluginChain;import com.valhalla.settings.Settings;import net.infonode.tabbedpanel.*;import net.infonode.tabbedpanel.titledtab.*;import net.infonode.util.*;/** * This is the panel that contains a groupchat conversation. It is placed in a * JTabbedPane in GroupChat frame. * * @author Adam Olsen */public class ChatRoomPanel extends JPanel implements LogViewerCaller, TabFramePanel { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private MJTextArea textEntryArea = new MJTextArea(2, 0); private StringBuffer conversationText = new StringBuffer(); private JScrollPane conversationScroll; private ConversationArea conversationArea = new ConversationArea(); private JMenuItem logItem = new JMenuItem(resources.getString("viewLog")), newItem = new JMenuItem(resources.getString("joinRoom")), leaveItem = new JMenuItem(resources.getString("leaveRoom")), nickItem = new JMenuItem(resources.getString("changeNickname")), registerItem = new JMenuItem(resources.getString("registerForRoom")), viewAdmins = new JMenuItem(resources.getString("viewAdmins")), viewModerators = new JMenuItem(resources .getString("viewModerators")), viewMembers = new JMenuItem( resources.getString("viewMembers")), viewParticipants = new JMenuItem(resources .getString("viewParticipants")), viewOwners = new JMenuItem(resources.getString("viewOwners")), viewOutcasts = new JMenuItem(resources.getString("viewOutcasts")), destroyRoom = new JMenuItem(resources.getString("destroyRoom")); private JPopupMenu popMenu = new JPopupMenu(); private TitledTab tab; private int oldMaximum = 0; private HTMLDocument document = (HTMLDocument) conversationArea .getDocument(); private JPanel scrollPanel = new JPanel(new GridLayout(1, 0)); private JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); private MultiUserChat chat; private String chatroom, nickname, pass; private Hashtable buddyStatuses = new Hashtable(); private GroupChatNickList nickList; private String subject = resources.getString("noSubject"); private MJTextField subjectField = new MJTextField(); // for logging private ChatRoomPanel thisPointer = this; private boolean listenersAdded = false; private GroupParticipantListener participantListener; private GroupChatMessagePacketListener messageListener = new GroupChatMessagePacketListener( this); private SubjectListener subjectListener = new SubjectListener(this); private StatusListener statusListener = new StatusListener(this); private UserStatusListener userStatusListener = new UserStatusListener(this); private boolean messageToMe = false; /** * This sets up the appearance of the chatroom window * * @param chatroom * the chatroom address * @param nickname * the nickname to use when joining */ public ChatRoomPanel(String chatroom, String nickname, String pass) { this.chatroom = chatroom; this.nickname = nickname; this.pass = pass; chat = new MultiUserChat(BuddyList.getInstance().getConnection(), chatroom); BuddyList.getInstance().startTabFrame(); setLayout(new BorderLayout(5, 5)); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); subjectField.setText(resources.getString("noSubject")); JPanel subjectPanel = new JPanel(); subjectPanel.setLayout(new BorderLayout()); subjectPanel.add(new JLabel("<html><b>" + resources.getString("subject") + ": </b></html>"), BorderLayout.WEST); subjectPanel.add(subjectField, BorderLayout.CENTER); add(subjectPanel, BorderLayout.NORTH); add(mainPanel); nickList = new GroupChatNickList(this); String divLocString = Settings.getInstance().getProperty( "chatWindowDividerLocation"); int divLoc = 0; Dimension dimension = BuddyList.getInstance().getTabFrame().getSize(); if (divLocString != null) { divLoc = Integer.parseInt(divLocString); } else { divLoc = (int) dimension.getWidth() - 127; } if (divLoc == 0) divLoc = (int) dimension.getWidth() - 127; mainPanel.setDividerLocation(divLoc); mainPanel.setOneTouchExpandable(true); mainPanel.setResizeWeight(1); mainPanel.addPropertyChangeListener("lastDividerLocation", new DividerListener("chatWindowDividerLocation")); conversationScroll = new JScrollPane(conversationArea); conversationScroll .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); conversationArea.setScroll(conversationScroll); setUpPopMenu(); scrollPanel.add(conversationScroll); JSplitPane containerPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPanel, new JScrollPane(textEntryArea)); containerPanel.setResizeWeight(1); textEntryArea.setLineWrap(true); textEntryArea.setWrapStyleWord(true); divLocString = Settings.getInstance().getProperty( "chatRoomPanelDividerLocation"); divLoc = 0; try { if (divLocString != null) { divLoc = Integer.parseInt(divLocString); } else { divLoc = (int) dimension.getWidth() - 127; Settings.getInstance().setProperty( "chatRoomPanelDividerLocation", divLoc + "" ); } } catch (NumberFormatException ex) { } if (divLoc == 0) divLoc = 317; containerPanel.setDividerLocation(divLoc); containerPanel.addPropertyChangeListener("lastDividerLocation", new DividerListener("chatRoomPanelDividerLocation")); containerPanel.repaint(); mainPanel.add(containerPanel); mainPanel.add(nickList); textEntryArea.grabFocus(); textEntryArea.setFocusTraversalKeysEnabled(false); //for disable focus // traversal with TAB setSubject(subject); addListeners(); } public void setTab( TitledTab tab ) { this.tab = tab; } public TitledTab getTab() { return tab; } public ConversationArea getConversationArea() { return conversationArea; } public GroupChatNickList getGroupChatNickList() { return nickList; } public void disconnect() { nickList.clear(); } public void doAction(String command, MUCBuddyStatus buddy) { MUCUser user = buddy.getMUCUser(); if (user == null) { serverErrorMessage(resources.getString("jidNotFound")); return; } MUCUser.Item item = user.getItem(); if (item == null) { serverErrorMessage(resources.getString("jidNotFound")); return; } if (item.getJid() == null) { serverErrorMessage(resources.getString("jidNotFound")); return; } com.valhalla.Logger.debug("Running " + command + " on " + buddy.getUser()); Thread thread = new Thread(new RunTaskThread(resources .getString("error") + ": ", command, item.getJid())); thread.start(); } public String getUser() { return chat.getRoom() + "/" + chat.getNickname(); } /** * @return true if the TabFrame panel listeners have already been added to * this panel */ public boolean listenersAdded() { return listenersAdded; } /** * Sets whether or not the TabFrame panel listeners have been added * * @param added * true if they have been added */ public void setListenersAdded(boolean added) { this.listenersAdded = added; } /** * @return the input area of this panel */ public JComponent getInputComponent() { return textEntryArea; } /** * @return the JList representing the nicklist */ public JList getNickList() { return nickList.getList(); } /** * @return the text entry area */ public JTextComponent getTextEntryArea() { return textEntryArea; } /** * Listens for a change in the divider location, and saves it for later * retreival * * @author Adam Olsen * @version 1.0 */ private class DividerListener implements PropertyChangeListener { String prop; public DividerListener(String prop) { this.prop = prop; } public void propertyChange(PropertyChangeEvent e) { if (e.getOldValue().toString().equals("-1")) return; Settings.getInstance() .setProperty(prop, e.getOldValue().toString()); BuddyList.getInstance().getTabFrame().saveStates(); } } /** * Updates the font in the ConversationArea * * @param font * the font to update to */ public void updateStyle(Font font) { conversationArea.loadStyleSheet(font); } /** * Look for a right click, and show a pop up menu * * @author Adam Olsen * @version 1.0 */ class RightClickListener extends MouseAdapter { public void mousePressed(MouseEvent e) { checkPop(e); } public void mouseReleased(MouseEvent e) { checkPop(e); } public void mouseClicked(MouseEvent e) { checkPop(e); } public void checkPop(MouseEvent e) { // look for the popup trigger.. usually a right click if (e.isPopupTrigger()) { if (conversationArea.getSelectedText() == null) { popMenu.show(e.getComponent(), e.getX(), e.getY()); } } } } /** * Add the various menu items to the popup menu */ private void setUpPopMenu() { MenuItemListener listener = new MenuItemListener(); conversationArea.addMouseListener(new RightClickListener()); CopyPasteContextMenu.registerComponent(conversationArea); popMenu.add(nickItem); popMenu.add(newItem); popMenu.add(logItem); popMenu.addSeparator(); popMenu.add(viewAdmins); popMenu.add(viewModerators); popMenu.add(viewMembers); popMenu.add(viewParticipants); popMenu.add(viewOwners); popMenu.add(viewOutcasts); popMenu.add(registerItem); popMenu.add(destroyRoom); popMenu.addSeparator(); popMenu.add(leaveItem); logItem.addActionListener(listener); newItem.addActionListener(listener); leaveItem.addActionListener(listener); nickItem.addActionListener(listener); registerItem.addActionListener(listener); viewAdmins.addActionListener(listener); viewOutcasts.addActionListener(listener); viewMembers.addActionListener(listener); viewParticipants.addActionListener(listener); viewOwners.addActionListener(listener); viewModerators.addActionListener(listener); destroyRoom.addActionListener(listener); } /** * Listens for items to be selected in the menu * * @author Adam Olsen * @version 1.0 */ private class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == nickItem) changeNickHandler(); else if (e.getSource() == leaveItem) { BuddyList.getInstance().getTabFrame().removePanel(thisPointer); BuddyList.getInstance().stopTabFrame(); } else if (e.getSource() == newItem) { GroupChatBookmarks gc = new GroupChatBookmarks(BuddyList .getInstance().getTabFrame()); gc.load(); gc.setVisible(true); gc.toFront(); } else if (e.getSource() == logItem) new LogViewerDialog(thisPointer, getRoomName()); else if (e.getSource() == registerItem) configurationHandler("registerFor"); else if (e.getSource() == viewAdmins) new ListViewDialog(thisPointer, ListViewDialog.TYPE_ADMIN); else if (e.getSource() == viewOutcasts) new ListViewDialog(thisPointer, ListViewDialog.TYPE_OUTCASTS); else if (e.getSource() == viewMembers) new ListViewDialog(thisPointer, ListViewDialog.TYPE_MEMBERS); else if (e.getSource() == viewParticipants) new ListViewDialog(thisPointer, ListViewDialog.TYPE_PARTICIPANTS); else if (e.getSource() == viewOwners) new ListViewDialog(thisPointer, ListViewDialog.TYPE_OWNERS); else if (e.getSource() == viewModerators) new ListViewDialog(thisPointer, ListViewDialog.TYPE_MODERATORS); else if (e.getSource() == destroyRoom) destroyHandler();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -