• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            package com.biao.mlm.ui;


            import java.awt.Color;

            import java.awt.Graphics;

            import java.awt.Rectangle;

            import java.awt.Shape;

            import java.awt.event.MouseAdapter;

            import java.awt.event.MouseEvent;


            import javax.swing.event.CaretEvent;

            import javax.swing.event.CaretListener;

            import javax.swing.text.BadLocationException;

            import javax.swing.text.Element;

            import javax.swing.text.Highlighter;

            import javax.swing.text.JTextComponent;


            // 當一個highlight被加到text component中后,每次更新高亮時,都會去遍歷highlighter的所有highlight,

            // 調用他們對應的painter.paing()方法,來進行繪制. 所以我們這里只需要加一次highlight, 就可以一直使用.

            // 而不是每次更新要高亮的行時就加一個新的highlight, 因為他會動態地計算當前行來進行高亮.

            public class CurrentLineHighlighter extends MouseAdapter implements Highlighter.HighlightPainter,

                    CaretListener {

                private JTextComponent editor;

                private Color color = Color.cyan;

                private int previousLine;


                public CurrentLineHighlighter() {

                }


                public CurrentLineHighlighter(JTextComponent editor) {

                    this(editor, null);

                }


                public CurrentLineHighlighter(JTextComponent editor, Color color) {

                    installEditor(editor);

                    setColor(color);

                }


                public void installEditor(JTextComponent editor) {

                    this.editor = editor;

                    editor.addCaretListener(this);

                    editor.addMouseListener(this);

                    editor.addMouseMotionListener(this);


                    // Turn highlight on

                    enableHighlight();

                }


                public void deinstallEditor() {

                    editor.removeCaretListener(this);

                    editor.removeMouseListener(this);

                    editor.removeMouseMotionListener(this);

                    editor = null;

                }


                public void enableHighlight() {

                    // Turn highlight on

                    try {

                        editor.getHighlighter().addHighlight(0, 0, this);

                    } catch (BadLocationException e) {

                        e.printStackTrace();

                    }

                }


                public Color getColor() {

                    return color;

                }


                public void setColor(Color color) {

                    if (color == null) { return; }

                    this.color = color;

                }


                @Override

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

                    try {

                        if (com.getSelectionStart() == com.getSelectionEnd()) { // No

                            // selection

                            Rectangle rect = com.modelToView(com.getCaretPosition());

                            g.setColor(color);

                            g.fillRect(0, rect.y, com.getWidth(), rect.height);

                            g.setColor(color.brighter().darker());

                            // g.drawLine(0, rect.y, com.getWidth(), rect.y);

                            // g.setColor(color.darker());

                            g.drawLine(0, rect.y + rect.height - 1, com.getWidth(), rect.y + rect.height - 1);

                        }

                    } catch (BadLocationException e) {

                        e.printStackTrace();

                    }

                }


                /**

                 * 只清除前一次的高亮,而不是整個editor

                 */

                public void resetHighlight() {

                    // 如果把下面的重繪放在一個SwingUtilities.invokeLater里的話,99%的繪制都是好的,

                    // 但唯一一點就是前一行光標所在處一瞬間會有一個很不明顯的空白,如果不仔細觀察是看不到的

                    Element root = editor.getDocument().getDefaultRootElement();

                    int caretPos = editor.getCaretPosition();


                    try {

                        // Remove the highlighting from the previously highlighted

                        // line

                        Element lineElement = root.getElement(previousLine);


                        // Might be null if an Undo action was performed on the

                        // text component

                        if (lineElement != null) {

                            // 清除前一次光標所在行的高亮0

                            int start = lineElement.getStartOffset();

                            Rectangle rect = editor.modelToView(start);

                            if (rect != null) {

                                editor.repaint(0, rect.y, editor.getWidth(), rect.height);


                                // 如果沒有這個, 當用中文輸入法輸入后, 鼠標再點擊時高亮當前行會有可能出問題

                                rect = editor.modelToView(caretPos);

                                editor.repaint(0, rect.y, editor.getWidth(), rect.height);

                            }

                        }

                    } catch (BadLocationException ble) {

                    }

                    /*

                     * // We need to use invokeLater to make sure that all updates to the //

                     * Document have been completed SwingUtilities.invokeLater(new

                     * Runnable() { public void run() { try { Element root =

                     * editor.getDocument().getDefaultRootElement(); int caretPos =

                     * editor.getCaretPosition(); int currentLine =

                     * root.getElementIndex(caretPos);

                     * 

                     * // Remove the highlighting from the previously highlighted // line if

                     * (currentLine != previousLine) { Element lineElement =

                     * root.getElement(previousLine);

                     * 

                     * // Might be null if an Undo action was performed on the // text

                     * component if (lineElement != null) { // 清除前一次光標所在行的高亮0 int start =

                     * lineElement.getStartOffset(); Rectangle rect =

                     * editor.modelToView(start); if (rect != null) { editor.repaint(0,

                     * rect.y, editor.getWidth(), rect.height);

                     * 

                     * // 如果沒有這個, 當用中文輸入法輸入后, 鼠標再點擊時高亮當前行會有可能出問題 rect =

                     * editor.modelToView(caretPos); editor.repaint(0, rect.y,

                     * editor.getWidth(), rect.height); } } previousLine = currentLine; } }

                     * catch (BadLocationException ble) { } } });

                     */

                }


                @Override

                public void mousePressed(MouseEvent e) {

                    resetHighlight();

                    // editor.repaint();

                }


                @Override

                public void mouseDragged(MouseEvent e) {

                    resetHighlight();

                    // editor.repaint();

                }


                public void caretUpdate(CaretEvent e) {

                    resetHighlight();

                    // editor.repaint();

                }

            }

            posted on 2010-05-22 03:27 逛奔的蝸牛 閱讀(455) 評論(0)  編輯 收藏 引用 所屬分類: Java
            久久综合狠狠综合久久激情 | 亚洲国产天堂久久综合| 性做久久久久久久久浪潮| 亚洲精品无码久久久久去q| 久久精品国产99国产电影网 | 久久露脸国产精品| 久久婷婷人人澡人人爽人人爱| 波多野结衣中文字幕久久| 欧美久久一级内射wwwwww.| 国产成人综合久久精品红| 久久r热这里有精品视频| 伊人久久成人成综合网222| 精品人妻久久久久久888| 久久人人青草97香蕉| 99久久精品无码一区二区毛片| 久久久久亚洲av综合波多野结衣| 26uuu久久五月天| 久久成人国产精品| 亚洲欧洲日产国码无码久久99| 色成年激情久久综合| 久久久久亚洲精品无码蜜桃| 亚洲国产成人乱码精品女人久久久不卡| 97热久久免费频精品99| 欧美亚洲国产精品久久| 无码乱码观看精品久久| 久久久久亚洲AV无码专区桃色| 伊人久久精品线影院| 99久久精品费精品国产一区二区| 久久精品国产亚洲AV香蕉| 亚洲va久久久久| 人人妻久久人人澡人人爽人人精品| 久久久久人妻一区精品| 91久久精品国产免费直播| 日本免费久久久久久久网站| 精品久久久久久中文字幕人妻最新| 国产毛片欧美毛片久久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 伊人久久精品线影院| 777久久精品一区二区三区无码| 免费国产99久久久香蕉| 丁香五月综合久久激情|