chatcontainer.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 1,309 行 · 第 1/3 页
JAVA
1,309 行
int tabLocation = indexOfComponent(chatRoom);
setSelectedIndex(tabLocation);
// If the ContactList is in the tray, we need better notification by flashing
// the chatframe.
startFlashing(chatRoom);
}
// Handle when chat frame is visible but the Contact List is not.
else if (chatFrame.isVisible() && !SparkManager.getMainWindow().isVisible() && !chatFrame.isInFocus()) {
startFlashing(chatRoom);
}
else if (!chatFrame.isVisible()) {
// Set to new tab.
int tabLocation = indexOfComponent(chatRoom);
setSelectedIndex(tabLocation);
if (Spark.isWindows()) {
chatFrame.setFocusableWindowState(false);
chatFrame.setState(Frame.ICONIFIED);
}
chatFrame.setVisible(true);
chatFrame.setFocusableWindowState(true);
// If the ContactList is in the tray, we need better notification by flashing
// the chatframe.
if (!SparkManager.getMainWindow().isVisible()) {
startFlashing(chatRoom);
}
else if (chatFrame.getState() == Frame.ICONIFIED) {
startFlashing(chatRoom);
}
chatFrame.setTitle(chatRoom.getRoomTitle());
}
else if (chatRoom != activeChatRoom) {
startFlashing(chatRoom);
}
}
/**
* Removes the ChatRoom resources.
*
* @param room the room to remove.
*/
private void cleanupChatRoom(ChatRoom room) {
if (room.isActive()) {
room.leaveChatRoom();
room.closeChatRoom();
}
final PacketListener listener = presenceMap.get(room.getRoomname());
if (listener != null) {
SparkManager.getConnection().removePacketListener(listener);
}
fireChatRoomClosed(room);
room.removeMessageListener(this);
// Remove mappings
presenceMap.remove(room.getRoomname());
chatRoomList.remove(room);
room.getChatInputEditor().removeKeyListener(this);
// Clear all Text :)
room.getTranscriptWindow().cleanup();
}
/**
* Close all chat rooms.
*/
public void closeAllChatRooms() {
LocalPreferences pref = SettingsManager.getLocalPreferences();
if (MainWindow.getInstance().isDocked() || !pref.isAutoCloseChatRoomsEnabled()) {
return;
}
final Iterator rooms = new ArrayList<ChatRoom>(chatRoomList).iterator();
while (rooms.hasNext()) {
ChatRoom chatRoom = (ChatRoom)rooms.next();
closeTab(chatRoom);
chatRoom.closeChatRoom();
}
for (int i = 0; i < getTabCount(); i++) {
Component comp = getComponentAt(i);
if (comp instanceof ContainerComponent) {
((ContainerComponent)comp).closing();
}
closeTab(comp);
}
}
/**
* Leaves a ChatRoom. Leaving a chat room does everything but close the room itself.
*
* @param room the room to leave.
*/
public void leaveChatRoom(ChatRoom room) {
// Notify that the chatroom has been left.
fireChatRoomLeft(room);
room.leaveChatRoom();
final PacketListener listener = (PacketListener)presenceMap.get(room.getRoomname());
if (listener != null && SparkManager.getConnection().isConnected()) {
SparkManager.getConnection().removePacketListener(listener);
}
}
/**
* Returns a ChatRoom by name.
*
* @param roomName the name of the ChatRoom.
* @return the ChatRoom
* @throws ChatRoomNotFoundException
*/
public ChatRoom getChatRoom(String roomName) throws ChatRoomNotFoundException {
for (int i = 0; i < getTabCount(); i++) {
ChatRoom room = null;
try {
room = getChatRoom(i);
}
catch (ChatRoomNotFoundException e1) {
// Ignore
}
if (room != null && room.getRoomname().equalsIgnoreCase(roomName) && room.isActive()) {
return room;
}
}
throw new ChatRoomNotFoundException(roomName + " not found.");
}
/**
* Returns a ChatRoom in the specified tab location.
*
* @param location the tab location.
* @return the ChatRoom found.
* @throws ChatRoomNotFoundException thrown if the room is not found.
*/
public ChatRoom getChatRoom(int location) throws ChatRoomNotFoundException {
if (getTabCount() < location) {
return null;
}
try {
Component comp = getComponentAt(location);
if (comp != null && comp instanceof ChatRoom) {
return (ChatRoom)comp;
}
}
catch (ArrayIndexOutOfBoundsException outOfBoundsEx) {
Log.error("Error getting Chat Room", outOfBoundsEx);
}
throw new ChatRoomNotFoundException();
}
/**
* Returns the Active ChatRoom.
*
* @return the ChatRoom active in the tabbed pane.
* @throws ChatRoomNotFoundException is thrown if no chat room is found.
*/
public ChatRoom getActiveChatRoom() throws ChatRoomNotFoundException {
int location = getSelectedIndex();
if (location != -1) {
return getChatRoom(location);
}
throw new ChatRoomNotFoundException();
}
/**
* Returns the Active Component.
*
* @return the Component active in the tabbed pane.
*/
public Component getActiveRoom() {
int location = getSelectedIndex();
if (location != -1) {
return getComponentAt(location);
}
return null;
}
/**
* Activates the specified ChatRoom.
*
* @param room the ChatRoom to activate.
*/
public void activateChatRoom(ChatRoom room) {
int tabLocation = indexOfComponent(room);
setSelectedIndex(tabLocation);
chatFrame.bringFrameIntoFocus();
focusChat();
}
/**
* Activates the component in tabbed pane.
*
* @param component the component contained within the tab to activate.
*/
public void activateComponent(Component component) {
int tabLocation = indexOfComponent(component);
if (tabLocation != -1) {
setSelectedIndex(tabLocation);
}
chatFrame.bringFrameIntoFocus();
focusChat();
}
/**
* Used for Tray Notifications.
*
* @param room the ChatRoom where the message was received.
* @param message the message received.
*/
public void messageReceived(ChatRoom room, Message message) {
room.increaseUnreadMessageCount();
// Check to see if it's a room update.
String from = message.getFrom();
String insertMessage = message.getBody();
if (room.getChatType() == Message.Type.chat) {
from = StringUtils.parseName(from);
}
else {
from = StringUtils.parseResource(from);
}
if (ModelUtil.hasLength(from)) {
insertMessage = from + ": " + insertMessage;
}
fireNotifyOnMessage(room);
}
public void fireNotifyOnMessage(final ChatRoom chatRoom) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
handleMessageNotification(chatRoom);
}
});
}
public void messageSent(ChatRoom room, Message message) {
fireChatRoomStateUpdated(room);
}
/**
* Notification that the tab pane has been modified. Generally by changing of the tabs.
*
* @param e the ChangeEvent.
*/
public void stateChanged(ChangeEvent e) {
// Stop the flashing only if the chat frame is in focus.
if (chatFrame.isInFocus()) {
stopFlashing();
}
final Object o = getSelectedComponent();
if (o instanceof ChatRoom) {
final ChatRoom room = (ChatRoom)o;
focusChat();
// Set the title of the room.
chatFrame.setTitle(room.getRoomTitle());
chatFrame.setIconImage(SparkManager.getMainWindow().getIconImage());
}
else if (o instanceof ContainerComponent) {
final ContainerComponent comp = (ContainerComponent)o;
chatFrame.setTitle(comp.getFrameTitle());
chatFrame.setIconImage(comp.getTabIcon().getImage());
SparkManager.getChatManager().notifySparkTabHandlers(comp.getGUI());
}
}
private void stopFlashing() {
try {
// Get current tab
int selectedIndex = getSelectedIndex();
if (selectedIndex != -1) {
Component comp = getComponentAt(selectedIndex);
if (comp != null) {
stopFlashing(comp);
}
}
}
catch (Exception e) {
Log.error(e);
}
}
/**
* Closes a tab of a room.
*
* @param component the component inside of the tab to close.
*/
public void closeTab(Component component) {
int location = indexOfComponent(component);
if (location == -1) {
return;
}
if (getTabCount() == 0) {
chatFrame.setTitle("");
chatFrame.dispose();
}
this.removeTabAt(location);
}
public void closeActiveRoom() {
ChatRoom room = null;
try {
room = getActiveChatRoom();
}
catch (ChatRoomNotFoundException e1) {
Component comp = getActiveRoom();
if (comp != null) {
boolean canClose = ((ContainerComponent)comp).closing();
if (canClose) {
closeTab(comp);
}
return;
}
if (room == null) {
return;
}
}
// Confirm end session
boolean isGroupChat = room.getChatType() == Message.Type.groupchat;
if (isGroupChat) {
final int ok = JOptionPane.showConfirmDialog(chatFrame, Res.getString("message.end.conversation"),
Res.getString("title.confirmation"), JOptionPane.YES_NO_OPTION);
if (ok == JOptionPane.OK_OPTION) {
room.closeChatRoom();
return;
}
}
else {
room.closeChatRoom();
return;
}
}
public String toString() {
StringBuffer buf = new StringBuffer();
Iterator iter = chatRoomList.iterator();
while (iter.hasNext()) {
ChatRoom room = (ChatRoom)iter.next();
buf.append("Roomname=").append(room.getRoomname()).append("\n");
}
return buf.toString();
}
/**
* Returns true if there are any Rooms present.
*
* @return true if Rooms are present, otherwise false.
*/
public boolean hasRooms() {
int count = getSelectedIndex();
return count != -1;
}
/**
* Adds a ChatRoom listener to ChatRooms. The
* listener will be called when either a ChatRoom has been
* added, removed, or activated.
*
* @param listener the <code>ChatRoomListener</code> to register
*/
public void addChatRoomListener(ChatRoomListener listener) {
if (!chatRoomListeners.contains(listener)) {
chatRoomListeners.add(listener);
}
}
/**
* Removes the specified <code>ChatRoomListener</code>.
*
* @param listener the <code>ChatRoomListener</code> to remove
*/
public void removeChatRoomListener(ChatRoomListener listener) {
chatRoomListeners.remove(listener);
}
/**
* Notifies users that a <code>ChatRoom</code> has been opened.
*
* @param room - the <code>ChatRoom</code> that has been opened.
*/
protected void fireChatRoomOpened(ChatRoom room) {
final Iterator iter = new ArrayList(chatRoomListeners).iterator();
while (iter.hasNext()) {
((ChatRoomListener)iter.next()).chatRoomOpened(room);
}
}
/**
* Notifies users that a <code>ChatRoom</code> has been left.
*
* @param room - the <code>ChatRoom</code> that has been left
*/
protected void fireChatRoomLeft(ChatRoom room) {
final Iterator iter = new HashSet(chatRoomListeners).iterator();
while (iter.hasNext()) {
final Object chatRoomListener = iter.next();
((ChatRoomListener)chatRoomListener).chatRoomLeft(room);
}
}
/**
* Notifies users that a <code>ChatRoom</code> has been closed.
*
* @param room - the <code>ChatRoom</code> that has been closed.
*/
protected void fireChatRoomClosed(ChatRoom room) {
final Iterator iter = new HashSet(chatRoomListeners).iterator();
while (iter.hasNext()) {
final Object chatRoomListener = iter.next();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?