source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/xhtml/Plugin.java @ 3102

Revision 3102, 13.1 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2002 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 me at jeti@jabber.org
21 */
22
23
24package nu.fw.jeti.plugins.xhtml;
25
26import java.awt.Color;
27import java.awt.Dimension;
28import java.awt.Frame;
29import java.awt.Insets;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.text.MessageFormat;
33import java.util.ArrayList;
34import java.util.List;
35
36import javax.swing.Action;
37import javax.swing.ImageIcon;
38import javax.swing.JButton;
39import javax.swing.JColorChooser;
40import javax.swing.JOptionPane;
41import javax.swing.JPanel;
42import javax.swing.JTextPane;
43import javax.swing.text.AttributeSet;
44import javax.swing.text.BadLocationException;
45import javax.swing.text.Element;
46import javax.swing.text.MutableAttributeSet;
47import javax.swing.text.SimpleAttributeSet;
48import javax.swing.text.StyleConstants;
49import javax.swing.text.StyledDocument;
50import javax.swing.text.StyledEditorKit;
51
52import nu.fw.jeti.jabber.Backend;
53import nu.fw.jeti.jabber.elements.Extension;
54import nu.fw.jeti.jabber.elements.XExtension;
55import nu.fw.jeti.plugins.FormattedMessage;
56import nu.fw.jeti.plugins.Plugins;
57import nu.fw.jeti.plugins.Word;
58import nu.fw.jeti.plugins.xhtml.fontchooser.DialogShell;
59import nu.fw.jeti.plugins.xhtml.fontchooser.FontDialog;
60import nu.fw.jeti.ui.ChatSplitPane;
61import nu.fw.jeti.util.I18N;
62import nu.fw.jeti.util.Preferences;
63
64import nu.fw.jeti.util.ServerExpresso;
65import nu.fw.jeti.plugins.filetransfer.buttonFileTransfer;
66import nu.fw.jeti.plugins.callVoip.buttonCallVoip;;
67
68/**
69 * @author E.S. de Boer
70 *
71 *
72 */
73public class Plugin implements Plugins, FormattedMessage
74{
75        private JButton btnFont;
76        private JButton btnColor;
77        private JButton btnBold;
78        private JButton btnItalic;
79        private JButton btnUnderline;
80        private JButton btnFileTransfer;
81        private JButton btnCallVoip;
82        private JTextPane txtInvoer;
83        private ChatSplitPane chatPane;
84        private Object jidUser;
85        private Object jidName;
86        private boolean showXHTML = true;
87        public final static String VERSION = "0.3";
88        public final static String DESCRIPTION = "xhtml.Formats_messages";
89        public final static String MIN_JETI_VERSION = "0.5";
90        public final static String NAME = "xhtml";
91        public final static String ABOUT = "by E.S. de boer, uses Fontchooser from SimplyHTML by Ulrich Hilger";
92       
93        public static void init(Backend backend)
94        {
95                backend.addExtensionHandler("http://jabber.org/protocol/xhtml-im",new XHTMLIMHandler());
96                backend.addExtensionHandler("http://www.w3.org/1999/xhtml",new XHTMLHandler());
97                backend.addCapability("xhtml","http://jabber.org/protocol/xhtml-im");
98        }
99
100        public void initXHTML(ChatSplitPane chatPane, final JTextPane txtInvoer, JPanel pnlControl, Object JIDUser, Object JIDName)
101        {
102                this.txtInvoer  = txtInvoer;
103                this.chatPane   = chatPane;
104                this.jidUser    = JIDUser;
105                this.jidName    = JIDName;
106               
107                MutableAttributeSet set = new SimpleAttributeSet();
108                int i = Preferences.getInteger("xhtml", "foreground", 0);
109                if (i != 0)
110                {
111                        StyleConstants.setForeground(set, new Color(i));
112                }
113                i = Preferences.getInteger("xhtml", "background", 0);
114                if (i != 0)
115                {
116                        StyleConstants.setBackground(set, new Color(i));
117                }
118                i = Preferences.getInteger("xhtml", "font-size", 0);
119                if (i != 0)
120                {
121                        StyleConstants.setFontSize(set, i);
122                }
123                StyleConstants.setFontFamily(set, Preferences.getString("xhtml", "font-family", "Arial"));
124                StyleConstants.setBold(set,Preferences.getBoolean("xhtml","bold",false));
125            StyleConstants.setItalic(set,Preferences.getBoolean("xhtml","italic",false));
126           
127                txtInvoer.setParagraphAttributes(set, false);
128
129                // BUTTON-font
130                btnFont = new JButton(new ImageIcon(getClass().getResource("font.gif")));
131                btnFont.setToolTipText(I18N.gettext("xhtml.Font"));
132                btnFont.setMargin(new Insets(0, 0, 0, 0));
133                btnFont.setPreferredSize(new Dimension(23, 23));
134                btnFont.addActionListener(new java.awt.event.ActionListener()
135                {
136                        public void actionPerformed(ActionEvent e)
137                        {
138
139                                AttributeSet attr = txtInvoer.getCharacterAttributes(); //new SimpleAttributeSet();
140                                Frame frame = JOptionPane.getFrameForComponent(txtInvoer.getTopLevelAncestor());
141                                FontDialog d = new FontDialog(frame , I18N.gettext("xhtml.Choose_Font"), attr);
142                                d.setModal(true);
143                                d.setVisible(true);
144
145                                if (d.getResult() == DialogShell.RESULT_OK)
146                                {
147                                        attr = d.getAttributes();
148                                        int start = txtInvoer.getSelectionStart();
149                                        int end = txtInvoer.getSelectionEnd();
150                                        if (end != start)
151                                        {
152                                                StyledDocument doc = (StyledDocument) txtInvoer.getDocument();
153                                                doc.setCharacterAttributes(start, end - start, attr, false);
154                                        }
155                                        else txtInvoer.setParagraphAttributes(attr, false);
156                                        txtInvoer.requestFocus();
157                                }
158                        }
159                });
160                pnlControl.add(btnFont);
161
162                // BUTTON-color
163                btnColor = new JButton(new ImageIcon(getClass().getResource("color.gif")));
164                btnColor.setToolTipText(I18N.gettext("xhtml.Color"));
165                btnColor.setMargin(new Insets(0, 0, 0, 0));
166                btnColor.setPreferredSize(new Dimension(23, 23));
167                btnColor.addActionListener(new java.awt.event.ActionListener()
168                {
169                        public void actionPerformed(ActionEvent e)
170                        {
171                                Color color = JColorChooser.showDialog(txtInvoer.getTopLevelAncestor(), I18N.gettext("xhtml.Color"), null);
172                                if (color != null)
173                                {
174                                        int start = txtInvoer.getSelectionStart();
175                                        int end = txtInvoer.getSelectionEnd();
176                                        MutableAttributeSet set = new SimpleAttributeSet();
177                                        set.addAttribute(StyleConstants.Foreground, color);
178                                        if (end != start)
179                                        {
180                                                StyledDocument doc = (StyledDocument) txtInvoer.getDocument();
181                                                doc.setCharacterAttributes(start, end - start, set, false);
182                                        }
183                                        else txtInvoer.setParagraphAttributes(set, false);
184                                        txtInvoer.requestFocus();
185                                }
186                        }
187                });
188                pnlControl.add(btnColor);
189
190                ActionListener returnFocus = new ActionListener()
191                {
192                        public void actionPerformed(ActionEvent e)
193                        {
194                txtInvoer.requestFocus();
195            }
196        };
197
198        // Click button filetransfer;   
199        ActionListener Ftransfer = new ActionListener()
200        {
201               
202                public void actionPerformed(ActionEvent e)
203                {
204                        buttonFileTransfer sFile = new buttonFileTransfer();
205                        sFile.sendFile(jidUser);
206                }
207        };
208       
209        // Click button CallVoip;
210        ActionListener callVoip = new ActionListener()
211        {
212                public void actionPerformed(ActionEvent e)
213                {
214                        String _return = buttonCallVoip.connExpresso(jidUser.toString(), nu.fw.jeti.applet.Jeti.USERID);                       
215                        javax.swing.JOptionPane.showMessageDialog(null, I18N.gettext("callVoip.Calling") + " : " + jidName + "\n\n" +
216                                        I18N.gettext("callVoip.Status") + " : " + _return );
217                }
218        };
219       
220                // BUTTON-bold
221                Action boldAction = new StyledEditorKit.BoldAction();
222                boldAction.putValue(Action.NAME, null);
223                boldAction.putValue(Action.SHORT_DESCRIPTION, I18N.gettext("xhtml.Bold"));
224                boldAction.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("Bold16.gif")));
225                btnBold = new JButton(boldAction);
226                btnBold.setMargin(new Insets(0, 0, 0, 0));
227                btnBold.setPreferredSize(new Dimension(23, 23));
228        btnBold.addActionListener(returnFocus);
229                pnlControl.add(btnBold);
230
231                // BUTTON-italic
232                Action italicAction = new StyledEditorKit.ItalicAction();
233                italicAction.putValue(Action.NAME, null);
234                italicAction.putValue(Action.SHORT_DESCRIPTION, I18N.gettext("xhtml.Italic"));
235                italicAction.putValue(Action.SMALL_ICON,new ImageIcon(getClass().getResource("Italic16.gif")));
236                btnItalic = new JButton(italicAction);
237                btnItalic.setMargin(new Insets(0, 0, 0, 0));
238                btnItalic.setPreferredSize(new Dimension(23, 23));
239        btnItalic.addActionListener(returnFocus);
240                pnlControl.add(btnItalic);
241
242                // BUTTON-underline
243                Action underlineAction = new StyledEditorKit.UnderlineAction();
244                underlineAction.putValue(Action.NAME, null);
245                underlineAction.putValue(Action.SHORT_DESCRIPTION, I18N.gettext("xhtml.Underline"));
246                underlineAction.putValue(Action.SMALL_ICON,new ImageIcon(getClass().getResource("Underline16.gif")));
247                btnUnderline = new JButton(underlineAction);
248                btnUnderline.setMargin(new Insets(0, 0, 0, 0));
249                btnUnderline.setPreferredSize(new Dimension(23, 23));
250        btnUnderline.addActionListener(returnFocus);
251                pnlControl.add(btnUnderline);
252
253                // BUTTON-filetransfer
254                if( nu.fw.jeti.applet.Jeti.JAVAPLUGINS.indexOf("filetransfer") != -1 )
255                {
256                        btnFileTransfer = new JButton(new ImageIcon(getClass().getResource("ftransfer16.gif")));
257                        btnFileTransfer.setMargin(new Insets(0,0,0,0));
258                        btnFileTransfer.setPreferredSize(new Dimension(23, 23));
259                        btnFileTransfer.setToolTipText(I18N.gettext("xhtml.Transfer_File"));
260                        btnFileTransfer.addActionListener(Ftransfer);
261                        pnlControl.add(btnFileTransfer);
262                }
263               
264                // BUTTON-callVoip
265                if( nu.fw.jeti.applet.Jeti.JAVAPLUGINS.indexOf("callVoip") != -1 )
266                {
267                        btnCallVoip = new JButton();
268                        btnCallVoip = buttonCallVoip.button(btnCallVoip);
269                        btnCallVoip.addActionListener(callVoip);
270                        pnlControl.add(btnCallVoip);
271                }
272        }
273
274        public List getWordList(Extension extension)
275        {
276                if(extension instanceof XHTML)
277                {       
278                        return ((XHTML) extension).getWordList();
279                }
280                return null;
281        }
282               
283        public void useXHTML(boolean useXHTMl,String name)
284        {
285                //look if other one sends xhtml
286                //javax.swing.JOptionPane.showMessageDialog(null, "Directory " + dir + " could not be created, Jeti will close");
287                if (useXHTMl != showXHTML && !useXHTMl)
288                {
289                        //chatPane.appendSystemMessage(MessageFormat.format(
290                        //              I18N.gettext("xhtml.{0}_does_not_support_formatted_messages")
291                        //              ,new Object[]{name}));
292                }
293                showXHTML=useXHTMl;
294        }
295       
296        public XExtension getXHTMLExtension(List wordList)
297        {
298                return new XHTML(wordList);
299        }
300       
301        public boolean sendXML()
302        {
303                return showXHTML;
304        }
305       
306        public List makeWordListFromDocument()
307        {
308                Element[] element = txtInvoer.getDocument().getRootElements();
309                List wordList = new ArrayList();
310                parseElement(element[0], wordList, new SimpleAttributeSet());
311                return wordList;
312        }
313       
314        private void addToWordList(String text,SimpleAttributeSet currentAttributes, List wordList)
315        {
316                if(text.equals(""))return;
317                StringBuffer temp = new StringBuffer();
318                for(int i = 0;i<text.length();i++)
319                {
320                        //split text up in words
321                        char c = text.charAt(i);
322                        switch (c)
323                        {
324                                case ' ':       addWordFromTemp(temp, wordList, currentAttributes); wordList.add(new Word(" ", (SimpleAttributeSet)currentAttributes.clone())); temp = new StringBuffer(); break;
325                                case '\n':      addWordFromTemp(temp, wordList, currentAttributes); temp = new StringBuffer(); break;
326                                case '\t':      addWordFromTemp(temp, wordList, currentAttributes); wordList.add(new Word("\t", (SimpleAttributeSet)currentAttributes.clone()));temp = new StringBuffer();break;
327                                default:        temp.append(c);
328                        }
329                }
330                addWordFromTemp(temp, wordList, currentAttributes);
331        }
332       
333        private void addWordFromTemp(StringBuffer temp, List wordList, SimpleAttributeSet currentAttributes)
334        {
335                if(temp.length()>0)wordList.add(new Word(temp,(SimpleAttributeSet)currentAttributes.clone()));
336        }
337       
338       
339        /**
340         * Method parseElement.
341         * parses a styled document to a wordList
342         * @param elem element to parse
343         *
344         */
345        private void parseElement(Element elem, List wordList,SimpleAttributeSet attr)
346        {
347                AttributeSet set = elem.getAttributes();
348                attr.addAttributes(set);
349                if (elem.getName().equals("paragraph"))
350                {
351                        if (elem.getStartOffset() != 0)
352                        {//only <br/>?
353                                wordList.add(new Word("\n",(SimpleAttributeSet)attr.clone()));
354                        }
355                }
356                if (elem.getName().equals("content"))
357                {
358                        if (elem.getElementCount() > 0)
359                        {
360                                for (int i = 0; i < elem.getElementCount(); i++)
361                                {
362                                        parseElement(elem.getElement(i),wordList, new SimpleAttributeSet(attr));
363                                }
364                        }
365                        else
366                        {
367                                try
368                                {
369                                        int offset = elem.getEndOffset() - elem.getStartOffset();
370                                        String text = elem.getDocument().getText(elem.getStartOffset(), offset);
371                                        //remove linebreaks
372                                        addToWordList(text, attr, wordList);
373                                }
374                                catch (BadLocationException ble)
375                                {
376                                        ble.printStackTrace();
377                                }
378                        }
379                }
380                else if (elem.getElementCount() > 0)
381                {
382                        for (int i = 0; i < elem.getElementCount(); i++)
383                        {
384                                parseElement(elem.getElement(i), wordList,new SimpleAttributeSet(attr));
385                        }
386                }
387        }
388
389        public void unload(){}
390       
391        public static void unload(Backend backend)
392        {
393                backend.removeExtensionHandler("http://jabber.org/protocol/xhtml-im");
394                backend.removeExtensionHandler("http://www.w3.org/1999/xhtml");
395                backend.removeCapability("xhtml","http://jabber.org/protocol/xhtml-im");
396        }
397}
398
399/*
400 * Overrides for emacs
401 * Local variables:
402 * tab-width: 4
403 * End:
404 */
Note: See TracBrowser for help on using the repository browser.