⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatdialogpanel.java

📁 jxta官方例程
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/**  Copyright (c) 2001 Sun Microsystems, Inc.  All rights*  reserved.**  Redistribution and use in source and binary forms, with or without*  modification, are permitted provided that the following conditions*  are met:**  1. Redistributions of source code must retain the above copyright*  notice, this list of conditions and the following disclaimer.**  2. Redistributions in binary form must reproduce the above copyright*  notice, this list of conditions and the following disclaimer in*  the documentation and/or other materials provided with the*  distribution.**  3. The end-user documentation included with the redistribution,*  if any, must include the following acknowledgment:*  "This product includes software developed by the*  Sun Microsystems, Inc. for Project JXTA."*  Alternately, this acknowledgment may appear in the software itself,*  if and wherever such third-party acknowledgments normally appear.**  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"*  must not be used to endorse or promote products derived from this*  software without prior written permission. For written*  permission, please contact Project JXTA at http://www.jxta.org.**  5. Products derived from this software may not be called "JXTA",*  nor may "JXTA" appear in their name, without prior written*  permission of Sun.**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF*  SUCH DAMAGE.*  ====================================================================**  This software consists of voluntary contributions made by many*  individuals on behalf of Project JXTA.  For more*  information on Project JXTA, please see*  <http://www.jxta.org/>.**  This license is based on the BSD license adopted by the Apache Foundation.**  $Id: ChatDialogPanel.java,v 1.9 2006/07/06 07:52:05 nano Exp $*/package net.jxta.myjxta.ui;import info.clearthought.layout.TableLayout;import net.jxta.myjxta.View;import net.jxta.myjxta.dialog.Dialog;import net.jxta.myjxta.dialog.DialogListener;import net.jxta.myjxta.dialog.DialogMessage;import net.jxta.myjxta.plugin.PluginPanel;import net.jxta.myjxta.util.Constants;import net.jxta.myjxta.util.Resources;import net.jxta.myjxta.util.exec.ExecFactory;import net.jxta.pipe.PipeService;import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.jdesktop.swingx.JXEditorPane;import org.jdesktop.swingx.action.ActionFactory;import org.jdesktop.swingx.action.ActionManager;import org.jdesktop.swingx.action.BoundAction;import javax.swing.*;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultEditorKit;import javax.swing.text.Document;import javax.swing.text.EditorKit;import javax.swing.text.Element;import javax.swing.text.StyledEditorKit;import javax.swing.text.StyledEditorKit.ForegroundAction;import javax.swing.text.html.HTML;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLEditorKit;import javax.swing.text.html.StyleSheet;import java.awt.*;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDragEvent;import java.awt.dnd.DropTargetDropEvent;import java.awt.dnd.DropTargetEvent;import java.awt.dnd.DropTargetListener;import java.awt.event.ActionEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringReader;import java.io.StringWriter;import java.io.Writer;import java.net.MalformedURLException;import java.net.URL;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.ResourceBundle;/** * @author james todd [gonzo at jxta dot org] * @author mike mcangus [mcangus at jxta dot org] * @version $Id: ChatDialogPanel.java,v 1.9 2006/07/06 07:52:05 nano Exp $ */public class ChatDialogPanel        extends JPanel        implements DialogListener, PluginPanel {    private static final String DEFAULT_LOG_PREFIX = "myjxta-";    private static final String DEFAULT_LOG_SUFFIX = ".html";    private static final String LOG_TIMESTAMP_FORMAT = "yyyy.MM.dd-HH.mm.ssZ";    private static final int BLOCK = 4 * 1024;    private static final int MAX_FONT_SIZE = 48;    private static final int MIN_FONT_SIZE = 1;    private static final int HISTORY_MAX = 15;    private static final ResourceBundle STRINGS = Resources.getStrings();    static final Logger LOG = Logger.getLogger(ChatDialogPanel.class);    static final List<String> imageTypes;    Dialog dialog = null;    JButton send = null;    JXEditorPane editor = null;    JPopupMenu editorPopup = null;    JXEditorPane renderer = null;    JPopupMenu rendererPopup = null;    private ActionManager manager = null;    private ArrayList<String> rendererActions = null;    private ArrayList<String> editorActions = null;    private ArrayList<ForegroundAction> colorActions = null;    private JScrollPane rendererScroller = null;    private JScrollPane editorScroller = null;    private StyleSheet style = null;    private SimpleDateFormat logFormatter = null;    private View view = null;    private MyDropTargetListener dropper = null;    private Font rendererDefaultFont = null;    private Font editorDefaultFont = null;    private List<String> history = null;    private int historyIndex = 0;    static {        imageTypes = new ArrayList<String>();        imageTypes.add(UIConstants.IMAGE_TIFF);        imageTypes.add(UIConstants.IMAGE_TIF);        imageTypes.add(UIConstants.IMAGE_GIF);        imageTypes.add(UIConstants.IMAGE_JPEG);        imageTypes.add(UIConstants.IMAGE_JPG);        imageTypes.add(UIConstants.IMAGE_PNG);    }    /**     * Creates a new ChatDialogPanel for the specified dialog within the specified view.     *     * @param view   the view (JFrame) within which this Dialog Panel will exist     * @param dialog The Dialog within the view in which this Panel will exist.     */    public ChatDialogPanel(View view, Dialog dialog) {        super();        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("Begin ChatDialogPanel(View, Dialog) Constructor");        }        this.view = view;        this.dialog = dialog;        this.dialog.addListener(this);        init();        setEditor(false);        if (this.dialog != null) {            updateConnectionState();        }        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("End   ChatDialogPanel(View, Dialog) Constructor");        }    }    /**     * TODO: Add documentation     *     * @param msg     */    public void receive(DialogMessage msg) {        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("Begin receive(DialogMessage)");            LOG.debug("msg = " + msg.getMessage());        }        final String m = msg.getMessage();        if (m != null &&                m.length() > 0) {            EventQueue.invokeLater(new Runnable() {                public void run() {                    appendText(renderer, m);                }            });        }        this.view.updatePluginPanel(this, msg.getLabel());        if (!this.getDialog().getPipeAdvertisement().getType().equals(PipeService.PropagateType)) {            //1:1 chat goodbye            if (Dialog.GOODBYE_CMD.equals(msg.getCommand())) {                //the oposide party has closed the communication channel                //give the user some feedback that it makes no sense to send a message --> disable the send button                send.setEnabled(false);                editor.setEnabled(false);            }        } else {            //groupchat goodbye.... not sure what we should do here            //nothing for now (maybe a rosterupdate later)        }        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("End   receive(DialogMessage)");        }    }    /**     * Returns the Dialog within which this ChatDialogPanel exists.     *     * @return The Dialog within which this ChatDialogPanel exists.     */    public Dialog getDialog() {        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("In getDialog()");        }        return this.dialog;    }    /**     * Removes this ChatDialogPanel from the dialog and view.     */    public void dismiss() {        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("In dismiss()");        }        dialog.close(); //removeListener(this);    }    /**     * Sets the focus in the main window to the Editor.     */    public void setFocus() {        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("In setFocus()");        }        this.editor.requestFocus();    }    /**     * TODO: Add documentation     */    // xxx: it would be nice to not make these public ... delegate if possible    public void saveAsDialog() {        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("Begin saveAsDialog()");        }        JFileChooser fc = new JFileChooser();        fc.setSelectedFile(getDefaultLogFile(fc.getCurrentDirectory()));        // xxx: ugly cast        if (fc.showSaveDialog((Frame) view) ==                JFileChooser.APPROVE_OPTION) {            File f = fc.getSelectedFile();            if (f.isDirectory()) {                f = getDefaultLogFile(f);            }            File p = f.getParentFile();            if (! p.exists()) {                p.mkdirs();            }            FileWriter w = null;            String msg = STRINGS.getString("status.dialog.save") + " to " +                    f.getName();            try {                w = new FileWriter(f, true);            } catch (IOException ioe) {                msg = STRINGS.getString("error.dialog.save") + " " +                        f.getName();            }            if (w != null) {                try {                    f.createNewFile();                    StringReader r = new StringReader(readText(renderer));                    char[] buf = new char[BLOCK];                    int l = 0;                    while ((l = r.read(buf, 0, BLOCK)) > -1) {                        w.write(buf, 0, l);                    }                } catch (IOException ioe) {                    msg = STRINGS.getString("error.dialog.save") + " " +                            f.getName();                } finally {                    try {                        w.close();                    } catch (IOException ioe) {                        if (LOG.isEnabledFor(Level.ERROR)) {                            LOG.error("Caught unexpected Exception", ioe);                        }                    }                }                this.view.setStatus(msg);            }        }        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("End   saveAsDialog()");        }    }    /**     * Clears the contents of this dialog's display.     */    public void clearDialog() {        if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("In clearDialog()");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -