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

📄 rsyntaxtextareaeditorkit.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * Action for increasing the font size of all fonts in the text area.
	 */
	public static class IncreaseFontSizeAction
					extends RTextAreaEditorKit.IncreaseFontSizeAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = -144074714712988061L;

		public IncreaseFontSizeAction() {
			super();
		}

		public IncreaseFontSizeAction(String name, Icon icon, String desc,
							Integer mnemonic, KeyStroke accelerator) {
			super(name, icon, desc, mnemonic, accelerator);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
			SyntaxHighlightingColorScheme scheme = rsta.
									getSyntaxHighlightingColorScheme();

			// All we need to do is update all of the fonts in syntax
			// schemes, then call setSyntaxHighlightingColorScheme with the
			// same scheme already being used.  This relies on the fact that
			// that method does not check whether the new scheme is different
			// from the old scheme before updating.

			boolean changed = false;
			int count = scheme.syntaxSchemes.length;
			for (int i=0; i<count; i++) {
				SyntaxScheme ss = scheme.syntaxSchemes[i];
				if (ss!=null) {
					Font font = ss.font;
					if (font!=null) {
						float oldSize = font.getSize2D();
						float newSize = oldSize + increaseAmount;
						if (newSize<=MAXIMUM_SIZE) {
							// Grow by increaseAmount.
							ss.font = font.deriveFont(newSize);
							changed = true;
						}
						else if (oldSize<MAXIMUM_SIZE) {
							// Can't grow by full increaseAmount, but
							// can grow a little bit.
							ss.font = font.deriveFont(MAXIMUM_SIZE);
							changed = true;
						}
					}
				}
			}

			// If we updated at least one font, update the screen.  If
			// all of the fonts were already the minimum size, beep.
			if (changed) {
				rsta.setSyntaxHighlightingColorScheme(scheme);
				// NOTE:  This is a hack to get an encompassing
				// RTextScrollPane to repaint its line numbers to account
				// for a change in line height due to a font change.  I'm
				// not sure why we need to do this here but not when we
				// change the syntax highlighting color scheme via the
				// Options dialog... setSyntaxHighlightingColorScheme()
				// calls revalidate() which won't repaint the scroll pane
				// if scrollbars don't change, which is why we need this.
				Component parent = rsta.getParent();
				if (parent instanceof javax.swing.JViewport) {
					parent = parent.getParent();
					if (parent instanceof JScrollPane) {
						parent.repaint();
					}
				}
			}
			else
				UIManager.getLookAndFeel().provideErrorFeedback(rsta);

		}

	}


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

	
	/**
	 * Action for when the user presses the Enter key.  This is here so we can
	 * be smart and "auto-indent" for programming languages.
	 */
	public static class InsertBreakAction extends RecordableTextAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = -4156839664037841427L;

		public InsertBreakAction() {
			super(DefaultEditorKit.insertBreakAction);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			if (!textArea.isEditable() || !textArea.isEnabled()) {
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);
				return;
			}

			RSyntaxTextArea sta = (RSyntaxTextArea)textArea;

			// If we're in insert-mode and auto-indenting...
			if (sta.getTextMode()==RTextArea.INSERT_MODE && 
				sta.isAutoIndentEnabled()) {

				try {

					int caretPosition = textArea.getCaretPosition();
					Document doc = textArea.getDocument();
					Element map = doc.getDefaultRootElement();
					int lineNum = map.getElementIndex(caretPosition);
					Element line = map.getElement(lineNum);
					int start = line.getStartOffset();
					int end = line.getEndOffset()-1; // Why always "-1"?
					String s = sta.getText(start,end-start);
					int len = s.length();

					// endWS is the end of the leading whitespace of the
					// current line.
					int endWS = 0;
					while (endWS<len &&
						RSyntaxUtilities.isWhitespace(s.charAt(endWS)))
						endWS++;

					// If there is only whitespace between the caret and
					// the EOL, pressing Enter auto-indents the new line to
					// the same place as the previous line.
					int nonWhitespacePos = atEndOfLine(caretPosition-start,
												s, len);
					if (nonWhitespacePos==-1) {
						// If the line was nothing but whitespace...
						if (endWS==len && sta.isClearWhitespaceLinesEnabled())
							sta.replaceRange(null, start,end);
						sta.replaceSelection("\n"+s.substring(0,endWS));
					}

					// If there is non-whitespace between the caret and the
					// EOL, pressing Enter takes that text to the next line
					// and auto-indents it to the same place as the last
					// line.
					else {
						sta.replaceRange("\n" + s.substring(0,endWS) +
								s.substring(nonWhitespacePos),
								caretPosition, end);
						sta.setCaretPosition(caretPosition + endWS + 1);
					}

				} catch (BadLocationException ble) {
					sta.replaceSelection("\n");
				}

			}

			// Otherwise, we're in overwrite-mode or not auto-indenting.
			else {
				textArea.replaceSelection("\n");
			}

		}

		/**
		 * @return The first location in the string past <code>pos</code> that
		 *         is NOT a whitespace char, or <code>-1</code> if only
		 *         whitespace chars follow <code>pos</code> (or it is the end
		 *         position in the string).
		 */
		private static final int atEndOfLine(int pos, String s, int sLen) {
			for (int i=pos; i<sLen; i++) {
				if (!RSyntaxUtilities.isWhitespace(s.charAt(i)))
					return i;
			}
			return -1;
		}

		public final String getMacroID() {
			return DefaultEditorKit.insertBreakAction;
		}

	}


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

	
	/**
	 * Action for inserting tabs.  This is extended to "block indent" a
	 * group of contiguous lines if they are selected.
	 */
	public static class InsertTabAction extends RecordableTextAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = -8377170913694877433L;

		public InsertTabAction() {
			super(insertTabAction);
		}

		public InsertTabAction(String name) {
			super(name);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			if (!textArea.isEditable() || !textArea.isEnabled()) {
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);
				return;
			}

			Document document = textArea.getDocument();
			Element map = document.getDefaultRootElement();
			Caret c = textArea.getCaret();
			int dot = c.getDot();
			int mark = c.getMark();
			int dotLine = map.getElementIndex(dot);
			int markLine = map.getElementIndex(mark);

			// If there is a multiline selection, indent all lines in
			// the selection.
			if (dotLine!=markLine) {
				int first = Math.min(dotLine, markLine);
				int last = Math.max(dotLine, markLine);
				Element elem; int start;
				try {
					for (int i=first; i<last; i++) {
						elem = map.getElement(i);
						start = elem.getStartOffset();
						document.insertString(start, "\t", null);
					}
					// Don't do the last line if the caret is at its
					// beginning.  We must call getDot() again and not just
					// use 'dot' as the caret's position may have changed
					// due to the insertion of the tabs above.
					elem = map.getElement(last);
					start = elem.getStartOffset();
					if (c.getDot()!=start)
						document.insertString(start, "\t", null);
				} catch (BadLocationException ble) { // Never happens.
					ble.printStackTrace();
					UIManager.getLookAndFeel().
									provideErrorFeedback(textArea);
				}
			}
			else {
				textArea.replaceSelection("\t");
			}

		}

		public final String getMacroID() {
			return insertTabAction;
		}

	}


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

	
	/**
	 * Action for when the user tries to insert a template (that is,
	 * they've typed a template ID and pressed the trigger character
	 * (a space) in an attempt to do the substitution).
	 */
	public static class PossiblyInsertTemplateAction extends RecordableTextAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = -6469882543060548701L;

		public PossiblyInsertTemplateAction() {
			super(rstaPossiblyInsertTemplateAction);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			if (!textArea.isEditable() || !textArea.isEnabled())
				return;

			RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;

			if (RSyntaxTextArea.getTemplatesEnabled()) {

				Document doc = textArea.getDocument();
				if (doc != null) {

					try {

						Caret c = textArea.getCaret();
						int dot = c.getDot();
						int mark = c.getMark();
						int p0 = Math.min(dot, mark);
						int p1 = Math.max(dot, mark);
						CodeTemplateManager manager = RSyntaxTextArea.
											getCodeTemplateManager();
						CodeTemplate template = 
							manager==null ? null :
										manager.getTemplate(rsta);

						// A non-null template means modify the text to insert!
						if (template!=null) {
							Element map = doc.getDefaultRootElement();
							int lineNum = map.getElementIndex(dot);
							Element line = map.getElement(lineNum);
							int start = line.getStartOffset();
							int end = line.getEndOffset()-1; // Why always "-1"?
							String s = textArea.getText(start,end-start);
							int len = s.length();
							// endWS is the end of the leading whitespace of the
							// current line.
							int endWS = 0;
							while (endWS<len && RSyntaxUtilities.
										isWhitespace(s.charAt(endWS)))
								endWS++;
							s = s.substring(0, endWS);
							p0 -= template.getID().length;
							s = template.getContentToInsert(s);
							((RSyntaxDocument)doc).replace(p0,p1-p0,
										s, null);
							textArea.setCaretPosition(p0 +
									template.getBeforeCaretText().length());
						}

						// No template - insert default text.  This is exactly what
						// DefaultKeyTypedAction does.
						else {
							doDefaultInsert(rsta);
						}

					} catch (BadLocationException ble) {
						UIManager.getLookAndFeel().
								provideErrorFeedback(textArea);
					}


				} // End of if (doc!=null).

			} // End of if (textArea.getTemplatesEnabled()).

			// If templates aren't enabled, just insert the text as usual.
			else {
				doDefaultInsert(rsta);
			}

		}

		private final void doDefaultInsert(RTextArea textArea) {
			// FIXME:  We need a way to get the "trigger string" (i.e.,
			// the text that was just typed); however, the text area's
			// template manager might be null (if templates are disabled).
			// Also, the manager's trigger string doesn't yet match up with
			// that defined in RSyntaxTextAreaEditorKit.java (which is
			// hardcoded as a space)...
			//String str = manager.getInsertTriggerString();
			//int mod = manager.getInsertTrigger().getModifiers();
			//if (str!=null && str.length()>0 &&
			//	((mod&ActionEvent.ALT_MASK)==(mod&ActionEvent.CTRL_MASK))) {
			//	char ch = str.charAt(0);
			//	if (ch>=0x20 && ch!=0x7F)
			//		textArea.replaceSelection(str);
			//}
			textArea.replaceSelection(" ");
		}

		public final String getMacroID() {
			return rstaPossiblyInsertTemplateAction;
		}

	}


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

}

⌨️ 快捷键说明

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