Example #1
0
    public void execute() {
      if (!isEditable() || !isEnabled()) {
        return;
      }
      int position = lastClickPoint != null ? viewToModel(lastClickPoint) : getCaretPosition();
      lastClickPoint = null;
      Document document = getDocument();
      String selectedText = getSelectedText();

      try {
        if (selectedText != null && !CommonUtil.isEmpty(selectedText)) {
          String trimmed = selectedText.trim();
          if (trimmed.startsWith("<!--") && trimmed.endsWith("-->")) {
            StringBuffer buffer = new StringBuffer(selectedText);
            int pos = buffer.indexOf("<!--");
            buffer.delete(pos, pos + 4);
            pos = buffer.lastIndexOf("-->");
            buffer.delete(pos, pos + 3);
            replaceSelection(buffer.toString());
          } else {
            String newSelection = "<!--" + selectedText + "-->";
            replaceSelection(newSelection);
          }
        } else {
          final int docLen = document.getLength();
          int fromIndex = Math.max(0, getText(0, position).lastIndexOf('\n'));
          int toIndex = getText(fromIndex + 1, docLen - position).indexOf('\n');
          toIndex = toIndex < 0 ? docLen : fromIndex + toIndex;
          String textToComment = getText(fromIndex, toIndex - fromIndex + 1);

          if (textToComment.startsWith("\n")) {
            textToComment = textToComment.substring(1);
            fromIndex++;
          }
          if (textToComment.endsWith("\n")) {
            textToComment = textToComment.substring(0, textToComment.length() - 1);
            toIndex--;
          }
          String trimmed = textToComment.trim();
          if (trimmed.startsWith("<!--") && trimmed.endsWith("-->")) {
            int pos = textToComment.lastIndexOf("-->");
            document.remove(fromIndex + pos, 3);
            pos = textToComment.indexOf("<!--");
            document.remove(fromIndex + pos, 4);
          } else {
            document.insertString(Math.min(toIndex + 1, docLen), "-->", null);
            document.insertString(fromIndex, "<!--", null);
          }
        }
      } catch (BadLocationException e1) {
        e1.printStackTrace();
      }
    }
Example #2
0
  private void onReceiveChar(char character) {

    buffer.append(character);

    if (buffer.charAt(buffer.length() - 1) == '\n') {
      onReceiveLine(buffer.toString());
      buffer.delete(0, buffer.length());
    }
  }
Example #3
0
  /**
   * Searches the contact list when any key, different from "space", "+" or "-" is typed. Selects
   * the Contact name closest to the typed string. The time between two button presses is checked to
   * determine whether the user makes a new search or a continuous search. When user types the same
   * letter consecutively the search mechanism selects the next Contact name starting with the same
   * letter.
   */
  public void keyTyped(KeyEvent e) {

    // Nothing to do if the contact list is empty
    if (contactList.getModel().getSize() <= 0) return;

    long eventTimestamp = e.getWhen();
    char keyChar = e.getKeyChar();

    if (keyChar == ' ') {
      openOrCloseGroup();
    } else if (keyChar == '+') {
      openGroup();
    } else if (keyChar == '-') {
      closeGroup();
    } else {

      if ((lastTypedTimestamp - eventTimestamp) > 1000) {
        keyBuffer.delete(0, keyBuffer.length() - 1);
      }
      this.lastTypedTimestamp = eventTimestamp;
      this.keyBuffer.append(keyChar);

      boolean selectedSameLetterContact = false;

      int selectedIndex = this.contactList.getSelectedIndex();

      // Check if there's any selected contact node and get its name.
      if (selectedIndex != -1) {
        Object selectedObject = this.contactList.getSelectedValue();

        if (selectedObject instanceof MetaContact) {
          String selectedContactName = ((MetaContact) selectedObject).getDisplayName();

          if (selectedContactName != null) {
            selectedSameLetterContact =
                selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString());
          }
        } else if (selectedObject instanceof ConferenceChatContact) {
          String selectedContactName = ((ConferenceChatContact) selectedObject).getName();

          if (selectedContactName != null) {
            selectedSameLetterContact =
                selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString());
          }
        }
      }
      // The search starts from the beginning if:
      // 1) the newly entered character is different from the last one
      // or
      // 2) the currently selected contact starts with a different letter
      int contactIndex =
          contactList.getNextMatch(
              keyBuffer.toString(),
              (lastTypedKey != keyChar || !selectedSameLetterContact) ? 0 : selectedIndex + 1,
              Position.Bias.Forward);

      int currentlySelectedIndex = this.contactList.getSelectedIndex();

      if (currentlySelectedIndex != contactIndex && contactIndex != -1) {
        this.contactList.setSelectedIndex(contactIndex);
        currentlySelectedIndex = contactList.getSelectedIndex();
      }

      this.contactList.ensureIndexIsVisible(currentlySelectedIndex);

      this.lastTypedKey = keyChar;
    }
  }