⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 highlighter.java

📁 java实现浏览器等本地桌面的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: Highlighter.java,v 1.6 2005/10/13 08:59:54 kleopatra Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */package org.jdesktop.swingx.decorator;import java.awt.Color;import java.awt.Component;import javax.swing.BoundedRangeModel;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.EventListenerList;/** * <p><code>Highlighter</code> is a lightweight mechanism to modify the behavior * and attributes of cell renderers such as {@link javax.swing.ListCellRenderer}, * {@link javax.swing.table.TableCellRenderer}, and * {@link javax.swing.tree.TreeCellRenderer} in a simple layered fashion. * While cell renderers are split along component lines, highlighters provide a * <em>common interface</em> for decorating cell renderers. * <code>Highlighter</code> achieves this by vectoring access to all component-specific * state and functionality through a {@link ComponentAdapter} object.</p> * * <p>The primary purpose of <code>Highlighter</code> is to decorate a cell * renderer in <em>controlled</em> ways, such as by applying a different color * or font to it. For example, {@link AlternateRowHighlighter} highlights cell * renderers with alternating background colors. In data visualization components * that support multiple columns with potentially different types of data, this * highlighter imparts the same background color consistently across <em>all</em> * columns of the {@link ComponentAdapter#target target} component * regardless of the actual cell renderer registered for any specific column. * Thus, the <code>Highlighter</code> mechanism is orthogonal to the cell * rendering mechanism.</p> * * <p>To use <code>Highlighter</code> you must first set up a * {@link HighlighterPipeline} using an array of <code>Highlighter</code> objects, * and then call setHighlighters() on a data visualization component, passing in * the highligher pipeline. If the array of highlighters is not null and is not * empty, the highlighters are applied to the selected renderer for each cell in * the order they appear in the array. * When it is time to render a cell, the cell renderer is primed as usual, after * which, the {@link Highlighter#highlight highlight} method of the first * highlighter in the {@link HighlighterPipeline} is invoked. The prepared * renderer, and a suitable {@link ComponentAdapter} object is passed to the * <code>highlight</code> method. The highlighter is expected to modify the * renderer in controlled ways, and return the modified renderer (or a substitute) * that is passed to the next highlighter, if any, in the pipeline. The renderer * returned by the <code>highlight</code> method of the last highlighter in the * pipeline is ultimately used to render the cell.</p> * * <p>The <code>Highlighter</code> mechanism enables multiple degrees of * freedom. In addition to specifying the actual cell renderer class, now you * can also specify the number, order, and class of highlighter objects. Using * highlighters is really simple, as shown by the following example:</p> * * <pre>  Highlighter[]   highlighters = new Highlighter[] {      new <b>AlternateRowHighlighter</b>(Color.white,                                         new Color(0xF0, 0xF0, 0xE0), null),      new <b>PatternHighlighter</b>(null, Color.red, "s.*", 0, 0)  };  HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters);  JXTable table = new JXTable();  table.setHighlighters(highlighterPipeline); * </pre> * * <p>The above example allocates an array of <code>Highlighter</code> and populates * it with a new {@link AlternateRowHighlighter} and {@link PatternHighlighter}. * The first one in this example highlights all cells in odd rows with a white * background, and all cells in even rows with a silver background, but it does * not specify a foreground color explicitly. The second highlighter does not * specify a background color explicitly, but sets the foreground color to red * <em>if certain conditions are met</em> (see {@link PatternHighlighter} for * more details). In this example, if the cells in the first column of any * row start with the letter 's', then all cells in that row are highlighted with * a red foreground. Also, as mentioned earlier, the highlighters are applied in * the order they appear in the list.</p> * * @author Ramesh Gupta * @see ComponentAdapter * @see javax.swing.ListCellRenderer * @see javax.swing.table.TableCellRenderer * @see javax.swing.tree.TreeCellRenderer */public class Highlighter {    /**     * Only one <code>ChangeEvent</code> is needed per model instance since the     * event's only (read-only) state is the source property.  The source     * of events generated here is always "this".     */    protected transient ChangeEvent changeEvent = null;    /** The listeners waiting for model changes. */    protected EventListenerList listenerList = new EventListenerList();        /**     * Predefined <code>Highlighter</code> that highlights the background of     * each cell with a pastel green "ledger" background color, and is most     * effective when the {@link ComponentAdapter#target} component has     * horizontal gridlines in <code>Color.cyan.darker()</code> color.     */    public final static Highlighter ledgerBackground =                new Highlighter(new Color(0xF5, 0xFF, 0xF5), null);    /**     * Predefined <code>Highlighter</code> that decorates the background of     * each cell with a pastel yellow "notepad" background color, and is most     * effective when the {@link ComponentAdapter#target} component has     * horizontal gridlines in <code>Color.cyan.darker()</code> color.     */    public final static Highlighter notePadBackground =                new Highlighter(new Color(0xFF, 0xFF, 0xCC), null);    private Color background = null;    private Color foreground = null;    private Color selectedBackground = null;    private Color selectedForeground = null;    /**     * Default constructor.     * Initializes background, foreground, selectedBackground, and     * selectedForeground to null.     */    public Highlighter() {        // default constructor    }    /**     * Constructs a <code>Highlighter</code> with the specified     * background and foreground colors.     *     * @param cellBackground background color for the renderer, or null,     *          to compute a suitable background     * @param cellForeground foreground color for the renderer, or null,     *          to compute a suitable foreground     */    public Highlighter(Color cellBackground, Color cellForeground) {        this.background = cellBackground; // could be null        this.foreground = cellForeground; // could be null    }    /**     * Decorates the specified cell renderer component for the given component     * data adapter using highlighters that were previously set for the component.     * This method unconditionally invokes {@link #doHighlight doHighlight} with     * the same arguments as were passed in.     *     * @param renderer the cell renderer component that is to be decorated     * @param adapter the {@link ComponentAdapter} for this decorate operation     * @return the decorated cell renderer component     */    public Component highlight(Component renderer, ComponentAdapter adapter) {        return doHighlight(renderer, adapter);    }    /**     * This is the bottleneck decorate method that all highlighters must invoke     * to decorate the cell renderer. This method invokes {@link #applyBackground     * applyBackground}, {@link #applyForeground applyForeground},     * {@link #applyFont applyFont} and so on, to decorate the corresponding     * attributes of the specified component within the given adapter.     *     * @param renderer the cell renderer component that is to be decorated     * @param adapter the {@link ComponentAdapter} for this decorate operation     * @return the decorated cell renderer component     */    protected Component doHighlight(Component renderer, ComponentAdapter adapter) {        applyBackground(renderer, adapter);        applyForeground(renderer, adapter);        applyFont(renderer, adapter); // e.g., make it bold        // and so on...        return renderer;    }    /**     * Computes a suitable background for the renderer component within the     * specified adapter by calling {@link #computeBackground computeBackground}     * and applies the computed color to the component. If the computed     * color is null, it leaves the background unchanged; Otherwise it sets the     * component's background to the computed color.     *     * @param renderer the cell renderer component that is to be decorated     * @param adapter the {@link ComponentAdapter} for this decorate operation     */    protected void applyBackground(Component renderer, ComponentAdapter adapter) {        Color color = computeBackground(renderer, adapter);        if (color != null) {            renderer.setBackground(color);        }    }    /**     * Computes a suitable foreground for the renderer component within the     * specified adapter by calling {@link #computeForeground computeForeground}     * and applies the computed color to the component. If the computed     * color is null, it leaves the foreground unchanged; Otherwise it sets the     * component's foreground to the computed color.     *     * @param renderer the cell renderer component that is to be decorated     * @param adapter the {@link ComponentAdapter} for this decorate operation     */    protected void applyForeground(Component renderer, ComponentAdapter adapter) {        Color color = computeForeground(renderer, adapter);        if (color != null) {            renderer.setForeground(color);        }    }    /**     * Empty method. Override it to change the font of the renderer component.     *     * @param renderer the cell renderer component that is to be decorated     * @param adapter the {@link ComponentAdapter} for this decorate operation     */    protected void applyFont(Component renderer, ComponentAdapter adapter) {        // must be overridden to cause any effect    }    /**     * <p>Computes a suitable background for the renderer component within the     * specified adapter and returns the computed color. The computed color

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -