📄 basictextui.java
字号:
/* BasicTextUI.java -- Copyright (C) 2002, 2003, 2004, 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.plaf.basic;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.Shape;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.Action;import javax.swing.ActionMap;import javax.swing.InputMap;import javax.swing.JComponent;import javax.swing.LookAndFeel;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.plaf.ActionMapUIResource;import javax.swing.plaf.TextUI;import javax.swing.plaf.UIResource;import javax.swing.text.BadLocationException;import javax.swing.text.Caret;import javax.swing.text.DefaultCaret;import javax.swing.text.DefaultEditorKit;import javax.swing.text.DefaultHighlighter;import javax.swing.text.Document;import javax.swing.text.EditorKit;import javax.swing.text.Element;import javax.swing.text.Highlighter;import javax.swing.text.JTextComponent;import javax.swing.text.Keymap;import javax.swing.text.Position;import javax.swing.text.View;import javax.swing.text.ViewFactory;/** * The abstract base class from which the UI classes for Swings text * components are derived. This provides most of the functionality for * the UI classes. * * @author original author unknown * @author Roman Kennke (roman@kennke.org) */public abstract class BasicTextUI extends TextUI implements ViewFactory{ /** * A {@link DefaultCaret} that implements {@link UIResource}. */ public static class BasicCaret extends DefaultCaret implements UIResource { public BasicCaret() { // Nothing to do here. } } /** * A {@link DefaultHighlighter} that implements {@link UIResource}. */ public static class BasicHighlighter extends DefaultHighlighter implements UIResource { public BasicHighlighter() { // Nothing to do here. } } /** * This view forms the root of the View hierarchy. However, it delegates * most calls to another View which is the real root of the hierarchy. * The purpose is to make sure that all Views in the hierarchy, including * the (real) root have a well-defined parent to which they can delegate * calls like {@link #preferenceChanged}, {@link #getViewFactory} and * {@link #getContainer}. */ private class RootView extends View { /** The real root view. */ private View view; /** * Creates a new RootView. */ public RootView() { super(null); } /** * Returns the ViewFactory for this RootView. If the current EditorKit * provides a ViewFactory, this is used. Otherwise the TextUI itself * is returned as a ViewFactory. * * @return the ViewFactory for this RootView */ public ViewFactory getViewFactory() { ViewFactory factory = null; EditorKit editorKit = BasicTextUI.this.getEditorKit(getComponent()); factory = editorKit.getViewFactory(); if (factory == null) factory = BasicTextUI.this; return factory; } /** * Indicates that the preferences of one of the child view has changed. * This calls revalidate on the text component. * * @param view the child view which's preference has changed * @param width <code>true</code> if the width preference has changed * @param height <code>true</code> if the height preference has changed */ public void preferenceChanged(View view, boolean width, boolean height) { textComponent.revalidate(); } /** * Sets the real root view. * * @param v the root view to set */ public void setView(View v) { if (view != null) view.setParent(null); if (v != null) v.setParent(null); view = v; } /** * Returns the real root view, regardless of the index. * * @param index not used here * * @return the real root view, regardless of the index. */ public View getView(int index) { return view; } /** * Returns <code>1</code> since the RootView always contains one * child, that is the real root of the View hierarchy. * * @return <code>1</code> since the RootView always contains one * child, that is the real root of the View hierarchy */ public int getViewCount() { if (view != null) return 1; else return 0; } /** * Returns the <code>Container</code> that contains this view. This * normally will be the text component that is managed by this TextUI. * * @return the <code>Container</code> that contains this view */ public Container getContainer() { return textComponent; } /** * Returns the preferred span along the specified <code>axis</code>. * This is delegated to the real root view. * * @param axis the axis for which the preferred span is queried * * @return the preferred span along the axis */ public float getPreferredSpan(int axis) { if (view != null) return view.getPreferredSpan(axis); return Integer.MAX_VALUE; } /** * Paints the view. This is delegated to the real root view. * * @param g the <code>Graphics</code> context to paint to * @param s the allocation for the View */ public void paint(Graphics g, Shape s) { if (view != null) view.paint(g, s); } /** * Maps a position in the document into the coordinate space of the View. * The output rectangle usually reflects the font height but has a width * of zero. * * This is delegated to the real root view. * * @param position the position of the character in the model * @param a the area that is occupied by the view * @param bias either {@link Position.Bias#Forward} or * {@link Position.Bias#Backward} depending on the preferred * direction bias. If <code>null</code> this defaults to * <code>Position.Bias.Forward</code> * * @return a rectangle that gives the location of the document position * inside the view coordinate space * * @throws BadLocationException if <code>pos</code> is invalid * @throws IllegalArgumentException if b is not one of the above listed * valid values */ public Shape modelToView(int position, Shape a, Position.Bias bias) throws BadLocationException { return ((View) view).modelToView(position, a, bias); } /** * Maps coordinates from the <code>View</code>'s space into a position * in the document model. * * @param x the x coordinate in the view space * @param y the y coordinate in the view space * @param a the allocation of this <code>View</code> * @param b the bias to use * * @return the position in the document that corresponds to the screen * coordinates <code>x, y</code> */ public int viewToModel(float x, float y, Shape a, Position.Bias[] b) { return view.viewToModel(x, y, a, b); } /** * Notification about text insertions. These are forwarded to the * real root view. * * @param ev the DocumentEvent describing the change * @param shape the current allocation of the view's display * @param vf the ViewFactory to use for creating new Views */ public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf) { view.insertUpdate(ev, shape, vf); } /** * Notification about text removals. These are forwarded to the * real root view. * * @param ev the DocumentEvent describing the change * @param shape the current allocation of the view's display * @param vf the ViewFactory to use for creating new Views */ public void removeUpdate(DocumentEvent ev, Shape shape, ViewFactory vf) { view.removeUpdate(ev, shape, vf); } /** * Notification about text changes. These are forwarded to the * real root view. * * @param ev the DocumentEvent describing the change * @param shape the current allocation of the view's display * @param vf the ViewFactory to use for creating new Views */ public void changedUpdate(DocumentEvent ev, Shape shape, ViewFactory vf) { view.changedUpdate(ev, shape, vf); } /** * Returns the document position that is (visually) nearest to the given * document position <code>pos</code> in the given direction <code>d</code>. * * @param c the text component * @param pos the document position * @param b the bias for <code>pos</code> * @param d the direction, must be either {@link SwingConstants#NORTH}, * {@link SwingConstants#SOUTH}, {@link SwingConstants#WEST} or * {@link SwingConstants#EAST} * @param biasRet an array of {@link Position.Bias} that can hold at least * one element, which is filled with the bias of the return position * on method exit * * @return the document position that is (visually) nearest to the given * document position <code>pos</code> in the given direction * <code>d</code> * * @throws BadLocationException if <code>pos</code> is not a valid offset in * the document model */ public int getNextVisualPositionFrom(JTextComponent c, int pos, Position.Bias b, int d, Position.Bias[] biasRet) throws BadLocationException { return view.getNextVisualPositionFrom(c, pos, b, d, biasRet); } } /** * Receives notifications when properties of the text component change. */ class PropertyChangeHandler implements PropertyChangeListener { /** * Notifies when a property of the text component changes. * * @param event the PropertyChangeEvent describing the change */ public void propertyChange(PropertyChangeEvent event) { if (event.getPropertyName().equals("document"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -