コード例 #1
0
 /**
  * Create a Transferable to use as the source for a data transfer.
  *
  * @param comp The component holding the data to be transfered. This argument is provided to
  *     enable sharing of TransferHandlers by multiple components.
  * @return The representation of the data to be transfered.
  */
 protected Transferable createTransferable(JComponent comp) {
   exportComp = (JTextComponent) comp;
   shouldRemove = true;
   p0 = exportComp.getSelectionStart();
   p1 = exportComp.getSelectionEnd();
   return (p0 != p1) ? (new TextTransferable(exportComp, p0, p1)) : null;
 }
コード例 #2
0
ファイル: EditorKit.java プロジェクト: niknah/SikuliX-2014
 public void insertBreak(JTextComponent text) {
   indentationLogic = ((EditorPane) text).getIndentationLogic();
   boolean noSelection = text.getSelectionStart() == text.getSelectionEnd();
   if (noSelection) {
     insertNewlineWithAutoIndent(text);
   } else {
     text.replaceSelection("\n");
     // TODO insertNewlineWithAutoIndent
   }
 }
コード例 #3
0
    private void outdentText(JTextComponent textComponent) throws BadLocationException {
      int tabSize =
          ((Integer) textComponent.getDocument().getProperty(PlainDocument.tabSizeAttribute))
              .intValue();

      String selectedText = textComponent.getSelectedText();
      int newLineIndex = selectedText != null ? selectedText.indexOf('\n') : -1;

      if (newLineIndex >= 0) {
        int originalSelectionStart = textComponent.getSelectionStart();
        int selectionStart = originalSelectionStart;
        int selectionEnd = textComponent.getSelectionEnd();

        int lastNewLineBeforeSelection = textComponent.getText(0, selectionStart).lastIndexOf('\n');
        int begin = lastNewLineBeforeSelection >= 0 ? lastNewLineBeforeSelection : 0;
        int end = selectionEnd;

        String text = textComponent.getText(begin, end - begin);
        if (lastNewLineBeforeSelection < 0) {
          text = "\n" + text;
        }
        int len = text.length();
        StringBuffer out = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
          char ch = text.charAt(i);
          out.append(ch);
          if (ch == '\n' && i < len - 1) {
            char next = text.charAt(i + 1);
            int stripCount = 0;
            if (next == '\t') {
              stripCount = 1;
            } else {
              for (; stripCount < tabSize && i + 1 + stripCount < len; stripCount++) {
                next = text.charAt(i + 1 + stripCount);
                if (next != ' ' && next != '\t') {
                  break;
                }
              }
            }

            selectionEnd -= stripCount;
            if (i + begin < originalSelectionStart - 1) {
              selectionStart -= stripCount;
            }
            i += stripCount;
          }
        }

        textComponent.select(begin, end);
        textComponent.replaceSelection(
            lastNewLineBeforeSelection < 0 ? out.toString().substring(1) : out.toString());

        textComponent.select(selectionStart, selectionEnd);
      }
    }
コード例 #4
0
 /**
  * Determines whether the image is selected, and if it's the only thing selected.
  *
  * @return 0 if not selected, 1 if selected, 2 if exclusively selected. "Exclusive" selection is
  *     only returned when editable.
  */
 protected int getSelectionState() {
   int p0 = fElement.getStartOffset();
   int p1 = fElement.getEndOffset();
   if (fContainer instanceof JTextComponent) {
     JTextComponent textComp = (JTextComponent) fContainer;
     int start = textComp.getSelectionStart();
     int end = textComp.getSelectionEnd();
     if (start <= p0 && end >= p1) {
       if (start == p0 && end == p1 && isEditable()) return 2;
       else return 1;
     }
   }
   return 0;
 }
コード例 #5
0
    private void indentText(JTextComponent textComponent) throws BadLocationException {
      String selectedText = textComponent.getSelectedText();
      int newLineIndex = selectedText != null ? selectedText.indexOf('\n') : -1;

      if (newLineIndex >= 0) {
        int originalSelectionStart = textComponent.getSelectionStart();
        int selectionStart = originalSelectionStart;
        int selectionEnd = textComponent.getSelectionEnd();

        int lastNewLineBeforeSelection = textComponent.getText(0, selectionStart).lastIndexOf('\n');
        int begin = lastNewLineBeforeSelection >= 0 ? lastNewLineBeforeSelection : 0;
        int end = selectionEnd;

        String text = textComponent.getText(begin, end - begin);
        int len = text.length();
        StringBuffer out = new StringBuffer(len);
        if (lastNewLineBeforeSelection < 0) {
          out.insert(0, '\t');
          selectionStart++;
          selectionEnd++;
        }
        for (int i = 0; i < len; i++) {
          char ch = text.charAt(i);
          out.append(ch);
          if (ch == '\n' && i < len - 1) {
            out.append("\t");
            selectionEnd++;
            if (begin + i < originalSelectionStart) {
              selectionStart++;
            }
          }
        }

        textComponent.select(begin, end);
        textComponent.replaceSelection(out.toString());

        textComponent.select(selectionStart, selectionEnd);
      } else {
        textComponent.replaceSelection("\t");
      }
    }
コード例 #6
0
 // Create a Transferable implementation that contains the selected text.
 protected Transferable createTransferable(JComponent c) {
   if (c != textComponent) { // ###
     System.out.println("*** createTransferable(): c=" + c);
   }
   source = (JTextComponent) c;
   int start = source.getSelectionStart();
   int end = source.getSelectionEnd();
   Document doc = source.getDocument();
   if (start == end) {
     return null;
   }
   try {
     p0 = doc.createPosition(start);
     p1 = doc.createPosition(end);
     System.out.println(">>> createTransferable(): p0=" + p0 + ", p1=" + p1);
   } catch (BadLocationException e) {
     System.out.println(
         "*** createTransferable(): "
             + "Can't create position - unable to remove text from source.");
   }
   shouldRemove = true;
   String data = source.getSelectedText();
   return new StringSelection(data);
 }