<commit_before>//
// $Id: ChatPanel.java,v 1.5 2001/10/18 20:52:58 mdb Exp $

package com.threerings.micasa.client;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.text.*;
import java.util.StringTokenizer;

import com.samskivert.swing.*;
import com.threerings.crowd.chat.*;
import com.threerings.crowd.client.*;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;

import com.threerings.micasa.Log;

public class ChatPanel
    extends JPanel
    implements ActionListener, ChatDisplay, OccupantObserver, PlaceView
{
    public ChatPanel (CrowdContext ctx)
    {
        // keep this around for later
        _ctx = ctx;

        // create our chat director and register ourselves with it
        _chatdtr = new ChatDirector(_ctx);
        _chatdtr.addChatDisplay(this);

        // register as an occupant observer
        _ctx.getOccupantManager().addOccupantObserver(this);

        GroupLayout gl = new VGroupLayout(GroupLayout.STRETCH);
	gl.setOffAxisPolicy(GroupLayout.STRETCH);
	setLayout(gl);

        // create our scrolling chat text display
        _text = new JTextPane();
        _text.setEditable(false);
        add(new JScrollPane(_text));

        // create our styles and add those to the text pane
        createStyles(_text);

        // add a label for the text entry stuff
        add(new JLabel("Type here to chat:"), GroupLayout.FIXED);

        // create a horizontal group for the text entry bar
        gl = new HGroupLayout(GroupLayout.STRETCH);
        JPanel epanel = new JPanel(gl);
        epanel.add(_entry = new JTextField());
        _entry.setActionCommand("send");
        _entry.addActionListener(this);
        _entry.setEnabled(false);

        _send = new JButton("Send");
        _send.setEnabled(false);
        _send.addActionListener(this);
        _send.setActionCommand("send");
        epanel.add(_send, GroupLayout.FIXED);
        add(epanel, GroupLayout.FIXED);

        // focus the chat input field by default
        _entry.requestFocus();
    }

    /**
     * For appliations where the chat box has extremely limited space, the
     * send button can be removed to leave more space for the text input
     * box.
     */
    public void removeSendButton ()
    {
        if (_send.isVisible()) {
            // _send.getParent().remove(_send);
            _send.setVisible(false);
        }
    }

    protected void createStyles (JTextPane text)
    {
        StyleContext sctx = StyleContext.getDefaultStyleContext();
        Style defstyle = sctx.getStyle(StyleContext.DEFAULT_STYLE);

        _nameStyle = text.addStyle("name", defstyle);
        StyleConstants.setForeground(_nameStyle, Color.blue);

        _msgStyle = text.addStyle("msg", defstyle);
        StyleConstants.setForeground(_msgStyle, Color.black);

        _errStyle = text.addStyle("err", defstyle);
        StyleConstants.setForeground(_errStyle, Color.red);

        _noticeStyle = text.addStyle("notice", defstyle);
        StyleConstants.setForeground(_noticeStyle, Color.magenta);
    }

    public void actionPerformed (ActionEvent e)
    {
	String cmd = e.getActionCommand();
	if (cmd.equals("send")) {
            sendText();

	} else {
	    System.out.println("Unknown action event: " + cmd);
	}
    }

    public void occupantEntered (OccupantInfo info)
    {
        displayOccupantMessage("*** " + info.username + " entered.");
    }

    public void occupantLeft (OccupantInfo info)
    {
        displayOccupantMessage("*** " + info.username + " left.");
    }

    public void occupantUpdated (OccupantInfo info)
    {
    }

    protected void displayOccupantMessage (String message)
    {
        // stick a newline on the message
        message = message + "\n";

        Document doc = _text.getDocument();
        try {
            doc.insertString(doc.getLength(), message, _noticeStyle);
        } catch (BadLocationException ble) {
            Log.warning("Unable to insert text!? [error=" + ble + "].");
        }
    }

    protected void sendText ()
    {
        String text = _entry.getText();

        // if the message to send begins with /tell then parse it and
        // generate a tell request rather than a speak request
        if (text.startsWith("/tell")) {
            StringTokenizer tok = new StringTokenizer(text);
            // there should be at least three tokens: '/tell target word'
            if (tok.countTokens() < 3) {
                displayError("Usage: /tell username message");
                return;
            }

            // skip the /tell and grab the username
            tok.nextToken();
            String username = tok.nextToken();

            // now strip off everything up to the username to get the
            // message
            int uidx = text.indexOf(username);
            String message = text.substring(uidx + username.length()).trim();

            // request to send this text as a tell message
            _chatdtr.requestTell(username, message);

        } else {
            // request to send this text as a chat message
            _chatdtr.requestSpeak(text);
        }

        // clear out the input because we sent a request
        _entry.setText("");
    }

    public void displaySpeakMessage (String speaker, String message)
    {
        // wrap the speaker in brackets
        speaker = "<" + speaker + "> ";
        // stick a newline on the message
        message = message + "\n";

        Document doc = _text.getDocument();
        try {
            doc.insertString(doc.getLength(), speaker, _nameStyle);
            doc.insertString(doc.getLength(), message, _msgStyle);
        } catch (BadLocationException ble) {
            Log.warning("Unable to insert text!? [error=" + ble + "].");
        }
    }

    protected void displayError (String message)
    {<commit_msg>Add /clear for clearing the chat box.<commit_after>//
// $Id: ChatPanel.java,v 1.6 2001/10/18 23:10:32 mdb Exp $

package com.threerings.micasa.client;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.text.*;
import java.util.StringTokenizer;

import com.samskivert.swing.*;
import com.threerings.crowd.chat.*;
import com.threerings.crowd.client.*;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;

import com.threerings.micasa.Log;

public class ChatPanel
    extends JPanel
    implements ActionListener, ChatDisplay, OccupantObserver, PlaceView
{
    public ChatPanel (CrowdContext ctx)
    {
        // keep this around for later
        _ctx = ctx;

        // create our chat director and register ourselves with it
        _chatdtr = new ChatDirector(_ctx);
        _chatdtr.addChatDisplay(this);

        // register as an occupant observer
        _ctx.getOccupantManager().addOccupantObserver(this);

        GroupLayout gl = new VGroupLayout(GroupLayout.STRETCH);
	gl.setOffAxisPolicy(GroupLayout.STRETCH);
	setLayout(gl);

        // create our scrolling chat text display
        _text = new JTextPane();
        _text.setEditable(false);
        add(new JScrollPane(_text));

        // create our styles and add those to the text pane
        createStyles(_text);

        // add a label for the text entry stuff
        add(new JLabel("Type here to chat:"), GroupLayout.FIXED);

        // create a horizontal group for the text entry bar
        gl = new HGroupLayout(GroupLayout.STRETCH);
        JPanel epanel = new JPanel(gl);
        epanel.add(_entry = new JTextField());
        _entry.setActionCommand("send");
        _entry.addActionListener(this);
        _entry.setEnabled(false);

        _send = new JButton("Send");
        _send.setEnabled(false);
        _send.addActionListener(this);
        _send.setActionCommand("send");
        epanel.add(_send, GroupLayout.FIXED);
        add(epanel, GroupLayout.FIXED);

        // focus the chat input field by default
        _entry.requestFocus();
    }

    /**
     * For appliations where the chat box has extremely limited space, the
     * send button can be removed to leave more space for the text input
     * box.
     */
    public void removeSendButton ()
    {
        if (_send.isVisible()) {
            // _send.getParent().remove(_send);
            _send.setVisible(false);
        }
    }

    protected void createStyles (JTextPane text)
    {
        StyleContext sctx = StyleContext.getDefaultStyleContext();
        Style defstyle = sctx.getStyle(StyleContext.DEFAULT_STYLE);

        _nameStyle = text.addStyle("name", defstyle);
        StyleConstants.setForeground(_nameStyle, Color.blue);

        _msgStyle = text.addStyle("msg", defstyle);
        StyleConstants.setForeground(_msgStyle, Color.black);

        _errStyle = text.addStyle("err", defstyle);
        StyleConstants.setForeground(_errStyle, Color.red);

        _noticeStyle = text.addStyle("notice", defstyle);
        StyleConstants.setForeground(_noticeStyle, Color.magenta);
    }

    public void actionPerformed (ActionEvent e)
    {
	String cmd = e.getActionCommand();
	if (cmd.equals("send")) {
            sendText();

	} else {
	    System.out.println("Unknown action event: " + cmd);
	}
    }

    public void occupantEntered (OccupantInfo info)
    {
        displayOccupantMessage("*** " + info.username + " entered.");
    }

    public void occupantLeft (OccupantInfo info)
    {
        displayOccupantMessage("*** " + info.username + " left.");
    }

    public void occupantUpdated (OccupantInfo info)
    {
    }

    protected void displayOccupantMessage (String message)
    {
        // stick a newline on the message
        message = message + "\n";

        Document doc = _text.getDocument();
        try {
            doc.insertString(doc.getLength(), message, _noticeStyle);
        } catch (BadLocationException ble) {
            Log.warning("Unable to insert text!? [error=" + ble + "].");
        }
    }

    protected void sendText ()
    {
        String text = _entry.getText();

        // if the message to send begins with /tell then parse it and
        // generate a tell request rather than a speak request
        if (text.startsWith("/tell")) {
            StringTokenizer tok = new StringTokenizer(text);
            // there should be at least three tokens: '/tell target word'
            if (tok.countTokens() < 3) {
                displayError("Usage: /tell username message");
                return;
            }

            // skip the /tell and grab the username
            tok.nextToken();
            String username = tok.nextToken();

            // now strip off everything up to the username to get the
            // message
            int uidx = text.indexOf(username);
            String message = text.substring(uidx + username.length()).trim();

            // request to send this text as a tell message
            _chatdtr.requestTell(username, message);

        } else if (text.startsWith("/clear")) {
            // clear the chat box
            _text.setText("");

        } else {
            // request to send this text as a chat message
            _chatdtr.requestSpeak(text);
        }

        // clear out the input because we sent a request
        _entry.setText("");
    }

    public void displaySpeakMessage (String speaker, String message)
    {
        // wrap the speaker in brackets
        speaker = "<" + speaker + "> ";
        // stick a newline on the message
        message = message + "\n";

        Document doc = _text.getDocument();
        try {
            doc.insertString(doc.getLength(), speaker, _nameStyle);
            doc.insertString(doc.getLength(), message, _msgStyle);
        } catch (BadLocationException ble) {
            Log.warning("Unable to insert text!? [error=" + ble + "].");
        }
    }

    protected void displayError (String message)
    {
{'fraction_non_alphanumeric': 0.10550534100246507, 'fraction_numerical': 0.002793755135579293, 'mean_word_length': 2.3217794759825328, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '131198', 'n_tokens_mistral': 3453, 'n_tokens_neox': 3351, 'n_words': 916}