• <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 逛奔的蝸牛 閱讀(453) 評論(0)  編輯 收藏 引用 所屬分類: Java
            女人香蕉久久**毛片精品| 久久综合五月丁香久久激情| 久久人与动人物a级毛片| 综合久久国产九一剧情麻豆| 久久精品人妻中文系列| 丰满少妇人妻久久久久久| 久久91这里精品国产2020| 久久人做人爽一区二区三区| .精品久久久麻豆国产精品| 久久国产香蕉一区精品| 亚洲AV日韩精品久久久久| yellow中文字幕久久网| 伊人久久大香线蕉影院95| 青青热久久国产久精品| 国产欧美久久久精品| 欧美久久亚洲精品| 国内精品久久久久影院日本 | 国产精品久久永久免费| 久久久久久久综合综合狠狠| 欧美噜噜久久久XXX| 婷婷久久综合九色综合绿巨人| 丰满少妇人妻久久久久久| 久久久久se色偷偷亚洲精品av| 国产精品成人无码久久久久久| 无码伊人66久久大杳蕉网站谷歌| 久久精品国产99久久久香蕉| 久久国产精品久久精品国产| 亚洲精品无码久久久久| 久久99国产精品久久99小说| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 久久A级毛片免费观看| 少妇人妻综合久久中文字幕| 国产真实乱对白精彩久久| 国产精品欧美久久久天天影视| 99久久国产精品免费一区二区| 性高湖久久久久久久久AAAAA| 国产精品无码久久四虎| 青青草原1769久久免费播放| 蜜桃麻豆www久久| 久久久久九九精品影院| 久久久久婷婷|