Java Code Examples for javax.swing.JEditorPane#setSelectionStart()

The following examples show how to use javax.swing.JEditorPane#setSelectionStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToggleBlockCommentActionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected void testSelectionInFile(String file) throws Exception {
    FileObject fo = getTestFile(file);
    assertNotNull(fo);
    String source = readFile(fo);

    int sourcePosStart = source.indexOf('^');
    int sourcePosEnd = source.lastIndexOf('^') - 1;
    assertNotNull(sourcePosStart);
    String sourceWithoutMarker = source.substring(0, sourcePosStart) + source.substring(sourcePosStart+1);
    sourceWithoutMarker = sourceWithoutMarker.substring(0, sourcePosEnd) + sourceWithoutMarker.substring(sourcePosEnd+1);

    JEditorPane ta = getPane(sourceWithoutMarker);
    ta.setSelectionStart(sourcePosStart);
    ta.setSelectionEnd(sourcePosEnd);
    BaseDocument doc = (BaseDocument) ta.getDocument();

    Action a = new ToggleBlockCommentAction();
    a.actionPerformed(new ActionEvent(ta, 0, null));

    doc.getText(0, doc.getLength());

    String target = doc.getText(0, doc.getLength());
    assertDescriptionMatches(file, target, false, ".toggleComment");
}
 
Example 2
Source File: ToggleBlockCommentActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void testSelectionInFile(String file) throws Exception {
    FileObject fo = getTestFile(file);
    assertNotNull(fo);
    String source = readFile(fo);

    int sourcePosStart = source.indexOf('^');
    assertNotNull(sourcePosStart);
    int sourcePosEnd = source.lastIndexOf('^');
    assertNotNull(sourcePosEnd);
    String sourceWithoutMarkers = source.substring(0, sourcePosStart) + source.substring(sourcePosStart + 1, sourcePosEnd) + source.substring(sourcePosEnd + 1);

    JEditorPane ta = getPane(sourceWithoutMarkers);
    Caret caret = ta.getCaret();
    caret.setDot(sourcePosStart);
    ta.setSelectionStart(sourcePosStart);
    ta.setSelectionEnd(sourcePosEnd - 1);
    BaseDocument doc = (BaseDocument) ta.getDocument();

    Action a = new ToggleBlockCommentAction();
    a.actionPerformed(new ActionEvent(ta, 0, null));

    doc.getText(0, doc.getLength());
    doc.insertString(caret.getDot(), "^", null);

    String target = doc.getText(0, doc.getLength());
    assertDescriptionMatches(file, target, false, ".toggleComment");
}
 
Example 3
Source File: PhpTypedTextInterceptorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void insertChar(String original, char insertText, String expected, String selection, boolean codeTemplateMode, Map<String, Object> formatPrefs) throws Exception {
    String source = wrapAsPhp(original);
    String reformatted = wrapAsPhp(expected);
    Formatter formatter = getFormatter(null);

    int sourcePos = source.indexOf('^');
    assertNotNull(sourcePos);
    source = source.substring(0, sourcePos) + source.substring(sourcePos+1);

    int reformattedPos = reformatted.indexOf('^');
    assertNotNull(reformattedPos);
    reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);

    JEditorPane ta = getPane(source);
    Caret caret = ta.getCaret();
    caret.setDot(sourcePos);
    if (selection != null) {
        int start = original.indexOf(selection);
        assertTrue(start != -1);
        assertTrue("Ambiguous selection - multiple occurrences of selection string",
                original.indexOf(selection, start+1) == -1);
        ta.setSelectionStart(start);
        ta.setSelectionEnd(start+selection.length());
        assertEquals(selection, ta.getSelectedText());
    }

    BaseDocument doc = (BaseDocument) ta.getDocument();

    if (codeTemplateMode) {
        // Copied from editor/codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java
        String EDITING_TEMPLATE_DOC_PROPERTY = "processing-code-template"; // NOI18N
        doc.putProperty(EDITING_TEMPLATE_DOC_PROPERTY, Boolean.TRUE);
    }

    if (formatter != null) {
        configureIndenters(doc, formatter, true);
    }

    setupDocumentIndentation(doc, null);

    if (formatter != null && formatPrefs != null) {
        setOptionsForDocument(doc, formatPrefs);
    }
    runKitAction(ta, DefaultEditorKit.defaultKeyTypedAction, ""+insertText);

    String formatted = doc.getText(0, doc.getLength());
    assertEquals(reformatted, formatted);

    if (reformattedPos != -1) {
        assertEquals(reformattedPos, caret.getDot());
    }
}
 
Example 4
Source File: CslTestBase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void insertChar(String original, char insertText, String expected, String selection, boolean codeTemplateMode) throws Exception {
    String source = original;
    String reformatted = expected;
    Formatter formatter = getFormatter(null);

    int sourcePos = source.indexOf('^');
    assertNotNull(sourcePos);
    source = source.substring(0, sourcePos) + source.substring(sourcePos+1);

    int reformattedPos = reformatted.indexOf('^');
    assertNotNull(reformattedPos);
    reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);

    JEditorPane ta = getPane(source);
    Caret caret = ta.getCaret();
    caret.setDot(sourcePos);
    if (selection != null) {
        int start = original.indexOf(selection);
        assertTrue(start != -1);
        assertTrue("Ambiguous selection - multiple occurrences of selection string",
                original.indexOf(selection, start+1) == -1);
        ta.setSelectionStart(start);
        ta.setSelectionEnd(start+selection.length());
        assertEquals(selection, ta.getSelectedText());
    }

    BaseDocument doc = (BaseDocument) ta.getDocument();

    if (codeTemplateMode) {
        // Copied from editor/codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java
        String EDITING_TEMPLATE_DOC_PROPERTY = "processing-code-template"; // NOI18N
        doc.putProperty(EDITING_TEMPLATE_DOC_PROPERTY, Boolean.TRUE);
    }

    if (formatter != null) {
        configureIndenters(doc, formatter, true);
    }

    setupDocumentIndentation(doc, null);

    runKitAction(ta, DefaultEditorKit.defaultKeyTypedAction, ""+insertText);

    String formatted = doc.getText(0, doc.getLength());
    assertEquals(reformatted, formatted);

    if (reformattedPos != -1) {
        assertEquals(reformattedPos, caret.getDot());
    }
}
 
Example 5
Source File: CslTestBase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected JEditorPane getPane(String text) throws Exception {
    if (!SwingUtilities.isEventDispatchThread()) {
        fail("You must run this test from the event dispatch thread! To do that, add @Override protected boolean runInEQ() { return true } from your testcase!");
    }
    String BEGIN = "$start$"; // NOI18N
    String END = "$end$"; // NOI18N
    int sourceStartPos = text.indexOf(BEGIN);
    int caretPos = -1;
    int sourceEndPos = -1;
    if (sourceStartPos != -1) {
        text = text.substring(0, sourceStartPos) + text.substring(sourceStartPos+BEGIN.length());
        sourceEndPos = text.indexOf(END);
        assertTrue(sourceEndPos != -1);
        text = text.substring(0, sourceEndPos) + text.substring(sourceEndPos+END.length());
    } else {
        caretPos = text.indexOf('^');
        if (caretPos != -1) {
            text = text.substring(0, caretPos) + text.substring(caretPos+1);
        }
    }

    JEditorPane pane = new JEditorPane();
    pane.setContentType(getPreferredMimeType());
    final NbEditorKit kit = ((NbEditorKit)getEditorKit(getPreferredMimeType()));


    Thread preload = new Thread(new Runnable() {

        @Override
        public void run() {
            // Preload actions and other stuff
            if (kit instanceof Callable) {
                try {
                    ((Callable) kit).call();
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
            kit.getActions();
        }
    });
    preload.start();
    preload.join();
    pane.setEditorKit(kit);
    pane.setText(text);

    BaseDocument bdoc = (BaseDocument)pane.getDocument();

    bdoc.putProperty(org.netbeans.api.lexer.Language.class, getPreferredLanguage().getLexerLanguage());
    bdoc.putProperty("mimeType", getPreferredMimeType());

    //bdoc.insertString(0, text, null);
    if (sourceStartPos != -1) {
        assertTrue(sourceEndPos != -1);
        pane.setSelectionStart(sourceStartPos);
        pane.setSelectionEnd(sourceEndPos);
    } else if (caretPos != -1) {
        pane.getCaret().setDot(caretPos);
    }
    pane.getCaret().setSelectionVisible(true);

    return pane;
}
 
Example 6
Source File: BugReportService.java    From Shuffle-Move with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return
 */
@SuppressWarnings("serial")
private JComponent getInstructionPanel() {
   ConfigManager preferencesManager = getUser().getPreferencesManager();
   final int width = preferencesManager.getIntegerValue(KEY_POPUP_WIDTH, DEFAULT_POPUP_WIDTH);
   JEditorPane textPane = new JEditorPane() {
      @Override
      public Dimension getPreferredSize() {
         Dimension d = super.getPreferredSize();
         d.width = width - 40;
         return d;
      }
   };
   textPane.setFocusable(false);
   textPane.setEditable(false);
   textPane.setContentType("text/html");
   textPane.addHyperlinkListener(new HyperlinkListener() {
      @Override
      public void hyperlinkUpdate(HyperlinkEvent e) {
         if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            if (Desktop.isDesktopSupported()) {
               try {
                  Desktop.getDesktop().browse(e.getURL().toURI());
               } catch (IOException | URISyntaxException | NullPointerException e1) {
                  LOG.severe(getString(KEY_BAD_LINK, e1.getMessage()));
               }
            }
         }
      }
   });
   JScrollPane jsp = new JScrollPane(textPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
         ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
   jsp.getVerticalScrollBar().setUnitIncrement(30);
   String text = getUser().getPathManager().readEntireDocument(getFilePath(), getDefaultFileKey());
   textPane.setText(text);
   textPane.setSelectionStart(0);
   textPane.setSelectionEnd(0);
   textPane.repaint();
   jsp.validate();
   return jsp;
}