swingmediator.java
来自「google公司的用Java写的一个聊天软件的原代码」· Java 代码 · 共 716 行 · 第 1/2 页
JAVA
716 行
try { controller.sendChatMessage( line ); msgController.showOwnMessage( line ); } catch ( final CommandException e ) { msgController.showSystemMessage( e.getMessage() ); } } } mainP.getMsgTF().setText( "" ); } /** * Gets the text from the input field of the private chat, and * sends it as a message to the user. */ @Override public void writePrivate( final PrivateChatWindow privchat ) { String line = privchat.getChatText(); NickDTO user = privchat.getUser(); if ( line.trim().length() > 0 ) { try { controller.sendPrivateMessage( line, user.getIpAddress(), user.getPrivateChatPort(), user.getCode() ); msgController.showPrivateOwnMessage( user, line ); } catch ( final CommandException e ) { msgController.showPrivateSystemMessage( user, e.getMessage() ); } } privchat.clearChatText(); } /** * Shows a list of the supported commands and their syntax. */ @Override public void showCommands() { cmdParser.showCommands(); } /** * Checks if the user is currently writing, and updates the status. */ @Override public void updateWriting() { if ( mainP.getMsgTF().getText().length() > 0 ) { if ( !controller.isWrote() ) { controller.changeWriting( me.getCode(), true ); } } else { if ( controller.isWrote() ) { controller.changeWriting( me.getCode(), false ); } } } /** * Changes the nick name of the user, if the nick is valid. */ @Override public boolean changeNick( final String nick ) { String trimNick = nick.trim(); if ( !trimNick.equals( me.getNick() ) ) { if ( controller.isNickInUse( trimNick ) ) { JOptionPane.showMessageDialog( null, "The nick is in use by someone else.", Constants.APP_NAME + " - Change nick", JOptionPane.WARNING_MESSAGE ); } else if ( !Tools.isValidNick( trimNick ) ) { JOptionPane.showMessageDialog( null, "'" + trimNick + "' is not a valid nick name.\n\n" + "A nick name can have between 1 and 10 characters.\nLegal characters are 'a-z'," + " '0-9', '-' and '_'.", Constants.APP_NAME + " - Change nick", JOptionPane.WARNING_MESSAGE ); } else { try { controller.changeMyNick( trimNick ); msgController.showSystemMessage( "You changed nick to " + me.getNick() ); updateTitleAndTray(); return true; } catch ( final CommandException e ) { JOptionPane.showMessageDialog( null, e.getMessage(), Constants.APP_NAME + " - Change nick", JOptionPane.WARNING_MESSAGE ); } } } else { return true; } return false; } /** * Runs when the user presses the cancel/close button in the * transfer dialog. If the button's text is close, the dialog should * close. If the text is cancel, the file transfer should stop, * and the button should change text to close. */ @Override public void transferCancelled( final TransferDialog transferDialog ) { if ( transferDialog.getCancelButtonText().equals( "Close" ) ) transferDialog.dispose(); else { transferDialog.setCancelButtonText( "Close" ); FileTransfer fileTransfer = transferDialog.getFileTransfer(); fileTransfer.cancel(); if ( fileTransfer instanceof FileSender ) { FileSender fs = (FileSender) fileTransfer; // This means that the other user has not answered yet if ( fs.isWaiting() ) { msgController.showSystemMessage( "You cancelled sending of " + fs.getFileName() + " to " + fs.getNick().getNick() ); tList.removeFileSender( fs ); } } } } /** * When a new message arrives, and the application is minimized to the * system tray, the system tray icon needs to be updated to show activity. * If sound is enabled, a beep will be played as well. */ @Override public void notifyMessageArrived() { if ( !gui.isVisible() && me.isAway() ) { sysTray.setAwayActivityState(); } else if ( !gui.isVisible() ) { sysTray.setNormalActivityState(); beeper.beep(); } } /** * Gives a notification beep, and opens a dialog box asking if the user * wants to accept a file transfer from another user. */ @Override public boolean askFileSave( final String user, final String fileName, final String size ) { beeper.beep(); Object[] options = { "Yes", "Cancel" }; int choice = JOptionPane.showOptionDialog( null, user + " wants to send you the file " + fileName + " (" + size + ")\nAccept?", Constants.APP_NAME + " - File send", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0] ); return choice == JOptionPane.YES_OPTION; } /** * Opens a file chooser so the user can choose where to save a file * another user is trying to send. Warns if the file name chosen * already exists. */ @Override public File showFileSave( final String fileName ) { File returnFile = null; JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle( Constants.APP_NAME + " - Save" ); chooser.setSelectedFile( new File( fileName ) ); boolean done = false; while ( !done ) { done = true; int returnVal = chooser.showSaveDialog( null ); if ( returnVal == JFileChooser.APPROVE_OPTION ) { File file = chooser.getSelectedFile().getAbsoluteFile(); if ( file.exists() ) { Object[] options = { "Yes", "Cancel" }; int overwrite = JOptionPane.showOptionDialog( null, file.getName() + " already exists.\nOverwrite?", Constants.APP_NAME + " - File exists", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0] ); if ( overwrite != JOptionPane.YES_OPTION ) { done = false; } } if ( done ) { returnFile = file; } } } return returnFile; } /** * Updates the titlebar and tray tooltip with current information. */ @Override public void showTopic() { updateTitleAndTray(); } /** * Creates a new {@link TransferDialog} for that {@link FileReceiver}. */ @Override public void showTransfer( final FileReceiver fileRes ) { new TransferDialog( this, fileRes ); } /** * Creates a new {@link TransferDialog} for that {@link FileSender}. */ @Override public void showTransfer( final FileSender fileSend ) { new TransferDialog( this, fileSend ); } /** * Updates the gui components depending on the away state. */ @Override public void changeAway( final boolean away ) { if ( away ) { sysTray.setAwayState(); mainP.getMsgTF().setEnabled( false ); menuBar.setAwayState( true ); buttonP.setAwayState( true ); } else { sysTray.setNormalState(); mainP.getMsgTF().setEnabled( true ); menuBar.setAwayState( false ); buttonP.setAwayState( false ); } updateAwayInPrivChats( away ); updateTitleAndTray(); } /** * Notifies the open private chat windows that the away state has changed. * * @param away If the user is away. */ private void updateAwayInPrivChats( final boolean away ) { NickList list = controller.getNickList(); for ( int i = 0; i < list.size(); i++ ) { NickDTO user = list.get( i ); if ( user.getPrivchat() != null ) { if ( !user.isAway() ) { user.getPrivchat().setAway( away ); } if ( away ) { msgController.showPrivateSystemMessage( user, "You went away: " + me.getAwayMsg() ); } else { msgController.showPrivateSystemMessage( user, "You came back" ); } } } } /** * If the user does not have a private chat window already, * one is created. */ @Override public void createPrivChat( final NickDTO user ) { if ( user.getPrivchat() == null ) user.setPrivchat( new PrivateChatFrame( this, user ) ); } /** * Shows the user's private chat window. */ @Override public void showPrivChat( final NickDTO user ) { createPrivChat( user ); user.getPrivchat().setVisible( true ); controller.changeNewMessage( user.getCode(), false ); } /** * Returns the message controller for swing. */ @Override public MessageController getMessageController() { return msgController; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?