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

📄 wrappedplainview.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* WrappedPlainView.java --    Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text;import java.awt.Color;import java.awt.Container;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.Shape;import javax.swing.SwingConstants;import javax.swing.event.DocumentEvent;import javax.swing.text.Position.Bias;/** * @author Anthony Balkissoon abalkiss at redhat dot com * */public class WrappedPlainView extends BoxView implements TabExpander{  /** The color for selected text **/  Color selectedColor;    /** The color for unselected text **/  Color unselectedColor;    /** The color for disabled components **/  Color disabledColor;    /** Stores the font metrics **/  protected FontMetrics metrics;    /** Whether or not to wrap on word boundaries **/  boolean wordWrap;    /** A ViewFactory that creates WrappedLines **/  ViewFactory viewFactory = new WrappedLineCreator();    /** The start of the selected text **/  int selectionStart;    /** The end of the selected text **/  int selectionEnd;    /**   * The instance returned by {@link #getLineBuffer()}.   */  private transient Segment lineBuffer;    public WrappedPlainView (Element elem)  {    this (elem, false);  }    public WrappedPlainView (Element elem, boolean wordWrap)  {    super (elem, Y_AXIS);    this.wordWrap = wordWrap;      }      /**   * Provides access to the Segment used for retrievals from the Document.   * @return the Segment.   */  protected final Segment getLineBuffer()  {    if (lineBuffer == null)      lineBuffer = new Segment();    return lineBuffer;  }    /**   * Returns the next tab stop position after a given reference position.   *   * This implementation ignores the <code>tabStop</code> argument.   *    * @param x the current x position in pixels   * @param tabStop the position within the text stream that the tab occured at   */  public float nextTabStop(float x, int tabStop)  {    JTextComponent host = (JTextComponent)getContainer();    float tabSizePixels = getTabSize()                          * host.getFontMetrics(host.getFont()).charWidth('m');    return (float) (Math.floor(x / tabSizePixels) + 1) * tabSizePixels;  }    /**   * Returns the tab size for the Document based on    * PlainDocument.tabSizeAttribute, defaulting to 8 if this property is   * not defined   *    * @return the tab size.   */  protected int getTabSize()  {    Object tabSize = getDocument().getProperty(PlainDocument.tabSizeAttribute);    if (tabSize == null)      return 8;    return ((Integer)tabSize).intValue();  }    /**   * Draws a line of text, suppressing white space at the end and expanding   * tabs.  Calls drawSelectedText and drawUnselectedText.   * @param p0 starting document position to use   * @param p1 ending document position to use   * @param g graphics context   * @param x starting x position   * @param y starting y position   */  protected void drawLine(int p0, int p1, Graphics g, int x, int y)  {    try    {      // We have to draw both selected and unselected text.  There are      // several cases:      //  - entire range is unselected      //  - entire range is selected      //  - start of range is selected, end of range is unselected      //  - start of range is unselected, end of range is selected      //  - middle of range is selected, start and end of range is unselected            // entire range unselected:            if ((selectionStart == selectionEnd) ||           (p0 > selectionEnd || p1 < selectionStart))        drawUnselectedText(g, x, y, p0, p1);            // entire range selected      else if (p0 >= selectionStart && p1 <= selectionEnd)        drawSelectedText(g, x, y, p0, p1);            // start of range selected, end of range unselected      else if (p0 >= selectionStart)        {          x = drawSelectedText(g, x, y, p0, selectionEnd);          drawUnselectedText(g, x, y, selectionEnd, p1);        }            // start of range unselected, end of range selected      else if (selectionStart > p0 && selectionEnd > p1)        {          x = drawUnselectedText(g, x, y, p0, selectionStart);          drawSelectedText(g, x, y, selectionStart, p1);        }            // middle of range selected      else if (selectionStart > p0)        {          x = drawUnselectedText(g, x, y, p0, selectionStart);          x = drawSelectedText(g, x, y, selectionStart, selectionEnd);          drawUnselectedText(g, x, y, selectionEnd, p1);        }            }    catch (BadLocationException ble)    {      // shouldn't happen    }  }  /**   * Renders the range of text as selected text.  Just paints the text    * in the color specified by the host component.  Assumes the highlighter   * will render the selected background.   * @param g the graphics context   * @param x the starting X coordinate   * @param y the starting Y coordinate   * @param p0 the starting model location   * @param p1 the ending model location    * @return the X coordinate of the end of the text   * @throws BadLocationException if the given range is invalid   */  protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)      throws BadLocationException  {    g.setColor(selectedColor);    Segment segment = getLineBuffer();    getDocument().getText(p0, p1 - p0, segment);    return Utilities.drawTabbedText(segment, x, y, g, this, p0);  }  /**   * Renders the range of text as normal unhighlighted text.   * @param g the graphics context   * @param x the starting X coordinate   * @param y the starting Y coordinate   * @param p0 the starting model location   * @param p1 the end model location   * @return the X location of the end off the range   * @throws BadLocationException if the range given is invalid   */  protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)      throws BadLocationException  {        JTextComponent textComponent = (JTextComponent) getContainer();    if (textComponent.isEnabled())      g.setColor(unselectedColor);    else      g.setColor(disabledColor);    Segment segment = getLineBuffer();    getDocument().getText(p0, p1 - p0, segment);    return Utilities.drawTabbedText(segment, x, y, g, this, p0);  }      /**   * Loads the children to initiate the view.  Called by setParent.   * Creates a WrappedLine for each child Element.   */  protected void loadChildren (ViewFactory f)  {    Element root = getElement();    int numChildren = root.getElementCount();    if (numChildren == 0)      return;        View[] children = new View[numChildren];    for (int i = 0; i < numChildren; i++)      children[i] = new WrappedLine(root.getElement(i));    replace(0, 0, children);  }    /**   * Calculates the break position for the text between model positions   * p0 and p1.  Will break on word boundaries or character boundaries   * depending on the break argument given in construction of this    * WrappedPlainView.  Used by the nested WrappedLine class to determine   * when to start the next logical line.   * @param p0 the start model position   * @param p1 the end model position   * @return the model position at which to break the text   */  protected int calculateBreakPosition(int p0, int p1)  {    Container c = getContainer();    Rectangle alloc = c.isValid() ? c.getBounds()                                 : new Rectangle(c.getPreferredSize());    updateMetrics();    try      {        getDocument().getText(p0, p1 - p0, getLineBuffer());      }    catch (BadLocationException ble)      {        // this shouldn't happen      }    // FIXME: Should we account for the insets of the container?    if (wordWrap)      return p0             + Utilities.getBreakLocation(lineBuffer, metrics, alloc.x,                                          alloc.x + alloc.width, this, 0);    else      {      return p0             + Utilities.getTabbedTextOffset(lineBuffer, metrics, alloc.x,                                             alloc.x + alloc.width, this, 0);      }  }    void updateMetrics()  {    Container component = getContainer();    metrics = component.getFontMetrics(component.getFont());  }    /**   * Determines the preferred span along the given axis.  Implemented to    * cache the font metrics and then call the super classes method.   */  public float getPreferredSpan (int axis)  {    updateMetrics();    return super.getPreferredSpan(axis);  }    /**   * Determines the minimum span along the given axis.  Implemented to    * cache the font metrics and then call the super classes method.   */  public float getMinimumSpan (int axis)  {    updateMetrics();    return super.getMinimumSpan(axis);  }    /**   * Determines the maximum span along the given axis.  Implemented to    * cache the font metrics and then call the super classes method.   */  public float getMaximumSpan (int axis)  {    updateMetrics();    return super.getMaximumSpan(axis);  }    /**   * Called when something was inserted.  Overridden so that   * the view factory creates WrappedLine views.   */  public void insertUpdate (DocumentEvent e, Shape a, ViewFactory f)  {    super.insertUpdate(e, a, viewFactory);    // FIXME: could improve performance by repainting only the necessary area    getContainer().repaint();  }    /**   * Called when something is removed.  Overridden so that   * the view factory creates WrappedLine views.   */  public void removeUpdate (DocumentEvent e, Shape a, ViewFactory f)  {    super.removeUpdate(e, a, viewFactory);    // FIXME: could improve performance by repainting only the necessary area

⌨️ 快捷键说明

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