missedcalls.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 247 行

SVN-BASE
247
字号
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2007 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.sparkplugin.ui.call;

import org.jivesoftware.spark.plugin.phone.resource.PhoneRes;
import org.jivesoftware.sparkplugin.callhistory.TelephoneUtils;
import net.java.sipmack.softphone.SoftPhoneManager;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.RolloverButton;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.sparkimpl.plugin.alerts.SparkToaster;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Responsible for the handling and displaying of missed calls. The toaster should remain visible until the user explicitly closes it.
 *
 * @author Derek DeMoro
 */
public class MissedCalls implements ActionListener {

    private SparkToaster toaster;
    private final DefaultListModel model;
    private final JList list;
    private JPanel gui;
    private RolloverButton callBackButton;
    private RolloverButton deleteButton;

    public MissedCalls() {
        model = new DefaultListModel();
        list = new JList(model);
        gui = getMissedCallPanel();


        list.setCellRenderer(new MissedCallRenderer());
        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent mouseEvent) {
                if (mouseEvent.getClickCount() == 2) {
                    placeCall((MissedCall)list.getSelectedValue());
                }
            }
        });

        list.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return;
                }

                int selectedIndex = list.getSelectedIndex();
                boolean enabled = selectedIndex != -1;
                callBackButton.setEnabled(enabled);
                deleteButton.setEnabled(enabled);
            }
        });
    }

    public JPanel getMissedCallPanel() {
        final JPanel panel = new JPanel(new GridBagLayout());
        panel.add(new JScrollPane(list), new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        callBackButton = new RolloverButton(PhoneRes.getIString("phone.callback"), PhoneRes.getImageIcon("PHONE_CALL_24x24_IMAGE"));
        deleteButton = new RolloverButton(PhoneRes.getIString("phone.delete"), PhoneRes.getImageIcon("DELETE_24x24_IMAGE"));
        callBackButton.setHorizontalAlignment(JLabel.CENTER);
        deleteButton.setHorizontalAlignment(JLabel.CENTER);
        final Font buttonFont = new Font("Dialog", Font.BOLD, 13);
        callBackButton.setFont(buttonFont);
        deleteButton.setFont(buttonFont);

        // Add Action Listener
        callBackButton.addActionListener(this);
        deleteButton.addActionListener(this);

        // Set as disabled by default, and only have them enabled if their is a valid
        // selection.
        callBackButton.setEnabled(false);
        deleteButton.setEnabled(false);

        final JPanel flowPanel = new JPanel(new FlowLayout());
        flowPanel.setOpaque(false);
        flowPanel.add(callBackButton);
        flowPanel.add(deleteButton);

        panel.setOpaque(false);
        panel.add(flowPanel, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        return panel;
    }

    /**
     * Called whenever the user misses a call. The information will be displayed in the Spark Toaster until the user
     * explicitly closes the window.
     *
     * @param callerID the callerID
     * @param number   the number the user dialed from.
     */
    public void addMissedCall(String callerID, String number) {
        VCard vcard = SparkManager.getVCardManager().searchPhoneNumber(number);
        if (vcard != null) {
            String firstName = vcard.getFirstName();
            String lastName = vcard.getLastName();
            if (ModelUtil.hasLength(firstName) && ModelUtil.hasLength(lastName)) {
                callerID = firstName + " " + lastName;
            }
            else if (ModelUtil.hasLength(firstName)) {
                callerID = firstName;
            }
        }

        final MissedCall missedCall = new MissedCall(callerID, new Date(), number);
        model.insertElementAt(missedCall, 0);

        if (toaster == null || !list.isShowing()) {
            toaster = new SparkToaster();
            toaster.setToasterHeight(230);
            toaster.setToasterWidth(300);
            toaster.setDisplayTime(500000000);
            toaster.showToaster(PhoneRes.getIString("phone.missedcalls"), gui);
        }
    }


    /**
     * Represents a single entry into the phone history list.
     */
    private class MissedCall extends JPanel {
        private String number;

        public MissedCall(String title, Date time, String number) {
            setLayout(new GridBagLayout());

            this.number =  TelephoneUtils.formatPattern(number,PhoneRes.getIString("phone.numpattern"));

            final JLabel titleLabel = new JLabel(title);
            titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));

            final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");    

            StringBuilder builder = new StringBuilder();
            builder.append(formatter.format(time));
            builder.append(" ");

            final JLabel descriptionLabel = new JLabel(builder.toString());
            descriptionLabel.setForeground(Color.gray);
            descriptionLabel.setFont(new Font("Dialog", Font.PLAIN, 11));

            // Add Title Label
            add(titleLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

            // Add Number Label
            final JLabel numberLabel = new JLabel();
            numberLabel.setFont(new Font("Dialog", Font.PLAIN, 11));
            numberLabel.setText(getNumber());
            add(numberLabel, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 0, 2), 0, 0));

            // Add call description
            add(descriptionLabel, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 2, 2), 0, 0));
        }

        public String getNumber() {
            return number;
        }
    }

    private void placeCall(MissedCall missedCall) {
        SoftPhoneManager.getInstance().getDefaultGuiManager().dial(missedCall.getNumber());
        SparkManager.getMainWindow().setVisible(true);
        SparkManager.getMainWindow().toFront();
    }

    public void actionPerformed(ActionEvent e) {
        final MissedCall missedCall = (MissedCall)list.getSelectedValue();

        if (e.getSource() == callBackButton) {
            placeCall(missedCall);
        }
        else {
            model.removeElement(missedCall);
        }
    }

    /**
     * Internal ListRenderer for MissedCallRenderer
     */
    private static class MissedCallRenderer extends JPanel implements ListCellRenderer {


        public Component getListCellRendererComponent(JList list,
                                                      Object value,
                                                      int index,
                                                      boolean isSelected,
                                                      boolean cellHasFocus) {
            MissedCall panel = (MissedCall)value;
            panel.setFocusable(false);

            if (isSelected) {
                panel.setForeground((Color)UIManager.get("List.selectionForeground"));
                panel.setBackground((Color)UIManager.get("List.selectionBackground"));
                panel.setBorder(BorderFactory.createLineBorder((Color)UIManager.get("List.selectionBorder")));
            }
            else {
                panel.setBackground(new Color(255, 224, 224));

                panel.setForeground(list.getForeground());
                panel.setBorder(BorderFactory.createLineBorder((Color)UIManager.get("List.background")));
            }

            list.setBackground((Color)UIManager.get("List.background"));


            return panel;
        }
    }

}

⌨️ 快捷键说明

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