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

📄 rtextscrollpane.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
字号:
/*
 * 11/14/2003
 *
 * RTextScrollPane.java - A JScrollPane that will only accept RTextAreas
 *                        so that it can display line numbers.
 * Copyright (C) 2003 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.rtextarea;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import org.fife.ui.RScrollPane;


/**
 * An extension of <code>org.fife.ui.RScrollPane</code> that will only take
 * <code>RTextArea</code>s for its view.  This class has the ability to show
 * line numbers for its text component view.<p>
 *
 * NOTE:  This class has two different implementations of line numbers,
 * <code>LineNumberList</code> and <code>LineNumberBorder</code>.
 * Currently, <code>LineNumberBorder</code> is used as it is much faster;
 * however, <code>LineNumberList</code> is more "object-oriented" and
 * would allow for more user-interaction with the line numbers...
 *
 * @author Robert Futrell
 * @version 0.5
 */
public class RTextScrollPane extends RScrollPane implements ActionListener {

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

	public RTextArea textArea;

	// NOTE:  Change which line numbering scheme you use by changing the
	// boolean below.
	private LineNumberList lineNumberList;		// More OO-ish.
	private LineNumberBorder lineNumberBorder;	// Faster.
	private static final boolean usingLineNumberList = false;


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


/*
 * NOTE:  This was just a test to see if we could identify lines with brackets
 * in them, and should be removed.
private class WorkerThread implements Runnable {
	private javax.swing.text.Segment s;
	public WorkerThread() {
		s = new javax.swing.text.Segment();
	}
	public void run() {
		while (true) {
			try {
				if (textArea!=null && lineNumberBorder!=null) {
					lineNumberBorder.clearHighlightedLines();
					for (int i=0; i<textArea.getLineCount(); i++) {
						int start = textArea.getLineStartOffset(i);
						int end = textArea.getLineEndOffset(i);
						javax.swing.text.Document doc = textArea.getDocument();
						doc.getText(start,end-start, s);
						boolean hasLeft = false;
						boolean hasRight = false;
						for (int j=s.offset; j<s.offset+s.count; j++) {
							switch (s.array[j]) {
								case '{':
									hasLeft = true;
									break;
								case '}':
									hasRight = true;
									break;
							}
						}
						if (hasLeft || hasRight)
							lineNumberBorder.addHighlightedLine(i);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException ie) {}
		}
	}
}
*/

	/**
	 * Creates a scroll pane with preferred size (width, height).  A default
	 * value will be used for line number color (gray), and the current
	 * line's line number will be highlighted.
	 *
	 * @param width The preferred width of <code>area</code>.
	 * @param height The preferred height of <code>area</code>.
	 * @param area The text area this scroll pane will contain.
	 * @param lineNumbersEnabled Whether line numbers are initially enabled.
	 */
	public RTextScrollPane(int width, int height, RTextArea area,
						boolean lineNumbersEnabled) {
		this(width, height, area, lineNumbersEnabled, new Color(128,128,128));
	}


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


	/**
	 * Creates a scroll pane with preferred size (width, height).
	 *
	 * @param width The preferred width of <code>area</code>.
	 * @param height The preferred height of <code>area</code>.
	 * @param area The text area this scroll pane will contain.
	 * @param lineNumbersEnabled Whether line numbers are initially enabled.
	 * @param lineNumberColor The color to use for line numbers.
	 */
	public RTextScrollPane(int width, int height, RTextArea area,
					boolean lineNumbersEnabled, Color lineNumberColor) {

		// Call RScrollPane's constructor.
		super(width, height, area);

/*
Thread workerThread = new Thread(new WorkerThread());
workerThread.start();
*/

		// Create the text area and set it inside this scrollbar area.
		textArea = area;

		// Create the line number list for this document.
		if (usingLineNumberList) {
			lineNumberList = new LineNumberList(textArea, lineNumberColor);
			lineNumberList.setFont(new Font("monospaced", Font.PLAIN, 12));
		}
		else {
			enableEvents(AWTEvent.MOUSE_EVENT_MASK);
			lineNumberBorder = new LineNumberBorder(this, textArea,
											lineNumberColor);
			lineNumberBorder.setFont(new Font("monospaced", Font.PLAIN, 12));
		}
		setLineNumbersEnabled(lineNumbersEnabled);

		// Set miscellaneous properties.
		setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

	}


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


	/**
	 * Returns <code>true</code> if the line numbers are enabled and visible.
	 *
	 * @return Whether or not line numbers are visible.
	 */
	public boolean areLineNumbersEnabled() {
		if (usingLineNumberList)
			return (getRowHeader()==null);
		return lineNumberBorder!=null;
	}


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


	/**
	 * This method is overridden so that if the user clicks in the line
	 * number border, the caret is moved.<p>
	 *
	 * This method will ONLY work if LineNumberBorder is used
	 * (not LineNumberList).
	 *
	 * @param e The mouse event.
	 */
	public void processMouseEvent(MouseEvent e) {
		if (e.getID()==MouseEvent.MOUSE_CLICKED) {
			int y = getViewport().getViewPosition().y + e.getY();
			int pos = textArea.viewToModel(new Point(0, y));
			textArea.setCaretPosition(pos);
		}
		super.processMouseEvent(e);
	}


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


	/**
	 * Toggles whether or not line numbers are visible.
	 *
	 * @param enabled Whether or not line numbers should be visible.
	 */
	public void setLineNumbersEnabled(boolean enabled) {
		if (usingLineNumberList) {
			if (enabled) {
				lineNumberList.updateCellWidths();
				JViewport viewport = new JViewport() {
						public void setViewPosition(java.awt.Point p) {
							Component c = getView();
							if (c!=null)
								c.repaint();
							//getView().setLocation(-p.x, -p.y);
							//fireStateChanged();
						}
					};
				viewport.setView(lineNumberList);
				setRowHeader(viewport);
				//setRowHeaderView(lineNumberList);
			}
			else
				setRowHeaderView(null);
		}
		else { // lineNumberBorder
			setViewportBorder(enabled ? lineNumberBorder : null);
			revalidate();
		}
	}



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

}

⌨️ 快捷键说明

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