📄 basictextui.java
字号:
*/ public void damageRange(JTextComponent tc, int p0, int p1) { damageRange(tc, p0, p1, Position.Bias.Forward, Position.Bias.Backward); } /** * Causes the portion of the view responsible for the * given part of the model to be repainted. * * @param p0 the beginning of the range >= 0 * @param p1 the end of the range >= p0 */ public void damageRange(JTextComponent t, int p0, int p1, Position.Bias p0Bias, Position.Bias p1Bias) { if (painted) { Rectangle alloc = getVisibleEditorRect(); if (alloc != null) { Document doc = t.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { rootView.setSize(alloc.width, alloc.height); Shape toDamage = rootView.modelToView(p0, p0Bias, p1, p1Bias, alloc); Rectangle rect = (toDamage instanceof Rectangle) ? (Rectangle)toDamage : toDamage.getBounds(); editor.repaint(rect.x, rect.y, rect.width, rect.height); } catch (BadLocationException e) { } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } } } } /** * Fetches the EditorKit for the UI. * * @param tc the text component for which this UI is installed * @return the editor capabilities * @see TextUI#getEditorKit */ public EditorKit getEditorKit(JTextComponent tc) { return defaultKit; } /** * Fetches a View with the allocation of the associated * text component (i.e. the root of the hierarchy) that * can be traversed to determine how the model is being * represented spatially. * <p> * <font color=red><b>NOTE:</b>The View hierarchy can * be traversed from the root view, and other things * can be done as well. Things done in this way cannot * be protected like simple method calls through the TextUI. * Therefore, proper operation in the presence of concurrency * must be arranged by any logic that calls this method! * </font> * * @param tc the text component for which this UI is installed * @return the view * @see TextUI#getRootView */ public View getRootView(JTextComponent tc) { return rootView; } /** * Returns the string to be used as the tooltip at the passed in location. * This forwards the method onto the root View. * * @see javax.swing.text.JTextComponent#getToolTipText * @see javax.swing.text.View#getToolTipText * @since 1.4 */ public String getToolTipText(JTextComponent t, Point pt) { if (!painted) { return null; } Document doc = editor.getDocument(); String tt = null; Rectangle alloc = getVisibleEditorRect(); if (alloc != null) { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { tt = rootView.getToolTipText(pt.x, pt.y, alloc); } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } } return tt; } // --- ViewFactory methods ------------------------------ /** * Creates a view for an element. * If a subclass wishes to directly implement the factory * producing the view(s), it should reimplement this * method. By default it simply returns null indicating * it is unable to represent the element. * * @param elem the element * @return the view */ public View create(Element elem) { return null; } /** * Creates a view for an element. * If a subclass wishes to directly implement the factory * producing the view(s), it should reimplement this * method. By default it simply returns null indicating * it is unable to represent the part of the element. * * @param elem the element * @param p0 the starting offset >= 0 * @param p1 the ending offset >= p0 * @return the view */ public View create(Element elem, int p0, int p1) { return null; } public static class BasicCaret extends DefaultCaret implements UIResource {} public static class BasicHighlighter extends DefaultHighlighter implements UIResource {} static class BasicCursor extends Cursor implements UIResource { BasicCursor(int type) { super(type); } BasicCursor(String name) { super(name); } } private static BasicCursor textCursor = new BasicCursor(Cursor.TEXT_CURSOR); // ----- member variables --------------------------------------- private static final EditorKit defaultKit = new DefaultEditorKit(); transient JTextComponent editor; transient boolean painted; transient RootView rootView = new RootView(); transient UpdateHandler updateHandler = new UpdateHandler(); private static final TransferHandler defaultTransferHandler = new TextTransferHandler(); private final DragListener dragListener = getDragListener(); private static final Position.Bias[] discardBias = new Position.Bias[1]; private DefaultCaret dropCaret; /** * Root view that acts as a gateway between the component * and the View hierarchy. */ class RootView extends View { RootView() { super(null); } void setView(View v) { View oldView = view; view = null; if (oldView != null) { // get rid of back reference so that the old // hierarchy can be garbage collected. oldView.setParent(null); } if (v != null) { v.setParent(this); } view = v; } /** * Fetches the attributes to use when rendering. At the root * level there are no attributes. If an attribute is resolved * up the view hierarchy this is the end of the line. */ public AttributeSet getAttributes() { return null; } /** * Determines the preferred span for this view along an axis. * * @param axis may be either X_AXIS or Y_AXIS * @return the span the view would like to be rendered into. * Typically the view is told to render into the span * that is returned, although there is no guarantee. * The parent may choose to resize or break the view. */ public float getPreferredSpan(int axis) { if (view != null) { return view.getPreferredSpan(axis); } return 10; } /** * Determines the minimum span for this view along an axis. * * @param axis may be either X_AXIS or Y_AXIS * @return the span the view would like to be rendered into. * Typically the view is told to render into the span * that is returned, although there is no guarantee. * The parent may choose to resize or break the view. */ public float getMinimumSpan(int axis) { if (view != null) { return view.getMinimumSpan(axis); } return 10; } /** * Determines the maximum span for this view along an axis. * * @param axis may be either X_AXIS or Y_AXIS * @return the span the view would like to be rendered into. * Typically the view is told to render into the span * that is returned, although there is no guarantee. * The parent may choose to resize or break the view. */ public float getMaximumSpan(int axis) { return Integer.MAX_VALUE; } /** * Specifies that a preference has changed. * Child views can call this on the parent to indicate that * the preference has changed. The root view routes this to * invalidate on the hosting component. * <p> * This can be called on a different thread from the * event dispatching thread and is basically unsafe to * propagate into the component. To make this safe, * the operation is transferred over to the event dispatching * thread for completion. It is a design goal that all view * methods be safe to call without concern for concurrency, * and this behavior helps make that true. * * @param child the child view * @param width true if the width preference has changed * @param height true if the height preference has changed */ public void preferenceChanged(View child, boolean width, boolean height) { editor.revalidate(); } /** * Determines the desired alignment for this view along an axis. * * @param axis may be either X_AXIS or Y_AXIS * @return the desired alignment, where 0.0 indicates the origin * and 1.0 the full span away from the origin */ public float getAlignment(int axis) { if (view != null) { return view.getAlignment(axis); } return 0; } /** * Renders the view. * * @param g the graphics context * @param allocation the region to render into */ public void paint(Graphics g, Shape allocation) { if (view != null) { Rectangle alloc = (allocation instanceof Rectangle) ? (Rectangle)allocation : allocation.getBounds(); setSize(alloc.width, alloc.height); view.paint(g, allocation); } } /** * Sets the view parent. * * @param parent the parent view */ public void setParent(View parent) { throw new Error("Can't set parent on root view"); } /** * Returns the number of views in this view. Since * this view simply wraps the root of the view hierarchy * it has exactly one child. * * @return the number of views * @see #getView */ public int getViewCount() { return 1; } /** * Gets the n-th view in this container. * * @param n the number of the view to get * @return the view */ public View getView(int n) { return view; } /** * Returns the child view index representing the given position in * the model. This is implemented to return the index of the only * child. * * @param pos the position >= 0 * @return index of the view representing the given position, or * -1 if no view represents that position * @since 1.3 */ public int getViewIndex(int pos, Position.Bias b) { return 0; } /** * Fetches the allocation for the given child view. * This enables finding out where various views * are located, without assuming the views store * their location. This returns the given allocation * since this view simply acts as a gateway between * the view hierarchy and the associated component. * * @param index the index of the child * @param a the allocation to this view. * @return the allocation to the child */ public Shape getChildAllocation(int index, Shape a) { return a; } /** * Provides a mapping from the document model coordinate space * to the coordinate space of the view mapped to it. * * @param pos the position to convert * @param a the allocated region to render into * @return the bounding box of the given position */ public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { if (view != null) { return view.modelToView(pos, a, b); } return null; } /** * Provides a mapping from the document model coordinate space * to the coordinate space of the view mapped to it. * * @param p0 the position to convert >= 0 * @param b0 the bias toward the previous character or the * next character represented by p0, in case the * position is a boundary of two views. * @param p1 the position to convert >= 0 * @param b1 the bias toward the previous character or the * next character represented by p1, in case the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -