UserTalkView.java :  » App » learnandroid » com » lgnortel » network » networkview » Android Open Source

Android Open Source » App » learnandroid 
learnandroid » com » lgnortel » network » networkview » UserTalkView.java
package com.lgnortel.network.networkview;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

import com.lgnortel.lib.logger.LoggerUtil;
import com.lgnortel.platform.db.DBDataDesc;
import com.lgnortel.platform.db.DBManager;
import com.lgnortel.platform.db.DBUserIF;
import com.lgnortel.platform.model.LabelMenuMgr;
import com.lgnortel.platform.model.NetworkElement;
import com.lgnortel.platform.model.NetworkMgr;
import com.lgnortel.r3.r3equipment.r3shelf.ShelfViewController;
import com.lgnortel.rmi.common.ClientManager;
import com.lgnortel.rmi.common.CommonIF;
import com.lgnortel.rmi.common.EmsCode;

/**
 * Copyright (c) 2008 LG-Nortel, Inc. All Rights Reserved.
 * 
 * CONFIDENTIALITY AND LIMITED USE: This software, including any software of <br>
 * third parties embodied herein, contains code, information, data and concepts <br>
 * which are confidential and/or proprietary to LG-Nortel and such third <br>
 * parties. This software is licensed for use solely in accordance with the <br>
 * terms and conditions of the applicable license agreement with LG-Nortel or <br>
 * its authorized distributor, and not for any other use or purpose. No <br>
 * redistribution of this software by any party is permitted. <br>
 * 
 * Title: CommonTableCellRenderer.java<br>
 * Description: <br>
 * Copyright: Copyright(c) 2008 LG-NORTEL ALL Rights Reserved<br>
 * Company: LG-Nortel<br>
 * 
 * Title: UserTalkView.java<br>
 * Description: <br>
 * Copyright: Copyright(c) 2008 LG-NORTEL ALL Rights Reserved<br>
 * Company: LG-Nortel<br>
 * 
 * @author JungGu Lee
 * @version 0.1
 * @created 2009. 1. 21.
 * @modified 2009. 1. 21.
 * @product EFA R4.0 EMS
 * @sw_block
 */

public class UserTalkView extends JFrame {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  // Log4J
  private Logger logger = LoggerUtil.getInstance().getLogger(this.getClass().getName());

  private JPanel pnlContent = null;
  private JPanel pnlInfo = null;
  private JPanel pnlLeft = null;
  private JPanel pnlRight = null;
  private JPanel pnlMessage;

  private JTextField tfMessage = null;

  private JButton btnRefresh = null;
  private JButton btnClose = null;
  private JButton btnHelp = null;
  private JButton btnSend;

  private JScrollPane spnUserList = null;
  private JScrollPane spnMessenger = null;

  private JTree tree = new JTree();
  private DefaultMutableTreeNode root = new DefaultMutableTreeNode("ALL");
  private DefaultTreeModel treeModel;

  private JTextArea taData = null;
  public static String[] userName = null;

  HelpView helpView = null;
  private String sTitle = null;
  int viewId;

  LabelMenuMgr labelMenuMgr;
  DBManager dbMgr;
  NetworkMgr networkMgr;

  private ImageIcon btnRefreshImg = new ImageIcon(getClass().getClassLoader().getResource("image/btn_refresh_user_list.gif"));
  private ImageIcon btnRefreshImgOver = new ImageIcon(getClass().getClassLoader().getResource("image/btn_refresh_user_list_over.gif"));

  private ImageIcon btnSendImg = new ImageIcon(getClass().getClassLoader().getResource("image/btn_send.gif"));
  private ImageIcon btnSendImgOver = new ImageIcon(getClass().getClassLoader().getResource("image/btn_send_over.gif"));

  public ImageIcon closeImg, closeImgRoll;

  private static UserTalkView userTalkView = null;

  /**
   * Singleton Pattern
   * @return
   */
  public static UserTalkView getInstance() {

    if (userTalkView == null) {
      userTalkView = new UserTalkView();
    }

    return userTalkView;
  }

