chatcontainer.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 1,309 行 · 第 1/3 页
JAVA
1,309 行
((ChatRoomListener)chatRoomListener).chatRoomClosed(room);
}
}
/**
* Notifies users that a <code>ChatRoom</code> has been activated.
*
* @param room - the <code>ChatRoom</code> that has been activated.
*/
protected void fireChatRoomActivated(ChatRoom room) {
final Iterator iter = new HashSet(chatRoomListeners).iterator();
while (iter.hasNext()) {
((ChatRoomListener)iter.next()).chatRoomActivated(room);
}
}
/**
* Notifies users that a user has joined a <code>ChatRoom</code>.
*
* @param room - the <code>ChatRoom</code> that a user has joined.
* @param userid - the userid of the person.
*/
protected void fireUserHasJoined(final ChatRoom room, final String userid) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final Iterator iter = new HashSet(chatRoomListeners).iterator();
while (iter.hasNext()) {
((ChatRoomListener)iter.next()).userHasJoined(room, userid);
}
}
});
}
/**
* Notifies users that a user has left a <code>ChatRoom</code>.
*
* @param room - the <code>ChatRoom</code> that a user has left.
* @param userid - the userid of the person.
*/
protected void fireUserHasLeft(final ChatRoom room, final String userid) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final Iterator iter = new HashSet(chatRoomListeners).iterator();
while (iter.hasNext()) {
((ChatRoomListener)iter.next()).userHasLeft(room, userid);
}
}
});
}
/**
* Starts flashing of MainWindow.
*
* @param comp the Component to check if a message has been inserted
* but the room is not the selected room.
*/
public void startFlashing(final Component comp) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
final int index = indexOfComponent(comp);
if (index != -1) {
// Check notifications.
if (SettingsManager.getLocalPreferences().isChatRoomNotificationsOn() || !(comp instanceof GroupChatRoom)) {
if (comp instanceof ChatRoom) {
checkNotificationPreferences((ChatRoom)comp);
}
}
// Notify decorators
SparkManager.getChatManager().notifySparkTabHandlers(comp);
}
boolean flashAllowed = SettingsManager.getLocalPreferences().isChatRoomNotificationsOn() || !(comp instanceof GroupChatRoom);
if (!chatFrame.isInFocus() && flashAllowed) {
SparkManager.getNativeManager().flashWindow(chatFrame);
}
}
catch (Exception ex) {
Log.error("Issue in ChatRooms with tab location.", ex);
}
}
});
}
public void fireChatRoomStateUpdated(final ChatRoom room) {
final int index = indexOfComponent(room);
if (index != -1) {
SparkTab tab = getTabAt(index);
SparkManager.getChatManager().notifySparkTabHandlers(room);
}
}
/**
* Checks to see if the <code>ChatFrame</code> should stop flashing.
*
* @param component the component that should be notified.
*/
public void stopFlashing(final Component component) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
// Stop the flashing
SparkManager.getNativeManager().stopFlashing(chatFrame);
// Notify decorators
SparkManager.getChatManager().notifySparkTabHandlers(component);
}
catch (Exception ex) {
Log.error("Could not stop flashing because " + ex.getMessage(), ex);
}
}
});
}
/**
* Handles Notification preferences for incoming messages and rooms.
*
* @param room the chat room.
*/
private void checkNotificationPreferences(final ChatRoom room) {
LocalPreferences pref = SettingsManager.getLocalPreferences();
if (pref.getWindowTakesFocus()) {
chatFrame.setState(Frame.NORMAL);
chatFrame.setVisible(true);
}
if (pref.getShowToasterPopup()) {
SparkToaster toaster = new SparkToaster();
toaster.setCustomAction(new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
chatFrame.setState(Frame.NORMAL);
chatFrame.setVisible(true);
int tabLocation = indexOfComponent(room);
if (tabLocation != -1) {
setSelectedIndex(tabLocation);
}
}
});
toaster.setDisplayTime(5000);
toaster.setBorder(BorderFactory.createBevelBorder(0));
String nickname = nickname = room.getRoomTitle();
toaster.setTitle(nickname);
toaster.setToasterHeight(150);
toaster.setToasterWidth(200);
int size = room.getTranscripts().size();
if (size > 0) {
Message message = (Message)room.getTranscripts().get(size - 1);
toaster.showToaster(room.getTabIcon(), message.getBody());
}
}
}
public void setChatRoomTitle(ChatRoom room, String title) {
int index = indexOfComponent(room);
if (index != -1) {
SparkTab tab = getTabAt(index);
fireChatRoomStateUpdated(room);
tab.setTabTitle(room.getTabTitle());
}
}
private void createFrameIfNeeded() {
if (chatFrame != null) {
return;
}
LocalPreferences pref = SettingsManager.getLocalPreferences();
if (pref.isDockingEnabled()) {
chatFrame = MainWindow.getInstance();
}
else {
chatFrame = new ChatFrame();
}
// The ultimate workground for 1.6
chatFrame.dispose();
chatFrame.setFocusableWindowState(false);
chatFrame.setFocusableWindowState(true);
chatFrame.dispose();
chatFrame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent windowEvent) {
stopFlashing();
int sel = getSelectedIndex();
if (sel == -1) {
return;
}
final ChatRoom room;
try {
room = getChatRoom(sel);
focusChat();
// Set the title of the room.
chatFrame.setTitle(room.getRoomTitle());
}
catch (ChatRoomNotFoundException e1) {
}
}
public void windowDeactivated(WindowEvent windowEvent) {
}
public void windowClosing(WindowEvent windowEvent) {
// Save layout
chatFrame.saveLayout();
SparkManager.getChatManager().getChatContainer().closeAllChatRooms();
}
});
// Start timer
handleStaleChats();
}
/**
* Brings the chat into focus.
*/
public void focusChat() {
TaskEngine.getInstance().schedule(focusTask, 50);
}
public Collection<ChatRoom> getChatRooms() {
return new ArrayList<ChatRoom>(chatRoomList);
}
public ChatFrame getChatFrame() {
return chatFrame;
}
public void blinkFrameIfNecessary(final JFrame frame) {
final MainWindow mainWindow = SparkManager.getMainWindow();
if (mainWindow.isFocusOwner()) {
frame.setVisible(true);
return;
}
else {
// Set to new tab.
if (Spark.isWindows()) {
frame.setState(Frame.ICONIFIED);
chatFrame.setFocusableWindowState(true);
SparkManager.getNativeManager().flashWindow(frame);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
SparkManager.getNativeManager().stopFlashing(frame);
}
});
}
}
}
private void checkTabPopup(MouseEvent e) {
final SparkTab tab = (SparkTab)e.getSource();
if (!e.isPopupTrigger()) {
return;
}
final JPopupMenu popup = new JPopupMenu();
// Handle closing this room.
Action closeThisAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
ChatRoom chatRoom = (ChatRoom)getComponentInTab(tab);
if (chatRoom != null) {
closeTab(chatRoom);
}
}
};
closeThisAction.putValue(Action.NAME, Res.getString("message.close.this.chat"));
popup.add(closeThisAction);
if (getChatRooms().size() > 1) {
// Handle closing other rooms.
Action closeOthersAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
ChatRoom chatRoom = (ChatRoom)getComponentInTab(tab);
if (chatRoom != null) {
for (ChatRoom cRoom : getChatRooms()) {
if (chatRoom != cRoom) {
closeTab(cRoom);
}
}
}
}
};
closeOthersAction.putValue(Action.NAME, Res.getString("message.close.other.chats"));
popup.add(closeOthersAction);
Action closeOldAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
for (ChatRoom rooms : getStaleChatRooms()) {
closeTab(rooms);
}
}
};
closeOldAction.putValue(Action.NAME, Res.getString("message.close.stale.chats"));
popup.add(closeOldAction);
}
popup.show(tab, e.getX(), e.getY());
}
/**
* Returns a Collection of stale chat rooms.
*
* @return a collection of stale chat rooms.
*/
public Collection<ChatRoom> getStaleChatRooms() {
final List<ChatRoom> staleRooms = new ArrayList<ChatRoom>();
for (ChatRoom chatRoom : getChatRooms()) {
long lastActivity = chatRoom.getLastActivity();
long currentTime = System.currentTimeMillis();
long diff = currentTime - lastActivity;
int minutes = (int)(diff / (60 * 1000F));
LocalPreferences pref = SettingsManager.getLocalPreferences();
int timeoutMinutes = pref.getChatLengthDefaultTimeout();
int unreadCount = chatRoom.getUnreadMessageCount();
if (timeoutMinutes <= minutes && unreadCount == 0) {
staleRooms.add(chatRoom);
}
}
return staleRooms;
}
/**
* Checks every room every 30 seconds to see if it's timed out.
*/
private void handleStaleChats() {
int delay = 1000; // delay for 1 second.
int period = 60000; // repeat every minute.
final TimerTask task = new SwingTimerTask() {
public void doRun() {
for (ChatRoom chatRoom : getStaleChatRooms()) {
// Notify decorators
SparkManager.getChatManager().notifySparkTabHandlers(chatRoom);
}
}
};
TaskEngine.getInstance().scheduleAtFixedRate(task, delay, period);
}
private void navigateRight() {
int selectedIndex = getSelectedIndex();
if (selectedIndex > -1) {
int count = getTabCount();
if (selectedIndex == (count - 1)) {
setSelectedIndex(0);
}
else {
setSelectedIndex(selectedIndex + 1);
}
}
}
private void navigateLeft() {
int selectedIndex = getSelectedIndex();
if (selectedIndex > 0) {
setSelectedIndex(selectedIndex - 1);
}
else {
setSelectedIndex(getTabCount() - 1);
}
}
// Handle key listener events for mac only :)
public void keyTyped(KeyEvent keyEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void keyPressed(KeyEvent keyEvent) {
if (keyEvent.isMetaDown()) {
if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
navigateRight();
}
else if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
navigateLeft();
}
}
}
public void keyReleased(KeyEvent keyEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Returns the total number of unread messages in Spark.
*
* @return the total number of unread messages in Spark.
*/
public int getTotalNumberOfUnreadMessages() {
int messageCount = 0;
for (ChatRoom chatRoom : chatRoomList) {
messageCount += chatRoom.getUnreadMessageCount();
}
return messageCount;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?