controller.java
来自「google公司的用Java写的一个聊天软件的原代码」· Java 代码 · 共 649 行 · 第 1/2 页
JAVA
649 行
/*************************************************************************** * Copyright 2006-2008 by Christian Ihle * * kontakt@usikkert.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/package net.usikkert.kouchat.misc;import java.util.Timer;import java.util.TimerTask;import java.util.logging.Level;import java.util.logging.Logger;import net.usikkert.kouchat.Constants;import net.usikkert.kouchat.autocomplete.AutoCompleter;import net.usikkert.kouchat.autocomplete.CommandAutoCompleteList;import net.usikkert.kouchat.autocomplete.NickAutoCompleteList;import net.usikkert.kouchat.net.DefaultPrivateMessageResponder;import net.usikkert.kouchat.net.MessageParser;import net.usikkert.kouchat.net.DefaultMessageResponder;import net.usikkert.kouchat.net.MessageResponder;import net.usikkert.kouchat.net.Messages;import net.usikkert.kouchat.net.PrivateMessageParser;import net.usikkert.kouchat.net.PrivateMessageResponder;import net.usikkert.kouchat.net.TransferList;import net.usikkert.kouchat.ui.UserInterface;import net.usikkert.kouchat.util.DayTimer;import net.usikkert.kouchat.util.Tools;/** * This controller gives access to the network and the state of the * application, like the user list and the topic. * <br><br> * When changing state, use the methods available <strong>here</strong> instead * of doing it manually, to make sure the state is consistent. * <br><br> * To connect to the network, use {@link #logOn()}. * * @author Christian Ihle */public class Controller{ private static final Logger LOG = Logger.getLogger( Controller.class.getName() ); private final ChatState chatState; private final NickController nickController; private final Messages messages; private final MessageParser msgParser; private final PrivateMessageParser privmsgParser; private final MessageResponder msgResponder; private final PrivateMessageResponder privmsgResponder; private final IdleThread idleThread; private final TransferList tList; private final WaitingList wList; private final NickDTO me; private final Timer delayedLogonTimer; /** * Constructor. Initializes the controller, but does not log on to * the network. * * @param ui The active user interface object. */ public Controller( final UserInterface ui ) { Runtime.getRuntime().addShutdownHook( new Thread( "ControllerShutdownHook" ) { @Override public void run() { logOff(); } } ); me = Settings.getSettings().getMe(); nickController = new NickController(); chatState = new ChatState(); tList = new TransferList(); wList = new WaitingList(); idleThread = new IdleThread( this, ui ); msgResponder = new DefaultMessageResponder( this, ui ); privmsgResponder = new DefaultPrivateMessageResponder( this, ui ); msgParser = new MessageParser( msgResponder ); privmsgParser = new PrivateMessageParser( privmsgResponder ); messages = new Messages(); delayedLogonTimer = new Timer( "DelayedLogonTimer" ); new DayTimer( ui ); } /** * Gets the current topic. * * @return The current topic. */ public TopicDTO getTopic() { return chatState.getTopic(); } /** * Gets the list of online users. * * @return The user list. */ public NickList getNickList() { return nickController.getNickList(); } /** * Returns if the application user wrote the last time * {@link #changeWriting(int, boolean)} was called. * * @return If the user wrote. * @see ChatState#isWrote() */ public boolean isWrote() { return chatState.isWrote(); } /** * Updates the write state for the user. This is useful to see which * users are currently writing. * * If the user is the application user, messages will be sent to the * other clients to notify of changes. * * @param code The user code for the user to update. * @param writing True if the user is writing. */ public void changeWriting( final int code, final boolean writing ) { nickController.changeWriting( code, writing ); if ( code == me.getCode() ) { chatState.setWrote( writing ); if ( writing ) messages.sendWritingMessage(); else messages.sendStoppedWritingMessage(); } } /** * Updates the away status and the away message for the user. * * @param code The user code for the user to update. * @param away If the user is away or not. * @param awaymsg The away message for that user. * @throws CommandException If there is no connection to the network, * or the user tries to set an away message that is to long. */ public void changeAwayStatus( final int code, final boolean away, final String awaymsg ) throws CommandException { if ( code == me.getCode() && !isConnected() ) throw new CommandException( "You can not change away mode without being connected." ); else if ( Tools.getBytes( awaymsg ) > Constants.MESSAGE_MAX_BYTES ) throw new CommandException( "You can not set an away message with more than " + Constants.MESSAGE_MAX_BYTES + " bytes." ); else nickController.changeAwayStatus( code, away, awaymsg ); } /** * Checks if the nick is in use by another user. * * @param nick The nick to check. * @return True if the nick is already in use. */ public boolean isNickInUse( final String nick ) { return nickController.isNickInUse( nick ); } /** * Checks if the user with that user code is already in the user list. * * @param code The user code of the user to check. * @return True if the user is not in the user list. */ public boolean isNewUser( final int code ) { return nickController.isNewUser( code ); } /** * Changes the nick for the application user, sends a message over the * network to notify the other clients of the change, and saves the changes. * * @param nick The new nick for the application user. * @throws CommandException If the user is away. */ public void changeMyNick( final String nick ) throws CommandException { if ( me.isAway() ) throw new CommandException( "You can not change nick while away." ); else { changeNick( me.getCode(), nick ); messages.sendNickMessage(); Settings.getSettings().saveSettings(); } } /** * Changes the nick of the user. * * @param code The user code for the user. * @param nick The new nick for the user. */ public void changeNick( final int code, final String nick ) { nickController.changeNick( code, nick ); } /** * Gets the user with the specified user code. * * @param code The user code for the user. * @return The user with the specified user code, or <em>null</em> if not found. */ public NickDTO getNick( final int code ) { return nickController.getNick( code ); } /** * Gets the user with the specified nick name. * * @param nick The nick name to check for. * @return The user with the specified nick name, or <em>null</em> if not found. */ public NickDTO getNick( final String nick ) { return nickController.getNick( nick ); } /** * Sends the necessary network messages to log the user onto the network * and query for the users and state. */ private void sendLogOn() { messages.sendLogonMessage(); messages.sendClient(); messages.sendExposeMessage(); messages.sendGetTopicMessage(); } /** * This should be run after a successful logon, to update the connection state. */ private void runDelayedLogon() { delayedLogonTimer.schedule( new DelayedLogonTask(), 0 ); } /** * Logs this client onto the network, and starts some necessary threads. */ public void logOn() { msgParser.start(); privmsgParser.start(); messages.start(); sendLogOn(); idleThread.start(); runDelayedLogon(); } /** * Logs this client off the network, and stops the threads. */ public void logOff() { idleThread.stopThread(); messages.sendLogoffMessage(); messages.stop(); msgParser.stop(); privmsgParser.stop(); wList.setLoggedOn( false ); } /** * Sends a message over the network, asking the other clients to identify * themselves. */ public void sendExposeMessage() { messages.sendExposeMessage(); } /** * Sends a message over the network to identify this client. */ public void sendExposingMessage() { messages.sendExposingMessage(); } /** * Sends a message over the network to ask for the current topic. */ public void sendGetTopicMessage() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?