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

📄 emoticons.java

📁 网站即时通讯系统
💻 JAVA
字号:
/* Copyright (C) 2003 Adam Olsen 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;import java.awt.Component;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.util.Iterator;import java.util.Properties;import javax.swing.BorderFactory;import javax.swing.BoxLayout;import javax.swing.ImageIcon;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.text.JTextComponent;import com.valhalla.gui.Standard;import com.valhalla.settings.Settings;/** * Replaces different emote symbols with images * * @author Adam Olsen * @created October 30, 2004 * @version 1.0 */public class Emoticons {    private String imageDir;    private static Emoticons instance;    private Properties map;    private String themeDir;    private InputStream emoticonStream;    private boolean switched = false;    /**     * Default constructor - opens the emoticon theme dir and reads in the data     * file containing the different emote definitions     */    private Emoticons() {    }    /**     * @return the Emoticons singleton     */    public static Emoticons getInstance() {        if (instance == null) {            instance = new Emoticons();        }        if (!instance.switched) {            instance.switchTheme(Settings.getInstance().getProperty(                    "emoticonTheme"));        }        return instance;    }    /**     * Switches the emoticon theme     *     * @param theme     *            the theme to switch to     */    public void switchTheme(String theme) {        switched = true;        map = new Properties();        themeDir = theme;        if (themeDir == null) {            themeDir = "default";        }        emoticonStream = getClass().getClassLoader().getResourceAsStream(                "imagethemes/emoticons/" + themeDir + '/' + "index.dat");        if (emoticonStream == null) {            com.valhalla.Logger.debug("Bad Emoticon File");            return;        }        InputStreamReader in = new InputStreamReader(emoticonStream);        BufferedReader reader = new BufferedReader(in);        String line;        String nameValue[] = new String[2];        try {            map.setProperty("(-)(-)", "fairy.gif");            while ((line = reader.readLine()) != null) {                nameValue = line.split(" ");                if (nameValue[0] == null || nameValue[1] == null) {                    break;                }                nameValue[0] = nameValue[0].replaceAll("<", "&lt;");                nameValue[0] = nameValue[0].replaceAll(">", "&gt;");                map.setProperty(nameValue[0], nameValue[1]);            }            in.close();            // close the file        } catch (IOException e) {            com.valhalla.Logger.debug("Couldn't read emoticon file.");        }    }    /**     * Replaces the different symbols with the images defined in the emote data     * file     *     * @param text     *            the text to modify     * @return the modified text     */    public String replaceIcons(String text) {        if (emoticonStream == null) {            return text;        }        Iterator iterator = map.keySet().iterator();        String symbol, image, imageLocation;        while (iterator.hasNext()) {            symbol = (String) iterator.next();            image = map.getProperty(symbol);            imageLocation = "imagethemes/emoticons/" + themeDir + "/" + image;            if (symbol.equals("(-)(-)")) {                imageLocation = "imagethemes/emoticons/" + image;            }            String icon = appendIcon(imageLocation);            if (icon == null)                continue;            StringBuffer newText = null;            int l1 = 0, l2 = 0;            while ((l2 = text.indexOf(symbol, l1)) != -1) {                if (newText == null)                    newText = new StringBuffer(text.length() + 40);                String before = text.substring(l1, l2);                newText.append(before);                int after = l2 + symbol.length();                if ((l1 == l2 ||                     before.endsWith(" ") ||                     before.endsWith("&nbsp;")) &&                    (after == text.length() ||                     text.startsWith(" ", after) ||                     text.startsWith("&nbsp;", after))                ) {                    newText.append(icon);                } else {                    newText.append(symbol);                }                l1 = after;            }            if (newText != null) {                if (l1 < text.length())                    newText.append(text.substring(l1));                text = newText.toString();            }        }        return text;    }    /**     * Appends an image tag onto a StringBuffer     *     * @param imageLocation     *            the location of the image     * @return the string with the appended icon (or null if no icon found)     */    private String appendIcon(String imageLocation) {        URL imageUrl = getClass().getClassLoader().getResource(imageLocation);        if (imageUrl == null) {            //com.valhalla.Logger.debug("No emoticon image found for "            //       + imageLocation);            return null;        }        ImageIcon iconTest = new ImageIcon(imageUrl);        int width = iconTest.getIconWidth();        int height = iconTest.getIconHeight();        return " <img border='0' width='" + width + "' height='" + height                + "' src='" + imageUrl + "'>&nbsp; ";    }    /**     * Shows a small window displaying all available emoticons.     *     * @param window     *            the parent window     * @param component     *            the Component to display this frame over     * @param area     *            the text area that the emoticons will be placed on when the     *            user clicks an image     */    public void displayEmoticonChooser(JFrame window, Component component,            JTextComponent area) {        JDialog dialog = new JDialog(window);        JPanel panel = (JPanel) dialog.getContentPane();        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));        EmoteClickListener listener = new EmoteClickListener(dialog, area);        int columns = 6;        int current = 0;        JPanel cPanel = null;        Iterator i = map.keySet().iterator();        Properties displayed = new Properties();        while (i.hasNext()) {            String symbol = (String) i.next();            if (symbol.equals("(-)(-)")) {                continue;            }            String image = map.getProperty(symbol);            if (displayed.getProperty(image) != null)                continue;            displayed.setProperty(image, "True");            String imageLocation = "imagethemes/emoticons/" + themeDir + "/"                    + image;            ImageIcon icon = Standard.getIcon(imageLocation);            if (icon == null) {                continue;            }            if (current > columns) {                panel.add(cPanel);                current = 0;            }            if (current == 0) {                cPanel = new JPanel();                cPanel.setLayout(new BoxLayout(cPanel, BoxLayout.X_AXIS));            }            JLabel label = new JLabel(icon);            label.setName(symbol);            label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));            label.addMouseListener(listener);            if (icon != null) {                cPanel.add(label);            }            current++;        }        dialog.pack();        dialog.setLocationRelativeTo(component);        dialog.setVisible(true);        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    }    /**     * Listens for an emoticon to get clicked     *     * @author synic     * @created October 30, 2004     */    class EmoteClickListener extends MouseAdapter {        JDialog dialog;        JTextComponent area;        /**         * Constructor for the EmoteClickListener object         *         * @param dialog         *            the emote dialog that called this listener         * @param area         *            the textarea to append the emote symbol to         */        public EmoteClickListener(JDialog dialog, JTextComponent area) {            this.dialog = dialog;            this.area = area;        }        /**         * Called by the mouse listener         *         * @param e         *            the mouse event         */        public void mouseReleased(MouseEvent e) {            dialog.dispose();            JLabel label = (JLabel) e.getSource();            String symbol = label.getName();            area.setText(area.getText()                    + symbol.replaceAll("&gt;", ">").replaceAll("&lt;", "<")                    + " ");            area.grabFocus();        }    }}

⌨️ 快捷键说明

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