📄 tabframe.java
字号:
* * @author Adam Olsen * @version 1.0 */ private class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == newItem) new GroupChatBookmarks(TabFrame.this).setVisible(true); if (e.getSource() == leaveItem) closeHandler(); } } /** * Updates the font in all the chat conversationareas * * @param font * the font to update to */ public void updateStyles(Font font) { for (int i = 0; i < tabPane.getTabCount(); i++) { TabFramePanel panel = (TabFramePanel) tabPane.getTabAt(i).getContentComponent(); panel.updateStyle(font); } } /** * Set the status in all the rooms * * @param mode * the presence mode * @param status * the status string */ public void setStatus(Presence.Mode mode, String status) { for (int i = 0; i < tabPane.getTabCount(); i++) { TabFramePanel panel = (TabFramePanel) tabPane.getTabAt(i).getContentComponent(); if (panel instanceof ChatRoomPanel) { ChatRoomPanel window = (ChatRoomPanel) panel; MultiUserChat chat = window.getChat(); if( chat == null || !chat.isJoined() ) continue; //set up a packet to be sent to my user in every groupchat Presence presence = new Presence(Presence.Type.AVAILABLE, status, 0, mode); presence.setTo(window.getRoomName() + '/' + window.getNickname()); if (!BuddyList.getInstance().checkConnection()) { BuddyList.getInstance().connectionError(); return; } BuddyList.getInstance().getConnection().sendPacket(presence); } } } /** * This not only closes the window, but it leaves all the rooms like it * should */ public void closeHandler() { removeTabListeners(); leaveAll(); } /** * Since there is no way to check to see if a message is from someone in a * chat room, we check to see if the message is coming from the same server * as a chatroom we are in. * * @param server * the server to check */ public boolean isRoomOpen(String server) { for (int i = 0; i < tabPane.getTabCount(); i++) { TabFramePanel panel = (TabFramePanel) tabPane.getTabAt(i).getContentComponent(); if (panel instanceof ChatRoomPanel) { ChatRoomPanel window = (ChatRoomPanel) panel; if (server.toLowerCase().equals( window.getRoomName().toLowerCase())) return true; } } return false; } /** * If there is a chatroom open in this frame with a server name, this * returns the ChatRoomPanel that contains it * * @param server * the name of the room to get the ChatRoomPanel for * @return the ChatRoomPanel requested, or <tt>null</tt> if it could not * be found */ public ChatRoomPanel getChatPanel(String server) { for (int i = 0; i < tabPane.getTabCount(); i++) { TabFramePanel panel = (TabFramePanel) tabPane.getTabAt(i).getContentComponent(); if (panel instanceof ChatRoomPanel) { ChatRoomPanel window = (ChatRoomPanel) panel; if (server.toLowerCase().equals( window.getRoomName().toLowerCase())) return window; } } return null; } /** * This leaves a chatroom and removes the associated ChatRoomPanel from the * TabPane * * @param window * the room to leave */ public void removePanel(TabFramePanel panel) { queueCounts.remove(panel); TitledTab tab = panel.getTab(); tab.setHighlightedStateTitleComponent(null); tab.setNormalStateTitleComponent(null); tab.setDisabledStateTitleComponent(null); tab.getProperties().setHoverListener(null); tabPane.removeTab(tab); tabPane.validate(); if(panel instanceof ConversationPanel) MessageDelegator.getInstance().removePanel((ConversationPanel)panel); if (panel instanceof ChatRoomPanel) { ((ChatRoomPanel) panel).leave(); ((ChatRoomPanel) panel).removed(); panel = null; } try { panel = (TabFramePanel) tabPane.getSelectedTab().getContentComponent(); } catch( NullPointerException npe ) { panel = null; } if (panel != null) { setTitle(panel.getWindowTitle()); TitledTab t = panel.getTab(); t.setText(panel.getPanelName()); } else { setTitle("JBother"); } BuddyList.getInstance().stopTabFrame(); } /** * Sets the subject of a ChatRoomPanel based on a message that was received * from the GroupChat server with <subject> in it * * @param window * the window to set the subject for */ public void setSubject(ChatRoomPanel window) { if( tabPane.getSelectedTab() == null) return; if (!(tabPane.getSelectedTab().getContentComponent() instanceof ChatRoomPanel)) return; if ((ChatRoomPanel) tabPane.getSelectedTab().getContentComponent() == window) { setTitle(resources.getString("groupChat") + ": " + window.getRoomName()); validate(); } } /** * @param panel * the panel to check * @return true if the tab panel is currently displayed in the tab frame */ public boolean contains(TabFramePanel panel) { for (int i = 0; i < tabPane.getTabCount(); i++) { TabFramePanel p = (TabFramePanel) tabPane.getTabAt(i).getContentComponent(); if (p == panel) return true; } return false; } /** * Returns the number of rooms currently open in the frame * * @return the number of rooms still open */ public int tabsLeft() { return tabPane.getTabCount(); } /** * Adds a chat room to the frame * * @param window * the room to add */ public void addPanel(final TabFramePanel panel) { panel.setListenersAdded(true); final TitledTab tab = new TitledTab( panel.getPanelName(), null, (JComponent) panel, null ); tab.getProperties().addSuperObject(theme.getTitledTabProperties()); //tab.getProperties().setSizePolicy(TitledTabSizePolicy.INDIVIDUAL_SIZE); //tab.getProperties().setBorderSizePolicy(TitledTabBorderSizePolicy.INDIVIDUAL_SIZE); CloseButton b = new CloseButton(tab); final CloseButton temp = b; tab.setHighlightedStateTitleComponent(b); if( !Settings.getInstance().getBoolean("closeButtonOnAll")) b = null; tab.setNormalStateTitleComponent(b); tab.setDisabledStateTitleComponent(b); tab.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if(e.isPopupTrigger()) { close.setTab(tab); close.show(tab, e.getX(), e.getY()); } } } ); tab.getProperties().setHoverListener( new HoverListener() { public void mouseEntered(HoverEvent e) { if(Settings.getInstance().getBoolean("closeButtonOnAll")) return; tab.setNormalStateTitleComponent(temp); tab.setDisabledStateTitleComponent(temp); tab.validate(); } public void mouseExited(HoverEvent e) { if(Settings.getInstance().getBoolean("closeButtonOnAll")) return; tab.setNormalStateTitleComponent(null); tab.setDisabledStateTitleComponent(null); tab.validate(); } } ); tabPane.addTab( tab ); panel.setTab( tab ); if (panel instanceof ChatRoomPanel) tabPane.setSelectedTab(tab); } public void resetCloseButtons(boolean selected) { for( int i = 0; i < tabPane.getTabCount(); i++ ) { TitledTab tab = (TitledTab)tabPane.getTabAt(i); CloseButton button = new CloseButton(tab); tab.setHighlightedStateTitleComponent(button); if( !selected) button = null; tab.setNormalStateTitleComponent(button); tab.setDisabledStateTitleComponent(button); tab.validate(); } } /** * Switches the tab based on CTRL+n keys */ class GCTabHandler implements KeyEventPostProcessor { boolean first = false; public boolean postProcessKeyEvent(KeyEvent e) { Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager() .getFocusedWindow(); if (!(w instanceof TabFrame)) return false; TabbedPanel tabPane = ((TabFrame) w).getTabPane(); // get the ASCII character code for the character typed int numPressed = (int) e.getKeyChar(); int mask = KeyEvent.CTRL_MASK; if (System.getProperty("mrj.version") != null) { mask = KeyEvent.META_DOWN_MASK; } // the integer characters start at ASCII table number 49, so we // subtract 49 numPressed -= 49; // if the new ASCII value is between 0 and 8, then the // key pressed was 1 through 9 - which is what we want // also check that the CTRL key was being held down if ((numPressed >= 0 && numPressed <= 8) && (e.getModifiers() & mask) == Toolkit.getDefaultToolkit() .getMenuShortcutKeyMask()) { e.consume(); if (tabPane.getTabCount() >= numPressed) tabPane.setSelectedTab(tabPane.getTabAt(numPressed)); } else if (e.getKeyCode() == KeyEvent.VK_TAB && (e.getModifiers() & mask) == Toolkit.getDefaultToolkit() .getMenuShortcutKeyMask()) { if (first == false) { first = true; } else { switchTab(tabPane); first = false; final TabFramePanel panel = (TabFramePanel) tabPane.getSelectedTab().getContentComponent(); focusComponent( panel.getInputComponent() ); } } return true; } } /** * Leaves all chatrooms (for if they close the window) */ public void leaveAll() { com.valhalla.Logger.debug("There are " + tabPane.getTabCount() + " rooms"); int tabCount = tabPane.getTabCount(); for (int i = 0; i < tabCount; i++) { TabFramePanel panel = (TabFramePanel) tabPane.getTabAt(0).getContentComponent(); if (panel instanceof ChatRoomPanel) { ChatRoomPanel window = (ChatRoomPanel) panel; //if this frame is closed as a result of connection loss and we // try to leave //the channel, it will not work, so we need to catch it. window.removed(); try { window.leave(); } catch (IllegalStateException e) { com.valhalla.Logger .debug("Caught Illegal State Exception when leaving window: " + window.toString()); } BuddyList.getInstance().removeTabPanel(panel); } else { ((ConversationPanel) panel).checkCloseHandler(); } } BuddyList.getInstance().stopTabFrame(); } /** * @return Returns the tabPane. */ public TabbedPanel getTabPane() { return tabPane; } class CloseMenu extends JPopupMenu { JMenuItem closeItem = new JMenuItem(resources.getString("closeButton")); JMenuItem closeAll = new JMenuItem(resources.getString("closeAllButton")); private TitledTab tab; public CloseMenu() { add(closeItem); add(closeAll); closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if( tab == null ) return; TabFramePanel panel = (TabFramePanel)tab.getContentComponent(); if (panel instanceof ConversationPanel) { ((ConversationPanel) panel).checkCloseHandler(); } else { removePanel(panel); } } } ); closeAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int count = tabPane.getTabCount(); for( int i = 0; i < count; i++ ) { TitledTab tab = (TitledTab)tabPane.getTabAt(0); TabFramePanel panel = (TabFramePanel)tab.getContentComponent(); if (panel instanceof ConversationPanel) { ((ConversationPanel) panel).checkCloseHandler(); } else { removePanel(panel); } } } } ); } public void setTab(TitledTab tab) { this.tab = tab; } } class CloseButton extends JLabel { TitledTab tab; public CloseButton( final TitledTab tab ) { super(Standard.getIcon("images/buttons/close.png")); setPreferredSize( new Dimension( 15, 8 ) ); this.tab = tab; setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); setToolTipText( resources.getString( "closeButton" ) ); addMouseListener( new MouseAdapter() { public void mouseEntered( MouseEvent e ) { setBorder(BorderFactory.createEtchedBorder()); validate(); } public void mouseExited( MouseEvent e ) { setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); validate(); } public void mouseClicked( MouseEvent e ) { TabFramePanel panel = (TabFramePanel)tab.getContentComponent(); if (panel instanceof ConversationPanel) { ((ConversationPanel) panel).checkCloseHandler(); } else { removePanel(panel); } } } ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -