gg.msn.ui.panel.MainPanel.java Source code

Java tutorial

Introduction

Here is the source code for gg.msn.ui.panel.MainPanel.java

Source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on 26-ago-2009, 14.22.12
 */
package gg.msn.ui.panel;

import chatcommons.Client;
import gg.msn.facebook.core.FacebookUserList;
import gg.msn.facebook.core.FacebookUser;
import gg.msn.ui.ChatClientAboutBox;
import gg.msn.ui.ChatClientView;
import gg.msn.ui.facebook.form.OptionsDialog;
import gg.msn.ui.form.LicenseDialog;
import gg.msn.ui.listener.ChatClientViewListeners;
import gg.msn.ui.theme.ThemeManager;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.application.Action;

/**
 *
 * @author Luigi
 */
public class MainPanel extends javax.swing.JPanel {

    private ChatClientView ccv;
    private Log log = LogFactory.getLog(this.getClass());

    /** Creates new form MainPanel */
    public MainPanel(ChatClientView ccv) {
        initComponents();
        this.ccv = ccv;

        //trasparenza lista
        clientListScrollPane.getViewport().setOpaque(false);

        ChatClientViewListeners listeners = new ChatClientViewListeners(ccv);

        clientsList.addMouseListener(listeners.getClientsListRightClickListener());
        clientsList.setCellRenderer(new ClientsListCellRenderer());
        clientsList.setFixedCellHeight(30);
        //mainpanel
        //nickTable.addMouseListener(new nickTableOutClickListener());
        this.addMouseListener(listeners.getClientsListOutClickListener());
        jToolBar1.addMouseListener(listeners.getClientsListOutClickListener());

        insertIcons();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nickLabel = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        selectThemeButton = new javax.swing.JButton();
        chat = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        disconnect = new javax.swing.JButton();
        clientListScrollPane = new javax.swing.JScrollPane();
        clientsList = new javax.swing.JList();
        nickIcon = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
                .getInstance(gg.msn.ui.ChatClientApp.class).getContext().getResourceMap(MainPanel.class);
        nickLabel.setFont(resourceMap.getFont("nickLabel.font")); // NOI18N
        nickLabel.setText(resourceMap.getString("nickLabel.text")); // NOI18N
        nickLabel.setName("nickLabel"); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setBorderPainted(false);
        jToolBar1.setName("jToolBar1"); // NOI18N
        jToolBar1.setOpaque(false);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application
                .getInstance(gg.msn.ui.ChatClientApp.class).getContext().getActionMap(MainPanel.class, this);
        selectThemeButton.setAction(actionMap.get("showFacebookOptionsDialog")); // NOI18N
        selectThemeButton.setIcon(resourceMap.getIcon("selectThemeButton.icon")); // NOI18N
        selectThemeButton.setText(resourceMap.getString("selectThemeButton.text")); // NOI18N
        selectThemeButton.setToolTipText(resourceMap.getString("selectThemeButton.toolTipText")); // NOI18N
        selectThemeButton.setFocusable(false);
        selectThemeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        selectThemeButton.setName("selectThemeButton"); // NOI18N
        selectThemeButton.setOpaque(false);
        selectThemeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(selectThemeButton);

        chat.setAction(actionMap.get("addChatWithSelected")); // NOI18N
        chat.setIcon(resourceMap.getIcon("chat.icon")); // NOI18N
        chat.setToolTipText(resourceMap.getString("chat.toolTipText")); // NOI18N
        chat.setFocusable(false);
        chat.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        chat.setName("chat"); // NOI18N
        chat.setOpaque(false);
        chat.setPreferredSize(new java.awt.Dimension(59, 59));
        chat.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(chat);

        jButton1.setAction(actionMap.get("showAboutBox")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        disconnect.setAction(actionMap.get("disconnetti")); // NOI18N
        disconnect.setIcon(resourceMap.getIcon("disconnect.icon")); // NOI18N
        disconnect.setToolTipText(resourceMap.getString("disconnect.toolTipText")); // NOI18N
        disconnect.setFocusable(false);
        disconnect.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        disconnect.setName("disconnect"); // NOI18N
        disconnect.setOpaque(false);
        disconnect.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(disconnect);

        clientListScrollPane.setBackground(new Color(255, 255, 255, 100));
        clientListScrollPane.setBorder(null);
        clientListScrollPane
                .setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        clientListScrollPane.setName("clientListScrollPane"); // NOI18N
        clientListScrollPane.setOpaque(false);

        clientsList.setBackground(new Color(255, 255, 255, 100));
        clientsList.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
                resourceMap.getString("clientsList.border.title"), javax.swing.border.TitledBorder.CENTER,
                javax.swing.border.TitledBorder.DEFAULT_POSITION,
                resourceMap.getFont("clientsList.border.titleFont"))); // NOI18N
        clientsList.setModel(new DefaultListModel());
        clientsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        clientsList.setCellRenderer(new ClientsListCellRenderer());
        clientsList.setFocusable(false);
        clientsList.setInheritsPopupMenu(true);
        clientsList.setName("clientsList"); // NOI18N
        clientsList.setOpaque(false);
        clientsList.setSelectionBackground(new Color(204, 204, 204, 150));
        clientsList.setVisibleRowCount(1);
        clientListScrollPane.setViewportView(clientsList);

        nickIcon.setIcon(resourceMap.getIcon("nickIcon.icon")); // NOI18N
        nickIcon.setName("nickIcon"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(clientListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 294,
                                        Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        layout.createSequentialGroup().addComponent(nickIcon)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(nickLabel)))
                                .addContainerGap())
                        .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 140,
                                javax.swing.GroupLayout.PREFERRED_SIZE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(nickLabel).addComponent(nickIcon,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 48,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clientListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 270,
                                Short.MAX_VALUE)
                        .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents
     // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JButton chat;
    private javax.swing.JScrollPane clientListScrollPane;
    private javax.swing.JList clientsList;
    private javax.swing.JButton disconnect;
    private javax.swing.JButton jButton1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel nickIcon;
    private javax.swing.JLabel nickLabel;
    private javax.swing.JButton selectThemeButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g2d);
        g2d.setRenderingHints(
                new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));

        try {

            ImageIcon icon = ThemeManager.getTheme().get(ThemeManager.MAIN_BACKGROUND);

            if (icon != null) {
                Image image = icon.getImage();
                g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            } else {
                log.warn("image " + icon.getDescription() + " not Found");
            }

            icon = ThemeManager.getTheme().get(ThemeManager.MAIN_IMAGE);

            if (icon != null) {

                Image cartel = icon.getImage();
                g2d.drawImage(cartel, 0 - cartel.getWidth(this) / 10, getHeight() - cartel.getHeight(this), this);
            } else {
                log.warn("image " + icon.getDescription() + " not Found");
            }
        } catch (Exception e) {
            //            log.error(e);
        }
    }

    /**
     * Inserisce l'immagine dll'utente o l'icona User
     */
    public void insertIcons() {

        try {
            //imposto le icone
            ImageIcon userIcon = null;
            if (StringUtils.equals(ChatClientView.protocol, ChatClientView.FACEBOOK_PROTOCOL)) {
                userIcon = FacebookUserList.me.portrait;
                log.debug("user icon [ " + userIcon + " ]");
                nickIcon.setIcon(userIcon);
            } else {
                userIcon = ThemeManager.getTheme().get(ThemeManager.USER_ICON);
                nickIcon.setIcon(userIcon);
            }
        } catch (Exception e) {
            log.warn("nessuna icona trovata");
        }
    }

    public synchronized void updateListWithFACEBOOKContacts() {
        //fmod.removeAll();
        ((DefaultListModel) clientsList.getModel()).removeAllElements();
        log.debug("utenti presenti [" + FacebookUserList.buddies.size() + "]");
        Iterator<String> it = FacebookUserList.buddies.keySet().iterator();

        ArrayList<FacebookUser> offlineUsers = new ArrayList<FacebookUser>();
        DefaultListModel listModel = (DefaultListModel) clientsList.getModel();

        //prima inserisco nella lista gli elementi online
        //po tutti quelli offline
        while (it.hasNext()) {
            String key = it.next();
            log.debug("userID: " + key);
            FacebookUser fu = FacebookUserList.buddies.get(key);
            log.debug("status: " + fu.status);
            if (StringUtils.equals(fu.status, FacebookUser.STATUS_ONLINE)) {
                listModel.addElement(fu);
            } else {
                offlineUsers.add(fu);
            }

        }
        for (FacebookUser facebookUser : offlineUsers) {
            listModel.addElement(facebookUser);
        }
        //clientsList.repaint();
        clientsList.validate();
        clientsList.repaint();

    }

    public JList getClientsList() {
        return clientsList;
    }

    public void setClientsList(JList clientsList) {
        this.clientsList = clientsList;
    }

    public JLabel getNickLabel() {
        return nickLabel;
    }

    public void setNickLabel(JLabel nickLabel) {
        this.nickLabel = nickLabel;
    }

    /**
     * Disconnettte dal serever principale
     */
    @Action
    public void disconnetti() {
        ccv.getHelper().disconnetti();
    }

    @Action
    public void addChatWithSelected() {
        ccv.getHelper().startChatWithSelected();
    }

    @Action
    public void showFacebookOptionsDialog() {
        OptionsDialog optionsDialog = new OptionsDialog(ccv.getFrame(), true);
        optionsDialog.setLocationRelativeTo(this);
        optionsDialog.setVisible(true);
    }

    @Action
    public void showAboutBox() {
        ChatClientAboutBox chatClientAboutBox = new ChatClientAboutBox(ccv.getFrame());
        chatClientAboutBox.setLocationRelativeTo(this);
        chatClientAboutBox.setVisible(true);
    }
}

