transcriptwindow.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 515 行 · 第 1/2 页

SVN-BASE
515
字号
     */
    public synchronized void insertCustomText(String text, boolean bold, boolean underline, Color foreground) {
        try {
            // Agent color is always blue
            StyleConstants.setBold(styles, true);
            StyleConstants.setForeground(styles, foreground);
            final Document doc = getDocument();
            styles.removeAttribute("link");

            StyleConstants.setFontSize(styles, defaultFont.getSize());
            doc.insertString(doc.getLength(), "", styles);

            // Reset Styles for message
            StyleConstants.setBold(styles, bold);
            StyleConstants.setUnderline(styles, underline);
            StyleConstants.setForeground(styles, foreground);
            setText(text);
            insertText("\n");
            StyleConstants.setUnderline(styles, false);
            StyleConstants.setForeground(styles, Color.black);
        }
        catch (BadLocationException ex) {
            Log.error("Error message.", ex);
        }
    }


    /**
     * Returns the formatted date.
     *
     * @param insertDate the date to format.
     * @return the formatted date.
     */
    private String getDate(Date insertDate) {
        final LocalPreferences pref = SettingsManager.getLocalPreferences();

        if (insertDate == null) {
            insertDate = new Date();
        }

        StyleConstants.setFontFamily(styles, defaultFont.getFontName());
        StyleConstants.setFontSize(styles, defaultFont.getSize());

        if (pref.isTimeDisplayedInChat()) {
            final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
            final String date = formatter.format(insertDate);

            return "(" + date + ") ";
        }
        lastUpdated = insertDate;
        return "";
    }


    /**
     * Return the last time the <code>TranscriptWindow</code> was updated.
     *
     * @return the last time the <code>TranscriptWindow</code> was updated.
     */
    public Date getLastUpdated() {
        return lastUpdated;
    }

    /**
     * Inserts a history message.
     *
     * @param userid  the userid of the sender.
     * @param message the message to insert.
     * @param date    the Date object created when the message was delivered.
     */
    public void insertHistoryMessage(String userid, String message, Date date) {
        try {
            String value = "";

            long lastPostTime = lastPost != null ? lastPost.getTime() : 0;
            long lastPostStartOfDay = DateUtils.startOfDayInMillis(lastPostTime);
            long newPostStartOfDay = DateUtils.startOfDayInMillis(date.getTime());

            int diff = DateUtils.getDaysDiff(lastPostStartOfDay, newPostStartOfDay);
            if (diff != 0) {
                insertCustomText(notificationDateFormatter.format(date), true, true, Color.BLACK);
            }

            value = "(" + messageDateFormatter.format(date) + ") ";
            value = value + userid + ": ";


            lastPost = date;

            // Agent color is always blue
            StyleConstants.setBold(styles, false);
            StyleConstants.setForeground(styles, Color.BLACK);
            final Document doc = getDocument();
            styles.removeAttribute("link");

            StyleConstants.setFontSize(styles, defaultFont.getSize());
            doc.insertString(doc.getLength(), value, styles);

            // Reset Styles for message
            StyleConstants.setBold(styles, false);
            StyleConstants.setForeground(styles, (Color)UIManager.get("History.foreground"));
            setText(message);
            StyleConstants.setForeground(styles, Color.BLACK);
            insertText("\n");
        }
        catch (BadLocationException ex) {
            Log.error("Error message.", ex);
        }
    }

    /**
     * Disable the entire <code>TranscriptWindow</code> and visually represent
     * it as disabled.
     */
    public void showWindowDisabled() {
        final Document document = getDocument();
        final SimpleAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setForeground(attrs, Color.LIGHT_GRAY);

        final int length = document.getLength();
        StyledDocument styledDocument = getStyledDocument();
        styledDocument.setCharacterAttributes(0, length, attrs, false);
    }

    /**
     * Persist a current transcript.
     *
     * @param fileName   the name of the file to save the transcript as. Note: This can be modified by the user.
     * @param transcript the collection of transcript.
     * @param headerData the string to prepend to the transcript.
     * @see ChatRoom#getTranscripts()
     */
    public void saveTranscript(String fileName, List transcript, String headerData) {
        final LocalPreferences pref = SettingsManager.getLocalPreferences();

        try {
            SimpleDateFormat formatter;

            File defaultSaveFile = new File(Spark.getSparkUserHome() + "/" + fileName);
            final JFileChooser fileChooser = new JFileChooser(defaultSaveFile);
            fileChooser.setSelectedFile(defaultSaveFile);

            // Show save dialog; this method does not return until the dialog is closed
            int result = fileChooser.showSaveDialog(this);
            final File selFile = fileChooser.getSelectedFile();

            if (selFile != null && result == JFileChooser.APPROVE_OPTION) {
                final StringBuffer buf = new StringBuffer();
                final Iterator transcripts = transcript.iterator();
                buf.append("<html><body>");
                if (headerData != null) {
                    buf.append(headerData);
                }

                buf.append("<table width=600>");
                while (transcripts.hasNext()) {
                    final Message message = (Message)transcripts.next();
                    String from = message.getFrom();
                    if (from == null) {
                        from = pref.getNickname();
                    }

                    if (Message.Type.groupchat == message.getType()) {
                        if (ModelUtil.hasLength(StringUtils.parseResource(from))) {
                            from = StringUtils.parseResource(from);
                        }
                    }

                    final String body = message.getBody();
                    final Date insertionDate = (Date)message.getProperty("insertionDate");
                    formatter = new SimpleDateFormat("hh:mm:ss");

                    String value = "";
                    if (insertionDate != null) {
                        value = "(" + formatter.format(insertionDate) + ") ";
                    }
                    buf.append("<tr><td nowrap><font size=2>").append(value).append("<strong>").append(from).append(":</strong>&nbsp;").append(body).append("</font></td></tr>");

                }
                buf.append("</table></body></html>");
                final BufferedWriter writer = new BufferedWriter(new FileWriter(selFile));
                writer.write(buf.toString());
                writer.close();
                JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "Chat transcript has been saved.",
                        "Chat Transcript Saved", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        catch (Exception ex) {
            Log.error("Unable to save chat transcript.", ex);
            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "Could not save transcript.", "Error", JOptionPane.ERROR_MESSAGE);
        }

    }

    public void cleanup() {
        super.releaseResources();

        clear();

        removeMouseListener(this);
        removeMouseMotionListener(this);

        removeContextMenuListener(this);
        getActionMap().remove("copy");
    }


    public void setFont(Font font) {
        this.defaultFont = font;
    }

    public Font getFont() {
        return defaultFont;
    }


    /**
     * Adds Print and Clear actions.
     *
     * @param object the TransferWindow
     * @param popup  the popup menu to add to.
     */
    public void poppingUp(final Object object, JPopupMenu popup) {
        Action printAction = new AbstractAction() {
            public void actionPerformed(ActionEvent actionEvent) {
                SparkManager.printChatTranscript((TranscriptWindow)object);
            }
        };


        Action clearAction = new AbstractAction() {
            public void actionPerformed(ActionEvent actionEvent) {
                clear();
            }
        };


        printAction.putValue(Action.NAME, "Print");
        printAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.PRINTER_IMAGE_16x16));

        clearAction.putValue(Action.NAME, "Clear");
        clearAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.ERASER_IMAGE));
        popup.addSeparator();
        popup.add(printAction);

        popup.add(clearAction);
    }

    public void poppingDown(JPopupMenu popup) {

    }

    public boolean handleDefaultAction(MouseEvent e) {
        return false;
    }
}

⌨️ 快捷键说明

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