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

The following examples show how to use javax.swing.JEditorPane#viewToModel() . 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: DrawEngineTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void checkModelToViewConsistency(JEditorPane jep) throws Exception {
    Document doc = jep.getDocument();
    for(int i = 0; i <= doc.getLength(); i++) {
        // model-to-view
        Rectangle r = jep.modelToView(i);
        assertNotNull("No m2v translation: offset = " + i + ", docLen = " + doc.getLength(), r);
        
        // view-to-model
        int offset = jep.viewToModel(r.getLocation());
        assertTrue("Invalid v2m translation: " + s(r.getLocation()) + " -> " + offset+ ", docLen = " + doc.getLength(),
                offset >= 0 && offset <= doc.getLength());
        
        // check
        assertEquals("Inconsistent m2v-v2m translation, offset = " + i 
            + ", rectangle = " + s(r) + ", docLen = " + doc.getLength(), i, offset);
    }
}
 
Example 2
Source File: HyperlinkListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void mouseMoved (MouseEvent e) {
    JEditorPane c = (JEditorPane) e.getComponent ();
    final NbEditorDocument doc = (NbEditorDocument) c.getDocument ();
    if (highlight != null) highlight.remove ();
    highlight = null;
    runnable = null;
    if (((e.getModifiers() | e.getModifiersEx()) & InputEvent.CTRL_DOWN_MASK) != InputEvent.CTRL_DOWN_MASK) {
        return;
    }

    int offset = c.viewToModel (e.getPoint ());
    highlight (doc, offset);
    c.repaint ();
}
 
Example 3
Source File: LineNumbersBreakpointsRuler.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void install(final JEditorPane editor) {
    super.install(editor);
    if (editor instanceof LineMarkerPainter) {
        ((LineMarkerPainter) editor).installLineMarker(this);
    }
    removeMouseListener(mouseListener);
    mouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            Point p = e.getPoint();
            p.x = 0;
            int loc = editor.viewToModel(p);

            int currentLine = -1;
            try {
                currentLine = ActionUtils.getLineNumber(editor, loc) + 1;
            } catch (BadLocationException ex) {
                //ignore
            }

            if (currentLine > -1 && (editor instanceof BreakPointListener)) {
                ((BreakPointListener) editor).toggled(currentLine);
            }
        }

    };
    addMouseListener(mouseListener);
}
 
Example 4
Source File: DrawEngineTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void checkViewToModelConsistency(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());
        
        Rectangle eodRectangle = null;
        Rectangle eolRectangle = null;
        for(int y = charHeight / 2 + margin.top; y < jep.getHeight(); y += charHeight) {
            if (eodRectangle == null) {
                eolRectangle = null;
            }
            for(int x = charWidth / 2 + margin.left; x < jep.getWidth(); x += charWidth) {
                Point p = new Point(x, y);

                // view-to-model translation
                int offset = jep.viewToModel(p);
                assertTrue("Invalid v2m translation: " + s(p) + " -> " + offset+ ", docLen = " + doc.getLength(),
                        offset >= 0 && offset <= doc.getLength());
                
                // model-to-view
                Rectangle r = jep.modelToView(offset);
                assertNotNull("No m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r);

                // check
                if (eodRectangle == null) {
                    boolean eod = offset == doc.getLength();
                    boolean eol = doc.getText(offset, 1).charAt(0) == '\n';
                    
                    if (eolRectangle == null) {
                        assertTrue("Inconsistent v2m-m2v translation, point = " + s(p) + " not within " + s(r)
                            + ", offset = " + offset + ", docLen = " + doc.getLength(), r.contains(p));
                        if (eol) {
                            eolRectangle = r;
                        }
                    } else {
                        assertEquals("Inconsistent v2m-m2v translation, for point = " + s(p) + " behing eol"
                            + ", offset = " + offset + ", docLen = " + doc.getLength(), eolRectangle, r);
                    }
                    
                    if (eod) {
                        eodRectangle = r;
                    }
                } else {
                    Point pointAtTheLastLine = new Point(Math.min(p.x, eolRectangle.x), eodRectangle.y);
                    assertTrue("Inconsistent v2m-m2v translation, for point = " + s(p)
                        + " behing eod, point at the last line " + s(pointAtTheLastLine) + " is outside of " + s(r)
                        + ", offset = " + offset + ", docLen = " + doc.getLength(), r.contains(pointAtTheLastLine));
                }
            }
        }
    }