class ClientsListCellRenderer extends JPanel implements ListCellRenderer {
    public static final int ARC_SIZE = 5;

    public static final int IMAGE_LATE = 28;
    public static final String ONLINE_STATUS = "ONLINE";
    public static final int STATUS_ICON_OFFSET = 15;
    public static final int STATUS_ICON_WIDTH = 6;
    public static final int WHITE_SPACE = 3;
    Log log = LogFactory.getLog(this.getClass());
    Object value;
    JList list;

    public ClientsListCellRenderer() {
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        this.value = value;
        this.list = list;
        /*se il valore non  nullo impossto il renderig altrimenti lo lascio nascosto,
        questo impedisce che lo spazio bianco selweziona sempre l'utlimo elemento*/

        //                log.debug("Render lement : " + value);
        if (isSelected) {
            setOpaque(true);
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setOpaque(false);
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        // <editor-fold defaultstate="collapsed" desc="Old Code">
        /*
                    
                try {
                //render per utenti Facebook
                if (ChatClientView.protocol.equals(ChatClientView.FACEBOOK_PROTOCOL)) {
                final FacebookUser user = (FacebookUser) value;
                setText(user.name);
                ImageIcon icon = user.portrait;
                //log.debug("icon [" + icon + "]");
                ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
                setFont(list.getFont());
                setIcon(scaledIcon);
                //render per utenti Client
                } else {
                Client client = (Client) value;
                setText((client).getNick());
                ImageIcon icon = null;
                try {
                new ImageIcon(client.getImage());
                } catch (Exception e) {
                //                    log.debug("immgine non presente");
                icon = ThemeManager.getTheme().get(ThemeManager.USER_ICON);
                }
                ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
                setFont(list.getFont());
                setIcon(scaledIcon);
                }
                } catch (Exception e) {
                log.warn(e);
                }*/// </editor-fold>

        return this;

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        try {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            //render per utenti Facebook
            if (ChatClientView.protocol.equals(ChatClientView.FACEBOOK_PROTOCOL)) {
                final FacebookUser user = (FacebookUser) value;
                int textY = (getHeight() / 2) + (g2.getFont().getSize() / 2);

                //name string
                g2.setFont(list.getFont());
                g2.drawString(user.name, WHITE_SPACE + IMAGE_LATE + 3, textY);

                //status string
                //                g2.setFont(list.getFont());
                //                g2.drawString(user.status, WHITE_SPACE + IMAGE_LATE + 3, textY);

                //icon
                ImageIcon icon = user.portrait;
                //log.debug("icon [" + icon + "]");
                //ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
                g2.drawImage(icon.getImage(), WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE,
                        IMAGE_LATE, null);
                //                 g2.setColor(Color.WHITE);
                //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE, IMAGE_LATE, ARC_SIZE, ARC_SIZE);
                //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - ((IMAGE_LATE+1) / 2), IMAGE_LATE+1, IMAGE_LATE+1, ARC_SIZE, ARC_SIZE);
                //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - ((IMAGE_LATE+2) / 2), IMAGE_LATE+2, IMAGE_LATE+2, ARC_SIZE, ARC_SIZE);
                //render per utenti Client
                //                log.debug("user status [" + user.status + "]");
                //                log.debug("user online status [" + user.onlineStatus + "]");
                if (StringUtils.equals(user.status, FacebookUser.STATUS_ONLINE)) {
                    g2.setColor(Color.GREEN);
                    g2.fillOval(getWidth() - STATUS_ICON_OFFSET, (getHeight() / 2) - (STATUS_ICON_WIDTH / 2),
                            STATUS_ICON_WIDTH, STATUS_ICON_WIDTH);
                } else {
                    g2.setColor(Color.GRAY);
                    g2.fillOval(getWidth() - STATUS_ICON_OFFSET, (getHeight() / 2) - (STATUS_ICON_WIDTH / 2),
                            STATUS_ICON_WIDTH, STATUS_ICON_WIDTH);
                }

            } else {
                g2.setFont(list.getFont());
                int textY = (getHeight() / 2) + (g2.getFont().getSize() / 2);

                Client client = (Client) value;
                //                setText((client).getNick());
                ImageIcon icon = null;
                try {
                    new ImageIcon(client.getImage());
                } catch (Exception e) {
                    //                    log.debug("immgine non presente");
                    icon = ThemeManager.getTheme().get(ThemeManager.USER_ICON);
                }
                g2.drawString(client.getNick(), WHITE_SPACE + IMAGE_LATE + 3, textY);
                //log.debug("icon [" + icon + "]");
                //ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
                g2.drawImage(icon.getImage(), WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE,
                        IMAGE_LATE, null);

                //                setFont(list.getFont());
                //                setIcon(scaledIcon);
            }
        } catch (Exception e) {
            log.warn(e);
        }

    }
}