[3102] | 1 | package nu.fw.jeti.ui; |
---|
| 2 | |
---|
| 3 | import java.awt.BorderLayout; |
---|
| 4 | import java.awt.Dimension; |
---|
| 5 | import java.awt.Font; |
---|
| 6 | import java.awt.Toolkit; |
---|
| 7 | import java.awt.event.ActionEvent; |
---|
| 8 | import java.awt.event.InputEvent; |
---|
| 9 | import java.awt.event.KeyEvent; |
---|
| 10 | import java.text.MessageFormat; |
---|
| 11 | import java.util.Iterator; |
---|
| 12 | import java.util.List; |
---|
| 13 | |
---|
| 14 | import javax.swing.*; |
---|
| 15 | import javax.swing.text.Document; |
---|
| 16 | |
---|
| 17 | import nu.fw.jeti.jabber.Backend; |
---|
| 18 | import nu.fw.jeti.jabber.JID; |
---|
| 19 | import nu.fw.jeti.jabber.JIDStatus; |
---|
| 20 | import nu.fw.jeti.jabber.elements.Message; |
---|
| 21 | import nu.fw.jeti.plugins.PluginsInfo; |
---|
| 22 | import nu.fw.jeti.plugins.Translator; |
---|
| 23 | import nu.fw.jeti.plugins.Word; |
---|
| 24 | import nu.fw.jeti.util.I18N; |
---|
| 25 | import nu.fw.jeti.util.Popups; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Title: im |
---|
| 29 | * Description: |
---|
| 30 | * Copyright: Copyright (c) 2001 |
---|
| 31 | * Company: |
---|
| 32 | * @author E.S. de Boer |
---|
| 33 | * @version 1.0 |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | public class SendMessage extends JFrame |
---|
| 37 | { |
---|
| 38 | private static final int MIN_WIDTH = 300; |
---|
| 39 | private static final int MIN_HEIGHT = 200; |
---|
| 40 | private JButton jButton1 = new JButton(); |
---|
| 41 | private Backend backend; |
---|
| 42 | private JID jid; |
---|
| 43 | private String user; |
---|
| 44 | private JScrollPane jScrollPane1 = new JScrollPane(); |
---|
| 45 | private JTextPane jTextArea1 = new JTextPane(); |
---|
| 46 | private JTextField txtSubject = new JTextField(); |
---|
| 47 | //private boolean enterSends; |
---|
| 48 | |
---|
| 49 | public SendMessage(Backend backend,JID jid, String user) |
---|
| 50 | {//send |
---|
| 51 | this.backend = backend; |
---|
| 52 | this.jid = jid; |
---|
| 53 | // jTextArea1.setLineWrap(true); |
---|
| 54 | //enterSends = Preferences.getBoolean("jeti","enterSends",true); |
---|
| 55 | setTitle(user); |
---|
| 56 | try |
---|
| 57 | { |
---|
| 58 | jbInit(); |
---|
| 59 | jButton1.setText(I18N.gettext("Send")); |
---|
| 60 | } |
---|
| 61 | catch(Exception e) |
---|
| 62 | { |
---|
| 63 | e.printStackTrace(); |
---|
| 64 | } |
---|
| 65 | jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() |
---|
| 66 | { |
---|
| 67 | public void keyPressed(KeyEvent e) |
---|
| 68 | { |
---|
| 69 | txtInvoer_keyPressed(e); |
---|
| 70 | } |
---|
| 71 | }); |
---|
| 72 | fixSize(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | public SendMessage(Backend backend,JIDStatus jidstatus,String subject,String text) |
---|
| 76 | {//xmppuri |
---|
| 77 | this(backend,jidstatus.getJID(),jidstatus.getNick()); |
---|
| 78 | txtSubject.setText(subject); |
---|
| 79 | jTextArea1.setText(text); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public SendMessage(Backend backend,JID jid, String user,String title) |
---|
| 83 | {//reply |
---|
| 84 | this(backend,jid,user); |
---|
| 85 | txtSubject.setText("Re: " + title); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | public SendMessage(Backend backend,Message messageElement) |
---|
| 89 | {//receive |
---|
| 90 | this.backend = backend; |
---|
| 91 | |
---|
| 92 | if (messageElement.getType().equals("error")) { |
---|
| 93 | handleError(messageElement); |
---|
| 94 | return; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | getRootPane().setDefaultButton(jButton1); |
---|
| 98 | JIDStatus jidStatus = backend.getJIDStatus(messageElement.getFrom()); |
---|
| 99 | jid = messageElement.getFrom(); |
---|
| 100 | if (jidStatus == null || jidStatus.getNick() == null ) { |
---|
| 101 | if (jid.getResource() != null) { |
---|
| 102 | user = jid.getUser() + "/" + jid.getResource(); |
---|
| 103 | } else { |
---|
| 104 | user = jid.getUser(); |
---|
| 105 | } |
---|
| 106 | } else { |
---|
| 107 | user = jidStatus.getNick(); |
---|
| 108 | } |
---|
| 109 | String subject = messageElement.getSubject(); |
---|
| 110 | if (subject == null) { |
---|
| 111 | setTitle(user); |
---|
| 112 | } else { |
---|
| 113 | setTitle(user +" | " + subject); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | try |
---|
| 117 | { |
---|
| 118 | jbInit(); |
---|
| 119 | //jButton1.setMnemonic('R'); |
---|
| 120 | I18N.setTextAndMnemonic("main.popup.Reply",jButton1); |
---|
| 121 | String message = messageElement.getBody(); |
---|
| 122 | if (PluginsInfo.isPluginLoaded("links")) |
---|
| 123 | { |
---|
| 124 | //jTextArea1.setDocument(new DefaultStyledDocument()); |
---|
| 125 | Translator links = (Translator) PluginsInfo.newPluginInstance("links"); |
---|
| 126 | links.init(jTextArea1); |
---|
| 127 | List wordList = ChatSplitPane.createWordList(message); |
---|
| 128 | links.translate(wordList); |
---|
| 129 | // Insert words from wordlist |
---|
| 130 | Document doc = jTextArea1.getDocument(); |
---|
| 131 | for(Iterator i = wordList.iterator();i.hasNext();) { |
---|
| 132 | Word w = (Word) i.next(); |
---|
| 133 | doc.insertString(doc.getLength(),w.toString(), |
---|
| 134 | w.getAttributes()); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | else jTextArea1.setText(message); |
---|
| 138 | jTextArea1.setEditable(false); |
---|
| 139 | txtSubject.setText(subject); |
---|
| 140 | txtSubject.setEditable(false); |
---|
| 141 | if(message.indexOf(System.getProperty("line.separator")) == -1) |
---|
| 142 | {// if enters message is expected to be well formated, otherwise format |
---|
| 143 | int naamLengte = (getFontMetrics((Font)UIManager.get("TextArea.font"))).stringWidth(message); |
---|
| 144 | //System.out.println(naamLengte); |
---|
| 145 | if(naamLengte > 400) |
---|
| 146 | {//message is to long enable autowrap |
---|
| 147 | setSize(400,300); |
---|
| 148 | // jTextArea1.setLineWrap(true); |
---|
| 149 | setVisible(true); |
---|
| 150 | return; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | catch(Exception e) |
---|
| 155 | { |
---|
| 156 | e.printStackTrace(); |
---|
| 157 | } |
---|
| 158 | pack(); |
---|
| 159 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
| 160 | if(getHeight() > screenSize.getHeight() || getWidth() > screenSize.getWidth()) |
---|
| 161 | { |
---|
| 162 | setSize((int)screenSize.getWidth()-50,(int)screenSize.getHeight()-50); |
---|
| 163 | } |
---|
| 164 | fixSize(); |
---|
| 165 | setVisible(true); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | private void jbInit() throws Exception |
---|
| 169 | { |
---|
| 170 | //setIconImage(nu.fw.jeti.images.StatusIcons.getImageIcon("jeti").getImage()); |
---|
| 171 | //getRootPane().setDefaultButton(jButton1); |
---|
| 172 | jButton1.addActionListener(new java.awt.event.ActionListener() |
---|
| 173 | { |
---|
| 174 | public void actionPerformed(ActionEvent e) |
---|
| 175 | { |
---|
| 176 | jButton1_actionPerformed(e); |
---|
| 177 | } |
---|
| 178 | }); |
---|
| 179 | this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
---|
| 180 | //jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
---|
| 181 | // jTextArea1.setWrapStyleWord(true); |
---|
| 182 | this.getContentPane().add(jScrollPane1, BorderLayout.CENTER); |
---|
| 183 | jScrollPane1.getViewport().add(jTextArea1, null); |
---|
| 184 | // this.getContentPane().add(jTextArea1, BorderLayout.CENTER); |
---|
| 185 | |
---|
| 186 | this.getContentPane().add(jButton1, BorderLayout.SOUTH); |
---|
| 187 | JPanel panel = new JPanel(new BorderLayout()); |
---|
| 188 | panel.add(new JLabel(I18N.gettext("main.popup.Subject")), BorderLayout.NORTH); |
---|
| 189 | panel.add(txtSubject, BorderLayout.CENTER); |
---|
| 190 | panel.add(new JLabel(I18N.gettext("main.popup.Message")), BorderLayout.SOUTH); |
---|
| 191 | this.getContentPane().add(panel,BorderLayout.NORTH); |
---|
| 192 | setLocationRelativeTo(null); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | private void fixSize() |
---|
| 196 | { |
---|
| 197 | Dimension d = getSize(); |
---|
| 198 | |
---|
| 199 | if (d.height < MIN_HEIGHT) { |
---|
| 200 | d.height = MIN_HEIGHT; |
---|
| 201 | } |
---|
| 202 | if (d.width < MIN_WIDTH) { |
---|
| 203 | d.width = MIN_WIDTH; |
---|
| 204 | } |
---|
| 205 | if (d != getSize()) { |
---|
| 206 | setSize(d); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | void jButton1_actionPerformed(ActionEvent e) |
---|
| 211 | { |
---|
| 212 | if(jButton1 .getText().equals(I18N.gettext("Send"))) |
---|
| 213 | { |
---|
| 214 | String subject =txtSubject.getText(); |
---|
| 215 | if(subject.equals("")) subject = null; |
---|
| 216 | backend.sendMessage(new Message(jTextArea1.getText(),subject,jid)); |
---|
| 217 | this.dispose(); |
---|
| 218 | } |
---|
| 219 | else new SendMessage(backend ,jid,user,txtSubject.getText()).show(); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void txtInvoer_keyPressed(KeyEvent e) |
---|
| 223 | { |
---|
| 224 | if (e.getKeyCode() == KeyEvent.VK_ENTER) |
---|
| 225 | { |
---|
| 226 | // if (enterSends) |
---|
| 227 | // { |
---|
| 228 | // if ((e.getModifiers() == KeyEvent.SHIFT_MASK) || (e.getModifiers() == KeyEvent.CTRL_MASK)) |
---|
| 229 | // { |
---|
| 230 | // //txtInvoer.setText(txtInvoer.getText() +"\n"); |
---|
| 231 | // Document doc = jTextArea1.getDocument(); |
---|
| 232 | // try |
---|
| 233 | // { |
---|
| 234 | // doc.insertString(jTextArea1.getCaretPosition(), "\n", null); |
---|
| 235 | // } |
---|
| 236 | // catch (BadLocationException e3) |
---|
| 237 | // {} |
---|
| 238 | // } |
---|
| 239 | // else |
---|
| 240 | // { |
---|
| 241 | // //send(); |
---|
| 242 | // jButton1.doClick(); |
---|
| 243 | // e.consume(); |
---|
| 244 | // } |
---|
| 245 | // } |
---|
| 246 | // else |
---|
| 247 | { |
---|
| 248 | if ((e.getModifiers() == InputEvent.SHIFT_MASK) || (e.getModifiers() == InputEvent.CTRL_MASK)) |
---|
| 249 | { |
---|
| 250 | //send(); |
---|
| 251 | jButton1.doClick(); |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | private void handleError(Message message) { |
---|
| 258 | String error; |
---|
| 259 | |
---|
| 260 | switch (message.getErrorCode()) { |
---|
| 261 | case 404: |
---|
| 262 | error = MessageFormat.format( |
---|
| 263 | I18N.gettext("main.error.User_{0}_could_not_be_found"), |
---|
| 264 | new Object[] {message.getFrom()}); |
---|
| 265 | break; |
---|
| 266 | default: |
---|
| 267 | error = I18N.gettext("main.error.Error_in_chat") |
---|
| 268 | + " " + message.getBody(); |
---|
| 269 | break; |
---|
| 270 | } |
---|
| 271 | Popups.errorPopup(error,I18N.gettext("main.error.Error")); |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /* |
---|
| 276 | * Overrides for emacs |
---|
| 277 | * Local variables: |
---|
| 278 | * tab-width: 4 |
---|
| 279 | * End: |
---|
| 280 | */ |
---|