  /**
   * Constructor of this class
   */
  private UserTalkView() {
    try {
      labelMenuMgr = LabelMenuMgr.getInstance();
      dbMgr = DBManager.getInstance();
      networkMgr = NetworkMgr.getInstance();

      this.sTitle = labelMenuMgr.getMenu("USER_MESSENGER");
      closeImg = new ImageIcon(getClass().getClassLoader().getResource("image/btn_close_normal.gif"));
      closeImgRoll = new ImageIcon(getClass().getClassLoader().getResource("image/btn_close_rollover.gif"));

      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * command request to getting the data
   */
  public void requestInitialCmd() {
    getUser();
  }

  /**
   * set ViewId
   * @param lViewId
   */
  public void setViewId(int lViewId) {
    this.viewId = lViewId;
  }

  /**
   * get ViewId
   * @return
   */
  public int getViewId() {
    return viewId;
  }

  /**
   * Initialization of GUI Component
   * @throws Exception
   */
  private void jbInit() throws Exception {
    refreshTreePanel();

    btnRefresh = new JButton();
    btnRefresh.setFont(new java.awt.Font("Dialog", 0, 12));
    btnRefresh.setPreferredSize(new Dimension(120, 25));
    btnRefresh.setMinimumSize(new Dimension(120, 25));
    btnRefresh.setMaximumSize(new Dimension(120, 25));
    btnRefresh.setIcon(btnRefreshImg);
    btnRefresh.setRolloverIcon(btnRefreshImgOver);
    btnRefresh.addActionListener(eventHandler);

    btnClose = new JButton();
    btnClose.setPreferredSize(new Dimension(72, 25));
    btnClose.setMinimumSize(new Dimension(72, 25));
    btnClose.setMaximumSize(new Dimension(72, 25));
    btnClose.setIcon(closeImg);
    btnClose.setRolloverIcon(closeImgRoll);
    btnClose.addActionListener(eventHandler);

    ImageIcon helpIcon = new ImageIcon(getClass().getClassLoader().getResource("image/helpButton.gif"));
    btnHelp = new JButton(null, helpIcon);
    btnHelp.setFont(new java.awt.Font("Dialog", 0, 12));
    btnHelp.setPreferredSize(new Dimension(25, 25));
    btnHelp.setMinimumSize(new Dimension(25, 25));
    btnHelp.setMaximumSize(new Dimension(25, 25));
    btnHelp.addActionListener(eventHandler);

    btnSend = new JButton();
    btnSend.setFont(new java.awt.Font("Dialog", 0, 12));
    btnSend.setPreferredSize(new Dimension(80, 25));
    btnSend.setMinimumSize(new Dimension(80, 25));
    btnSend.setMaximumSize(new Dimension(80, 25));
    btnSend.setIcon(btnSendImg);
    btnSend.setRolloverIcon(btnSendImgOver);
    btnSend.addActionListener(eventHandler);

    tfMessage = new JTextField();
    tfMessage.setPreferredSize(new Dimension(162, 25));
    tfMessage.setMinimumSize(new Dimension(162, 25));
    tfMessage.setMaximumSize(new Dimension(162, 25));
    tfMessage.addActionListener(eventHandler);
    tfMessage.setBackground(new Color(218, 234, 242));
    tfMessage.requestFocus();

    spnUserList = new JScrollPane();
    spnUserList.setViewportView(tree);

    taData = new JTextArea();
    taData.setEditable(false);
    taData.setFont(new Font("Dialog", 0, 12));
    taData.setBackground(new Color(218, 234, 242));
    taData.setAutoscrolls(true);

    spnMessenger = new JScrollPane();
    spnMessenger.setViewportView(taData);

    User user = User.getInstance();
    pnlMessage = new JPanel();
    pnlMessage.setLayout(new GridBagLayout());
    pnlMessage.setBorder(new TitledBorder(user.getUserName() + " says: "));
    pnlMessage.add(tfMessage, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0));
    pnlMessage.add(btnSend, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
    pnlMessage.add(btnHelp, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));

    pnlLeft = new JPanel();
    pnlLeft.setLayout(new GridBagLayout());
    pnlLeft.setBorder(new TitledBorder(" Message "));
    pnlLeft.add(spnMessenger, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
    pnlLeft.add(pnlMessage, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0));

    pnlRight = new JPanel();
    pnlRight.setLayout(new GridBagLayout());
    pnlRight.setBorder(new TitledBorder(" User List "));
    pnlRight.add(spnUserList, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
    pnlRight.add(btnRefresh, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0));

    pnlInfo = new JPanel();
    pnlInfo.setLayout(new GridBagLayout());
    pnlInfo.add(pnlLeft, new GridBagConstraints(1, 1, 1, 1, 0.8, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
    pnlInfo.add(pnlRight, new GridBagConstraints(2, 1, 1, 1, 0.2, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));

    pnlContent = new JPanel();
    pnlContent.setLayout(new GridBagLayout());
    pnlContent.add(pnlInfo, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
    pnlContent.add(btnClose, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(5, 1, 1, 1), 0, 0));

    initialize();
  }

  /**
   * command request to getting the data
   */
  public void getUser() {

    ClientManager clientMgr = ClientManager.getInstance();
    User user = User.getInstance();

    int neId = NetworkElement.ROOT_NODEID;
    int shelfId = DBDataDesc.SHELF_OLT1;

    CommonIF.RtrvSessionIF[] data = new CommonIF.RtrvSessionIF[1];
    data[0] = new CommonIF.RtrvSessionIF();

    String cmdStr = new String("");

    clientMgr.cmdRequest(getViewId(), user.getUserId(), neId, shelfId, EmsCode.CMD_RTRV_SESSION, data, cmdStr);
    btnRefresh.setEnabled(false);
  }

  /**
   * command request to getting the data
   */
  public boolean sendMessage(String message) {

    ClientManager clientMgr = ClientManager.getInstance();
    User user = User.getInstance();

    int neId = NetworkElement.ROOT_NODEID;
    int shelfId = DBDataDesc.SHELF_OLT1;

    TreePath treePath = tree.getSelectionPath();

    if (treePath == null) {
      Object[] options = new Object[1];
      options[0] = new String(labelMenuMgr.getLabel("CONFIRM"));

      JOptionPane.showOptionDialog(this, "Choose User who you want to send message !!", "CMD-SEND-MESSAGE", JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
      return false;
    }

    DefaultMutableTreeNode targetUser = (DefaultMutableTreeNode) (treePath.getLastPathComponent());
    DBManager dbManager = DBManager.getInstance();

    CommonIF.SendMessageIF[] data = new CommonIF.SendMessageIF[1];
    data[0] = new CommonIF.SendMessageIF();

    data[0].sendUserId = user.getUserId();

    if (targetUser.toString().equals("ALL")) {
      data[0].receiveUserId = 0;
    } else {
      data[0].receiveUserId = dbManager.getUserId(targetUser.toString());
    }

    data[0].message = message;

    String cmdStr = new String("");

    clientMgr.cmdRequest(getViewId(), user.getUserId(), neId, shelfId, EmsCode.CMD_TALK_USER, data, cmdStr);
    return true;
  }

  /**
   * command response from server
   * @param viewId
   * @param userId
   * @param nodeId
   * @param cmdCode
   * @param data
   * @param result
   * @return
   */
  public boolean cmdResponse(int viewId, int userId, int nodeId, int cmdCode, Object[] data, int result) {
    if (cmdCode == EmsCode.CMD_RTRV_SESSION) {
      refreshUser((CommonIF.RtrvSessionIF[]) data);
      btnRefresh.setEnabled(true);
    }
    return true;
  }

  /**
   * refresh user
   * @param data
   */
  public void refreshUser(CommonIF.RtrvSessionIF[] data) {

    userName = new String[data.length];

    for (int i = 0; i < data.length; i++) {
      userName[i] = new String(data[i].userName);
    }
    refreshTreePanel();
  }

  /**
   * event response
   * @param data
   */
  public void eventResponse(CommonIF.SendMessageIF[] data) {
    DBManager dbManager = DBManager.getInstance();
    String sendUserName = dbManager.getUserName(data[0].sendUserId);

    User user = User.getInstance();

    if (user.getUserId() != data[0].sendUserId) {
      taData.append("  " + sendUserName + " says:\n    " + data[0].message + "\n\n");
    }
    tfMessage.requestFocus();
  }

  /**
   * refresh tree panel
   */
  public void refreshTreePanel() {

    root.removeAllChildren();

    root = new DefaultMutableTreeNode("ALL");
    treeModel = new DefaultTreeModel(root);

    tree.setModel(treeModel);
    tree.setBounds(new Rectangle(0, 0, 196, 443));
    tree.setLayout(null);
    tree.setBackground(Color.white);

    tree.setCellRenderer(new UserIconRenderer());

    addAllUsers();

    TreePath treePath = tree.getPathForRow(0);
    tree.fireTreeExpanded(treePath);
  }

  /**
   * add all users
   */
  private void addAllUsers() {
    DBManager dbMgr = DBManager.getInstance();
    int cnt = dbMgr.getUserCnt();
    DBUserIF[] userData = dbMgr.getUser(cnt);

    if (userData != null) {
      for (int i = 0; i < userData.length; i++) {
        if ("nbi_agent".equals(userData[i].userName)) {
          continue;
        }
        DefaultMutableTreeNode user = new DefaultMutableTreeNode(userData[i].userName);
        root.add(user);
      }
    }
  }

  /**
   * event handler
   * action with event occurred location.
   */
  IvjEventHandler eventHandler = new IvjEventHandler();
  class IvjEventHandler implements java.awt.event.ActionListener {
    public void actionPerformed(ActionEvent e) {

      java.lang.Object src = e.getSource();

      if (src == btnRefresh) {
        getUser();
      } else if (src == btnClose) {
        close();
      } else if (src == btnHelp) {
        help();
      } else if (src == tfMessage || btnSend == src) {
        User user = User.getInstance();
        String currentUserName = user.getUserName();
        String sendMessage = tfMessage.getText();
        if (sendMessage(sendMessage)) {
          taData.append("  " + currentUserName + " says:\n     " + sendMessage + "\n\n");
          tfMessage.setText("");
          tfMessage.requestFocus();
        }
      }
    }
  }

  /**
   * when window closing event is occured
   * call the close operation to dispose
   */
  private void close() {
    MainMenuBar mainMenuBar = MainMenuBar.getInstance();
    mainMenuBar.removeCommand(viewId);
    setVisible(false);
  }

  /**
   * when window closing event is occured
   * call the close operation to dispose
   */
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      close();
    }
  }

  /**
   * window size & title initialization
   */
  private void initialize() {
    try {
      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      setSize(700, 700);
      setLocation(0, 0);
      setResizable(false);
      setContentPane(pnlContent);
      setTitle(sTitle);
    } catch (java.lang.Throwable ivjExc) {
      logger.log(Level.SEVERE, "Exception", ivjExc);
    }
  }

  /**
   * Help View dialogue is opened which is related to help dialog number
   */
  private void help() {
    helpView = HelpView.getInstance(ShelfViewController.DIALOG_USER_MESSENGER);
    helpView.setVisible(true);
  }
}

/**
 * 
 * Title: UserTalkView.java<br>
 * Description: <br>
 * Copyright: Copyright(c) 2008 LG-NORTEL ALL Rights Reserved<br>
 * Company: LG-Nortel<br>
 * 
 * @author JungGu Lee
 * @version 0.1
 * @created 2009. 2. 6.
 * @modified 2009. 2. 6.
 * @product EFA R4.0 EMS
 * @sw_block
 */
class UserIconRenderer extends JLabel implements TreeCellRenderer {

  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private ImageIcon IconNormal = new ImageIcon(getClass().getClassLoader().getResource("image/msnNormal.gif"));
  private ImageIcon IconOut = new ImageIcon(getClass().getClassLoader().getResource("image/msnOut.gif"));

  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

    setText(value.toString());

    setForeground(selected ? Color.pink : Color.black);

    if (isLoginUserName(value.toString())) {
      super.setIcon(IconNormal);
    } else {
      super.setIcon(IconOut);
    }
    return this;
  }

  public boolean isLoginUserName(String myName) {

    if (UserTalkView.userName == null)
      return false;

    if (myName.equals("ALL"))
      return true;

    for (int i = 0; i < UserTalkView.userName.length; i++) {
      if (myName.equals(UserTalkView.userName[i]))
        return true;
    }

    return false;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.