/** Determine if message text changed and handle appropriately. */
 private void checkMessageTextChanged() {
   String newText = message.getText();
   if (!newText.equals(messageText)) {
     messageText = newText;
     Session.getInstance().getEventBus().notifyObservers(MessageTextAreaChangedEvent.getEvent());
   }
 }
  /** Wires up events. */
  private void setupEvents() {
    // user clicked in message text box
    message.addFocusHandler(
        new FocusHandler() {
          public void onFocus(final FocusEvent inEvent) {
            if ((" " + getStyleName() + " ")
                .contains(StaticResourceBundle.INSTANCE.coreCss().small())) {
              removeStyleName(StaticResourceBundle.INSTANCE.coreCss().small());
              onExpand();
            }
          }
        });

    message.addValueChangedHandler(
        new ValueChangeHandler<String>() {
          public void onValueChange(final ValueChangeEvent<String> newValue) {
            checkMessageTextChanged();
          }
        });

    // user typed in message text box
    message.addKeystrokeHandler(
        new KeyUpHandler() {
          public void onKeyUp(final KeyUpEvent ev) {
            if (ev.getNativeKeyCode() == KeyCodes.KEY_ENTER
                && ev.isControlKeyDown()
                && message.getText().length() > 0) {
              checkMessageTextChanged();
              handlePostMessage();
            } else {
              checkMessageTextChanged();
            }
          }
        });

    // user clicked on post button
    postButton.addClickHandler(
        new ClickHandler() {
          public void onClick(final ClickEvent ev) {
            checkMessageTextChanged();
            handlePostMessage();
          }
        });

    final EventBus eventBus = Session.getInstance().getEventBus();
    eventBus.addObserver(
        MessageStreamAppendEvent.class,
        new Observer<MessageStreamAppendEvent>() {
          public void update(final MessageStreamAppendEvent event) {
            errorMsg.setVisible(false);
            addStyleName(StaticResourceBundle.INSTANCE.coreCss().small());
            messageText = "";
            message.setText(postBoxDefaultText);
            onRemainingCharactersChanged();
            links.close();
          }
        });

    eventBus.addObserver(
        GotSystemSettingsResponseEvent.class,
        new Observer<GotSystemSettingsResponseEvent>() {
          public void update(final GotSystemSettingsResponseEvent event) {
            String warning = event.getResponse().getContentWarningText();
            if (warning != null && !warning.isEmpty()) {
              contentWarning.getElement().setInnerHTML(warning);
            } else {
              contentWarning.setVisible(false);
            }
            message.setVisible(true);
          }
        });

    eventBus.addObserver(
        MessageTextAreaChangedEvent.getEvent(),
        new Observer<MessageTextAreaChangedEvent>() {
          public void update(final MessageTextAreaChangedEvent arg1) {
            // the value changed - make sure we're not stuck in the disabled, non-editable mode
            if ((" " + getStyleName() + " ")
                .contains(StaticResourceBundle.INSTANCE.coreCss().small())) {
              removeStyleName(StaticResourceBundle.INSTANCE.coreCss().small());
            }
            onRemainingCharactersChanged();
          }
        });

    eventBus.addObserver(
        MessageAttachmentChangedEvent.class,
        new Observer<MessageAttachmentChangedEvent>() {
          public void update(final MessageAttachmentChangedEvent evt) {
            errorMsg.setVisible(false);
            attachment = evt.getAttachment();
            if (attachment == null && messageText.isEmpty()) {
              hidePostButton();
            } else {
              showPostButton();
            }
          }
        });

    eventBus.addObserver(
        new ErrorPostingMessageToNullScopeEvent(),
        new Observer<ErrorPostingMessageToNullScopeEvent>() {
          public void update(final ErrorPostingMessageToNullScopeEvent event) {
            errorMsg.setText(event.getErrorMsg());
            errorMsg.setVisible(true);
            showPostButton();
          }
        });
  }