📄 chatdialogview.java
字号:
/** 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: ChatDialogView.java,v 1.1 2007/06/10 21:21:51 nano Exp $*/package net.jxta.myjxta.ui;import info.clearthought.layout.TableLayout;import net.jxta.logging.Logging;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.PluginView;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.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.*;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.*;import java.awt.event.*;import java.io.*;import java.net.MalformedURLException;import java.net.URL;import java.text.SimpleDateFormat;import java.util.*;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;/** * @author james todd [gonzo at jxta dot org] * @author mike mcangus [mcangus at jxta dot org] * @version $Id: ChatDialogView.java,v 1.1 2007/06/10 21:21:51 nano Exp $ */public final class ChatDialogView extends JPanel implements DialogListener, PluginView { 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(ChatDialogView.class.getName()); 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<StyledEditorKit.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 ChatDialogView 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 ChatDialogView(View view, Dialog dialog) { super(); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Begin ChatDialogView(View, Dialog) Constructor"); } this.view = view; this.dialog = dialog; this.dialog.addListener(this); init(); setEditor(false); if (this.dialog != null) { updateConnectionState(); } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("End ChatDialogView(View, Dialog) Constructor"); } } /** * TODO: Add documentation * * @param msg */ public void receive(DialogMessage msg) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Begin receive(DialogMessage)"); LOG.fine("msg = " + msg.getHtmlMessage()); } final String m = msg.getHtmlMessage(); 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 (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("End receive(DialogMessage)"); } } /** * Returns the Dialog within which this ChatDialogView exists. * * @return The Dialog within which this ChatDialogView exists. */ public Dialog getDialog() { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("In getDialog()"); } return this.dialog; } /** * Removes this ChatDialogView from the dialog and view. */ public void dismiss() { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("In dismiss()"); } dialog.close(); //removeListener(this); } /** * Sets the focus in the main window to the Editor. */ public void setFocus() { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("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 (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("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; 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 (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, "Caught unexpected Exception", ioe); } } } this.view.setStatus(msg); } } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("End saveAsDialog()"); } } /** * Clears the contents of this dialog's display. */ public void clearDialog() { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("In clearDialog()"); } removeText(this.renderer); } /** * TODO: Add documentation * * @param selected */ public void setEditor(boolean selected) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("In setEditor(boolean)"); } setEditor(selected, false); } /** * TODO: Add documentation * * @param selected
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -