📄 rtextareaui.java~1~
字号:
return "TextArea";
}
/*****************************************************************************/
/**
* Returns the root view for this UI. The child view of the root view is
* the view responsible for painting the text of the text area.
*
* @param textComponent This parameter is ignored.
*/
public View getRootView(JTextComponent textComponent) {
return rootView;
}
/*****************************************************************************/
/**
* Returns the text area for which we are the UI.
*
* @return The text area.
*/
public RTextArea getRTextArea() {
return textArea;
}
/*****************************************************************************/
/**
* Returns the <code>TransferHandler</code> that will be installed if
* their isn't one installed on the <code>JTextComponent</code>.
*/
TransferHandler getTransferHandler() {
return transferHandler;
}
/*****************************************************************************/
/**
* 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 = textArea.getBounds();
if ((alloc.width > 0) && (alloc.height > 0)) {
alloc.x = alloc.y = 0;
Insets insets = textArea.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;
}
/*****************************************************************************/
/**
* Initializes component properties, e.g. font, foreground, background,
* caret color, selection color, selected text color, disabled text color,
* and border color. The properties are only set if the current value is
* <code>null</code> (as opposed to BasicTextUI, which checks for instances
* of <code>UIResource</code>).
*
* @see #uninstallDefaults
* @see #installUI
*/
protected void installDefaults() {
String prefix = getPropertyPrefix();
Font f = textArea.getFont();
if ((f == null) || (f instanceof UIResource))
textArea.setFont(UIManager.getFont(prefix + ".font"));
Object bg = textArea.getBackgroundObject();
if (bg==null)
textArea.setBackground(UIManager.getColor(prefix +
".background"));
Color fg = textArea.getForeground();
if (fg == null)
textArea.setForeground(UIManager.getColor(prefix +
".foreground"));
Color color = textArea.getCaretColor();
if (color == null)
textArea.setCaretColor(RTextArea.getDefaultCaretColor());
Color s = textArea.getSelectionColor();
if (s == null)
textArea.setSelectionColor(UIManager.getColor(prefix +
".selectionBackground"));
Color sfg = textArea.getSelectedTextColor();
if (sfg == null)
textArea.setSelectedTextColor(UIManager.getColor(prefix +
".selectionForeground"));
Color dfg = textArea.getDisabledTextColor();
if (dfg == null)
textArea.setDisabledTextColor(UIManager.getColor(prefix +
".inactiveForeground"));
Border b = textArea.getBorder();
if (b == null)
textArea.setBorder(UIManager.getBorder(prefix + ".border"));
Insets margin = textArea.getMargin();
if (margin == null)
textArea.setMargin(UIManager.getInsets(prefix + ".margin"));
}
/*****************************************************************************/
protected void installKeyboardActions() {
// backward compatibility support... keymaps for the UI
// are now installed in the more friendly input map.
textArea.setKeymap(createKeymap());
InputMap km = getInputMap();
if (km != null) {
SwingUtilities.replaceUIInputMap(textArea,
JComponent.WHEN_FOCUSED, km);
}
ActionMap map = getActionMap();
if (map != null) {
SwingUtilities.replaceUIActionMap(textArea, map);
}
updateFocusAcceleratorBinding(false);
}
/*****************************************************************************/
/**
* Installs listeners for the UI.
*/
protected void installListeners() {
}
/*****************************************************************************/
/**
* Installs defaults that the user shouldn't be able to muck with.
*/
private void installPrivateDefaults() {
textArea.addMouseListener(defaultDragRecognizer);
textArea.addMouseMotionListener(defaultDragRecognizer);
// NOTE: At a minimum, we must at least reinstall the current caret;
// otherwise, our added mouse listeners won't take effect (as
// ConfigurableCaret is not an instance of UIResource, and
// unfortunately, due to the design of the Java Swing text package,
// the drag listeners must be registered before the caret for DnD to
// work).
Caret caret = textArea.getCaret();
if (caret==null)
textArea.setCaret(createCaret());
else
textArea.setCaret(caret);
Highlighter highlighter = textArea.getHighlighter();
if (highlighter == null || highlighter instanceof UIResource) {
textArea.setHighlighter(createHighlighter());
}
TransferHandler th = textArea.getTransferHandler();
if (th == null || th instanceof UIResource) {
textArea.setTransferHandler(getTransferHandler());
}
DropTarget dropTarget = textArea.getDropTarget();
if (dropTarget instanceof UIResource) {
if (dropTargetListener == null) {
dropTargetListener = new RTADropTargetListener();
}
try {
dropTarget.addDropTargetListener(dropTargetListener);
} catch (TooManyListenersException tmle) {
// should not happen... swing drop target is multicast
}
}
}
/*****************************************************************************/
/**
* Installs this UI to the given text component.
*/
public void installUI(JComponent c) {
if (c instanceof RTextArea) {
textArea = (RTextArea)c;
// Create our view.
rootView = new RTARootView(this);
// Install defaults.
installDefaults();
installPrivateDefaults();
// NOTE: the opaque property should be set to true when the
// background is a color. When an image is used for the
// background, opaque should be set to false. This is because
// we perform faster when setOpaque is true, but if we use an
// image for the background when opaque is true, we get on-screen
// garbage when the user scrolls via the arrow keys. Thus we
// need setOpaque to be false in that case.
textArea.setOpaque(true);
textArea.setAutoscrolls(true);
textArea.setDragEnabled(true);
// Attach to the model and editor
textArea.addPropertyChangeListener(updateHandler);
Document doc = textArea.getDocument();
if (doc == null) {
// If there is no model, create a default one. This will
// fire a notification to the updateHandler which takes
// care of the rest.
textArea.setDocument(getEditorKit(textArea).
createDefaultDocument());
}
else {
doc.addDocumentListener(updateHandler);
modelChanged();
}
// install keymap
installListeners();
installKeyboardActions();
LayoutManager oldLayout = textArea.getLayout();
if ((oldLayout == null) || (oldLayout instanceof UIResource)) {
// by default, use default LayoutManger implementation that
// will position the components associated with a View object.
textArea.setLayout(updateHandler);
} // End of if (c instanceof RTextArea).
}
else
throw new Error("RTextAreaUI needs an instance of RTextArea!");
}
/*****************************************************************************/
/**
* Flags model changes.
* This is called whenever the model has changed.
* It is implemented to rebuild the view hierarchy
* to represent the default root element of the
* associated model.
*/
protected void modelChanged() {
// create a view hierarchy
ViewFactory f = rootView.getViewFactory();
Document doc = textArea.getDocument();
Element elem = doc.getDefaultRootElement();
setView(f.create(elem));
}
/*****************************************************************************/
/**
* 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 view 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 {
RTextAreaDocument doc = (RTextAreaDocument)textArea.getDocument();
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 {
doc.readUnlock();
}
return null;
}
/*****************************************************************************/
/**
* Paints the text area. This is routed to the <code>paintSafely</code>
* method under the guarantee that the model won't change from the view of
* this thread while it's rendering. This enables the model to
* potentially be updated asynchronously.
*
* @param g The graphics context in which to paint.
* @param c The <code>RTextArea</code> to paint.
*/
public final void paint(Graphics g, JComponent c) {
if ((rootView.getViewCount() > 0) && (rootView.getView(0) != null)) {
RTextAreaDocument doc = (RTextAreaDocument)textArea.getDocument();
doc.readLock();
try {
paintSafely(g);
} finally {
doc.readUnlock();
}
}
}
/*****************************************************************************/
/**
* Paints the highlighted current line, if it is enabled.
*
* @param g The graphics context with which to paint.
* @param visibleRect The visible rectangle of the text area.
*/
protected void paintCurrentLineHighlight(Graphics g, Rectangle visibleRect) {
if (textArea.isCurrentLineHighlightEnabled()) {
Caret caret = textArea.getCaret();
if (caret.getDot()==caret.getMark()) {
Color highlight = textArea.getCurrentLineHighlightColor();
// NOTE: We use the getLineHeight() method below instead
// of currentCaretRect.height because of a bug where
// currentCaretRect.height is incorrect when an
// RSyntaxTextArea is first displayed (it is initialized
// with the text area's font.getHeight() (via RTextArea),
// but isn't changed to account for the syntax styles
// before it is displayed).
//int height = textArea.currentCaretRect.height);
int height = textArea.getLineHeight();
if (textArea.getFadeCurrentLineHighlight()) {
Graphics2D g2d = (Graphics2D)g;
Color bg = textArea.getBackground();
GradientPaint paint = new GradientPaint(
visibleRect.x,0, highlight,
visibleRect.x+visibleRect.width,0,
bg==null ? Color.WHITE : bg);
g2d.setPaint(paint);
g2d.fillRect(visibleRect.x,textArea.currentCaretY,
visibleRect.width, height);
}
else {
g.setColor(highlight);
g.fillRect(visibleRect.x,textArea.currentCaretY,
visibleRect.width, height);
}
} // End of if (caret.getDot()==caret.getMark()).
} // End of if (textArea.isCurrentLineHighlightEnabled()...
}
/*****************************************************************************/
/**
* Draws the "margin line" if enabled.
*
* @param g The graphics context to paint with.
* @param visibleRect The visible rectangle of this text area.
*/
protected void paintMarginLine(Graphics g, Rectangle visibleRect) {
if (textArea.isMarginLineEnabled()) {
g.setColor(textArea.getMarginLineColor());
Insets insets = textArea.getInsets();
int marginLineX = textArea.getMarginLinePixelLocation() +
(insets==null ? 0 : insets.left);
g.drawLine(marginLineX,visibleRect.y,
marginLineX,visibleRect.y+visibleRect.height);
}
}
/*****************************************************************************/
/**
* Paints the root view (i.e., all of the text), if necessary.
*
* @param g The graphics context.
*/
protected void paintRootView(Graphics g) {
Rectangle alloc = getVisibleEditorRect();
if (alloc != null)
rootView.paint(g, alloc);
}
/*****************************************************************************/
/**
* Paints the interface safely with a guarantee that the model won't change
* from the view of this thread. This does the following things, rendering
* from back to front.
* <ol>
* <li>If the component is marked as opaque, the background is painted in
* the current background color of the component.</li>
* <li>The highlights (if any) are painted.</li>
* <li>The view hierarchy is painted.</li>
* <li>The caret is painted.</li>
* </ol>
*
* @param g The graphics context.
*/
protected void paintSafely(Graphics g) {
painted = true;
// Paint the "background" stuff (though main background
// is already done).
Rectangle visibleRect = textArea.getVisibleRect();
paintCurrentLineHighlight(g, visibleRect);
paintMarginLine(g, visibleRect);
// Paint the actual text.
paintRootView(g);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -