package nu.fw.jeti.ui; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.text.MessageFormat; import java.util.Iterator; import java.util.List; import javax.swing.*; import javax.swing.text.Document; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.JID; import nu.fw.jeti.jabber.JIDStatus; import nu.fw.jeti.jabber.elements.Message; import nu.fw.jeti.plugins.PluginsInfo; import nu.fw.jeti.plugins.Translator; import nu.fw.jeti.plugins.Word; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Popups; /** * Title: im * Description: * Copyright: Copyright (c) 2001 * Company: * @author E.S. de Boer * @version 1.0 */ public class SendMessage extends JFrame { private static final int MIN_WIDTH = 300; private static final int MIN_HEIGHT = 200; private JButton jButton1 = new JButton(); private Backend backend; private JID jid; private String user; private JScrollPane jScrollPane1 = new JScrollPane(); private JTextPane jTextArea1 = new JTextPane(); private JTextField txtSubject = new JTextField(); //private boolean enterSends; public SendMessage(Backend backend,JID jid, String user) {//send this.backend = backend; this.jid = jid; // jTextArea1.setLineWrap(true); //enterSends = Preferences.getBoolean("jeti","enterSends",true); setTitle(user); try { jbInit(); jButton1.setText(I18N.gettext("Send")); } catch(Exception e) { e.printStackTrace(); } jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { txtInvoer_keyPressed(e); } }); fixSize(); } public SendMessage(Backend backend,JIDStatus jidstatus,String subject,String text) {//xmppuri this(backend,jidstatus.getJID(),jidstatus.getNick()); txtSubject.setText(subject); jTextArea1.setText(text); } public SendMessage(Backend backend,JID jid, String user,String title) {//reply this(backend,jid,user); txtSubject.setText("Re: " + title); } public SendMessage(Backend backend,Message messageElement) {//receive this.backend = backend; if (messageElement.getType().equals("error")) { handleError(messageElement); return; } getRootPane().setDefaultButton(jButton1); JIDStatus jidStatus = backend.getJIDStatus(messageElement.getFrom()); jid = messageElement.getFrom(); if (jidStatus == null || jidStatus.getNick() == null ) { if (jid.getResource() != null) { user = jid.getUser() + "/" + jid.getResource(); } else { user = jid.getUser(); } } else { user = jidStatus.getNick(); } String subject = messageElement.getSubject(); if (subject == null) { setTitle(user); } else { setTitle(user +" | " + subject); } try { jbInit(); //jButton1.setMnemonic('R'); I18N.setTextAndMnemonic("main.popup.Reply",jButton1); String message = messageElement.getBody(); if (PluginsInfo.isPluginLoaded("links")) { //jTextArea1.setDocument(new DefaultStyledDocument()); Translator links = (Translator) PluginsInfo.newPluginInstance("links"); links.init(jTextArea1); List wordList = ChatSplitPane.createWordList(message); links.translate(wordList); // Insert words from wordlist Document doc = jTextArea1.getDocument(); for(Iterator i = wordList.iterator();i.hasNext();) { Word w = (Word) i.next(); doc.insertString(doc.getLength(),w.toString(), w.getAttributes()); } } else jTextArea1.setText(message); jTextArea1.setEditable(false); txtSubject.setText(subject); txtSubject.setEditable(false); if(message.indexOf(System.getProperty("line.separator")) == -1) {// if enters message is expected to be well formated, otherwise format int naamLengte = (getFontMetrics((Font)UIManager.get("TextArea.font"))).stringWidth(message); //System.out.println(naamLengte); if(naamLengte > 400) {//message is to long enable autowrap setSize(400,300); // jTextArea1.setLineWrap(true); setVisible(true); return; } } } catch(Exception e) { e.printStackTrace(); } pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); if(getHeight() > screenSize.getHeight() || getWidth() > screenSize.getWidth()) { setSize((int)screenSize.getWidth()-50,(int)screenSize.getHeight()-50); } fixSize(); setVisible(true); } private void jbInit() throws Exception { //setIconImage(nu.fw.jeti.images.StatusIcons.getImageIcon("jeti").getImage()); //getRootPane().setDefaultButton(jButton1); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // jTextArea1.setWrapStyleWord(true); this.getContentPane().add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.getViewport().add(jTextArea1, null); // this.getContentPane().add(jTextArea1, BorderLayout.CENTER); this.getContentPane().add(jButton1, BorderLayout.SOUTH); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JLabel(I18N.gettext("main.popup.Subject")), BorderLayout.NORTH); panel.add(txtSubject, BorderLayout.CENTER); panel.add(new JLabel(I18N.gettext("main.popup.Message")), BorderLayout.SOUTH); this.getContentPane().add(panel,BorderLayout.NORTH); setLocationRelativeTo(null); } private void fixSize() { Dimension d = getSize(); if (d.height < MIN_HEIGHT) { d.height = MIN_HEIGHT; } if (d.width < MIN_WIDTH) { d.width = MIN_WIDTH; } if (d != getSize()) { setSize(d); } } void jButton1_actionPerformed(ActionEvent e) { if(jButton1 .getText().equals(I18N.gettext("Send"))) { String subject =txtSubject.getText(); if(subject.equals("")) subject = null; backend.sendMessage(new Message(jTextArea1.getText(),subject,jid)); this.dispose(); } else new SendMessage(backend ,jid,user,txtSubject.getText()).show(); } void txtInvoer_keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { // if (enterSends) // { // if ((e.getModifiers() == KeyEvent.SHIFT_MASK) || (e.getModifiers() == KeyEvent.CTRL_MASK)) // { // //txtInvoer.setText(txtInvoer.getText() +"\n"); // Document doc = jTextArea1.getDocument(); // try // { // doc.insertString(jTextArea1.getCaretPosition(), "\n", null); // } // catch (BadLocationException e3) // {} // } // else // { // //send(); // jButton1.doClick(); // e.consume(); // } // } // else { if ((e.getModifiers() == InputEvent.SHIFT_MASK) || (e.getModifiers() == InputEvent.CTRL_MASK)) { //send(); jButton1.doClick(); } } } } private void handleError(Message message) { String error; switch (message.getErrorCode()) { case 404: error = MessageFormat.format( I18N.gettext("main.error.User_{0}_could_not_be_found"), new Object[] {message.getFrom()}); break; default: error = I18N.gettext("main.error.Error_in_chat") + " " + message.getBody(); break; } Popups.errorPopup(error,I18N.gettext("main.error.Error")); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */