/* * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer * * 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 2 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or mail/IM me at jeti@jabber.org */ package nu.fw.jeti.ui; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.*; import java.text.MessageFormat; import java.util.Iterator; import java.util.Map; import javax.swing.*; import javax.swing.plaf.basic.BasicSplitPaneDivider; import javax.swing.plaf.basic.BasicSplitPaneUI; import nu.fw.jeti.images.StatusIcons; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.JIDStatus; import nu.fw.jeti.jabber.UnknownJIDStatus; import nu.fw.jeti.jabber.elements.*; import nu.fw.jeti.plugins.RosterMenuListener; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Preferences; import javax.swing.JOptionPane; /** * @author E.S. de Boer */ public class ChatWindow extends JPanel { private JPanel jPanel1 = new JPanel(); private ChatSplitPane chatSplitPane; private JButton jButton1 = new JButton(); private Backend backend; private String thread; private JLabel lblComposing = new JLabel(); private JIDStatus jidStatus; private JFrame window; private JMenu menu; private ChatWindows chatWindows; public ChatWindow(Backend backend,ChatWindows chatWindows,JFrame frame, JIDStatus jidStatus, String thread) { this.backend = backend; this.chatWindows=chatWindows; //this.tabs = tabs; //toName = jidStatus.getNick(); //to = jidStatus.getCompleteJID(); //type = jidStatus.getType(); this.jidStatus = jidStatus; //int status = jidStatus.getShow(); this.thread = thread; makeMenu(); if( frame==null ) { makeWindow(); frame = window; } String me = backend.getMyJID().getUser(); chatSplitPane = new ChatSplitPane(backend,jidStatus,me,thread,menu,frame); init(); } private void init() { lblComposing.setIcon(StatusIcons.getImageIcon("keys")); lblComposing.setVisible(false); I18N.setTextAndMnemonic("Send",jButton1); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); setLayout(new BorderLayout()); add(jPanel1, BorderLayout.SOUTH); jPanel1.setLayout(new GridLayout(1,3)); jPanel1.add(lblComposing); jPanel1.add(jButton1); jPanel1.add(Box.createHorizontalStrut(0)); add(chatSplitPane , BorderLayout.CENTER); addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { chatSplitPane.requestFocusInWindow(); } }); /** * @Author : Rodrigo Souza - rodsouza@celepar.pr.gov.br * @Date : 31/10/2008 * @Brief : Comentado trecho abaixo, pois o mesmo dimensionava o componente splitPane * deixando a foto (avatar) pequeno. */ //chatSplitPane.setDividerLocation(Preferences.getInteger("jeti","chatDivider",170)); if( window!=null ) window.setVisible(true); } private void makeMenu() { /** * * @Author : Alexandre Correia -alexandrecorreia@celepar.pr.gov.br * @Description : Desabilitado no menu o item Local Data e Local Versao * */ menu = new JMenu(); I18N.setTextAndMnemonic("main.chat.menu", menu); /*JMenuItem item = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Local_Time", item, true); item.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { //backend.send(new InfoQuery(jidStatus.getCompleteJID(), "get", new IQTime())); } }); menu.add(item); item = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Local_Version", item,true); item.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { //backend.send(new InfoQuery(jidStatus.getCompleteJID(), "get", new IQVersion())); } }); menu.add(item); Map menuItems = backend.getMain().getRosterMenuItems(); if (menuItems != null) { for (Iterator i = menuItems.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); item = new JMenuItem((String) entry.getKey()); final RosterMenuListener listener = (RosterMenuListener) entry.getValue(); item.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { listener.actionPerformed(jidStatus, null); } }); menu.add(item); } }*/ } public void addSplitBarListener(ComponentListener listener) { BasicSplitPaneDivider divider = ((BasicSplitPaneUI)chatSplitPane.getUI()).getDivider(); divider.addComponentListener(listener); } public void removeSplitBarListener(ComponentListener listener) { BasicSplitPaneDivider divider = ((BasicSplitPaneUI)chatSplitPane.getUI()).getDivider(); divider.removeComponentListener(listener); } public void setDividerLocation(int location) { chatSplitPane.setDividerLocation(location); } public void setParentFrame(JFrame frame) { chatSplitPane.setParentFrame(frame); } private void makeWindow() { window = new JFrame(); setIcon(StatusIcons.getStatusIcon(jidStatus)); if(jidStatus instanceof UnknownJIDStatus)setTitle(jidStatus.getCompleteJID().toString()); else setTitle(jidStatus.getNick()); window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { Preferences.putInteger("jeti","chatPosX",window.getX()); Preferences.putInteger("jeti","chatPosY",window.getY()); Preferences.putInteger("jeti","chatHeight",window.getHeight()); Preferences.putInteger("jeti","chatWidth",window.getWidth()); Preferences.putInteger("jeti","chatDivider",chatSplitPane.getDividerLocation()); chatWindows.removeChatWindow(ChatWindow.this); exit(); } }); /** * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br * @Date : 03/11/2008 * @Description : Funcao abaixo comentada, tira o foco das outras janelas. */ /*window.addWindowFocusListener(new WindowAdapter() { public void windowGainedFocus(WindowEvent e) { ChatWindow.this.requestFocusInWindow(); } });*/ JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); final JMenuItem item = new JMenuItem(); I18N.setTextAndMnemonic("main.chat.menu.Convert_to_tab", item,true); item.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menu.remove(item); chatWindows.convertToTab(ChatWindow.this,jidStatus); window.dispose(); } }); menu.add(item); window.setJMenuBar(menuBar); window.setContentPane(this); window.setSize(Preferences.getInteger("jeti","chatWidth",300),Preferences.getInteger("jeti","chatHeight",350)); } public void changeToWindow() { makeWindow(); chatSplitPane.setDividerLocation(Preferences.getInteger("jeti","chatDivider",200)); chatSplitPane.setParentFrame(window); window.setVisible(true); } public void setLocationOnScreen(int posX,int posY) { window.setLocation(posX,posY); } public void setLocationOnScreen(ChatWindow oldWindow) { if(oldWindow.window!=null) { window.setLocation(oldWindow.window.getX()+50 ,oldWindow.window.getY()+50); } } public void exit() { chatSplitPane.close(); if(window!=null)window.dispose(); } // public boolean compareJID(JID jid) // { // return jidStatus.getJID().equals(jid); // } public String getThread() { return thread; } public void appendMessage(Message message) { String name; if (jidStatus.getJID().equals(message.getFrom())) name = jidStatus.getNick(); else name = message.getFrom().getUser(); Runnable updateAComponent = new Runnable() { public void run() { //lblComposing.setText(""); lblComposing.setVisible(false); } }; SwingUtilities.invokeLater(updateAComponent); chatSplitPane.appendMessage(message,name); } public void appendPresenceChange(final Presence presence) { Runnable updateAComponent = new Runnable() { public void run() { lblComposing.setVisible(false); int show = presence.getShow(); String status = presence.getStatus(); setIcon(StatusIcons.getStatusIcon(jidStatus)); String text; if (show == Presence.UNAVAILABLE) { text = " " + I18N.gettext("main.chat.is_currently_offline"); setTitle(MessageFormat.format(I18N.gettext("main.chat.{0}_(offline)"),new Object[]{jidStatus.getNick()})); } else { text = " " + I18N.gettext("main.chat.changed_status_to") + " " + Presence.toLongShow(show); if (status != null) { text += '\n' + I18N.gettext("main.chat.with_status_message") + " " + status; } if(jidStatus instanceof UnknownJIDStatus)setTitle(jidStatus.getCompleteJID().toString()); else setTitle(jidStatus.getNick()); } chatSplitPane.appendSystemMessage(jidStatus.getNick() + text); } }; SwingUtilities.invokeLater(updateAComponent); } public JIDStatus getJIDStatus() { return jidStatus; } public void composingID(String id) { if (id == null)id = ""; chatSplitPane.composingID(id); } public void composing(String type) { if (type != null) { Runnable updateAComponent = new Runnable() { public void run() { lblComposing.setVisible(true); } }; SwingUtilities.invokeLater(updateAComponent); } else { Runnable updateAComponent = new Runnable() { public void run() { lblComposing.setVisible(false); } }; SwingUtilities.invokeLater(updateAComponent); } } void jButton1_actionPerformed(ActionEvent e) { chatSplitPane.send(); } private void setTitle(String title) { if(window!=null)window.setTitle(title); else chatWindows.setTitleAt(this,title); } private void setIcon(ImageIcon icon) { if(window!=null) window.setIconImage(icon.getImage()); else chatWindows.setIconAt(this,icon); } public JMenu getMenu() { return menu; } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */