Example usage for javax.swing.text JTextComponent modelToView

List of usage examples for javax.swing.text JTextComponent modelToView

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent modelToView.

Prototype

@Deprecated(since = "9")
public Rectangle modelToView(int pos) throws BadLocationException 

Source Link

Document

Converts the given location in the model to a place in the view coordinate system.

Usage

From source file:LineHighlightPainter.java

public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {

    Rectangle r0 = null, r1 = null, rbounds = bounds.getBounds();
    int xmax = rbounds.x + rbounds.width;
    try {//from   w w  w.  j  a  v  a2s  .  c  o m
        r0 = c.modelToView(p0);
        r1 = c.modelToView(p1);
    } catch (BadLocationException ex) {
        return;
    }
    if ((r0 == null) || (r1 == null))
        return;

    g.setColor(c.getSelectionColor());

    // special case if p0 and p1 are on the same line
    if (r0.y == r1.y) {
        paintLine(g, r0, r1.x);
        return;
    }

    // first line, from p1 to end-of-line
    paintLine(g, r0, xmax);

    // all the full lines in between, if any (assumes that all lines have
    // the same height--not a good assumption with JEditorPane/JTextPane)
    r0.y += r0.height; // move r0 to next line
    r0.x = rbounds.x; // move r0 to left edge
    while (r0.y < r1.y) {
        paintLine(g, r0, xmax);
        r0.y += r0.height; // move r0 to next line
    }

    // last line, from beginning-of-line to p1
    paintLine(g, r0, r1.x);
}

From source file:CornerCaret.java

public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == null)
        return;/*from  w  w  w .j  a  va2  s .  c  o  m*/

    int dot = getDot();
    Rectangle r = null;
    try {
        r = comp.modelToView(dot);
    } catch (BadLocationException e) {
        return;
    }
    if (r == null)
        return;

    int dist = r.height * 4 / 5 - 3; // will be distance from r.y to top

    if ((x != r.x) || (y != r.y + dist)) {
        // paint() has been called directly, without a previous call to
        // damage(), so do some cleanup. (This happens, for example, when
        // the
        // text component is resized.)
        repaint(); // erase previous location of caret
        x = r.x; // set new values for x,y,width,height
        y = r.y + dist;
        width = 5;
        height = 5;
    }

    if (isVisible()) {
        g.setColor(comp.getCaretColor());
        g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4); // 5 vertical
        // pixels
        g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4); // 5 horiz
        // px
    }
}

From source file:FancyCaret.java

public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == null)
        return;/*from w w w  .j  a  v a  2s.  c  o m*/

    int dot = getDot();
    Rectangle r = null;
    char dotChar;
    try {
        r = comp.modelToView(dot);
        if (r == null)
            return;
        dotChar = comp.getText(dot, 1).charAt(0);
    } catch (BadLocationException e) {
        return;
    }

    if ((x != r.x) || (y != r.y)) {
        repaint();
        x = r.x;
        y = r.y;
        height = r.height;
    }

    g.setColor(comp.getCaretColor());
    g.setXORMode(comp.getBackground());

    if (dotChar == '\n') {
        int diam = r.height;
        if (isVisible())
            g.fillArc(r.x - diam / 2, r.y, diam, diam, 270, 180); // half circle
        width = diam / 2 + 2;
        return;
    }

    if (dotChar == '\t')
        try {
            Rectangle nextr = comp.modelToView(dot + 1);
            if ((r.y == nextr.y) && (r.x < nextr.x)) {
                width = nextr.x - r.x;
                if (isVisible())
                    g.fillRoundRect(r.x, r.y, width, r.height, 12, 12);
                return;
            } else
                dotChar = ' ';
        } catch (BadLocationException e) {
            dotChar = ' ';
        }

    width = g.getFontMetrics().charWidth(dotChar);
    if (isVisible())
        g.fillRect(r.x, r.y, width, r.height);
}

From source file:LineHighlightPainter.java

public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {

    Rectangle r0 = null, r1 = null, rbounds = bounds.getBounds();
    int xmax = rbounds.x + rbounds.width; // x coordinate of right edge
    try { // convert positions to pixel coordinates
        r0 = c.modelToView(p0);
        r1 = c.modelToView(p1);/*from  w w  w . jav a  2 s  .c o  m*/
    } catch (BadLocationException ex) {
        return;
    }
    if ((r0 == null) || (r1 == null))
        return;

    g.setColor(c.getSelectionColor());

    // special case if p0 and p1 are on the same line
    if (r0.y == r1.y) {
        paintLine(g, r0, r1.x);
        return;
    }

    // first line, from p1 to end-of-line
    paintLine(g, r0, xmax);

    // all the full lines in between, if any (assumes that all lines have
    // the same height--not a good assumption with JEditorPane/JTextPane)
    r0.y += r0.height; // move r0 to next line
    r0.x = rbounds.x; // move r0 to left edge
    while (r0.y < r1.y) {
        paintLine(g, r0, xmax);
        r0.y += r0.height; // move r0 to next line
    }

    // last line, from beginning-of-line to p1
    paintLine(g, r0, r1.x);
}

From source file:org.executequery.gui.editor.autocomplete.QueryEditorAutoCompletePopupProvider.java

public void firePopupTrigger() {

    try {/* ww w .  jav  a 2s  .  co m*/

        final JTextComponent textComponent = queryEditorTextComponent();

        Caret caret = textComponent.getCaret();
        final Rectangle caretCoords = textComponent.modelToView(caret.getDot());

        addFocusActions();

        resetCount = 0;
        captureAndResetListValues();

        popupMenu().show(textComponent, caretCoords.x, caretCoords.y + caretCoords.height);
        textComponent.requestFocus();

    } catch (BadLocationException e) {

        debug("Error on caret coordinates", e);
    }

}

From source file:ro.nextreports.designer.querybuilder.SQLViewPanel.java

private void initUI() {
    sqlEditor = new Editor() {
        public void afterCaretMove() {
            removeHighlightErrorLine();// w  w  w .j  a  va  2 s . c  om
        }
    };
    this.queryArea = sqlEditor.getEditorPanel().getEditorPane();
    queryArea.setText(DEFAULT_QUERY);

    errorPainter = new javax.swing.text.Highlighter.HighlightPainter() {
        @Override
        public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
            try {
                Rectangle r = c.modelToView(c.getCaretPosition());
                g.setColor(Color.RED.brighter().brighter());
                g.fillRect(0, r.y, c.getWidth(), r.height);
            } catch (BadLocationException e) {
                // ignore
            }
        }
    };

    ActionMap actionMap = sqlEditor.getEditorPanel().getEditorPane().getActionMap();

    // create the toolbar
    JToolBar toolBar = new JToolBar();
    toolBar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); // hide buttons borders
    toolBar.putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.BOTH);
    toolBar.setBorderPainted(false);

    // add cut action
    Action cutAction = actionMap.get(BaseEditorKit.cutAction);
    cutAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("cut"));
    cutAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqlviewpanel.cut"));
    toolBar.add(cutAction);

    // add copy action
    Action copyAction = actionMap.get(BaseEditorKit.copyAction);
    copyAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("copy"));
    copyAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqlviewpanel.copy"));
    toolBar.add(copyAction);

    // add paste action
    Action pasteAction = actionMap.get(BaseEditorKit.pasteAction);
    pasteAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("paste"));
    pasteAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqlviewpanel.paste"));
    toolBar.add(pasteAction);

    // add separator
    SwingUtil.addCustomSeparator(toolBar);

    // add undo action
    Action undoAction = actionMap.get(BaseEditorKit.undoAction);
    undoAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("undo"));
    undoAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("undo"));
    toolBar.add(undoAction);

    // add redo action
    Action redoAction = actionMap.get(BaseEditorKit.redoAction);
    redoAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("redo"));
    redoAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("redo"));
    toolBar.add(redoAction);

    // add separator
    SwingUtil.addCustomSeparator(toolBar);

    // add find action
    Action findReplaceAction = actionMap.get(BaseEditorKit.findReplaceAction);
    findReplaceAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("find"));
    findReplaceAction.putValue(Action.SHORT_DESCRIPTION,
            I18NSupport.getString("sqleditor.findReplaceActionName"));
    toolBar.add(findReplaceAction);

    // add separator
    SwingUtil.addCustomSeparator(toolBar);

    // add run action
    runAction = new SQLRunAction();
    runAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("run"));
    runAction.putValue(Action.ACCELERATOR_KEY,
            KeyStroke.getKeyStroke(ShortcutsUtil.getShortcut("query.run.accelerator", "control 4")));
    runAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("run.query") + " ("
            + ShortcutsUtil.getShortcut("query.run.accelerator.display", "Ctrl 4") + ")");
    // runAction is globally registered in QueryBuilderPanel !
    toolBar.add(runAction);

    //        ro.nextreports.designer.util.SwingUtil.registerButtonsForFocus(buttonsPanel);

    // create the table
    resultTable = new JXTable();
    resultTable.setDefaultRenderer(Integer.class, new ToStringRenderer()); // to remove thousand separators
    resultTable.setDefaultRenderer(Long.class, new ToStringRenderer());
    resultTable.setDefaultRenderer(Date.class, new DateRenderer());
    resultTable.setDefaultRenderer(Double.class, new DoubleRenderer());
    resultTable.addMouseListener(new CopyTableMouseAdapter(resultTable));
    TableUtil.setRowHeader(resultTable);
    resultTable.setColumnControlVisible(true);
    //        resultTable.getTableHeader().setReorderingAllowed(false);
    resultTable.setHorizontalScrollEnabled(true);

    // highlight table
    Highlighter alternateHighlighter = HighlighterFactory.createAlternateStriping(Color.WHITE,
            ColorUtil.PANEL_BACKROUND_COLOR);
    Highlighter nullHighlighter = new TextHighlighter(ResultSetTableModel.NULL_VALUE, Color.YELLOW.brighter());
    Highlighter blobHighlighter = new TextHighlighter(ResultSetTableModel.BLOB_VALUE, Color.GRAY.brighter());
    Highlighter clobHighlighter = new TextHighlighter(ResultSetTableModel.CLOB_VALUE, Color.GRAY.brighter());
    resultTable.setHighlighters(alternateHighlighter, nullHighlighter, blobHighlighter, clobHighlighter);
    resultTable.setBackground(ColorUtil.PANEL_BACKROUND_COLOR);
    resultTable.setGridColor(Color.LIGHT_GRAY);

    resultTable.setRolloverEnabled(true);
    resultTable.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, Color.RED));

    JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    split.setResizeWeight(0.66);
    split.setOneTouchExpandable(true);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridBagLayout());
    topPanel.add(toolBar, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    topPanel.add(sqlEditor, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new GridBagLayout());
    JScrollPane scrPanel = new JScrollPane(resultTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    statusPanel = new SQLStatusPanel();
    bottomPanel.add(scrPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,
            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    bottomPanel.add(statusPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

    split.setTopComponent(topPanel);
    split.setBottomComponent(bottomPanel);
    split.setDividerLocation(400);

    setLayout(new BorderLayout());
    this.add(split, BorderLayout.CENTER);
}