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

📄 rtextareabase.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
📖 第 1 页 / 共 3 页
字号:
	/**
	 * Sets the background color of this text editor.  Note that this is
	 * equivalent to calling <code>setBackgroundObject(bg)</code>.
	 *
	 * NOTE:  the opaque property is set to <code>true</code> when the
	 * background is set to a color (by this method).  When an image is used
	 * for the background, opaque is set to false.  This is because
	 * we perform better 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.<p>
	 * You never have to change the opaque property yourself; it is always done
	 * for you.
	 *
	 * @param bg The color to use as the background color.
	 */
	public void setBackground(Color bg) {
		Object oldBG = getBackgroundObject();
		if (oldBG instanceof Color) { // Just change color of strategy.
			((ColorBackgroundPainterStrategy)backgroundPainter).
						setColor(bg);
		}
		else { // Was an image painter...
			backgroundPainter = new ColorBackgroundPainterStrategy(bg);
		}
		setOpaque(true);
		firePropertyChange("background", oldBG, bg);
		repaint();
	}


/*****************************************************************************/


	/**
	 * Sets this image as the background image.<p>
	 *
	 * NOTE:  the opaque property is set to <code>true</code> when the
	 * background is set to a color.  When an image is used for the
	 * background (by this method), opaque is set to false.  This is because
	 * we perform better 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.<p>
	 * You never have to change the opaque property yourself; it is always done
	 * for you.
	 *
	 * @param image The image to use as this text area's background.
	 * @see #getBackgroundImage
	 */
	public void setBackgroundImage(Image image) {
		Object oldBG = getBackgroundObject();
		if (oldBG instanceof Image) { // Just change image being displayed.
			((BufferedImageBackgroundPainterStrategy)backgroundPainter).
					setImage(image);
		}
		else { // Was a color strategy...
			BufferedImageBackgroundPainterStrategy strategy =
				new BufferedImageBackgroundPainterStrategy(this);
			strategy.setImage(image);
			backgroundPainter = strategy;
		}
		setOpaque(false);
		firePropertyChange("background.image", oldBG, image);
		repaint();
	}


/*****************************************************************************/


	/**
	 * Makes the background into this <code>Object</code>.
	 *
	 * @param newBackground The <code>java.awt.Color</code> or
	 *                      <code>java.awt.Image</code> object.
	 *                      If <code>newBackground</code> is not either of
	 *                      these, the background is set to plain white.
	 */
	public void setBackgroundObject(Object newBackground) {
		if (newBackground instanceof Color) {
			setBackground((Color)newBackground);
		}
		else if (newBackground instanceof Image) {
			setBackgroundImage((Image)newBackground);
		}
		else {
			setBackground(Color.WHITE);
		}

	}


/*****************************************************************************/


	/**
	 * Sets the color to use to highlight the current line.  Note that if
	 * highlighting the current line is turned off, you will not be able to
	 * see this highlight.  This method fires a property change of type
	 * <code>CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY</code>.
	 *
	 * @param color The color to use to highlight the current line.
	 * @throws NullPointerException if <code>color</code> is <code>null</code>.
	 * @see #isCurrentLineHighlightEnabled
	 * @see #setCurrentLineHighlightEnabled
	 * @see #getCurrentLineHighlightColor
	 */
	public void setCurrentLineHighlightColor(Color color)
										throws NullPointerException {
		if (color==null)
			throw new NullPointerException();
		if (!color.equals(currentLineColor)) {
			Color old = currentLineColor;
			currentLineColor = color;
			firePropertyChange(CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY,
							old, color);
		}
	}


/*****************************************************************************/


	/**
	 * Sets whether or not the current line is highlighted.  This method
	 * fires a property change of type
	 * <code>CURRENT_LINE_HIGHLIGHT_PROPERTY</code>.
	 *
	 * @param enabled Whether or not to highlight the current line.
	 * @see #isCurrentLineHighlightEnabled
	 * @see #getCurrentLineHighlightColor
	 * @see #setCurrentLineHighlightColor
	 */
	public void setCurrentLineHighlightEnabled(boolean enabled) {
		if (enabled!=highlightCurrentLine) {
			highlightCurrentLine = enabled;
			firePropertyChange(CURRENT_LINE_HIGHLIGHT_PROPERTY,
							!enabled, enabled);
		}
	}


/*****************************************************************************/


	/**
	 * Sets whether the current line highlight should have a "fade" effect.
	 * This method fires a property change event of type
	 * <code>CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY</code>.
	 *
	 * @param fade Whether the fade effect should be enabled.
	 * @see #getFadeCurrentLineHighlight
	 */
	public void setFadeCurrentLineHighlight(boolean fade) {
		if (fade!=fadeCurrentLineHighlight) {
			fadeCurrentLineHighlight = fade;
			if (isCurrentLineHighlightEnabled())
				repaint();
			firePropertyChange(CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY,
							!fade, fade);
		}
	}


/*****************************************************************************/


	/**
	 * Sets the font for this text area.  This is overridden only so that we
	 * can update the size of the "current line highlight" and the location of
	 * the "margin line," if necessary.
	 *
	 * @param font The font to use for this text component.
	 */
	public void setFont(Font font) {
		super.setFont(font);
		updateMarginLineX();
		if (highlightCurrentLine)
			possiblyUpdateCurrentLineHighlightLocation();
	}


/*****************************************************************************/


	/**
	 * Sets whether or not word wrap is eanbled.  This is overridden so that
	 * the "current line highlight" gets updated if it needs to be.
	 *
	 * @param wrap Whether or not word wrap should be enabled.
	 */
	public void setLineWrap(boolean wrap) {
		super.setLineWrap(wrap);
		forceCurrentLineHighlightRepaint();
	}


