📄 jchatpanel.java
字号:
*/
public void changeMainJChatRoom(String roomName) {
tabbedPane.setTitleAt(0, roomName);
JChatRoom jchatRoom = (JChatRoom) tabbedPane.getComponentAt(0);
jchatRoom.removeAllPlayers();
}
/** To get a JChatRoom.
*
* @param primaryKey primary key of JChatRoom we want to get
*/
public JChatRoom getJChatRoom(String primaryKey) {
for (int i=0; i<tabbedPane.getTabCount();i++) {
if ( tabbedPane.getComponentAt(i).getName().equals(primaryKey) ) {
if (DataManager.SHOW_DEBUG)
System.out.println("getJChatRoom");
return (JChatRoom) tabbedPane.getComponentAt(i);
}
}
if (DataManager.SHOW_DEBUG)
System.out.println("ERROR : Couldn't getJChatRoom");
return null;
}
/** To get current JChatRoom.
*/
public JChatRoom getCurrentJChatRoom() {
return getJChatRoom(currentPrimaryKey);
}
/** To set the current active window.
*
* @param primaryKey primary key of current ChatRoom
*
public boolean setCurrentJChatRoom(String primaryKey) {
this.currentPrimaryKey = primaryKey;
for (int i=0; i<tabbedPane.getTabCount();i++) {
if ( tabbedPane.getComponentAt(i).getName().equals(primaryKey) ) {
tabbedPane.setEnabledAt(i, true);
tabbedPane.setSelectedIndex(i);
return true;
}
}
System.out.println("ERROR : Couldn't setCurrentJChatRoom");
return false;
}
*/
/*------------------------------------------------------------------------------------*/
/** To add a player to a JChatRoom.
*
* @param primaryKey primary key of ChatRoom to modify
* @param player player to add
*/
public boolean addPlayer(String primaryKey, PlayerImpl player) {
for (int i=0; i<tabbedPane.getTabCount();i++) {
if ( tabbedPane.getComponentAt(i).getName().equals(primaryKey) ) {
JChatRoom jchatRoom = (JChatRoom) tabbedPane.getComponentAt(i);
jchatRoom.addPlayer(player.getPrimaryKey(), player.getFullPlayerName());
return true;
}
}
if (DataManager.SHOW_DEBUG)
System.out.println("ERROR : Couldn't addPlayer");
return false;
}
/** To remove a player from a ChatRoom.
*
* @param primaryKey primary key of ChatRoom to modify
* @param player player to remove
*/
public boolean removePlayer(String primaryKey, PlayerImpl player) {
for (int i=0; i<tabbedPane.getTabCount();i++) {
if ( tabbedPane.getComponentAt(i).getName().equals(primaryKey) ) {
JChatRoom jchatRoom = (JChatRoom) tabbedPane.getComponentAt(i);
jchatRoom.removePlayer(player.getPrimaryKey());
return true;
}
}
if (DataManager.SHOW_DEBUG)
System.out.println("ERROR : Couldn't removePlayer");
return false;
}
/** To remove a player from all the ChatRooms.
*
* @param primaryKey primary key of Player to remove
*/
public void removePlayerFromAllchatRooms(String primaryKey) {
for (int i=0; i<tabbedPane.getTabCount();i++) {
JChatRoom jchatRoom = (JChatRoom) tabbedPane.getComponentAt(i);
jchatRoom.removePlayer(primaryKey);
}
}
/** To get the list of players of a ChatRoom
*
* @param primaryKey primary key of the ChatRoom
*/
public Hashtable getPlayers(String primaryKey) {
for (int i=0; i<tabbedPane.getTabCount();i++) {
if ( tabbedPane.getComponentAt(i).getName().equals(primaryKey) ) {
JChatRoom jchatRoom = (JChatRoom) tabbedPane.getComponentAt(i);
return jchatRoom.getPlayers();
}
}
if (DataManager.SHOW_DEBUG)
System.out.println("ERROR : Couldn't get players");
return null;
}
/*------------------------------------------------------------------------------------*/
/** action when the user wants to send a message
*/
private void okAction() {
String message;
message = inputBox.getText();
// I - We control the length of the message the user wants to send.
if(message.length()==0)
return;
messageHistory.add(message);
// We get the MacroPlugIn and process the chat message with it.
MacroPlugIn macroPlugIn = (MacroPlugIn) ClientDirector.getDataManager().getClientScreen().getPlayerPanel().getPlugIn("Macro");
if(macroPlugIn!=null)
message = macroPlugIn.processMacros( message );
if(message.length()>ChatRoom.MAXIMUM_MESSAGE_SIZE)
message = message.substring(0,ChatRoom.MAXIMUM_MESSAGE_SIZE-4) +"...";
// II - Any Shortcuts ?
if (message.startsWith("/whisper")) {
chatVoiceLevel.setValue(ChatRoom.WHISPERING_VOICE_LEVEL);
message = message.substring(8);
inputBox.setText(message);
if(message.length()==0) return;
}
else if (message.startsWith("/shout")) {
chatVoiceLevel.setValue(ChatRoom.SHOUTING_VOICE_LEVEL);
message = message.substring(6);
inputBox.setText(message);
if(message.length()==0) return;
}
// III - We send the message
DataManager dManager = ClientDirector.getDataManager();
dManager.sendMessage( new SendTextMessage( dManager.getMyPlayer().getPrimaryKey(),
dManager.getMyPlayer().getPlayerName(),
getMyCurrentChatPrimaryKey(),
message,
(byte)chatVoiceLevel.getValue() ));
// IV - entry reset
inputBox.setText("");
chatVoiceLevel.setValue(ChatRoom.NORMAL_VOICE_LEVEL);
}
/*------------------------------------------------------------------------------------*/
/** To auto complete the name of a player
*/
private String nameCompletion() {
if (e==null) {
input = inputBox.getText();
if (input.length()==0)
return input;
int lastIndex = input.lastIndexOf(' ');
autoName = input.substring(lastIndex+1, input.length());
input = input.substring(0, lastIndex+1);
e = getPlayers(currentPrimaryKey).elements();
}
String playerKey;
for (; e.hasMoreElements() ;) {
playerKey = ((PlayerState) e.nextElement()).fullName;
if ( playerKey.startsWith(autoName)) {
// player found
return input + playerKey;
}
}
// no player found
e = getPlayers(currentPrimaryKey).elements();
return input + autoName;
}
/*------------------------------------------------------------------------------------*/
/** ActionListener Implementation **/
/** Called when an action is performed.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
// 0 - Command Control
if (actionCommand == null)
return;
if (DataManager.SHOW_DEBUG)
System.out.println("Action command : " + actionCommand);
DataManager dataManager = ClientDirector.getDataManager();
PlayerImpl myPlayer = dataManager.getMyPlayer();
if( !myPlayer.getLocation().isRoom() && actionCommand.equals("createChatRoom")) {
JOptionPane.showMessageDialog(null, "Sorry, but you can not create/leave chat channels\n"
+"on World/Town Maps.", "INFORMATION", JOptionPane.INFORMATION_MESSAGE);
return;
}
// 1 - Get Button
if (actionCommand.equals("createChatRoom")) {
WotlasLocation chatRoomLocation = myPlayer.getLocation();
String chatRoomName = JOptionPane.showInputDialog("Please enter a Name:");
if( chatRoomName==null || chatRoomName.length()==0 )
return;
if (tabbedPane.getTabCount()>=ChatRoom.MAXIMUM_CHATROOMS_PER_ROOM-1)
b_createChatRoom.setEnabled(false);
else
b_createChatRoom.setEnabled(true);
myPlayer.sendMessage( new ChatRoomCreationMessage( chatRoomName,
myPlayer.getPrimaryKey() ) );
}
else if (actionCommand.equals("leaveChatRoom")) {
//removeCurrentChatRoom();
// Sending Message
if( !currentPrimaryKey.equals( ChatRoom.DEFAULT_CHAT ) )
myPlayer.sendMessage( new RemPlayerFromChatRoomMessage( myPlayer.getPrimaryKey(),
currentPrimaryKey) );
}
else if (actionCommand.equals("helpChat")) {
DataManager dManager = ClientDirector.getDataManager();
dManager.sendMessage( new SendTextMessage( dManager.getMyPlayer().getPrimaryKey(),
dManager.getMyPlayer().getPlayerName(),
getMyCurrentChatPrimaryKey(),
"/help",
ChatRoom.NORMAL_VOICE_LEVEL ));
}
else if (actionCommand.equals("imageChat")) {
// ask for an image URL
String imageURL = JOptionPane.showInputDialog("Please enter your image's URL:\nExample: http://wotlas.sf.net/images/wotlas.gif");
if( imageURL==null || imageURL.length()==0 )
return;
// control the URL & image
try {
URL url = new URL(imageURL);
URLConnection urlC = url.openConnection();
urlC.connect();
String ctype = urlC.getContentType();
if( !ctype.startsWith("image/") ) {
JOptionPane.showMessageDialog(null, "The specified URL does not refer to an image !",
"Information", JOptionPane.INFORMATION_MESSAGE);
return;
}
if( urlC.getContentLength()> 50*1024 ) {
JOptionPane.showMessageDialog(null, "The specified image is too big (above 50kB).",
"Information", JOptionPane.INFORMATION_MESSAGE);
return;
}
}catch(Exception ex) {
Debug.signal(Debug.ERROR,this,"Failed to get image: "+ex);
JOptionPane.showMessageDialog(null, "Failed to get the specified image...\nCheck your URL.",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
// send the Image URL to other players...
DataManager dManager = ClientDirector.getDataManager();
dManager.sendMessage( new SendTextMessage( dManager.getMyPlayer().getPrimaryKey(),
dManager.getMyPlayer().getPlayerName(),
getMyCurrentChatPrimaryKey(),
"<img src='"+imageURL+"'>",
ChatRoom.NORMAL_VOICE_LEVEL ));
}
else {
if(DataManager.SHOW_DEBUG) {
System.out.println("Err : unknown actionCommand");
System.out.println("No action command found!");
}
}
}
/** To update players lists of all chat rooms
*
* @param searchedPlayer player we want to update the state
*/
public void updateAllChatRooms(Player searchedPlayer) {
for (int i=0; i<tabbedPane.getTabCount();i++) {
JChatRoom jchatRoom = (JChatRoom) tabbedPane.getComponentAt(i);
//jchatRoom.updatePlayer(searchedPlayer.getPrimaryKey(), searchedPlayer.isConnectedToGame());
jchatRoom.updatePlayer(searchedPlayer.getPrimaryKey(), searchedPlayer.getPlayerState().value);
}
}
/*------------------------------------------------------------------------------------*/
/** MouseListener Implementation **/
/**
* Invoked when the mouse button is clicked
*/
public void mouseClicked(MouseEvent e) {}
/**
* Invoked when the mouse enters a component
*/
public void mouseEntered(MouseEvent e) {}
/**
* Invoked when the mouse exits a component
*/
public void mouseExited(MouseEvent e) {}
/**
* Invoked when a mouse button has been pressed on a component
*/
public void mousePressed(MouseEvent e) {}
/**
* Invoked when a mouse button has been released on a component
*/
public void mouseReleased(MouseEvent e) {}
/*------------------------------------------------------------------------------------*/
}
/*------------------------------------------------------------------------------------*/
/**
* Private class to prevent loss of focus of a JComponent
*/
class NoFocusInputVerifier extends InputVerifier {
/** Checks whether the JComponent's input is valid.
*/
public boolean verify(JComponent input) {
// returning false prevents loss of focus
return false;
}
}
/*------------------------------------------------------------------------------------*/
/**
* Private class to prevent loss of focus of a JComponent
*/
class NoFocusJTextField extends JTextField {
/** Override this method and return true if your JComponent manages focus
*/
public boolean isManagingFocus() {
// return true to inform focus manager that component is managing focus changes
return true;
}
}
/*------------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -