Ejemplo n.º 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();
      }
    }
  /**
   * Makes the label clickable or makes it unclickable. Clickable label features bold and underlined
   * text
   *
   * @param clickable Is the label clickable
   * @param action Action to be performed on click
   * @param rolloverIcon Icon to display on rollover
   * @since Jun 13, 2006 2:00:57 AM
   */
  public void setClickable(
      final boolean clickable, final ActionListener action, final Icon rolloverIcon) {
    if (clickable) {
      //  Set the action
      this.action = action;

      //  Set the offIcon;
      this.offIcon = getIcon();
      setIcon(offIcon);

      //  Add a Mouse Listener
      addMouseListener(this);

      //  Set the text (Undelined and Bolded)
      setText(
          new StringBuffer("<html><b><u>").append(getText()).append("</u></b></html>").toString());

      //  Set the rollover icon
      this.rolloverIcon = rolloverIcon;

      //  Init the Color and Mouse Cursor
      setForeground(offColor);
    } else {
      // Set the action to null
      this.action = null;

      // Remove the Mouse Listener
      removeMouseListener(this);

      //  Change the text back to normal
      final StringBuffer textBuffer = new StringBuffer(getText());
      final int msgEnd = textBuffer.indexOf("</u></b></html>");
      setText(textBuffer.substring(12, msgEnd));

      //  Set the rollover icon
      this.rolloverIcon = null;
    }

    // Repaint the Label
    repaint();
  }