📄 basictextui.java
字号:
* @param c the editor component * @see ComponentUI#installUI */ public void installUI(JComponent c) { if (c instanceof JTextComponent) { editor = (JTextComponent) c; // install defaults installDefaults(); installDefaults2(); // This is a workaround as these should not override what synth has // set them to if (!(this instanceof sun.swing.plaf.synth.SynthUI)){ // common case is background painted... this can // easily be changed by subclasses or from outside // of the component. LookAndFeel.installProperty(editor, "opaque", Boolean.TRUE); LookAndFeel.installProperty(editor, "autoscrolls", Boolean.TRUE); } // attach to the model and editor editor.addPropertyChangeListener(updateHandler); Document doc = editor.getDocument(); if (doc == null) { // no model, create a default one. This will // fire a notification to the updateHandler // which takes care of the rest. editor.setDocument(getEditorKit(editor).createDefaultDocument()); } else { doc.addDocumentListener(updateHandler); modelChanged(); } // install keymap installListeners(); installKeyboardActions(); LayoutManager oldLayout = editor.getLayout(); if ((oldLayout == null) || (oldLayout instanceof UIResource)) { // by default, use default LayoutManger implementation that // will position the components associated with a View object. editor.setLayout(updateHandler); } updateBackground(editor); } else { throw new Error("TextUI needs JTextComponent"); } } /** * Deinstalls the UI for a component. This removes the listeners, * uninstalls the highlighter, removes views, and nulls out the keymap. * * @param c the editor component * @see ComponentUI#uninstallUI */ public void uninstallUI(JComponent c) { // detach from the model editor.removePropertyChangeListener(updateHandler); editor.getDocument().removeDocumentListener(updateHandler); // view part painted = false; uninstallDefaults(); rootView.setView(null); c.removeAll(); LayoutManager lm = c.getLayout(); if (lm instanceof UIResource) { c.setLayout(null); } // controller part uninstallKeyboardActions(); uninstallListeners(); editor = null; } /** * Superclass paints background in an uncontrollable way * (i.e. one might want an image tiled into the background). * To prevent this from happening twice, this method is * reimplemented to simply paint. * <p> * <em>NOTE:</em> Superclass is also not thread-safe in * it's rendering of the background, although that's not * an issue with the default rendering. */ public void update(Graphics g, JComponent c) { paint(g, c); } /** * Paints the interface. This is routed to the * paintSafely method under the guarantee that * the model won't change from the view of this thread * while it's rendering (if the associated model is * derived from AbstractDocument). This enables the * model to potentially be updated asynchronously. * * @param g the graphics context * @param c the editor component */ public final void paint(Graphics g, JComponent c) { if ((rootView.getViewCount() > 0) && (rootView.getView(0) != null)) { Document doc = editor.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { paintSafely(g); } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } } } /** * Gets the preferred size for the editor component. If the component * has been given a size prior to receiving this request, it will * set the size of the view hierarchy to reflect the size of the component * before requesting the preferred size of the view hierarchy. This * allows formatted views to format to the current component size before * answering the request. Other views don't care about currently formatted * size and give the same answer either way. * * @param c the editor component * @return the size */ public Dimension getPreferredSize(JComponent c) { Document doc = editor.getDocument(); Insets i = c.getInsets(); Dimension d = c.getSize(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { if ((d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) { rootView.setSize(d.width - i.left - i.right, d.height - i.top - i.bottom); } else if (d.width == 0 && d.height == 0) { // Probably haven't been layed out yet, force some sort of // initial sizing. rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE); } d.width = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) + (long) i.left + (long) i.right, Integer.MAX_VALUE); d.height = (int) Math.min((long) rootView.getPreferredSpan(View.Y_AXIS) + (long) i.top + (long) i.bottom, Integer.MAX_VALUE); } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return d; } /** * Gets the minimum size for the editor component. * * @param c the editor component * @return the size */ public Dimension getMinimumSize(JComponent c) { Document doc = editor.getDocument(); Insets i = c.getInsets(); Dimension d = new Dimension(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { d.width = (int) rootView.getMinimumSpan(View.X_AXIS) + i.left + i.right; d.height = (int) rootView.getMinimumSpan(View.Y_AXIS) + i.top + i.bottom; } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return d; } /** * Gets the maximum size for the editor component. * * @param c the editor component * @return the size */ public Dimension getMaximumSize(JComponent c) { Document doc = editor.getDocument(); Insets i = c.getInsets(); Dimension d = new Dimension(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { d.width = (int) Math.min((long) rootView.getMaximumSpan(View.X_AXIS) + (long) i.left + (long) i.right, Integer.MAX_VALUE); d.height = (int) Math.min((long) rootView.getMaximumSpan(View.Y_AXIS) + (long) i.top + (long) i.bottom, Integer.MAX_VALUE); } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return d; } // ---- TextUI methods ------------------------------------------- /** * Gets the allocation to give the root View. Due * to an unfortunate set of historical events this * method is inappropriately named. The Rectangle * returned has nothing to do with visibility. * The component must have a non-zero positive size for * this translation to be computed. * * @return the bounding box for the root view */ protected Rectangle getVisibleEditorRect() { Rectangle alloc = editor.getBounds(); if ((alloc.width > 0) && (alloc.height > 0)) { alloc.x = alloc.y = 0; Insets insets = editor.getInsets(); alloc.x += insets.left; alloc.y += insets.top; alloc.width -= insets.left + insets.right; alloc.height -= insets.top + insets.bottom; return alloc; } return null; } /** * Converts the given location in the model to a place in * the view coordinate system. * The component must have a non-zero positive size for * this translation to be computed. * * @param tc the text component for which this UI is installed * @param pos the local location in the model to translate >= 0 * @return the coordinates as a rectangle, null if the model is not painted * @exception BadLocationException if the given position does not * represent a valid location in the associated document * @see TextUI#modelToView */ public Rectangle modelToView(JTextComponent tc, int pos) throws BadLocationException { return modelToView(tc, pos, Position.Bias.Forward); } /** * Converts the given location in the model to a place in * the view coordinate system. * The component must have a non-zero positive size for * this translation to be computed. * * @param tc the text component for which this UI is installed * @param pos the local location in the model to translate >= 0 * @return the coordinates as a rectangle, null if the model is not painted * @exception BadLocationException if the given position does not * represent a valid location in the associated document * @see TextUI#modelToView */ public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException { Document doc = editor.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { Rectangle alloc = getVisibleEditorRect(); if (alloc != null) { rootView.setSize(alloc.width, alloc.height); Shape s = rootView.modelToView(pos, alloc, bias); if (s != null) { return s.getBounds(); } } } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return null; } /** * Converts the given place in the view coordinate system * to the nearest representative location in the model. * The component must have a non-zero positive size for * this translation to be computed. * * @param tc the text component for which this UI is installed * @param pt the location in the view to translate. This * should be in the same coordinate system as the mouse events. * @return the offset from the start of the document >= 0, * -1 if not painted * @see TextUI#viewToModel */ public int viewToModel(JTextComponent tc, Point pt) { return viewToModel(tc, pt, discardBias); } /** * Converts the given place in the view coordinate system * to the nearest representative location in the model. * The component must have a non-zero positive size for * this translation to be computed. * * @param tc the text component for which this UI is installed * @param pt the location in the view to translate. This * should be in the same coordinate system as the mouse events. * @return the offset from the start of the document >= 0, * -1 if the component doesn't yet have a positive size. * @see TextUI#viewToModel */ public int viewToModel(JTextComponent tc, Point pt, Position.Bias[] biasReturn) { int offs = -1; Document doc = editor.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { Rectangle alloc = getVisibleEditorRect(); if (alloc != null) { rootView.setSize(alloc.width, alloc.height); offs = rootView.viewToModel(pt.x, pt.y, alloc, biasReturn); } } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return offs; } /** * {@inheritDoc} */ public int getNextVisualPositionFrom(JTextComponent t, int pos, Position.Bias b, int direction, Position.Bias[] biasRet) throws BadLocationException{ Document doc = editor.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readLock(); } try { if (painted) { Rectangle alloc = getVisibleEditorRect(); if (alloc != null) { rootView.setSize(alloc.width, alloc.height); } return rootView.getNextVisualPositionFrom(pos, b, alloc, direction, biasRet); } } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument)doc).readUnlock(); } } return -1; } /** * Causes the portion of the view responsible for the * given part of the model to be repainted. Does nothing if * the view is not currently painted. * * @param tc the text component for which this UI is installed * @param p0 the beginning of the range >= 0 * @param p1 the end of the range >= p0 * @see TextUI#damageRange
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -