/**
   * Parse the passed text string for style names and add the styled text to the text pane's
   * document.
   *
   * @param text Text to parse
   * @param pane Pane to modify. The pane also provides the style names
   */
  public static final void parse(String text, JTextPane pane) {
    try {
      Matcher match = TEXT_PATTERN.matcher(text);
      Document doc = pane.getDocument();
      int textStart = 0; // Start of current text area
      Style style = null; // Style for next set of text
      while (match.find()) {

        // Save the current text first
        String styledText = text.substring(textStart, match.start());
        textStart = match.end() + 1;
        if (style != null && styledText != null) {
          doc.insertString(doc.getLength(), styledText, style);
        } // endif

        // Get the next style
        style = pane.getStyle(match.group(1));
        if (style == null) throw new IllegalArgumentException("Unknown style: '" + match.group(1));
      } // endwhile

      // Add the last of the text
      doc.insertString(doc.getLength(), text.substring(textStart), null);
    } catch (BadLocationException e) {
      e.printStackTrace();
      throw new IllegalStateException(
          "This should not happen since I always use the document to "
              + "determine the location to write. It might be due to synchronization problems though");
    }
  }
 private void addText(String str, String styleName, JTextPane pane) {
   Document doc = pane.getDocument();
   int len = doc.getLength();
   try {
     doc.insertString(len, str, pane.getStyle(styleName));
   } catch (javax.swing.text.BadLocationException e) {
   }
 }
Пример #3
0
  public void printText(String s, String username) {
    textPane.setEditable(true);
    if (lastEnd >= 0) {
      textPane.setSelectionStart(lastEnd);
      lastEnd = -1;
    } else textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.replaceSelection("");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    if (username.equals(cwc.fbClient.getUserName()))
      textPane.setCharacterAttributes(textPane.getStyle("UserName"), true);
    else textPane.setCharacterAttributes(textPane.getStyle("FriendName"), true);
    textPane.replaceSelection(username + ":\n");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.setCharacterAttributes(textPane.getStyle("NormalMessage"), true);
    textPane.replaceSelection(" \u2027 " + s + "\n");
    textPane.setEditable(false);
  }
Пример #4
0
  public void printSystemMsg(String s) {
    textPane.setEditable(true);
    if (lastEnd >= 0) {
      textPane.setSelectionStart(lastEnd);
      lastEnd = -1;
    } else textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.replaceSelection("");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.setCharacterAttributes(textPane.getStyle("SystemMessage"), true);
    textPane.replaceSelection(s);
    textPane.setEditable(false);
  }
Пример #5
0
  public void printInputMsg(String name) {
    textPane.setEditable(true);
    if (lastEnd >= 0) {
      textPane.setSelectionStart(lastEnd);
      lastEnd = -1;
    } else textPane.setSelectionStart(textPane.getText().length());
    lastEnd = textPane.getSelectionStart();
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.replaceSelection("");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.setCharacterAttributes(textPane.getStyle("SystemMessage"), true);
    textPane.replaceSelection(name + "\u6b63\u5728\u8f38\u5165\u8a0a\u606f..");
    textPane.setEditable(false);
  }
Пример #6
0
  public ChatPanel(final String roomId, final String gameId) {
    this.roomId = roomId;
    this.gameId = gameId;

    setLayout(new BorderLayout());

    textPane = new WrapTextPane();
    textPane.setEditable(false);

    JScrollPane scrollPane = new JScrollPane(textPane);

    Style defaultStyle = textPane.getStyle("default");
    userNameStyle = textPane.addStyle("userNameStyle", defaultStyle);
    textStyle = textPane.addStyle("textStyle", defaultStyle);
    iconStyle = textPane.addStyle("iconStyle", defaultStyle);
    actionStyle = textPane.addStyle("actionStyle", defaultStyle);

    StyleConstants.setBold(userNameStyle, true);
    StyleConstants.setAlignment(iconStyle, StyleConstants.ALIGN_CENTER);
    StyleConstants.setBold(actionStyle, true);

    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    add(scrollPane, BorderLayout.CENTER);

    text = (StyledDocument) textPane.getDocument();

    final JTextField userInputField = new JTextField(20);
    userInputField.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            // We get the text from the textfield
            String messageContent = userInputField.getText();

            if (messageContent != null) {
              if (ChatPanel.this.gameId != null || ChatPanel.this.roomId != null) {
                AbstractMessage message = null;
                if (ChatPanel.this.gameId != null) {
                  message =
                      new PostGameMessageMessage(
                          CurrentUserInfo.getInstance().getPlayerId(),
                          messageContent,
                          ChatPanel.this.gameId);
                } else {
                  message =
                      new PostRoomMessageMessage(
                          CurrentUserInfo.getInstance().getId(),
                          messageContent,
                          ChatPanel.this.roomId);
                }

                CoreMessageBus.post(message);
                // We reset our text field to "" each time the user presses Enter
                userInputField.setText("");
              }
            }
          }
        });

    add(userInputField, BorderLayout.PAGE_END);

    Dimension dim = new Dimension(200, 400);
    setPreferredSize(dim);

    GuiMessageBus.register(this);
  }