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

📄 codedocument.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * JSide is an Integrated Development Environment for JavaScript Copyright
 * (C) 2006 JSide Development Team
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library 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 Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package cn.myapps.core.macro.editor.text;

import java.awt.Color;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

/**
 * A styled document that performs syntax coloring and brace matching on
 * the text pane
 * 
 * @author Adeel
 */
public class CodeDocument extends DefaultStyledDocument
{
	// syntax highlighting variables
	private String word;

	private SimpleAttributeSet keyword;

	private SimpleAttributeSet string;

	private SimpleAttributeSet normal;

	private SimpleAttributeSet number;

	private SimpleAttributeSet comments;

	private int currentPos = 0;

	private Vector keywords;

	public static int STRING_MODE = 10;

	public static int TEXT_MODE = 11;

	public static int NUMBER_MODE = 12;

	public static int COMMENT_MODE = 13;

	private static int mode = TEXT_MODE;

	// brace matching variables
	private int braceIndex1;

	private int braceIndex2;

	private Style braceStyle;

	private Style normalStyle;

	private static final char[] BRACES = { '(', ')', '{', '}', '[', ']' };

	/**
	 * Constructor
	 */
	public CodeDocument(boolean codeStyling, HashMap colorsMap, Vector keywords)
	{
		if (codeStyling)
		{
			initSyntaxColoring(colorsMap, keywords);
			initBraceMatching(colorsMap);
		}
	}

	/**
	 * Initializes variables and sets syntax coloring style attributes
	 */
	private void initSyntaxColoring(HashMap colorsMap, Vector keywords)
	{
		// initialize variables
		this.word = "";
		this.keyword = new SimpleAttributeSet();
		this.string = new SimpleAttributeSet();
		this.normal = new SimpleAttributeSet();
		this.number = new SimpleAttributeSet();
		this.comments = new SimpleAttributeSet();
		this.keywords = keywords;

		// set syntax highlighting style attributes
		StyleConstants.setForeground(normal, (Color) colorsMap
				.get("normalColor"));
		StyleConstants.setBold(keyword, true);
		StyleConstants.setForeground(keyword, (Color) colorsMap
				.get("keywordsColor"));
		StyleConstants.setForeground(string, (Color) colorsMap
				.get("stringColor"));
		StyleConstants.setForeground(number, (Color) colorsMap
				.get("numbersColor"));
		StyleConstants.setForeground(comments, (Color) colorsMap
				.get("commentsColor"));
		StyleConstants.setItalic(comments, true);
	}

	/**
	 * Initializes variables and sets brace matchin style attributes
	 */
	private void initBraceMatching(HashMap colorsMap)
	{
		// set brace matching style attributes
		normalStyle = addStyle("normal", null);
		StyleConstants.setForeground(normalStyle, Color.BLACK);
		braceStyle = addStyle("bracket-highlight", null);
		StyleConstants.setForeground(braceStyle, Color.BLUE.darker());
		StyleConstants.setBackground(braceStyle, (Color) colorsMap
				.get("braceMatchingColor"));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.text.Document#insertString(int, java.lang.String,
	 *      javax.swing.text.AttributeSet)
	 */
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException
	{
		resetBracePosition();
		super.insertString(offs, str, normal);
		updateBraces(offs + 1);

		int strLen = str.length();
		int endpos = offs + strLen;
		int strpos;
		for (int i = offs; i < endpos; i++)
		{
			currentPos = i;
			strpos = i - offs;
			processChar(str.charAt(strpos));
		}
		currentPos = offs;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.text.Document#remove(int, int)
	 */
	public void remove(int offset, int length) throws BadLocationException
	{
		resetBracePosition();
		super.remove(offset, length);
		if (offset > 0)
		{
			updateBraces(offset);
		}
	}

	// ////////////////////////////////////////////////////////////////////////
	// Syntax highlighting specific methods
	// ////////////////////////////////////////////////////////////////////////

	private void insertKeyword(String str, int pos)
	{
		try
		{
			// remove the old word and formatting
			this.remove(pos - str.length(), str.length());
			// replace it with the same word, but new formatting we MUST
			// call the super class insertString method here, otherwise we
			// would end up in an infinite loop
			super.insertString(pos - str.length(), str, keyword);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	private void insertTextString(String str, int pos)
	{
		try
		{
			// remove the old word and formatting
			this.remove(pos, str.length());
			super.insertString(pos, str, string);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	private void insertNumberString(String str, int pos)
	{
		try
		{
			// remove the old word and formatting
			this.remove(pos, str.length());
			super.insertString(pos, str, number);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	private void insertCommentString(String str, int pos)
	{
		try
		{
			// remove the old word and formatting
			this.remove(pos, str.length());
			super.insertString(pos, str, comments);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	private void checkForString()
	{
		int offs = this.currentPos;
		Element element = this.getParagraphElement(offs);
		String elementText = "";
		try
		{
			// this gets our chuck of current text for the element
			// we're on
			elementText = this.getText(element.getStartOffset(), element
					.getEndOffset()
					- element.getStartOffset());
		}
		catch (Exception ex)
		{
			// whoops!
			System.out.println("no text");
		}
		int strLen = elementText.length();
		if (strLen == 0)
		{
			return;
		}
		int i = 0;

		if (element.getStartOffset() > 0)
		{
			// translates backward if neccessary
			offs = offs - element.getStartOffset();
		}
		int quoteCount = 0;
		if ((offs >= 0) && (offs <= strLen - 1))
		{
			i = offs;
			while (i > 0)
			{
				// the while loop walks back until we hit a delimiter

				char charAt = elementText.charAt(i);
				if ((charAt == '"'))
				{
					quoteCount++;
				}
				i--;
			}
			int rem = quoteCount % 2;
			mode = (rem == 0) ? TEXT_MODE : STRING_MODE;
		}
	}

	private void checkForKeyword()
	{
		if (mode != TEXT_MODE)
		{
			return;
		}
		int offs = this.currentPos;
		Element element = this.getParagraphElement(offs);
		String elementText = "";
		try
		{
			// this gets our chuck of current text for the element
			// we're on
			elementText = this.getText(element.getStartOffset(), element
					.getEndOffset()
					- element.getStartOffset());
		}
		catch (Exception ex)
		{
			System.err.println("no text");
		}
		int strLen = elementText.length();
		if (strLen == 0)
		{
			return;
		}
		int i = 0;

		if (element.getStartOffset() > 0)
		{
			// translates backward if neccessary
			offs = offs - element.getStartOffset();
		}
		if ((offs >= 0) && (offs <= strLen - 1))
		{
			i = offs;
			while (i > 0)
			{
				// the while loop walks back until we hit a delimiter
				i--;
				char charAt = elementText.charAt(i);
				if ((charAt == ' ') | (i == 0) | (charAt == '(')
						| (charAt == ')') | (charAt == '{') | (charAt == '}'))
				{ // if i == 0 then we're at the begininng
					if (i != 0)
					{
						i++;
					}
					word = elementText.substring(i, offs);// skip the
					// period

					String s = word.trim().toLowerCase();
					// this is what actually checks for a matching
					// keyword
					if (keywords.contains(s))
					{
						insertKeyword(word, currentPos);
					}
					break;
				}
			}
		}
	}

	private void checkForNumber()
	{
		int offs = this.currentPos;

⌨️ 快捷键说明

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