/*****************************************************************************/


	/**
	 * Sets the color used to paint the margin line.
	 *
	 * @param color The new margin line color.
	 * @see #getDefaultMarginLineColor
	 * @see #getMarginLineColor
	 */
	public void setMarginLineColor(Color color) {
		marginLineColor = color;
		if (marginLineEnabled) {
			Rectangle visibleRect = getVisibleRect();
			repaint(marginLineX,visibleRect.y,
				marginLineX,visibleRect.y+visibleRect.height);
		}
	}


/*****************************************************************************/


	/**
	 * Enables or disables the margin line.
	 *
	 * @param enabled Whether or not the margin line should be enabled.
	 * @see #isMarginLineEnabled
	 */
	public void setMarginLineEnabled(boolean enabled) {
		if (enabled!=marginLineEnabled) {
			marginLineEnabled = enabled;
			if (marginLineEnabled) {
				Rectangle visibleRect = getVisibleRect();
				repaint(marginLineX,visibleRect.y,
						marginLineX,visibleRect.y+visibleRect.height);
			}
		}
	}


/*****************************************************************************/


	/**
	 * Sets the number of 'm' widths the margin line is over.
	 *
	 * @param size The margin size.
	 * #see #getDefaultMarginLinePosition
	 * @see #getMarginLinePosition
	 */
	public void setMarginLinePosition(int size) {
		marginSizeInChars = size;
		if (marginLineEnabled) {
			Rectangle visibleRect = getVisibleRect();
			repaint(marginLineX,visibleRect.y,
					marginLineX,visibleRect.y+visibleRect.height);
			updateMarginLineX();
			repaint(marginLineX,visibleRect.y,
					marginLineX,visibleRect.y+visibleRect.height);
		}
	}


/*****************************************************************************/


	/**
	 * Sets whether the edges of selections are rounded in this text area.
	 * This method fires a property change of type
	 * <code>ROUNDED_SELECTION_PROPERTY</code>.
	 *
	 * @param rounded Whether selection edges should be rounded.
	 * @see #getRoundedSelectionEdges
	 */
	public void setRoundedSelectionEdges(boolean rounded) {
		if (roundedSelectionEdges!=rounded) {
			roundedSelectionEdges = rounded;
			ConfigurableCaret cc = (ConfigurableCaret)getCaret();
			cc.setRoundedSelectionEdges(rounded);
			repaint();
			firePropertyChange(ROUNDED_SELECTION_PROPERTY, !rounded,
											rounded);
		}
	}


/*****************************************************************************/


	/**
	 * Sets the UI for this <code>RTextArea</code>.  Note that, for instances
	 * of <code>RTextArea</code>, <code>setUI</code> only updates the popup
	 * menu; this is because <code>RTextArea</code>s' look and feels are
	 * independent of the Java Look and Feel.  This method is here so
	 * subclasses can set a UI (subclass of <code>RTextAreaUI</code>) if they
	 * have to.
	 *
	 * @param ui The new UI.
	 * @see #setUI
	 */
	private void setRTextAreaUI(RTextAreaUI ui) {

		super.setUI(ui);

		// Workaround as setUI makes the text area opaque, even if we don't
		// want it to be.
		setOpaque(getBackgroundObject() instanceof Color);

	}


/*****************************************************************************/


	/**
	 * Changes whether or not tabs should be emulated with spaces (i.e., soft
	 * tabs).  Note that this affects all tabs inserted AFTER this call, not
	 * tabs already in the document.  For that, see
	 * {@link #convertTabsToSpaces} and {@link #convertSpacesToTabs}.
	 *
	 * @param areEmulated Whether or not tabs should be emulated with spaces.
	 * @see #convertSpacesToTabs
	 * @see #convertTabsToSpaces
	 * @see #getTabsEmulated
	 */
	public void setTabsEmulated(boolean areEmulated) {
		tabsEmulatedWithSpaces = areEmulated;
	}


/*****************************************************************************/


	/**
	 * Workaround, since in JDK1.4 it appears that <code>setTabSize()</code>
	 * doesn't work for a <code>JTextArea</code> unless you use the constructor
	 * specifying the number of rows and columns...<p>
	 * Sets the number of characters to expand tabs to. This will be multiplied
	 * by the maximum advance for variable width fonts. A PropertyChange event
	 * ("tabSize") is fired when the tab size changes.
	 *
	 * @param size Number of characters to expand to.
	 */
	public void setTabSize(int size) {
		super.setTabSize(size);
		boolean b = getLineWrap();
		setLineWrap(!b);
		setLineWrap(b);
	}


/*****************************************************************************/


	/**
	 * This is here so subclasses such as <code>RSyntaxTextArea</code> that
	 * have multiple fonts can define exactly what it means, for example, for
	 * the margin line to be "80 characters" over.
	 */
	protected void updateMarginLineX() {
		marginLineX = getFontMetrics(getFont()).charWidth('m') *
												marginSizeInChars;
	}


/*****************************************************************************/
/********************** INNER CLASSES ****************************************/
/*****************************************************************************/


	protected class RTAMouseListener extends CaretEvent implements
					MouseListener, MouseMotionListener, FocusListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5409600229096811489L;

		RTAMouseListener(RTextAreaBase textArea) {
			super(textArea);
		}

		public void focusGained(FocusEvent e) {}
		public void focusLost(FocusEvent e) {}
		public void mouseDragged(MouseEvent e) {}
		public void mouseMoved(MouseEvent e) {}
		public void mouseClicked(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}

		public int getDot() {
			return dot;
		}

		public int getMark() {
			return mark;
		}

		protected int dot;
		protected int mark;

	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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