/** Sends the message that the user typed in the input text area. */ private void doSendMessage() { assert (SwingUtilities.isEventDispatchThread()) : "not in UI thread"; try { String message = mInputText.getText(); /* Make sure we've got the right resource. */ mRemoteIdFull = applyLastKnownResource(mRemoteIdBare); mChatObject.setParticipant(mRemoteIdFull); mChatObject.sendMessage(message); mInputText.setText(""); mLog.message(mLocalId, mLocalId, message); // Make the noise, since we won't get an incoming copy of this Audio.playMessage(); } catch (XMPPException ex) { new ErrorWrapper(ex); JOptionPane.showMessageDialog( this, ex.toString(), JavolinApp.getAppName() + ": Error", JOptionPane.ERROR_MESSAGE); } }
/** Populates the frame with UI controls. */ private void buildUI() { Container cPane = getContentPane(); cPane.setLayout(new BorderLayout()); // Split pane for message text area and input text area mChatSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT); mChatSplitter.setResizeWeight(1); mChatSplitter.setBorder(BorderFactory.createEmptyBorder()); mLog = new ChatLogPanel(mColorMap); mChatSplitter.setTopComponent(mLog); mInputText = new JTextArea(); mInputText.setLineWrap(true); mInputText.setWrapStyleWord(true); mInputText.setBorder(BorderFactory.createEmptyBorder(1, 4, 1, 4)); mChatSplitter.setBottomComponent(new JScrollPane(mInputText)); cPane.add(mChatSplitter, BorderLayout.CENTER); // Necessary for all windows, for Mac support AppMenuBar.applyPlatformMenuBar(this); }
/** Create the window UI. */ private void buildUI() { Container cPane = getContentPane(); cPane.setLayout(new GridBagLayout()); GridBagConstraints c; JLabel label; String msg; JTextField field; int row = 0; label = new JLabel(sTimeStampFormat.format(mInvite.getTimestamp())); label.setFont(new Font("SansSerif", Font.PLAIN, 9)); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row++; c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.EAST; c.insets = new Insets(SPACING, MARGIN, 0, MARGIN); cPane.add(label, c); msg = mInvite.getPlayerJID(); msg = StringUtils.parseBareAddress(msg); field = new JTextField(msg); field.setEditable(false); field.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row++; c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(SPACING, MARGIN, 0, MARGIN); cPane.add(field, c); String gamename = mInvite.getGameName(); if (gamename != null) gamename = gamename.trim(); if (gamename != null && !gamename.equals("")) msg = " " + localize("MessageInvitedOf"); else msg = " " + localize("MessageInvited"); label = new JLabel(msg); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row++; c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(SPACING, MARGIN, 0, MARGIN); cPane.add(label, c); if (gamename != null && !gamename.equals("")) { field = new JTextField(gamename); field.setEditable(false); field.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row++; c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(SPACING, MARGIN, 0, MARGIN); cPane.add(field, c); } String message = mInvite.getMessage(); if (message != null) message = message.trim(); if (message != null && !message.equals("")) { JTextArea textarea = new JTextArea(); textarea.setEditable(false); textarea.setRows(4); textarea.setLineWrap(true); textarea.setWrapStyleWord(true); textarea.setText(message); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row++; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(GAP, MARGIN, 0, MARGIN); JScrollPane scroller = new JScrollPane(textarea); scroller.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); cPane.add(scroller, c); } label = new JLabel(localize("LabelNickname")); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(GAP, MARGIN, 0, MARGIN); cPane.add(label, c); mNicknameField = new JTextField(20); c = new GridBagConstraints(); c.gridx = 1; c.gridy = row++; c.weightx = 1; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(GAP, SPACING, 0, MARGIN); cPane.add(mNicknameField, c); // Add panel with Cancel and Create buttons JPanel buttonPanel = new JPanel(new GridBagLayout()); c = new GridBagConstraints(); c.gridx = 0; c.gridy = row++; c.gridwidth = GridBagConstraints.REMAINDER; c.insets = new Insets(GAP, MARGIN, MARGIN, MARGIN); c.anchor = GridBagConstraints.EAST; c.weightx = 1; cPane.add(buttonPanel, c); mAcceptButton = new JButton(localize("ButtonAccept")); c = new GridBagConstraints(); c.gridx = 2; c.gridy = 0; c.insets = new Insets(0, 0, 0, 0); c.anchor = GridBagConstraints.EAST; buttonPanel.add(mAcceptButton, c); mChatButton = new JButton(localize("ButtonDeclineChat")); c = new GridBagConstraints(); c.gridx = 1; c.gridy = 0; c.insets = new Insets(0, SPACING, 0, 0); c.anchor = GridBagConstraints.EAST; buttonPanel.add(mChatButton, c); mDeclineButton = new JButton(localize("ButtonDecline")); c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.insets = new Insets(0, SPACING, 0, 0); c.anchor = GridBagConstraints.EAST; buttonPanel.add(mDeclineButton, c); }
/** * Constructor. * * @param connection The current active XMPPConnection. * @param remoteId The ID of the user to chat with. (May have a resource, but this will be used * only as a hint for sending replies.) * @exception XMPPException If an error occurs joining the room. */ public ChatWindow(XMPPConnection connection, String remoteId) throws XMPPException { mConnection = connection; mRemoteIdFull = remoteId; mRemoteIdBare = StringUtils.parseBareAddress(remoteId); if (!mRemoteIdFull.equals(mRemoteIdBare)) setLastKnownResource(mRemoteIdFull); mChatObject = new BetterChat(mConnection, mRemoteIdFull); // Get nickname for remote user and use for window title RosterEntry entry = mConnection.getRoster().getEntry(mRemoteIdBare); if (entry != null) { mRemoteNick = entry.getName(); } if (mRemoteNick == null || mRemoteNick.equals("")) { mRemoteNick = mRemoteIdBare; } String val = JavolinApp.resources.getString("ChatWindow_WindowTitle"); setTitle(JavolinApp.getAppName() + ": " + val + " " + mRemoteNick); // Get local user ID and chat color mLocalId = StringUtils.parseBareAddress(mConnection.getUser()); mColorMap = new UserColorMap(); mColorMap.getUserNameColor(mLocalId); // Give user first color // Set up UI buildUI(); setSize(500, 400); mSizePosSaver = new SizeAndPositionSaver(this, NODENAME); restoreWindowState(); // Send message when user presses Enter while editing input text mSendMessageAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { doSendMessage(); } }; mInputText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), mSendMessageAction); // Handle window events setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent we) { saveWindowState(); if (mLog != null) { mLog.dispose(); } mColorMap.dispose(); } public void windowOpened(WindowEvent we) { // Give focus to input text area when the window is created mInputText.requestFocusInWindow(); } }); /* We do *not* register as a message listener. The Smack Chat object is * kind of a crock; there's no documented way to turn off its packet * interception when we close this window. And the main JavolinApp * listener is already grabbing all CHAT and NORMAL message packets, so * there's no need for us to listen -- in fact, it leads to double * printing in some cases. */ }