예제 #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();
      }
    }
예제 #2
0
    public void actionPerformed(ActionEvent e) {
      JTextComponent textComponent = getTextComponent(e);
      if (!textComponent.isEditable() || !textComponent.isEnabled()) {
        return;
      }

      try {
        final int position = getCaretPosition();
        final Document document = getDocument();
        int docLen = document.getLength();

        if (docLen == 0) {
          return;
        }

        int fromIndex = Math.max(0, getText(0, position).lastIndexOf('\n'));
        int toIndex = getText(fromIndex + 1, docLen - fromIndex - 1).indexOf('\n');
        toIndex = toIndex < 0 ? docLen : fromIndex + toIndex + 1;
        String text = getText(fromIndex, toIndex - fromIndex);
        if (text.startsWith("\n") || toIndex >= docLen) {
          document.remove(fromIndex, toIndex - fromIndex);
        } else {
          document.remove(fromIndex, toIndex - fromIndex + 1);
        }

        int newPosition = 0;
        if (fromIndex > 0) {
          newPosition = fromIndex + 1;
        }
        docLen = document.getLength();
        if (newPosition > docLen) {
          newPosition = getText().lastIndexOf('\n') + 1;
        }
        setCaretPosition(newPosition);
      } catch (BadLocationException e1) {
        e1.printStackTrace();
      }
    }
예제 #3
0
 @Override
 protected void exportDone(JComponent source, Transferable data, int action) {
   if (action == TransferHandler.MOVE) {
     JTextPane aTextPane = (JTextPane) source;
     int sel_start = aTextPane.getSelectionStart();
     int sel_end = aTextPane.getSelectionEnd();
     Document doc = aTextPane.getDocument();
     try {
       doc.remove(sel_start, sel_end - sel_start);
     } catch (BadLocationException e) {
       Debug.error(me + "exportDone: Problem while trying to remove text\n%s", e.getMessage());
     }
   }
 }
예제 #4
0
 private void expandTab() throws BadLocationException {
   int pos = getCaretPosition();
   Document doc = getDocument();
   doc.remove(pos - 1, 1);
   doc.insertString(pos - 1, _tabString, null);
 }