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

📄 rsyntaxtextarea.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
📖 第 1 页 / 共 3 页
字号:
	/**
	 * Attempts to save all currently-known templates to the current template
	 * directory, as set by <code>setTemplateDirectory</code>.  Templates
	 * will be saved as XML files with names equal to their abbreviations; for
	 * example, a template that expands on the word "forb" will be saved as
	 * <code>forb.xml</code>.
	 *
	 * @return Whether or not the save was successful.  The save will
	 *         be unsuccessful if the template directory does not exist or
	 *         if it has not been set (i.e., you have not yet called
	 *         <code>setTemplateDirectory</code>).
	 * @see #getTemplatesEnabled
	 * @see #setTemplateDirectory
	 * @see #setTemplatesEnabled
	 */
	public synchronized static boolean saveTemplates() {
		if (!getTemplatesEnabled() || codeTemplateManager==null)
			return false;
		return codeTemplateManager.saveTemplates();
	}


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


	/**
	 * Sets whether or not auto-indent is enabled.  This fires a property
	 * change event of type <code>AUTO_INDENT_PROPERTY</code>.
	 *
	 * @param enabled Whether or not auto-indent is enabled.
	 * @see #isAutoIndentEnabled
	 */
	public void setAutoIndentEnabled(boolean enabled) {
		if (autoIndentEnabled!=enabled) {
			autoIndentEnabled = enabled;
			firePropertyChange(AUTO_INDENT_PROPERTY, !enabled, enabled);
		}
	}


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


	/**
	 * Sets whether bracket matching is enabled.  This fires a property change
	 * event of type <code>BRACKET_MATCHING_PROPERTY</code>.
	 *
	 * @param enabled Whether or not bracket matching should be enabled.
	 * @see #isBracketMatchingEnabled
	 */
	public void setBracketMatchingEnabled(boolean enabled) {
		if (enabled!=bracketMatchingEnabled) {
			bracketMatchingEnabled = enabled;
			repaint();
			firePropertyChange(BRACKET_MATCHING_PROPERTY, !enabled, enabled);
		}
	}


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


	/**
	 * Sets whether or not lines containing nothing but whitespace are made
	 * into blank lines when Enter is pressed in them.  This method fires
	 * a property change event of type
	 * <code>CLEAR_WHITESPACE_LINES_PROPERTY</code>.
	 *
	 * @param enabled Whether or not whitespace-only lines are cleared when
	 *                the user presses Enter on them.
	 * @see #isClearWhitespaceLinesEnabled
	 */
	public void setClearWhitespaceLinesEnabled(boolean enabled) {
		if (enabled!=clearWhitespaceLines) {
			clearWhitespaceLines = enabled;
			firePropertyChange(CLEAR_WHITESPACE_LINES_PROPERTY,
							!enabled, enabled);
		}
	}


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


	/**
	 * Sets the document used by this text area.  This is overridden so that
	 * only instances of {@link RSyntaxDocument} are accepted; for all
	 * others, an exception will be thrown.
	 *
	 * @param document The new document for this text area.
	 * @throws IllegalArgumentException If the document is not an
	 *                                  <code>RSyntaxDocument</code>.
	 */
	public void setDocument(Document document) {
		if (!(document instanceof RSyntaxDocument))
			throw new IllegalArgumentException("Documents for " +
					"RSyntaxTextArea must be instances of " +
					"RSyntaxDocument!");
		super.setDocument(document);
	}


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


	/**
	 * Sets the font used by this text area.  Note that this method does not
	 * alter the appearance of an <code>RSyntaxTextArea</code> since it uses
	 * different fonts for each token type.
	 *
	 * @param font The font.
	 */
	public void setFont(Font font) {
		if (font!=null) {
			// We must be connected to a screen resource for our
			// graphics to be non-null.
			if (isDisplayable())
				refreshFontMetrics(getGraphics2D(getGraphics()));
			super.setFont(font);
		}
	}


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


	/**
	 * Sets the color used as the background for a matched bracket.
	 *
	 * @param color The color to use.
	 * @see #getMatchedBracketBGColor
	 * @see #setMatchedBracketBorderColor
	 */
	public void setMatchedBracketBGColor(Color color) {
		matchedBracketBGColor = color;
		if (match!=null)
			repaint();
	}


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


	/**
	 * Sets the color used as the border for a matched bracket.
	 *
	 * @param color The color to use.
	 * @see #getMatchedBracketBorderColor
	 * @see #setMatchedBracketBGColor
	 */
	public void setMatchedBracketBorderColor(Color color) {
		matchedBracketBorderColor = color;
		if (match!=null)
			repaint();
	}


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


	public void setParser(Parser parser) {
		if (parserManager==null)
			parserManager = new ParserManager(this);
		clearParserNoticeHighlights();
		parserManager.setParser(parser);
	}

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


	/**
	 * Sets what type of syntax highlighting this editor is doing.  This method
	 * fires a property change of type <code>SYNTAX_STYLE_PROPERTY</code>.
	 *
	 * @param style The syntax editing style to use, for example,
	 *              <code>RSyntaxTextArea.NO_SYNTAX_STYLE</code> or
	 *              <code>RSyntaxArea.JAVA_SYNTAX_STYLE</code>.
	 * @see #getSyntaxEditingStyle
	 */
	public void setSyntaxEditingStyle(int style) {

		if (style<NO_SYNTAX_STYLE || style>MAX_SYNTAX_STYLE_NUMBER)
			style = NO_SYNTAX_STYLE;

		if (style != syntaxStyle) {
			int oldStyle = syntaxStyle;
			syntaxStyle = style;
			((RSyntaxDocument)getDocument()).setSyntaxStyle(style);
			firePropertyChange(SYNTAX_STYLE_PROPERTY, oldStyle, syntaxStyle);
		}

	}


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


	/**
	 * Sets all of the colors used in syntax highlighting to the colors
	 * specified.  This uses a shallow copy of the color scheme so that
	 * multiple text areas can share the same color scheme and have their
	 * properties changed simultaneously.<p>
	 *
	 * This method fires a property change event of type
	 * <code>SYNTAX_SCHEME_PROPERTY</code>.
	 *
	 * @param colorScheme The instance of
	 *        <code>SyntaxHighlightingColorScheme</code> to use.
	 * @see #getSyntaxHighlightingColorScheme
	 */
	public void setSyntaxHighlightingColorScheme(final
							SyntaxHighlightingColorScheme colorScheme) {

		// NOTE:  We don't check whether colorScheme is the same as the
		// current scheme because DecreaseFontSizeAction and
		// IncreaseFontSizeAction need it this way.
		// FIXME:  Find a way around this.

		SyntaxHighlightingColorScheme old = this.colorScheme;
		this.colorScheme = colorScheme;

		// Recalculate the line height.  We do this here instead of in
		// refreshFontMetrics() as this method is called less often and we
		// don't need the rendering hints to get the font's height.
		lineHeight = maxAscent = 0;
		for (int i=0; i<colorScheme.syntaxSchemes.length; i++) {
			SyntaxScheme ss = colorScheme.syntaxSchemes[i];
			if (ss!=null) {
				FontMetrics fm = getFontMetrics(ss.font);
				int height = fm.getHeight();
				if (height>lineHeight)
					lineHeight = height;
				int ascent = fm.getMaxAscent();
				if (ascent>maxAscent)
					maxAscent = ascent;
			}
		}

		// Set the font of the text area to be that of an Identifier token.
		// We do this because ConfigurableCaret will default to the
		// width of a space (' ') character in this font if a
		// block/underline caret is at the end of a line (and thus
		// SyntaxView/WrappedSyntaxView.modelToView() does not
		// return a width, it returns 1).  Doing this also refreshes our
		// FontMetrics cache.
		setFont(colorScheme.syntaxSchemes[Token.IDENTIFIER].font);

		// Updates the margin line.
		updateMarginLineX();

		// Force the current line highlight to be repainted, even though the
		// caret's location hasn't changed.
		forceCurrentLineHighlightRepaint();

		// So any encompassing JScrollPane will have its scrollbars updated.
		revalidate();

		firePropertyChange(SYNTAX_SCHEME_PROPERTY, old, this.colorScheme);

	}


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


	/**
	 * If templates are enabled, all currently-known templates are forgotten
	 * and all templates are loaded from all files in the specified directory
	 * ending in "*.xml".  If templates aren't enabled, nothing happens.
	 *
	 * @param dir The directory containing files ending in extension
	 *            <code>.xml</code> that contain templates to load.
	 * @return <code>true</code> if the load was successful;
	 *         <code>false</code> if either templates aren't currently
	 *         enabled or the load failed somehow (most likely, the
	 *         directory doesn't exist).	 
	 * @see #getTemplatesEnabled
	 * @see #setTemplatesEnabled
	 * @see #saveTemplates
	 */
	public synchronized static boolean setTemplateDirectory(String dir) {
		if (getTemplatesEnabled() && dir!=null) {
			File directory = new File(dir);
			if (directory.isDirectory()) {
				return codeTemplateManager.
					setTemplateDirectory(directory)>-1;
			}
			else {
				boolean created = directory.mkdir();
				if (created) {
					return codeTemplateManager.
						setTemplateDirectory(directory)>-1;
				}
			}
		}
		return false;
	}


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


	/**
	 * Enables or disables templates.<p>
	 *
	 * Templates are a set of "shorthand identifiers" that you can configure
	 * so that you only have to type a short identifier (such as "forb") to
	 * insert a larger amount of code into the document (such as:<p>
	 *
	 * <pre>
	 *   for (&lt;caret&gt;) {
	 *
	 *   }
	 * </pre>
	 *
	 * Templates are a shared resource among all instances of
	 * <code>RSyntaxTextArea</code>; that is, templates can only be
	 * enabled/disabled for all text areas globally, not individually, and
	 * all text areas have access of the same templates.  This should not
	 * be an issue; rather, it should be beneficial as it promotes
	 * uniformity among all text areas in an application.
	 *
	 * @param enabled Whether or not templates should be enabled.
	 * @see #getTemplatesEnabled
	 */
	public synchronized static void setTemplatesEnabled(boolean enabled) {
		if (enabled!=templatesEnabled) {
			templatesEnabled = enabled;
			if (enabled)
				codeTemplateManager = new CodeTemplateManager();
			else
				codeTemplateManager = null;
		}
	}


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


	/**
	 * Sets whether whitespace is visible.  This method fires a property change
	 * of type <code>VISIBLE_WHITESPACE_PROPERTY</code>.
	 *
	 * @param visible Whether whitespace should be visible.
	 * @see #isWhitespaceVisible
	 */
	public void setWhitespaceVisible(boolean visible) {
		if (whitespaceVisible!=visible) {
			whitespaceVisible = visible;
//			((RSyntaxDocument)getDocument()).setWhitespaceVisible(
//														visible);
			((RSyntaxDocument)getDocument()).setWhitespaceVisible(
													visible, this);
			repaint();
			firePropertyChange(VISIBLE_WHITESPACE_PROPERTY,
							!visible, visible);
		}
	}


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


	/**
	 * 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() {
		if (colorScheme!=null) {
			FontMetrics fm = getFontMetricsForTokenType(Token.IDENTIFIER);
			if (fm!=null)
				marginLineX = fm.charWidth('m') * marginSizeInChars;
		}
	}


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

}

⌨️ 快捷键说明

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