sparktoaster.java

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

JAVA
682
字号
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.sparkimpl.plugin.alerts;

/**
 * SparkToaster is an improvement of Java Toaster which is a java utility class for your swing applications
 * that show an animate box coming from the bottom of your screen
 * with a notification message and/or an associated image
 * (like msn online/offline notifications).
 *
 * Toaster panel in windows system follow the taskbar; So if
 * the taskbar is into the bottom the panel coming from the bottom
 * and if the taskbar is on the top then the panel coming from the top.
 *
 * This is a simple example of utilization:
 *
 * import com.nitido.utils.toaster.*;
 * import javax.swing.*;
 *
 */


import org.jivesoftware.resource.Default;
import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.component.RolloverButton;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;

/**
 * Class to show tosters in multiplatform
 *
 * @author daniele piras
 */
public class SparkToaster {

    /**
     * The default Hand cursor.
     */
    public static final Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);

    /**
     * The default Text Cursor.
     */
    public static final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);

    // Width of the toster
    private int toasterWidth = 200;

    // Height of the toster
    private int toasterHeight = 150;

    // Step for the toaster
    private int step = 20;

    // Step time
    private int stepTime = 20;

    // Show time
    private int displayTime = 3000;

    // Current number of toaster...
    private int currentNumberOfToaster = 0;

    // Last opened toaster
    private int maxToaster = 0;

    // Max number of toasters for the sceen
    private int maxToasterInSceen;

    // Font used to display message
    private Font font;

    // Color for border
    private Color borderColor;

    // Color for toaster
    private Color toasterColor;

    // Set message color
    private Color messageColor;

    // Set the margin
    int margin;

    // Flag that indicate if use alwaysOnTop or not.
    // method always on top start only SINCE JDK 5 !
    boolean useAlwaysOnTop = true;

    private static final long serialVersionUID = 1L;

    private String title;

    private Border border;


    private Action customAction;

    private Window window;

    private JPanel mainPanel = new JPanel();

    private TitleLabel titleLabel;

    private boolean hideable = true;

    /**
     * Constructor to initialized toaster component...
     */
    public SparkToaster() {
        // Set default font...
        font = new Font("Dialog", Font.PLAIN, 11);

        // Border color
        borderColor = new Color(245, 153, 15);
        toasterColor = Color.WHITE;
        messageColor = Color.BLACK;
        useAlwaysOnTop = true;
    }

    /**
     * Class that rappresent a single toaster
     *
     * @author daniele piras
     */
    class SingleToaster extends javax.swing.JWindow {
        private static final long serialVersionUID = 1L;

        // Label to store Icon

        // Text area for the message
        private JTextArea message = new JTextArea();

        /**
         * Simple costructor that initialized components...
         */
        public SingleToaster() {
            initComponents();
        }

        /**
         * Function to initialized components
         */
        private void initComponents() {
            message.setFont(getToasterMessageFont());


            mainPanel.setBackground(Color.white);
            message.setOpaque(false);
            mainPanel.setLayout(new GridBagLayout());
            message.setMargin(new Insets(2, 2, 2, 2));
            message.setLineWrap(true);
            message.setWrapStyleWord(true);

            message.setForeground(getMessageColor());
            titleLabel = new TitleLabel(getTitle(), true);
            titleLabel.setForeground(new Color(87, 166, 211));
            titleLabel.setFont(new Font("Dialog", Font.BOLD, 13));

            mainPanel.add(titleLabel, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

            titleLabel.getCloseButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                    dispose();
                }
            });

            if (border != null) {
                mainPanel.setBorder(border);
            }

            message.setForeground(Color.BLACK);

            message.setOpaque(false);

            getContentPane().add(mainPanel);


            mainPanel.addMouseListener(new PaneMouseListener());
            message.addMouseListener(new PaneMouseListener());

            pack();
            setSize(toasterWidth, toasterHeight);
            mainPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        }


        /**
         * Start toaster animation...
         */
        public void animate() {
            (new Animation(this)).start();
        }

        private class PaneMouseListener extends MouseAdapter {

            public void mouseClicked(MouseEvent e) {
                if (customAction != null) {
                    customAction.actionPerformed(null);
                }

                if (hideable) {
                    setVisible(false);
                    dispose();
                }
            }

            public void mouseEntered(MouseEvent e) {
                message.setCursor(HAND_CURSOR);
                setCursor(HAND_CURSOR);
            }

            public void mouseExited(MouseEvent e) {
                message.setCursor(DEFAULT_CURSOR);
                setCursor(DEFAULT_CURSOR);
            }
        }
    }


    /**
     * Class that manage the animation
     */
    class Animation extends Thread {
        SingleToaster toaster;

        public Animation(SingleToaster toaster) {
            this.toaster = toaster;
        }


        /**
         * Animate vertically the toaster. The toaster could be moved from bottom
         * to upper or to upper to bottom
         *
         * @param posx
         * @throws InterruptedException
         */
        protected void animateVertically(int posx, int fromY, int toY) throws InterruptedException {

            toaster.setLocation(posx, fromY);
            if (toY < fromY) {
                for (int i = fromY; i > toY; i -= step) {
                    toaster.setLocation(posx, i);
                    Thread.sleep(stepTime);
                }
            }
            else {
                for (int i = fromY; i < toY; i += step) {
                    toaster.setLocation(posx, i);
                    Thread.sleep(stepTime);
                }
            }
            toaster.setLocation(posx, toY);
            toaster.invalidate();
            toaster.validate();
            toaster.repaint();
        }

        public void run() {
            try {
                boolean animateFromBottom = true;
                GraphicsEnvironment ge = GraphicsEnvironment
                        .getLocalGraphicsEnvironment();
                Rectangle screenRect = ge.getMaximumWindowBounds();

                int screenHeight = (int)screenRect.height;

                int startYPosition;
                int stopYPosition;

                if (screenRect.y > 0) {
                    animateFromBottom = false; // Animate from top!
                }

                maxToasterInSceen = screenHeight / toasterHeight;


                int posx = (int)screenRect.width - toasterWidth - 1;

                toaster.setLocation(posx, screenHeight);
                toaster.setVisible(true);
                if (useAlwaysOnTop) {
                    toaster.setAlwaysOnTop(true);
                }

                if (animateFromBottom) {
                    startYPosition = screenHeight;
                    stopYPosition = startYPosition - toasterHeight - 1;
                    if (currentNumberOfToaster > 0) {
                        stopYPosition = stopYPosition - (maxToaster % maxToasterInSceen * toasterHeight);
                    }
                    else {
                        maxToaster = 0;
                    }
                }
                else {
                    startYPosition = screenRect.y - toasterHeight;
                    stopYPosition = screenRect.y;

                    if (currentNumberOfToaster > 0) {
                        stopYPosition = stopYPosition + (maxToaster % maxToasterInSceen * toasterHeight);
                    }
                    else {

⌨️ 快捷键说明

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