source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/xhtml/Plugin.java @ 1001

Revision 1001, 12.0 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #552 - Inclusão do projeto Java referente ao applet do módulo.

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