Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.apache.cayenne.swing.components.textpane;
 
 import java.awt.Color;
 import java.awt.Image;
 import java.awt.Point;
 
 
 
 public class JCayenneTextPane extends JPanel {
 
     protected Highlighter.HighlightPainter painter;
     private JTextPaneScrollable pane;
     private JScrollPane scrollPane;
 
     private boolean imageError;
     private String tooltipTextError;
     private int startYPositionToolTip;
     private int endYPositionToolTip;
 
     public boolean repaint;
 
     public JScrollPane getScrollPane() {
         return ;
     }
 
     public String getTooltipTextError() {
         return ;
     }
 
     public void setTooltipTextError(String tooltipTextError) {
         this. = tooltipTextError;
     }
 
     private static Log logObj = LogFactory.getLog(Main.class);
 
     public void setText(String text) {
         .setText(text);
     }
 
     public String getText() {
         return .getText();
     }
 
     public JTextComponent getPane() {
         return ;
     }
 
     public int getStartPositionInDocument() {
         return .viewToModel(.getViewport().getViewPosition());
         // starting pos
         // in document
     }
 
     public int getEndPositionInDocument() {
         return .viewToModel(new Point(.getViewport().getViewPosition().
                 + .getWidth(), .getViewport().getViewPosition().
                 + .getHeight()));
     }
 
    public void repaintPane() {
        .repaint();
    }

    
Return an int containing the wrapped line index at the given position

Parameters:
int pos
Returns:
int
    public int getLineNumber(int pos) {
        int posLine;
        int y = 0;
        try {
            Rectangle caretCoords = .modelToView(pos);
            y = (intcaretCoords.getY();
        }
        catch (BadLocationException ex) {
            .warn("Error: "ex);
        }
        int lineHeight = .getFontMetrics(.getFont()).getHeight();
        posLine = (y / lineHeight) + 1;
        return posLine;
    }

    
Return an int position at the given line number and symbol position in this line

Parameters:
int posInLine
int line
Returns:
int
Throws:
javax.swing.text.BadLocationException
    public int getPosition(int lineint posInLinethrows BadLocationException {
        // translate lines to offsets
        int position = -1;
        int numrows = 1;
        char[] chararr = .getText().toCharArray();
        for (int i = 0; i < chararr.lengthi++) {
            if (chararr[i] == '\n') {
                numrows++;
                if (numrows == line) {
                    position = i;
                    break;
                }
            }
        }
        return position + posInLine;
    }
    public JCayenneTextPane(SyntaxConstant syntaxConstant) {
        super();
        Dimension dimention = new Dimension(15, 15);
        setMinimumSize(dimention);
        setPreferredSize(dimention);
        setMinimumSize(dimention);
        setBackground(new Color(245, 238, 238));
        setBorder(null);
         = new JTextPaneScrollable(new EditorKit(syntaxConstant)) {
            public void paint(Graphics g) {
                super.paint(g);
                JCayenneTextPane.this.repaint();
            }
        };
        .setBorder(new LineNumberedBorder(this));
         = new JScrollPane();
        .setBorder(BorderFactory.createLineBorder(new Color(115, 115, 115)));
                .);
            public void insertUpdate(DocumentEvent evt) {
                try {
                    String text = .getText(evt.getOffset(), 1).toString();
                    if (text.equals("/") || text.equals("*")) {
                        removeHighlightText();
                        .repaint();
                    }
                    if (text.equals(" ") || text.equals("\t") || text.equals("\n")) {
                        .repaint();
                    }
                }
                catch (Exception e) {
                    .warn("Error: "e);
                }
            }
            public void removeUpdate(DocumentEvent evt) {
            }
            public void changedUpdate(DocumentEvent evt) {
            }
        });
    }
    public void setHighlightText(int lastIndexint endIndexthrows BadLocationException {
        Highlighter highlighter = .getHighlighter();
        removeHighlightText(highlighter);
        highlighter.addHighlight(lastIndexendIndex);
    }

    
set underlines text in JCayenneTextPane

Parameters:
int line - starting line for underlined text
int lastIndex - starting position in line for underlined text
int size
String message - text for toolTip, contains the text of the error
    public void setHighlightText(int lineint lastIndexint sizeString message) {
        Highlighter highlighter = .getHighlighter();
        removeHighlightText(highlighter);
        if (getText().length() > 0) {
            try {
                int position = getPosition(linelastIndex);
                int positionEnd = position + size;
                highlighter.addHighlight(positionpositionEnd);
                setToolTipPosition(linemessage);
                repaintPane();
            }
            catch (BadLocationException e) {
                .warn("Error: "e);
            }
        }
        else {
            setToolTipPosition(0, "");
        }
    }
    public void removeHighlightText(Highlighter highlighter) {
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        for (int i = 0; i < highlights.lengthi++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() instanceof UnderlineHighlighterForText.UnderlineHighlightPainter) {
                highlighter.removeHighlight(h);
            }
        }
    }
    public void setToolTipPosition(int lineString string) {
        if (line != 0) {
            int height = .getFontMetrics(.getFont()).getHeight();
            int start = (line - 1) * height;
            this. = start;
            this. = start + height;
            setTooltipTextError(string);
            if (string != "") {
                 = true;
            }
            else {
                 = false;
            }
            setToolTipText("");
        }
        else {
            this. = -1;
            this. = -1;
            setTooltipTextError("");
            setToolTipText("");
             = false;
        }
    }
    public String getToolTipText(MouseEvent e) {
        if (e.getPoint(). > 
                && e.getPoint(). < 
                && ) {
            setCursor(Cursor.getPredefinedCursor(.));
            String htmlText = getTooltipTextError()
                    .replaceAll("\n""<br>&nbsp;")
                    .replaceAll("\t""&nbsp;")
                    .replaceAll("\r""<br>&nbsp;");
            return "<HTML>"
                    + "<body bgcolor='#FFEBCD' text='black'>"
                    + htmlText
                    + "</body>";
        }
        else {
            setCursor(Cursor.getDefaultCursor());
            return null;
        }
    }
    public void removeHighlightText() {
         = false;
        Highlighter highlighter = .getHighlighter();
        removeHighlightText(highlighter);
    }
    
    public int getCaretPosition() {
        return .getCaretPosition();        
    }
    public void paint(Graphics g) {
        super.paint(g);
        int start = getStartPositionInDocument();
        int end = getEndPositionInDocument();
        // end pos in doc
        // translate offsets to lines
        Document doc = .getDocument();
        int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
        int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;
        int fontHeight = g.getFontMetrics(.getFont()).getHeight();
        int fontDesc = g.getFontMetrics(.getFont()).getDescent();
        int starting_y = -1;
        try {
            if (.modelToView(start) == null) {
                starting_y = -1;
            }
            else {
                starting_y = .modelToView(start).
                        - .getViewport().getViewPosition().
                        + fontHeight
                        - fontDesc;
            }
        }
        catch (Exception e1) {
            .warn("Error: "e1);
        }
        for (int line = startliney = starting_yline <= endliney += fontHeightline++) {
            Color color = g.getColor();
            if (line - 1 == doc.getDefaultRootElement().getElementIndex(
                    .getCaretPosition())) {
                g.setColor(new Color(224, 224, 255));
                g.fillRect(0, y - fontHeight + 3, 30, fontHeight + 1);
            }
            if () {
                Image img = ModelerUtil.buildIcon("error.gif").getImage();
                g.drawImage(img, 0, this);
            }
            g.setColor(color);
        }
    }
    public Document getDocument() {
        return .getDocument();
    }
    class JTextPaneScrollable extends JTextPane {
        
        public JTextPaneScrollable(EditorKit editorKit) {
            // Set editor kit
            this.setEditorKitForContentType(editorKit.getContentType(), editorKit);
            this.setContentType(editorKit.getContentType());
        }
        @Override
        public boolean getScrollableTracksViewportWidth() {
            if (getParent() instanceof JViewport) {
                JViewport port = (JViewportgetParent();
                if (port.getWidth() > getUI().getPreferredSize(this).) {
                    return true;
                }
            }
            return false;
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        }
    }