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

📄 rsyntaxtextareaui.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 02/24/2004
 *
 * RSyntaxTextAreaUI.java - UI for an RSyntaxTextArea.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.rsyntaxtextarea;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeEvent;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.InputMapUIResource;
import javax.swing.text.*;

import org.fife.ui.rtextarea.RTextArea;
import org.fife.ui.rtextarea.RTextAreaUI;


/**
 * UI used by <code>RSyntaxTextArea</code>.  This allows us to implement
 * syntax highlighting.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class RSyntaxTextAreaUI extends RTextAreaUI {

	private static final String SHARED_ACTION_MAP_NAME	= "RSyntaxTextAreaUI.actionMap";
	private static final String SHARED_INPUT_MAP_NAME		= "RSyntaxTextAreaUI.inputMap";
	private static final EditorKit defaultKit			= new RSyntaxTextAreaEditorKit();


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


	public static ComponentUI createUI(JComponent ta) {
		return new RSyntaxTextAreaUI(ta);
	}


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


	/**
	 * Constructor.
	 */
	public RSyntaxTextAreaUI(JComponent rSyntaxTextArea) {
		super(rSyntaxTextArea);
	}


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


    /**
     * Creates the view for an element.
     *
     * @param elem The element.
     * @return The view.
     */
	public View create(Element elem) {
		RTextArea c = getRTextArea();
		if (c instanceof RSyntaxTextArea) {
			RSyntaxTextArea area = (RSyntaxTextArea) c;
			View v;
			if (area.getLineWrap())
				//v = new WrappedSyntaxView(elem); // Always do wraps on word boundaries...
				v = new WrappedSyntaxView_TEST(elem);
			else
				v = new SyntaxView(elem);
			return v;
		}
		return null;
	}


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


	/**
	 * Returns the name to use to cache/fetch the shared action map.  This
	 * should be overridden by subclasses if the subclass has its own custom
	 * editor kit to install, so its actions get picked up.
	 *
	 * @return The name of the cached action map.
	 */
	protected String getActionMapName() {
		return SHARED_ACTION_MAP_NAME;
	}


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


	/**
	 * Fetches the EditorKit for the UI.
	 *
	 * @param tc The text component for which this UI is installed.
	 * @return The editor capabilities.
	 * @see javax.swing.plaf.TextUI#getEditorKit
	 */
	public EditorKit getEditorKit(JTextComponent tc) {
		return defaultKit;
	}


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


	/**
	 * Get the InputMap to use for the UI.  
	 */
	protected InputMap getInputMap() {
		InputMap map = new InputMapUIResource();
		InputMap shared = (InputMap)UIManager.get(SHARED_INPUT_MAP_NAME);
		if (shared==null) {
			shared = new RSyntaxTextAreaDefaultInputMap();
			UIManager.put(SHARED_INPUT_MAP_NAME, shared);
		}
		//KeyStroke[] keys = shared.allKeys();
		//for (int i=0; i<keys.length; i++)
		//	System.err.println(keys[i] + " -> " + shared.get(keys[i]));
		if (shared != null)
			map.setParent(shared);
		return map;
	}


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


	/**
	 * Paints the "matched bracket", if any.
	 *
	 * @param g The graphics context.
	 */
	public void paintMatchedBracket(Graphics g) {
		// We must add "-1" to the height because otherwise we'll paint below
		// the region that gets invalidated.
		RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
		if (rsta.isBracketMatchingEnabled()) {
			Rectangle match = rsta.match;
			if (match!=null) {
				g.setColor(rsta.getMatchedBracketBGColor());
				g.fillRect(match.x,match.y, match.width,match.height-1);
				g.setColor(rsta.getMatchedBracketBorderColor());
				g.drawRect(match.x,match.y, match.width,match.height-1);
			}
		}
	}


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


	/**
	 * 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 "matched bracket" if any.
		paintMatchedBracket(g);

		// Paint the actual text.
		paintRootView(g);

		// Paint the caret.
		Caret caret = textArea.getCaret();
		if (caret != null)
			caret.paint(g);

	}


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


	/**
	 * Gets called whenever a bound property is changed on this UI's
	 * <code>RSyntaxTextArea</code>.
	 *
	 * @param e The property change event.
	 */
	protected void propertyChange(PropertyChangeEvent e) {

		String name = e.getPropertyName();

		// If they change the syntax scheme, we must do this so that
		// WrappedSyntaxView(_TEST) updates its child views properly.
		if (name.equals(RSyntaxTextArea.SYNTAX_SCHEME_PROPERTY)) {
			modelChanged();
		}

		// Everything else is general to all RTextAreas.
		else
			super.propertyChange(e);


	}


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


	/**
	 * Updates the view.  This should be called when the underlying
	 * <code>RSyntaxTextArea</code> changes its syntax editing style.
	 */
	public void refreshSyntaxHighlighting() {
		modelChanged();
	}


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

}

⌨️ 快捷键说明

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