/* * Jeti, a Java Jabber client, Copyright (C) 2002 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or mail me at jeti@jabber.org */ package nu.fw.jeti.plugins.xhtml; import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import javax.swing.Action; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextPane; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Element; import javax.swing.text.MutableAttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; import javax.swing.text.StyledEditorKit; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.elements.Extension; import nu.fw.jeti.jabber.elements.XExtension; import nu.fw.jeti.plugins.FormattedMessage; import nu.fw.jeti.plugins.Plugins; import nu.fw.jeti.plugins.Word; import nu.fw.jeti.plugins.xhtml.fontchooser.DialogShell; import nu.fw.jeti.plugins.xhtml.fontchooser.FontDialog; import nu.fw.jeti.ui.ChatSplitPane; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Preferences; import nu.fw.jeti.util.ServerExpresso; import nu.fw.jeti.plugins.filetransfer.buttonFileTransfer; import nu.fw.jeti.plugins.callVoip.buttonCallVoip;; /** * @author E.S. de Boer * * */ public class Plugin implements Plugins, FormattedMessage { private JButton btnFont; private JButton btnColor; private JButton btnBold; private JButton btnItalic; private JButton btnUnderline; private JButton btnFileTransfer; private JButton btnCallVoip; private JTextPane txtInvoer; private ChatSplitPane chatPane; private Object jidUser; private Object jidName; private boolean showXHTML = true; public final static String VERSION = "0.3"; public final static String DESCRIPTION = "xhtml.Formats_messages"; public final static String MIN_JETI_VERSION = "0.5"; public final static String NAME = "xhtml"; public final static String ABOUT = "by E.S. de boer, uses Fontchooser from SimplyHTML by Ulrich Hilger"; public static void init(Backend backend) { backend.addExtensionHandler("http://jabber.org/protocol/xhtml-im",new XHTMLIMHandler()); backend.addExtensionHandler("http://www.w3.org/1999/xhtml",new XHTMLHandler()); backend.addCapability("xhtml","http://jabber.org/protocol/xhtml-im"); } public void initXHTML(ChatSplitPane chatPane, final JTextPane txtInvoer, JPanel pnlControl, Object JIDUser, Object JIDName) { this.txtInvoer = txtInvoer; this.chatPane = chatPane; this.jidUser = JIDUser; this.jidName = JIDName; MutableAttributeSet set = new SimpleAttributeSet(); int i = Preferences.getInteger("xhtml", "foreground", 0); if (i != 0) { StyleConstants.setForeground(set, new Color(i)); } i = Preferences.getInteger("xhtml", "background", 0); if (i != 0) { StyleConstants.setBackground(set, new Color(i)); } i = Preferences.getInteger("xhtml", "font-size", 0); if (i != 0) { StyleConstants.setFontSize(set, i); } StyleConstants.setFontFamily(set, Preferences.getString("xhtml", "font-family", "Arial")); StyleConstants.setBold(set,Preferences.getBoolean("xhtml","bold",false)); StyleConstants.setItalic(set,Preferences.getBoolean("xhtml","italic",false)); txtInvoer.setParagraphAttributes(set, false); // BUTTON-font btnFont = new JButton(new ImageIcon(getClass().getResource("font.gif"))); btnFont.setToolTipText(I18N.gettext("xhtml.Font")); btnFont.setMargin(new Insets(0, 0, 0, 0)); btnFont.setPreferredSize(new Dimension(23, 23)); btnFont.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { AttributeSet attr = txtInvoer.getCharacterAttributes(); //new SimpleAttributeSet(); Frame frame = JOptionPane.getFrameForComponent(txtInvoer.getTopLevelAncestor()); FontDialog d = new FontDialog(frame , I18N.gettext("xhtml.Choose_Font"), attr); d.setModal(true); d.setVisible(true); if (d.getResult() == DialogShell.RESULT_OK) { attr = d.getAttributes(); int start = txtInvoer.getSelectionStart(); int end = txtInvoer.getSelectionEnd(); if (end != start) { StyledDocument doc = (StyledDocument) txtInvoer.getDocument(); doc.setCharacterAttributes(start, end - start, attr, false); } else txtInvoer.setParagraphAttributes(attr, false); txtInvoer.requestFocus(); } } }); pnlControl.add(btnFont); // BUTTON-color btnColor = new JButton(new ImageIcon(getClass().getResource("color.gif"))); btnColor.setToolTipText(I18N.gettext("xhtml.Color")); btnColor.setMargin(new Insets(0, 0, 0, 0)); btnColor.setPreferredSize(new Dimension(23, 23)); btnColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog(txtInvoer.getTopLevelAncestor(), I18N.gettext("xhtml.Color"), null); if (color != null) { int start = txtInvoer.getSelectionStart(); int end = txtInvoer.getSelectionEnd(); MutableAttributeSet set = new SimpleAttributeSet(); set.addAttribute(StyleConstants.Foreground, color); if (end != start) { StyledDocument doc = (StyledDocument) txtInvoer.getDocument(); doc.setCharacterAttributes(start, end - start, set, false); } else txtInvoer.setParagraphAttributes(set, false); txtInvoer.requestFocus(); } } }); pnlControl.add(btnColor); ActionListener returnFocus = new ActionListener() { public void actionPerformed(ActionEvent e) { txtInvoer.requestFocus(); } }; // Click button filetransfer; ActionListener Ftransfer = new ActionListener() { public void actionPerformed(ActionEvent e) { buttonFileTransfer sFile = new buttonFileTransfer(); sFile.sendFile(jidUser); } }; // Click button CallVoip; ActionListener callVoip = new ActionListener() { public void actionPerformed(ActionEvent e) { String _return = buttonCallVoip.connExpresso(jidUser.toString(), nu.fw.jeti.applet.Jeti.USERID); javax.swing.JOptionPane.showMessageDialog(null, I18N.gettext("callVoip.Calling") + " : " + jidName + "\n\n" + I18N.gettext("callVoip.Status") + " : " + _return ); } }; // BUTTON-bold Action boldAction = new StyledEditorKit.BoldAction(); boldAction.putValue(Action.NAME, null); boldAction.putValue(Action.SHORT_DESCRIPTION, I18N.gettext("xhtml.Bold")); boldAction.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("Bold16.gif"))); btnBold = new JButton(boldAction); btnBold.setMargin(new Insets(0, 0, 0, 0)); btnBold.setPreferredSize(new Dimension(23, 23)); btnBold.addActionListener(returnFocus); pnlControl.add(btnBold); // BUTTON-italic Action italicAction = new StyledEditorKit.ItalicAction(); italicAction.putValue(Action.NAME, null); italicAction.putValue(Action.SHORT_DESCRIPTION, I18N.gettext("xhtml.Italic")); italicAction.putValue(Action.SMALL_ICON,new ImageIcon(getClass().getResource("Italic16.gif"))); btnItalic = new JButton(italicAction); btnItalic.setMargin(new Insets(0, 0, 0, 0)); btnItalic.setPreferredSize(new Dimension(23, 23)); btnItalic.addActionListener(returnFocus); pnlControl.add(btnItalic); // BUTTON-underline Action underlineAction = new StyledEditorKit.UnderlineAction(); underlineAction.putValue(Action.NAME, null); underlineAction.putValue(Action.SHORT_DESCRIPTION, I18N.gettext("xhtml.Underline")); underlineAction.putValue(Action.SMALL_ICON,new ImageIcon(getClass().getResource("Underline16.gif"))); btnUnderline = new JButton(underlineAction); btnUnderline.setMargin(new Insets(0, 0, 0, 0)); btnUnderline.setPreferredSize(new Dimension(23, 23)); btnUnderline.addActionListener(returnFocus); pnlControl.add(btnUnderline); // BUTTON-filetransfer if( nu.fw.jeti.applet.Jeti.JAVAPLUGINS.indexOf("filetransfer") != -1 ) { btnFileTransfer = new JButton(new ImageIcon(getClass().getResource("ftransfer16.gif"))); btnFileTransfer.setMargin(new Insets(0,0,0,0)); btnFileTransfer.setPreferredSize(new Dimension(23, 23)); btnFileTransfer.setToolTipText(I18N.gettext("xhtml.Transfer_File")); btnFileTransfer.addActionListener(Ftransfer); pnlControl.add(btnFileTransfer); } // BUTTON-callVoip if( nu.fw.jeti.applet.Jeti.JAVAPLUGINS.indexOf("callVoip") != -1 ) { btnCallVoip = new JButton(); btnCallVoip = buttonCallVoip.button(btnCallVoip); btnCallVoip.addActionListener(callVoip); pnlControl.add(btnCallVoip); } } public List getWordList(Extension extension) { if(extension instanceof XHTML) { return ((XHTML) extension).getWordList(); } return null; } public void useXHTML(boolean useXHTMl,String name) { //look if other one sends xhtml //javax.swing.JOptionPane.showMessageDialog(null, "Directory " + dir + " could not be created, Jeti will close"); if (useXHTMl != showXHTML && !useXHTMl) { //chatPane.appendSystemMessage(MessageFormat.format( // I18N.gettext("xhtml.{0}_does_not_support_formatted_messages") // ,new Object[]{name})); } showXHTML=useXHTMl; } public XExtension getXHTMLExtension(List wordList) { return new XHTML(wordList); } public boolean sendXML() { return showXHTML; } public List makeWordListFromDocument() { Element[] element = txtInvoer.getDocument().getRootElements(); List wordList = new ArrayList(); parseElement(element[0], wordList, new SimpleAttributeSet()); return wordList; } private void addToWordList(String text,SimpleAttributeSet currentAttributes, List wordList) { if(text.equals(""))return; StringBuffer temp = new StringBuffer(); for(int i = 0;i0)wordList.add(new Word(temp,(SimpleAttributeSet)currentAttributes.clone())); } /** * Method parseElement. * parses a styled document to a wordList * @param elem element to parse * */ private void parseElement(Element elem, List wordList,SimpleAttributeSet attr) { AttributeSet set = elem.getAttributes(); attr.addAttributes(set); if (elem.getName().equals("paragraph")) { if (elem.getStartOffset() != 0) {//only
? wordList.add(new Word("\n",(SimpleAttributeSet)attr.clone())); } } if (elem.getName().equals("content")) { if (elem.getElementCount() > 0) { for (int i = 0; i < elem.getElementCount(); i++) { parseElement(elem.getElement(i),wordList, new SimpleAttributeSet(attr)); } } else { try { int offset = elem.getEndOffset() - elem.getStartOffset(); String text = elem.getDocument().getText(elem.getStartOffset(), offset); //remove linebreaks addToWordList(text, attr, wordList); } catch (BadLocationException ble) { ble.printStackTrace(); } } } else if (elem.getElementCount() > 0) { for (int i = 0; i < elem.getElementCount(); i++) { parseElement(elem.getElement(i), wordList,new SimpleAttributeSet(attr)); } } } public void unload(){} public static void unload(Backend backend) { backend.removeExtensionHandler("http://jabber.org/protocol/xhtml-im"); backend.removeExtensionHandler("http://www.w3.org/1999/xhtml"); backend.removeCapability("xhtml","http://jabber.org/protocol/xhtml-im"); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */