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

The following examples show how to use javax.swing.JEditorPane#modelToView() . 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: JEditorPaneTagJavaElement.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@Override
public Object _makeVisible() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        if (bounds != null) {
            bounds.height = editor.getVisibleRect().height;
            editor.scrollRectToVisible(bounds);
        }
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
    return null;
}
 
Example 2
Source File: ViewHierarchyTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testLongLineUndo() throws Exception {
    loggingOn();
    JEditorPane pane = ViewUpdatesTesting.createPane();
    Document doc = pane.getDocument();
    UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
    int lineLen = 4000;
    StringBuilder sb = new StringBuilder(lineLen + 10);
    for (int i = 0; i < lineLen; i++) {
        sb.append('a');
    }
    sb.append('\n');
    doc.insertString(0, sb.toString(), null);
    pane.modelToView(0);
    doc.remove(0, lineLen);
    pane.modelToView(0);
    undoManager.undo();
    undoManager.redo();
}
 
Example 3
Source File: ViewHierarchyTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testSimpleUndoRedo() throws Exception {
    loggingOn();
    JEditorPane pane = ViewUpdatesTesting.createPane();
    Document doc = pane.getDocument();
    UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
    doc.insertString(0, "abc\ndef\nghi\n", null);
    ViewUpdatesTesting.setViewBounds(pane, 4, 8);
    pane.modelToView(0);
    doc.insertString(4, "o", null);
    doc.remove(3, 3);
    doc.insertString(4, "ab", null);
    doc.remove(7, 2);
    pane.modelToView(0);
    undoManager.undo(); // insert(7,2)
    undoManager.undo(); // remove(4,2)
    undoManager.undo(); // insert(3,3)
    undoManager.undo();
    undoManager.redo();
    undoManager.redo();
    undoManager.redo();
    undoManager.redo();
}
 
Example 4
Source File: JEditorPaneTagJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public void _moveto() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        getDriver().getDevices().moveto(parent.getComponent(), bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
}
 
Example 5
Source File: ViewHierarchyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testRemoveNewline() throws Exception {
    loggingOn();
    JEditorPane pane = ViewUpdatesTesting.createPane();
    Document doc = pane.getDocument();
    UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
    doc.insertString(0, "a\nb", null);
    doc.remove(1, 1);
    undoManager.undo();
    pane.modelToView(0);
}
 
Example 6
Source File: GeneralPHP.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void ClickForTextPopup(EditorOperator eo, String menu) {
    JEditorPaneOperator txt = eo.txtEditorPane();
    JEditorPane epane = (JEditorPane) txt.getSource();
    try {
        Rectangle rct = epane.modelToView(epane.getCaretPosition());
        txt.clickForPopup(rct.x, rct.y);
        JPopupMenuOperator popup = new JPopupMenuOperator();
        popup.pushMenu(menu);
    } catch (BadLocationException ex) {
        System.out.println("=== Bad location");
    }
}
 
Example 7
Source File: GeneralGroovy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void clickForTextPopup(EditorOperator eo, String menu) {
    JEditorPaneOperator txt = eo.txtEditorPane();
    JEditorPane epane = (JEditorPane) txt.getSource();
    try {
        Rectangle rct = epane.modelToView(epane.getCaretPosition());
        txt.clickForPopup(rct.x, rct.y);
        JPopupMenuOperator popup = new JPopupMenuOperator();
        popup.pushMenu(menu);
    } catch (BadLocationException ex) {
        System.out.println("=== Bad location");
    }
}
 
Example 8
Source File: RootViewRandomTesting.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void run(Context context) throws Exception {
    List<JEditorPane> paneList = getPaneList(context);
    Random random = context.container().random();
    StringBuilder log = context.logOpBuilder();
    if (CREATE_PANE == name()) { // Just use ==
        if (log != null) {
            log.append("CREATE_ROOT_VIEW");
        }
        boolean createBounded = !paneList.isEmpty();
        JEditorPane pane = ViewUpdatesTesting.createPane(DocumentTesting.getDocument(context));
        paneList.add(pane);
        if (createBounded) {
            Document doc = pane.getDocument();
            int startOffset = random.nextInt(doc.getLength());
            int endOffset = startOffset + random.nextInt(doc.getLength() - startOffset) + 1;
            ViewUpdatesTesting.setViewBounds(pane, startOffset, endOffset);
            if (log != null) {
                log.append("(").append(startOffset).append(",").append(endOffset).append(")");
            }
        }
        if (log != null) {
            log.append("\n");
            context.logOp(log);
        }
        pane.modelToView(0);

    } else if (RELEASE_PANE == name()) { // Just use ==
        if (!paneList.isEmpty()) {
            int index = random.nextInt(paneList.size());
            paneList.remove(index);
            if (log != null) {
                log.append("DESTROY_ROOT_VIEW[" + index + "]\n");
            }
        }
    }
}
 
Example 9
Source File: GeneralNodeJs.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void clickForTextPopup(EditorOperator eo, String menu) {
    JEditorPaneOperator txt = eo.txtEditorPane();
    JEditorPane epane = (JEditorPane) txt.getSource();
    try {
        Rectangle rct = epane.modelToView(epane.getCaretPosition());
        txt.clickForPopup(rct.x, rct.y);
        JPopupMenuOperator popup = new JPopupMenuOperator();
        popup.pushMenu(menu);
    } catch (BadLocationException ex) {
        System.out.println("=== Bad location");
    }
}
 
Example 10
Source File: GeneralRequire.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void clickForTextPopup(EditorOperator eo, String menu) {
    JEditorPaneOperator txt = eo.txtEditorPane();
    JEditorPane epane = (JEditorPane) txt.getSource();
    try {
        Rectangle rct = epane.modelToView(epane.getCaretPosition());
        txt.clickForPopup(rct.x, rct.y);
        JPopupMenuOperator popup = new JPopupMenuOperator();
        popup.pushMenu(menu);
    } catch (BadLocationException ex) {
        System.out.println("=== Bad location");
    }
}
 
Example 11
Source File: GeneralAngular.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void clickForTextPopup(EditorOperator eo, String menu) {
    JEditorPaneOperator txt = eo.txtEditorPane();
    JEditorPane epane = (JEditorPane) txt.getSource();
    try {
        Rectangle rct = epane.modelToView(epane.getCaretPosition());
        txt.clickForPopup(rct.x, rct.y);
        JPopupMenuOperator popup = new JPopupMenuOperator();
        popup.pushMenu(menu);
    } catch (BadLocationException ex) {
        System.out.println("=== Bad location");
    }
}
 
Example 12
Source File: GeneralKnockout.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void clickForTextPopup(EditorOperator eo, String menu) {
    JEditorPaneOperator txt = eo.txtEditorPane();
    JEditorPane epane = (JEditorPane) txt.getSource();
    try {
        Rectangle rct = epane.modelToView(epane.getCaretPosition());
        txt.clickForPopup(rct.x, rct.y);
        JPopupMenuOperator popup = new JPopupMenuOperator();
        popup.pushMenu(menu);
    } catch (BadLocationException ex) {
        System.out.println("=== Bad location");
    }
}
 
Example 13
Source File: ViewUpdatesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLongLineInsert() throws Exception {
        loggingOn();
        ViewUpdatesTesting.setTestValues(ViewUpdatesTesting.NO_OP_TEST_VALUE);
        JEditorPane pane = ViewUpdatesTesting.createPane();
        Document doc = pane.getDocument();
        DocumentView docView = DocumentView.get(pane);
        pane.modelToView(0); // Cause initialization
        int offset;
        String text;
        
        offset = 0;
        int lineLen = 4000;
        StringBuilder sb = new StringBuilder(lineLen + 10);
        for (int i = 0; i < lineLen; i++) {
            sb.append('a');
        }
        sb.append('\n');
        text = sb.toString();
        ViewUpdatesTesting.setTestValues(
/*rebuildCause*/        ViewBuilder.RebuildCause.MOD_UPDATE,
/*createLocalViews*/    false,
/*startCreationOffset*/ 0,
/*matchOffset*/         offset + text.length() + 1, // createLocalViews == false
/*endCreationOffset*/   offset + text.length() + 1,
/*bmReuseOffset*/       0,
/*bmReusePView*/        docView.getParagraphView(0),
/*bmReuseLocalIndex*/   0,
/*amReuseOffset*/       Integer.MAX_VALUE, // createLocalViews == false
/*amReusePIndex*/       1,
/*amReusePView*/        null,
/*amReuseLocalIndex*/   0
        );
        doc.insertString(offset, text, null);
        ViewUpdatesTesting.checkViews(docView, 0, -1,
            4001, P_VIEW /* e:4001 */,
            1, P_VIEW /* e:4002 */
        );

        ViewUpdatesTesting.setTestValues();
    }
 
Example 14
Source File: JEditorPanePosJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public void _moveto() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    try {
        Rectangle bounds = editor.modelToView(pos);
        getDriver().getDevices().moveto(parent.getComponent(), bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Invalid position " + pos + "(" + e.getMessage() + ")", e);
    }
}
 
Example 15
Source File: JEditorPanePosJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public Point _getMidpoint() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    try {
        Rectangle bounds = editor.modelToView(pos);
        return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Invalid position " + pos + "(" + e.getMessage() + ")", e);
    }
}
 
Example 16
Source File: ViewHierarchyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testEmptyCustomBounds() throws Exception {
    loggingOn();    
    JEditorPane pane = ViewUpdatesTesting.createPane();
    Document doc = pane.getDocument();
    pane.modelToView(0);
    doc.insertString(0, "hello\nworld\ngood\nmorning", null);
    Position startPos = doc.createPosition(3);
    pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, startPos);
    pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, startPos);
    pane.modelToView(0); // Force rebuild of VH

    Position endPos = doc.createPosition(2);
    pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos);
    pane.modelToView(0); // Force rebuild of VH
}
 
Example 17
Source File: JEditorPaneTagJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public Point _getMidpoint() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index + "("
                + "StartOffset: " + startOffset + " EndOffset: " + endOffset + ")", e);
    }
}
 
Example 18
Source File: CloneableEditorSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Forcibly create one editor component. Then set the caret
* to the given position.
* @param pos where to place the caret
* @param column where to place the caret
* @param reuse if true, the infrastructure tries to reuse other, already opened editor
 * for the purpose of opening this file/line. 
* @return always non-<code>null</code> editor
*/
private final Pane openAtImpl(final PositionRef pos, final int column, boolean reuse) {
    CloneableEditorSupport redirect = CloneableEditorSupportRedirector.findRedirect(this);
    if (redirect != null) {
        return redirect.openAtImpl(pos, column, reuse);
    }
    final Pane e = openPane(reuse);
    final Task t = prepareDocument();
    e.ensureVisible();
    class Selector implements TaskListener, Runnable {
        private boolean documentLocked = false;

        public void taskFinished(org.openide.util.Task t2) {
            javax.swing.SwingUtilities.invokeLater(this);
            t2.removeTaskListener(this);
        }

        public void run() {
            // #25435. Pane can be null.
            JEditorPane ePane = e.getEditorPane();

            if (ePane == null) {
                return;
            }

            StyledDocument doc = getDocument();

            if (doc == null) {
                return; // already closed or error loading
            }

            if (!documentLocked) {
                documentLocked = true;
                doc.render(this);
            } else {
                Caret caret = ePane.getCaret();

                if (caret == null) {
                    return;
                }

                int offset;

                // Pane's document may differ - see #204980
                Document paneDoc = ePane.getDocument();
                if (paneDoc instanceof StyledDocument && paneDoc != doc) {
                    if (ERR.isLoggable(Level.FINE)) {
                        ERR.fine("paneDoc=" + paneDoc + "\n !=\ndoc=" + doc); // NOI18N
                    }
                    doc = (StyledDocument) paneDoc;
                }

                javax.swing.text.Element el = NbDocument.findLineRootElement(doc);
                el = el.getElement(el.getElementIndex(pos.getOffset()));
                offset = el.getStartOffset() + Math.max(0, column);

                if (offset > el.getEndOffset()) {
                    offset = Math.max(el.getStartOffset(), el.getEndOffset() - 1);
                }

                caret.setDot(offset);

                try { // scroll to show reasonable part of the document
                    Rectangle r = ePane.modelToView(offset);
                    if (r != null) {
                        r.height *= 5;
                        ePane.scrollRectToVisible(r);
                    }
                } catch (BadLocationException ex) {
                    ERR.log(Level.WARNING, "Can't scroll to text: pos.getOffset=" + pos.getOffset() //NOI18N
                        + ", column=" + column + ", offset=" + offset //NOI18N
                        + ", doc.getLength=" + doc.getLength(), ex); //NOI18N
                }
            }
        }
    }
    t.addTaskListener(new Selector());

    return e;
}
 
Example 19
Source File: testPalette.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void DragSomething(
    String sFile,
    String sLocation,
    String sComponent
  )
{
  // Locate coords of HTML code
  EditorOperator eoPHP = new EditorOperator( sFile );
  eoPHP.setCaretPosition( sLocation, false );

 int iX = 0, iY = 0;

 JEditorPaneOperator txt = eoPHP.txtEditorPane( );
 JEditorPane epane =  ( JEditorPane )txt.getSource( );
 try
 {
   Rectangle rct = epane.modelToView( epane.getCaretPosition( ) );
   iX = rct.x;
   iY = rct.y;
 }
 catch( BadLocationException ex )
 {
   fail( "Unable to detect destination location." );
 }

 //TopComponentOperator top = new TopComponentOperator( "EmptyPHPWebPage.php" );
 TopComponentOperator pal = new TopComponentOperator( "Palette" );
 JListOperator list = new JListOperator( pal, 0 );

 ListModel lmd = list.getModel( );
 int iIndex = list.findItemIndex( sComponent );
 list.selectItem( iIndex );
 Point pt = list.getClickPoint( iIndex );

 MouseRobotDriver m_mouseDriver = new MouseRobotDriver(new Timeout("", 500));
 m_mouseDriver.moveMouse( list, pt.x, pt.y );
 m_mouseDriver.pressMouse( InputEvent.BUTTON1_MASK, 0 );
 m_mouseDriver.enterMouse( txt );
 m_mouseDriver.dragMouse( txt, iX, iY, InputEvent.BUTTON1_MASK, 0 );
 m_mouseDriver.releaseMouse( InputEvent.BUTTON1_MASK, 0 );

 return;
}
 
Example 20
Source File: DrawEngineTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void checkModelToViewCorrectness(JEditorPane jep) throws Exception {
        Document doc = jep.getDocument();
        
        assertTrue("Expecting BaseTextUI", jep.getUI() instanceof BaseTextUI);
        BaseTextUI btui = (BaseTextUI) jep.getUI();
        Insets margin = btui.getEditorUI().getTextMargin();
        int charWidth = btui.getEditorUI().defaultSpaceWidth;
        int charHeight = btui.getEditorUI().getLineHeight();

//        System.out.println("### charWidth = " + charWidth + ", charHeight = " + charHeight
//            + ", docLen = " + doc.getLength()
//            + ", jep.width = " + jep.getWidth()
//            + ", jep.height = " + jep.getHeight());
        
        for(int offset = 0; offset <= doc.getLength(); offset++) {
            // model-to-view translation
            Rectangle r = jep.modelToView(offset);
            assertNotNull("No m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r);

            View rootView = Utilities.getRootView(jep, DrawEngineDocView.class);
            int line = rootView.getViewIndex(offset, Position.Bias.Forward);
            int col = offset - rootView.getView(line).getStartOffset();

// XXX: this would be necessary for handling tabs, but it uses DrawEngineLineView
//      and therefore is not independent from the tested code, the inverse transformation
//      will be needed in checkViewToModel
//            int col = Utilities.getVisualColumn((BaseDocument)doc, offset);
//            int nextCol = offset >= rootView.getView(line).getEndOffset() - 1 ? col + 1 : Utilities.getVisualColumn((BaseDocument)doc, offset + 1);
//            System.out.println("### offset = " + offset + ", col = " + col + ", nextCol = " + nextCol + ", docLen = " + doc.getLength());

            Rectangle r2 = new Rectangle(
                margin.left + col * charWidth,
                margin.top + line * charHeight,
// XXX: see above comment about the tabs handling
//                (nextCol - col) * charWidth,
                charWidth,
                charHeight
            );

            assertEquals("Incorrect m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r2, r);
        }
    }