1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail/IM me at jeti@jabber.org |
---|
21 | */ |
---|
22 | |
---|
23 | package nu.fw.jeti.ui; |
---|
24 | |
---|
25 | import java.awt.BorderLayout; |
---|
26 | import java.awt.GridLayout; |
---|
27 | import java.awt.event.*; |
---|
28 | import java.text.MessageFormat; |
---|
29 | import java.util.Iterator; |
---|
30 | import java.util.Map; |
---|
31 | |
---|
32 | import javax.swing.*; |
---|
33 | import javax.swing.plaf.basic.BasicSplitPaneDivider; |
---|
34 | import javax.swing.plaf.basic.BasicSplitPaneUI; |
---|
35 | |
---|
36 | import nu.fw.jeti.images.StatusIcons; |
---|
37 | import nu.fw.jeti.jabber.Backend; |
---|
38 | import nu.fw.jeti.jabber.JIDStatus; |
---|
39 | import nu.fw.jeti.jabber.UnknownJIDStatus; |
---|
40 | import nu.fw.jeti.jabber.elements.*; |
---|
41 | import nu.fw.jeti.plugins.RosterMenuListener; |
---|
42 | import nu.fw.jeti.util.I18N; |
---|
43 | import nu.fw.jeti.util.Preferences; |
---|
44 | |
---|
45 | |
---|
46 | import javax.swing.JOptionPane; |
---|
47 | |
---|
48 | /** |
---|
49 | * @author E.S. de Boer |
---|
50 | */ |
---|
51 | |
---|
52 | public class ChatWindow extends JPanel |
---|
53 | { |
---|
54 | private JPanel jPanel1 = new JPanel(); |
---|
55 | private ChatSplitPane chatSplitPane; |
---|
56 | private JButton jButton1 = new JButton(); |
---|
57 | private Backend backend; |
---|
58 | private String thread; |
---|
59 | private JLabel lblComposing = new JLabel(); |
---|
60 | private JIDStatus jidStatus; |
---|
61 | private JFrame window; |
---|
62 | private JMenu menu; |
---|
63 | private ChatWindows chatWindows; |
---|
64 | |
---|
65 | |
---|
66 | public ChatWindow(Backend backend,ChatWindows chatWindows,JFrame frame, JIDStatus jidStatus, String thread) |
---|
67 | { |
---|
68 | this.backend = backend; |
---|
69 | this.chatWindows=chatWindows; |
---|
70 | //this.tabs = tabs; |
---|
71 | //toName = jidStatus.getNick(); |
---|
72 | //to = jidStatus.getCompleteJID(); |
---|
73 | //type = jidStatus.getType(); |
---|
74 | this.jidStatus = jidStatus; |
---|
75 | //int status = jidStatus.getShow(); |
---|
76 | this.thread = thread; |
---|
77 | |
---|
78 | makeMenu(); |
---|
79 | |
---|
80 | if( frame==null ) |
---|
81 | { |
---|
82 | makeWindow(); |
---|
83 | frame = window; |
---|
84 | } |
---|
85 | |
---|
86 | String me = backend.getMyJID().getUser(); |
---|
87 | |
---|
88 | chatSplitPane = new ChatSplitPane(backend,jidStatus,me,thread,menu,frame); |
---|
89 | |
---|
90 | init(); |
---|
91 | } |
---|
92 | |
---|
93 | private void init() |
---|
94 | { |
---|
95 | lblComposing.setIcon(StatusIcons.getImageIcon("keys")); |
---|
96 | lblComposing.setVisible(false); |
---|
97 | |
---|
98 | I18N.setTextAndMnemonic("Send",jButton1); |
---|
99 | jButton1.addActionListener(new java.awt.event.ActionListener() |
---|
100 | { |
---|
101 | public void actionPerformed(ActionEvent e) |
---|
102 | { |
---|
103 | jButton1_actionPerformed(e); |
---|
104 | } |
---|
105 | }); |
---|
106 | setLayout(new BorderLayout()); |
---|
107 | add(jPanel1, BorderLayout.SOUTH); |
---|
108 | |
---|
109 | jPanel1.setLayout(new GridLayout(1,3)); |
---|
110 | jPanel1.add(lblComposing); |
---|
111 | jPanel1.add(jButton1); |
---|
112 | jPanel1.add(Box.createHorizontalStrut(0)); |
---|
113 | add(chatSplitPane , BorderLayout.CENTER); |
---|
114 | addFocusListener(new FocusAdapter() |
---|
115 | { |
---|
116 | public void focusGained(FocusEvent e) |
---|
117 | { |
---|
118 | chatSplitPane.requestFocusInWindow(); |
---|
119 | } |
---|
120 | }); |
---|
121 | |
---|
122 | /** |
---|
123 | * @Author : Rodrigo Souza - rodsouza@celepar.pr.gov.br |
---|
124 | * @Date : 31/10/2008 |
---|
125 | * @Brief : Comentado trecho abaixo, pois o mesmo dimensionava o componente splitPane |
---|
126 | * deixando a foto (avatar) pequeno. |
---|
127 | */ |
---|
128 | |
---|
129 | //chatSplitPane.setDividerLocation(Preferences.getInteger("jeti","chatDivider",170)); |
---|
130 | |
---|
131 | if( window!=null ) |
---|
132 | window.setVisible(true); |
---|
133 | } |
---|
134 | |
---|
135 | private void makeMenu() |
---|
136 | { |
---|
137 | /** |
---|
138 | * |
---|
139 | * @Author : Alexandre Correia -alexandrecorreia@celepar.pr.gov.br |
---|
140 | * @Description : Desabilitado no menu o item Local Data e Local Versao |
---|
141 | * |
---|
142 | */ |
---|
143 | |
---|
144 | menu = new JMenu(); |
---|
145 | I18N.setTextAndMnemonic("main.chat.menu", menu); |
---|
146 | |
---|
147 | /*JMenuItem item = new JMenuItem(); |
---|
148 | I18N.setTextAndMnemonic("main.main.rostermenu.Local_Time", item, true); |
---|
149 | item.addActionListener(new java.awt.event.ActionListener() |
---|
150 | { |
---|
151 | public void actionPerformed(ActionEvent e) |
---|
152 | { |
---|
153 | //backend.send(new InfoQuery(jidStatus.getCompleteJID(), "get", new IQTime())); |
---|
154 | |
---|
155 | } |
---|
156 | }); |
---|
157 | menu.add(item); |
---|
158 | |
---|
159 | item = new JMenuItem(); |
---|
160 | I18N.setTextAndMnemonic("main.main.rostermenu.Local_Version", item,true); |
---|
161 | item.addActionListener(new java.awt.event.ActionListener() |
---|
162 | { |
---|
163 | public void actionPerformed(ActionEvent e) |
---|
164 | { |
---|
165 | //backend.send(new InfoQuery(jidStatus.getCompleteJID(), "get", new IQVersion())); |
---|
166 | } |
---|
167 | }); |
---|
168 | |
---|
169 | menu.add(item); |
---|
170 | |
---|
171 | Map menuItems = backend.getMain().getRosterMenuItems(); |
---|
172 | if (menuItems != null) |
---|
173 | { |
---|
174 | for (Iterator i = menuItems.entrySet().iterator(); i.hasNext();) |
---|
175 | { |
---|
176 | Map.Entry entry = (Map.Entry) i.next(); |
---|
177 | item = new JMenuItem((String) entry.getKey()); |
---|
178 | final RosterMenuListener listener = (RosterMenuListener) entry.getValue(); |
---|
179 | item.addActionListener(new java.awt.event.ActionListener() |
---|
180 | { |
---|
181 | public void actionPerformed(ActionEvent e) |
---|
182 | { |
---|
183 | listener.actionPerformed(jidStatus, null); |
---|
184 | } |
---|
185 | }); |
---|
186 | menu.add(item); |
---|
187 | } |
---|
188 | }*/ |
---|
189 | } |
---|
190 | |
---|
191 | public void addSplitBarListener(ComponentListener listener) |
---|
192 | { |
---|
193 | BasicSplitPaneDivider divider = |
---|
194 | ((BasicSplitPaneUI)chatSplitPane.getUI()).getDivider(); |
---|
195 | divider.addComponentListener(listener); |
---|
196 | } |
---|
197 | |
---|
198 | public void removeSplitBarListener(ComponentListener listener) |
---|
199 | { |
---|
200 | BasicSplitPaneDivider divider = |
---|
201 | ((BasicSplitPaneUI)chatSplitPane.getUI()).getDivider(); |
---|
202 | divider.removeComponentListener(listener); |
---|
203 | } |
---|
204 | |
---|
205 | public void setDividerLocation(int location) |
---|
206 | { |
---|
207 | chatSplitPane.setDividerLocation(location); |
---|
208 | } |
---|
209 | |
---|
210 | public void setParentFrame(JFrame frame) |
---|
211 | { |
---|
212 | chatSplitPane.setParentFrame(frame); |
---|
213 | } |
---|
214 | |
---|
215 | private void makeWindow() |
---|
216 | { |
---|
217 | window = new JFrame(); |
---|
218 | setIcon(StatusIcons.getStatusIcon(jidStatus)); |
---|
219 | if(jidStatus instanceof UnknownJIDStatus)setTitle(jidStatus.getCompleteJID().toString()); |
---|
220 | else setTitle(jidStatus.getNick()); |
---|
221 | window.addWindowListener(new WindowAdapter() |
---|
222 | { |
---|
223 | public void windowClosing(WindowEvent e) |
---|
224 | { |
---|
225 | Preferences.putInteger("jeti","chatPosX",window.getX()); |
---|
226 | Preferences.putInteger("jeti","chatPosY",window.getY()); |
---|
227 | Preferences.putInteger("jeti","chatHeight",window.getHeight()); |
---|
228 | Preferences.putInteger("jeti","chatWidth",window.getWidth()); |
---|
229 | Preferences.putInteger("jeti","chatDivider",chatSplitPane.getDividerLocation()); |
---|
230 | chatWindows.removeChatWindow(ChatWindow.this); |
---|
231 | exit(); |
---|
232 | } |
---|
233 | }); |
---|
234 | |
---|
235 | /** |
---|
236 | * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
237 | * @Date : 03/11/2008 |
---|
238 | * @Description : Funcao abaixo comentada, tira o foco das outras janelas. |
---|
239 | */ |
---|
240 | |
---|
241 | /*window.addWindowFocusListener(new WindowAdapter() |
---|
242 | { |
---|
243 | public void windowGainedFocus(WindowEvent e) |
---|
244 | { |
---|
245 | ChatWindow.this.requestFocusInWindow(); |
---|
246 | } |
---|
247 | });*/ |
---|
248 | |
---|
249 | JMenuBar menuBar = new JMenuBar(); |
---|
250 | menuBar.add(menu); |
---|
251 | final JMenuItem item = new JMenuItem(); |
---|
252 | I18N.setTextAndMnemonic("main.chat.menu.Convert_to_tab", item,true); |
---|
253 | item.addActionListener(new java.awt.event.ActionListener() |
---|
254 | { |
---|
255 | public void actionPerformed(ActionEvent e) |
---|
256 | { |
---|
257 | menu.remove(item); |
---|
258 | chatWindows.convertToTab(ChatWindow.this,jidStatus); |
---|
259 | window.dispose(); |
---|
260 | } |
---|
261 | }); |
---|
262 | menu.add(item); |
---|
263 | window.setJMenuBar(menuBar); |
---|
264 | window.setContentPane(this); |
---|
265 | window.setSize(Preferences.getInteger("jeti","chatWidth",300),Preferences.getInteger("jeti","chatHeight",350)); |
---|
266 | } |
---|
267 | |
---|
268 | public void changeToWindow() |
---|
269 | { |
---|
270 | makeWindow(); |
---|
271 | chatSplitPane.setDividerLocation(Preferences.getInteger("jeti","chatDivider",200)); |
---|
272 | chatSplitPane.setParentFrame(window); |
---|
273 | window.setVisible(true); |
---|
274 | } |
---|
275 | |
---|
276 | public void setLocationOnScreen(int posX,int posY) |
---|
277 | { |
---|
278 | window.setLocation(posX,posY); |
---|
279 | } |
---|
280 | |
---|
281 | public void setLocationOnScreen(ChatWindow oldWindow) |
---|
282 | { |
---|
283 | if(oldWindow.window!=null) |
---|
284 | { |
---|
285 | window.setLocation(oldWindow.window.getX()+50 |
---|
286 | ,oldWindow.window.getY()+50); |
---|
287 | } |
---|
288 | } |
---|
289 | public void exit() |
---|
290 | { |
---|
291 | chatSplitPane.close(); |
---|
292 | if(window!=null)window.dispose(); |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | // public boolean compareJID(JID jid) |
---|
297 | // { |
---|
298 | // return jidStatus.getJID().equals(jid); |
---|
299 | // } |
---|
300 | |
---|
301 | public String getThread() |
---|
302 | { |
---|
303 | return thread; |
---|
304 | } |
---|
305 | |
---|
306 | public void appendMessage(Message message) |
---|
307 | { |
---|
308 | String name; |
---|
309 | |
---|
310 | if (jidStatus.getJID().equals(message.getFrom())) |
---|
311 | name = jidStatus.getNick(); |
---|
312 | |
---|
313 | else |
---|
314 | name = message.getFrom().getUser(); |
---|
315 | |
---|
316 | Runnable updateAComponent = new Runnable() |
---|
317 | { |
---|
318 | public void run() |
---|
319 | { |
---|
320 | //lblComposing.setText(""); |
---|
321 | lblComposing.setVisible(false); |
---|
322 | } |
---|
323 | }; |
---|
324 | SwingUtilities.invokeLater(updateAComponent); |
---|
325 | |
---|
326 | chatSplitPane.appendMessage(message,name); |
---|
327 | } |
---|
328 | |
---|
329 | public void appendPresenceChange(final Presence presence) |
---|
330 | { |
---|
331 | Runnable updateAComponent = new Runnable() |
---|
332 | { |
---|
333 | public void run() |
---|
334 | { |
---|
335 | lblComposing.setVisible(false); |
---|
336 | int show = presence.getShow(); |
---|
337 | String status = presence.getStatus(); |
---|
338 | setIcon(StatusIcons.getStatusIcon(jidStatus)); |
---|
339 | |
---|
340 | String text; |
---|
341 | if (show == Presence.UNAVAILABLE) |
---|
342 | { |
---|
343 | text = " " + I18N.gettext("main.chat.is_currently_offline"); |
---|
344 | setTitle(MessageFormat.format(I18N.gettext("main.chat.{0}_(offline)"),new Object[]{jidStatus.getNick()})); |
---|
345 | } |
---|
346 | else |
---|
347 | { |
---|
348 | text = " " + I18N.gettext("main.chat.changed_status_to") + " " + Presence.toLongShow(show); |
---|
349 | if (status != null) |
---|
350 | { |
---|
351 | text += '\n' + I18N.gettext("main.chat.with_status_message") + " " + status; |
---|
352 | } |
---|
353 | if(jidStatus instanceof UnknownJIDStatus)setTitle(jidStatus.getCompleteJID().toString()); |
---|
354 | else setTitle(jidStatus.getNick()); |
---|
355 | } |
---|
356 | chatSplitPane.appendSystemMessage(jidStatus.getNick() + text); |
---|
357 | } |
---|
358 | }; |
---|
359 | SwingUtilities.invokeLater(updateAComponent); |
---|
360 | } |
---|
361 | |
---|
362 | public JIDStatus getJIDStatus() |
---|
363 | { |
---|
364 | return jidStatus; |
---|
365 | } |
---|
366 | |
---|
367 | public void composingID(String id) |
---|
368 | { |
---|
369 | if (id == null)id = ""; |
---|
370 | chatSplitPane.composingID(id); |
---|
371 | } |
---|
372 | |
---|
373 | public void composing(String type) |
---|
374 | { |
---|
375 | if (type != null) |
---|
376 | { |
---|
377 | Runnable updateAComponent = new Runnable() |
---|
378 | { |
---|
379 | public void run() |
---|
380 | { |
---|
381 | lblComposing.setVisible(true); |
---|
382 | } |
---|
383 | }; |
---|
384 | SwingUtilities.invokeLater(updateAComponent); |
---|
385 | } |
---|
386 | else |
---|
387 | { |
---|
388 | Runnable updateAComponent = new Runnable() |
---|
389 | { |
---|
390 | public void run() |
---|
391 | { |
---|
392 | lblComposing.setVisible(false); |
---|
393 | } |
---|
394 | }; |
---|
395 | SwingUtilities.invokeLater(updateAComponent); |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | void jButton1_actionPerformed(ActionEvent e) |
---|
400 | { |
---|
401 | chatSplitPane.send(); |
---|
402 | } |
---|
403 | |
---|
404 | private void setTitle(String title) |
---|
405 | { |
---|
406 | if(window!=null)window.setTitle(title); |
---|
407 | else chatWindows.setTitleAt(this,title); |
---|
408 | } |
---|
409 | |
---|
410 | private void setIcon(ImageIcon icon) |
---|
411 | { |
---|
412 | if(window!=null) window.setIconImage(icon.getImage()); |
---|
413 | else chatWindows.setIconAt(this,icon); |
---|
414 | } |
---|
415 | |
---|
416 | public JMenu getMenu() |
---|
417 | { |
---|
418 | return menu; |
---|
419 | } |
---|
420 | } |
---|
421 | /* |
---|
422 | * Overrides for emacs |
---|
423 | * Local variables: |
---|
424 | * tab-width: 4 |
---|
425 | * End: |
---|
426 | */ |
---|