📄 friendslistwindow.java
字号:
/*
* Created on Nov 11, 2005
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.GTADS.client;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import org.GTADS.protocol.*;
import org.GTADS.usermanager.TranslateManager;
import org.GTADS.messenger.*;
import org.GTADS.client.swing.*;
import java.io.*;
/**
* @author sday
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FriendsListWindow extends GTADSFrame implements ActionListener, MouseListener{
private static FriendsListWindow instance;
private TranslateManager locale = TranslateManager.getInstance();
private GTADSLabel friendsCaption = new GTADSLabel("Friends List");
private GTADSList friendsListDisplay = new GTADSList();
private GTADSScrollPane FriendsScroll;
private Vector friendsList = new Vector();
private GTADSButton addFriend = new GTADSButton("+ " + locale.printLocale("Friend"));
private GTADSButton deleteFriend = new GTADSButton("- " + locale.printLocale("Friend"));
public static FriendsListWindow getInstance() throws IOException{
synchronized(FriendsListWindow.class){
if (instance == null){
instance = new FriendsListWindow();
}
return instance;
}
}
public static void clearInstance(){
synchronized(FriendsListWindow.class){
if (instance != null && instance instanceof FriendsListWindow){
instance.dispose();
instance.hide();
instance = null;
}
}
}
public FriendsListWindow() throws IOException{
super("Friends List");
init();
}
private void init() throws IOException{
GTADSPanel mainContentPanel = new GTADSPanel();
GTADSPanel flowPanel1 = new GTADSPanel();
GTADSPanel flowPanel2 = new GTADSPanel();
GTADSPanel flowPanel3 = new GTADSPanel();
mainContentPanel.setLayout(new BoxLayout(mainContentPanel, BoxLayout.Y_AXIS));
flowPanel1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
flowPanel2.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
flowPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));
setUpComponents();
flowPanel1.add(friendsCaption);
flowPanel2.add(FriendsScroll);
flowPanel3.add(addFriend);
flowPanel3.add(Box.createHorizontalStrut(5));
flowPanel3.add(deleteFriend);
mainContentPanel.add(flowPanel1);
mainContentPanel.add(flowPanel2);
mainContentPanel.add(flowPanel3);
this.setSize(200,550);
setContentPane(mainContentPanel);
String username = DSChatClient.clientUser.getUsername();
MetaData getFriendsList = new MetaData(username,MetaData.FRIENDSLIST, "SEND_FRIENDSLIST");
MessageAdapter.sendData(null, DSChatClient.ClientSocket, getFriendsList, "NULL");
}
private void setUpComponents(){
friendsListDisplay.setVisibleRowCount(20);
friendsListDisplay.setPrototypeCellValue("####################");
FriendsScroll = new GTADSScrollPane(friendsListDisplay, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
FriendsScroll.setWheelScrollingEnabled(true);
FriendsScroll.setAutoscrolls(true);
friendsList = new Vector();
addFriend.addActionListener(this);
deleteFriend.addActionListener(this);
friendsListDisplay.addMouseListener(this);
}
public void actionPerformed(ActionEvent evt){
String result = new String();
String username = DSChatClient.clientUser.getUsername();
try {
if (evt.getSource().equals(addFriend)){
result = JOptionPane.showInputDialog(this, "Friend to add", "Add Buddy",
JOptionPane.OK_CANCEL_OPTION);
if (result != null && !result.equalsIgnoreCase("")){
addFriendToList(result);
}
}
else if (evt.getSource().equals(deleteFriend)){
result = JOptionPane.showInputDialog(this, "Friend to remove", "Remove Buddy",
JOptionPane.OK_CANCEL_OPTION);
if (result != null && !result.equalsIgnoreCase("")){
removeFriendFromList(result);
}
}
} catch (IOException ioe){
}
}
public void addFriendToList(String friend) throws IOException {
String username = DSChatClient.clientUser.getUsername();
MetaData sendAddFriend = new MetaData(username, MetaData.FRIENDSLIST, "ADD_FRIEND");
MessageAdapter.sendData(null, DSChatClient.ClientSocket, sendAddFriend, friend);
}
public void removeFriendFromList (String friend) throws IOException {
String username = DSChatClient.clientUser.getUsername();
MetaData sendRemoveFriend = new MetaData(username, MetaData.FRIENDSLIST, "REMOVE_FRIEND");
MessageAdapter.sendData(null, DSChatClient.ClientSocket, sendRemoveFriend, friend);
removeNameFromFriendsList(friend);
}
public void requestWholeListFromServer() throws IOException {
String username = DSChatClient.clientUser.getUsername();
MetaData getFriendsList = new MetaData(username,MetaData.FRIENDSLIST, "SEND_FRIENDSLIST");
MessageAdapter.sendData(null, DSChatClient.ClientSocket, getFriendsList, "NULL");
}
public void addNameToFriendsList(String newFriend) throws IOException {
if (!friendsList.contains(newFriend)) {
friendsList.addElement(newFriend);
refreshFriendsList();
}
}
public void removeNameFromFriendsList(String deadFriend) throws IOException{
if (friendsList.contains(deadFriend)) {
friendsList.removeElement(deadFriend);
refreshFriendsList();
}
}
private void refreshFriendsList () {
int p = friendsListDisplay.getSelectedIndex();
friendsListDisplay.setListData(friendsList);
friendsListDisplay.setSelectedIndex(p);
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() == friendsListDisplay){
if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1){
InstantMessageWindow.getInstance((String)friendsListDisplay.getSelectedValue()).show();
}
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
try {
FriendsListWindow.getInstance().show();
} catch (IOException ioe){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -