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

📄 chatwindowdialog.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on Jun 11, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.GTADS.client;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import org.GTADS.protocol.*;
import org.GTADS.usermanager.TranslateManager;
import org.GTADS.messenger.*;
import org.GTADS.client.swing.*;

import java.io.IOException;

import org.GTADS.client.preferences.PreferencesDialog;
import org.GTADS.client.preferences.PreferencesManager;
import org.GTADS.client.swing.GTADSFrame;
import org.GTADS.helper.*;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ChatWindowDialog extends GTADSFrame implements ActionListener, KeyListener, MouseListener {
	private TranslateManager locale = TranslateManager.getInstance();
	private GTADSPanel chatPanel = new GTADSPanel();
	private GTADSTabbedPanel tabbedChatFrame = new GTADSTabbedPanel();
	private GTADSPanel chatroom = new GTADSPanel();
	private GTADSScrollPane chatroomScroll = new GTADSScrollPane();
	private GTADSTextArea messageChatBox = new GTADSTextArea();
	private GTADSTextField inputChatBox = new GTADSTextField();
	private GTADSList usersList = new GTADSList();
	private Vector userListVector = new Vector();
	private GTADSButton joinGameButton = new GTADSButton("Join");
	private GTADSButton hostGameButton = new GTADSButton("Host");
	private GTADSButton sendTextButton = new GTADSButton("Send");
	private GTADSScrollPane usersListScroll = new GTADSScrollPane();
	private Hashtable messageBuffer = new Hashtable();
	private String username;
	private DashboardArea dashboard = new DashboardArea();
	
	private GTADSMenuBar gtadsMenuBar = new GTADSMenuBar();
	private GTADSMenu fileMenu;
	private GTADSMenu chatMenu;
	private GTADSMenu aboutMenu;
	private GTADSMenuItem showFriendsList = new GTADSMenuItem("Show FriendsList");
	private GTADSMenuItem editPreferences = new GTADSMenuItem(locale.printLocale("Preferences") + "...");
	private GTADSMenuItem quitProgram = new GTADSMenuItem("Quit");
	private GTADSMenuItem joinChatroom = new GTADSMenuItem(locale.printLocale("Join Chatroom") + "...");
	private GTADSMenuItem quitChatroom = new GTADSMenuItem("Part Chatroom");
	private GTADSMenuItem openChatroomList = new GTADSMenuItem("Get Chatroom List");
	private GTADSMenuItem aboutWindow = new GTADSMenuItem(locale.printLocale("About") + "...");
	private GTADSMenuItem sendIM;
	
	private JLabel chatroomLabel = new JLabel("");
	private static ChatWindowDialog instance;
	String chatroomName = new String(PreferencesManager.getInstance().getDefaultChatroom());
	
	public static ChatWindowDialog getInstance() {
		if (instance instanceof ChatWindowDialog) {
			return instance;
		}
		else {
			return null;
		}	
	}
	
	public static void createInstance(String username) throws IOException{
		instance = new ChatWindowDialog(username);
	}
	
	public static void destroyInstance(){
		instance.dispose();
		instance = null;
	}
	
	ChatWindowDialog (String username) throws IOException{
		super(TranslateManager.getInstance().printLocale("GTA Multiplayer Client") + ": " + DSChatClient.ClientSocket.getInetAddress() + ":" 
				+ DSChatClient.ClientSocket.getPort());
		this.username = username;
		init();
	}
		
	private void init(){
		chatPanel.setLayout(new BoxLayout(chatPanel, BoxLayout.Y_AXIS));
		this.setResizable(false);
		
		if (!DSChatClient.isApplet) {
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
		}
		
		tabbedChatFrame.addTab("Main Chat", chatroom);
		setUpChatBoxes();
		setUpMenuItems();
		tabbedChatFrame.setSize(chatPanel.getWidth(), chatPanel.getHeight());
		//usersList = new JList(userListVector);
		usersList.addMouseListener(this);
		
		chatroom.setBackground(Color.darkGray);
		GTADSPanel Panel1 = new GTADSPanel(new FlowLayout(FlowLayout.LEFT));
		GTADSPanel Panel2 = new GTADSPanel(new FlowLayout(FlowLayout.LEFT));
		GTADSPanel Panel3 = new GTADSPanel(new FlowLayout(FlowLayout.LEFT));
		
		Panel2.setAlignmentX(Box.CENTER_ALIGNMENT);
		
		sendTextButton.addActionListener(this);
		messageChatBox.setAutoscrolls(true);
		messageChatBox.setWrapStyleWord(true);
		inputChatBox.setColumns(messageChatBox.getColumns());
		inputChatBox.addKeyListener(this);
		
		Panel1.add(chatroomLabel);
		Panel2.add(tabbedChatFrame);
		Panel2.add(usersListScroll);
		Panel3.add(inputChatBox);
		Panel3.add(Box.createHorizontalStrut(60));
		Panel3.add(sendTextButton);
		
		chatPanel.add(dashboard);
		chatPanel.add(Panel1);
		chatPanel.add(Panel2);
		chatPanel.add(Panel3);
		
		setContentPane(chatPanel);
		this.setJMenuBar(gtadsMenuBar);
		
		if (DSChatClient.isApplet){
			this.addWindowListener(new WindowAdapter() {
				public void windowClosed(WindowEvent e){
					try {
						DSChatClient.ClientSocket.close();
					} catch (IOException ioe) {}
				}
			});
		}
		
		pack();
		show();
		
		/*
		chatPanel.setLayout(null);
		this.setSize(1000,600);
		if (!DSChatClient.isApplet) {
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		
		tabbedChatFrame.addTab("Main Chat", chatroom);
		tabbedChatFrame.setBounds(new Rectangle(160,100,600,300));
		setUpChatBoxes();
		setUpMenuItems();
		usersList = new JList(userListVector);
		usersList.addMouseListener(this);
		usersList.setBounds(new Rectangle (780, 120, 150, 277));
		inputChatBox.setBounds(new Rectangle(163,410,597,20));
		sendTextButton.setBounds(new Rectangle(780,410,150, 20));
		
//		inputChatBox.setLineWrap(false);
		
		sendTextButton.addActionListener(this);
		messageChatBox.setAutoscrolls(true);
		messageChatBox.setWrapStyleWord(true);
		inputChatBox.setColumns(50);
		inputChatBox.addKeyListener(this);
		chatPanel.add(inputChatBox);
		chatPanel.add(sendTextButton);
		chatPanel.add(tabbedChatFrame);
		chatPanel.add(usersList);
		
		this.setJMenuBar(gtadsMenuBar);
		
		if (DSChatClient.isApplet){
			this.addWindowListener(new WindowAdapter() {
				public void windowClosed(WindowEvent e){
					try {
						DSChatClient.ClientSocket.close();
					} catch (IOException ioe) {}
				}
			});
		}
		
		setContentPane(chatPanel);
		show();
		*/
		
		//MetaData joinChatroomHeader = new MetaData(username, MetaData.GENERIC, MetaData.JOIN_CHATOOM);
		//inputChatBox.setText("/join GTADS");
		//processCommand();
		try {
			MessageAdapter.joinChatroom(chatroomName);
			tabbedChatFrame.setTitleAt(0, chatroomName);
		} catch (IOException ioe) {
			DialogManager.ErrorDialog("Could not Join " + chatroomName);
		}
	}
	
	public void appendToChatroom(int msgType, String newText) {
		if (msgType == 0)
			newText = "<System> " + newText + "\n";
		else if (msgType == 1)
			newText = " *" + newText + " has joined chatroom\n";
		else if (msgType == 2)
			newText = " *" + newText + " has left chatroom\n";
		else
			newText += "\n";
		
		if (PreferencesManager.getInstance().getTimeStamps()){
			newText = "[" + Helper.getDate(false) + "] " + newText;
		}
		
		messageChatBox.setText(messageChatBox.getText() + newText);
		int scrollbarValue = chatroomScroll.getVerticalScrollBar().getValue();
		int scrollbarMax = chatroomScroll.getVerticalScrollBar().getMaximum();
		//chatroomScroll.getVerticalScrollBar()
		
		int end = messageChatBox.getText().length();
		messageChatBox.select(end,end);

		messageChatBox.repaint();
	}
	
	private void setUpChatBoxes(){
		messageChatBox.setEditable(false);
		messageChatBox.setLineWrap(true);
		chatroom.setLayout(new FlowLayout());
		chatroomScroll = new GTADSScrollPane(messageChatBox, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		chatroom.removeAll();
		chatroom.add(chatroomScroll);
		messageChatBox.setRows(20);
		messageChatBox.setColumns(60);
		chatroomScroll.setAutoscrolls(true);
		
		usersListScroll = new GTADSScrollPane(this.usersList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		usersListScroll.setAutoscrolls(true);
		usersList.setVisibleRowCount(15);
		usersList.setPrototypeCellValue("##################");
	}
	
	private void setUpMenuItems(){
		gtadsMenuBar = new GTADSMenuBar();
		
		fileMenu = new GTADSMenu("File");
		chatMenu = new GTADSMenu("Chat");
		aboutMenu = new GTADSMenu("Help");
		
		showFriendsList = new GTADSMenuItem("Show FriendsList");
		quitProgram = new GTADSMenuItem("Quit");
		sendIM = new GTADSMenuItem(locale.printLocale("Send IM") + "...");
		
		gtadsMenuBar.add(fileMenu);
		gtadsMenuBar.add(chatMenu);
		gtadsMenuBar.add(aboutMenu);
		fileMenu.add(showFriendsList);
		fileMenu.add(editPreferences);
		fileMenu.add(quitProgram);
		chatMenu.add(joinChatroom);
		chatMenu.add(quitChatroom);
		chatMenu.add(openChatroomList);
		chatMenu.add(sendIM);
		aboutMenu.add(aboutWindow);
		
		showFriendsList.addActionListener(this);
		editPreferences.addActionListener(this);
		quitProgram.addActionListener(this);
		joinChatroom.addActionListener(this);
		openChatroomList.addActionListener(this);
		quitChatroom.addActionListener(this);
		sendIM.addActionListener(this);
		aboutWindow.addActionListener(this);
	}
	
	public void addUserToList(String userName){
		if (userListVector.contains(userName))
			return;
		userListVector.add(userName);
		usersList.setListData(userListVector);
	}
	
	public void removeUserFromList(String userName){
		if (userListVector.contains(userName)){
			userListVector.remove(userName);
			usersList.setListData(userListVector);
		}
	}
	
	public void actionPerformed (ActionEvent evt){
		if (evt.getSource() == sendTextButton) {
			sendChatToRoom();
		}
		else if (evt.getSource().equals(showFriendsList)){
			try {
			FriendsListWindow.getInstance().show();
			} catch (IOException ioe) {}
		}
		else if (evt.getSource().equals(editPreferences)){
			PreferencesDialog.getInstance().show();
		}
		else if (evt.getSource().equals(quitProgram)){
			try {
				MessageAdapter.quitServer(chatroomName);
			}catch (IOException ioe){
			}
		}
		else if (evt.getSource().equals(joinChatroom)){
			try {
				String newChatroom = JOptionPane.showInputDialog(this, locale.printLocale("Chatroom Name"), locale.printLocale("Join Chatroom"), 
						JOptionPane.OK_CANCEL_OPTION);
				if (newChatroom != null && newChatroom != ""){
					joinChatroom(newChatroom, chatroomName);
				}

⌨️ 快捷键说明

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