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

📄 rtextareaeditorkit.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
📖 第 1 页 / 共 4 页
字号:
		}

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

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			textArea.copy();
			textArea.requestFocusInWindow();
		}

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

	}


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


	/**
	 * Action for cutting text.
	 */
	public static class CutAction extends RecordableTextAction {

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

		public CutAction() {
			super(DefaultEditorKit.cutAction);
		}

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

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			textArea.cut();
			textArea.requestFocusInWindow();
		}

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

	}


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


	/**
	 * Action for decreasing the font size.
	 */
	public static class DecreaseFontSizeAction extends RecordableTextAction {

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

		protected float decreaseAmount;

		protected static final float MINIMUM_SIZE	= 2.0f;

		public DecreaseFontSizeAction() {
			super(rtaDecreaseFontSizeAction);
			initialize();
		}

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

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			Font font = textArea.getFont();
			float oldSize = font.getSize2D();
			float newSize = oldSize - decreaseAmount;
			if (newSize>=MINIMUM_SIZE) {
				// Shrink by decreaseAmount.
				font = font.deriveFont(newSize);
				textArea.setFont(font);
			}
			else if (oldSize>MINIMUM_SIZE) {
				// Can't shrink by full decreaseAmount, but can shrink a
				// little bit.
				font = font.deriveFont(MINIMUM_SIZE);
				textArea.setFont(font);
			}
			else {
				// Our font size must be at or below MINIMUM_SIZE.
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);
			}
			textArea.requestFocusInWindow();
		}

		public final String getMacroID() {
			return rtaDecreaseFontSizeAction;
		}

		protected void initialize() {
			decreaseAmount = 1.0f;
		}

	}


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


	/**
	 * The action to use when no actions in the input/action map meet the key
	 * pressed.  This is actually called from the keymap I believe.
	 */
	public static class DefaultKeyTypedAction extends RecordableTextAction {

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

		public DefaultKeyTypedAction() {
			super(DefaultEditorKit.defaultKeyTypedAction, null, null, null,
					null);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			if (!textArea.isEditable() || !textArea.isEnabled())
				return;
			String content = e.getActionCommand();
			int mod = e.getModifiers();
			if ((content != null) && (content.length() > 0) &&
				((mod & ActionEvent.ALT_MASK) == (mod & ActionEvent.CTRL_MASK))) {
				char c = content.charAt(0);
				if ((c >= 0x20) && (c != 0x7F))
					textArea.replaceSelection(content);
			}
		}

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

	}


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


	/**
	 * Deletes the character of content that follows the current caret
	 * position.
	 */
	public static class DeleteNextCharAction extends RecordableTextAction {

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

		public DeleteNextCharAction() {
			super(DefaultEditorKit.deleteNextCharAction, null, null,
												null, null);
		}

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

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			boolean beep = true;
			if ((textArea != null) && (textArea.isEditable())) {
				try {
					Document doc = textArea.getDocument();
					Caret caret = textArea.getCaret();
					int dot = caret.getDot();
					int mark = caret.getMark();
					if (dot != mark) {
						doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
						beep = false;
					}
					else if (dot < doc.getLength()) {
						int delChars = 1;
						if (dot < doc.getLength() - 1) {
							String dotChars = doc.getText(dot, 2);
							char c0 = dotChars.charAt(0);
							char c1 = dotChars.charAt(1);
							if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
								c1 >= '\uDC00' && c1 <= '\uDFFF') {
								delChars = 2;
							}
						}
						doc.remove(dot, delChars);
						beep = false;
					}
				} catch (BadLocationException bl) {
				}
			}

			if (beep)
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);

			textArea.requestFocusInWindow();

		}

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

	}


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


	/**
	 * Deletes the character of content that precedes the current caret
	 * position.
	 */
	public static class DeletePrevCharAction extends RecordableTextAction {

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

		public DeletePrevCharAction() {
			super(deletePrevCharAction);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			boolean beep = true;
			if ((textArea != null) && (textArea.isEditable())) {
				try {
					Document doc = textArea.getDocument();
					Caret caret = textArea.getCaret();
					int dot = caret.getDot();
					int mark = caret.getMark();
					if (dot != mark) {
						doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
						beep = false;
					}
					else if (dot > 0) {
						int delChars = 1;
						if (dot > 1) {
							String dotChars = doc.getText(dot - 2, 2);
							char c0 = dotChars.charAt(0);
							char c1 = dotChars.charAt(1);
							if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
								c1 >= '\uDC00' && c1 <= '\uDFFF') {
								delChars = 2;
							}
						}
						doc.remove(dot - delChars, delChars);
						beep = false;
					}
				} catch (BadLocationException bl) {
				}
			}

			if (beep)
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);

		}

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

	}


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


	/**
	 * Action that deletes all text from the caret position to the end of the
	 * caret's line.
	 */
	public static class DeleteRestOfLineAction extends RecordableTextAction {

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

		public DeleteRestOfLineAction() {
			super(rtaDeleteRestOfLineAction);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			try {

				// We use the elements instead of calling getLineOfOffset(),
				// etc. to speed things up just a tad (i.e. micro-optimize).
				Document document = textArea.getDocument();
				int caretPosition = textArea.getCaretPosition();
				Element map = document.getDefaultRootElement();
				int currentLineNum = map.getElementIndex(caretPosition);
				Element currentLineElement = map.getElement(currentLineNum);
				// Always take -1 as we don't want to remove the newline.
				int currentLineEnd = currentLineElement.getEndOffset()-1;
				if (caretPosition<currentLineEnd)
					document.remove(caretPosition,
									currentLineEnd-caretPosition);

			} catch (BadLocationException ble) {
				ble.printStackTrace();
			}

		}

		public final String getMacroID() {
			return rtaDeleteRestOfLineAction;
		}

	}


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


	/**
	 * Moves the caret to the end of the document.
	 */
	public static class EndAction extends RecordableTextAction {

		/**
	 * 
	 */
	private static final long serialVersionUID = 8985841694929860823L;
		private boolean select;

		public EndAction(String name, boolean select) {
			super(name);
			this.select = select;
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			Document doc = textArea.getDocument();
			int dot = doc.getLength();
			if (select)
				textArea.moveCaretPosition(dot);
			else
				textArea.setCaretPosition(dot);
		}

		public final String getMacroID() {
			return getName();
		}

	}


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


	/**
	 * Positions the caret at the end of the line.
	 */
	public static class EndLineAction extends RecordableTextAction {

		/**
	 * 
	 */
	private static final long serialVersionUID = -6780582445481818758L;
		private boolean select;

		public EndLineAction(String name, boolean select) {
			super(name);
			this.select = select;
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			try {
				int offs = textArea.getCaretPosition();
                    // FIXME:  Replace Utilities call with custom version to
                    // cut down on all of the modelToViews, as each call causes
                    // a getTokenList => expensive!
				int endOffs = Utilities.getRowEnd(textArea, offs);
				if (select)
					textArea.moveCaretPosition(endOffs);
				else
					textArea.setCaretPosition(endOffs);
			} catch (Exception exc) {
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);
			}
		}

		public final String getMacroID() {
			return getName();
		}

	}


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


	/**
	 * Action that ends recording a macro.
	 */
	public static class EndRecordingMacroAction extends RecordableTextAction {

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

		public EndRecordingMacroAction() {
			super(rtaEndRecordingMacroAction);
		}

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

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			RTextArea.endRecordingMacro();
		}

		public final String getMacroID() {
			return rtaEndRecordingMacroAction;
		}

		public boolean isRecordable() {
			return false; // Never record the recording of a macro!
		}

	}


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


	/**
	 * Positions the caret at the end of the word.
	 */
	static class EndWordAction extends RecordableTextAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 8636594372227516193L;
		private boolean select;

		EndWordAction(String name, boolean select) {
			super(name);
			this.select = select;
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
			try {
				int offs = textArea.getCaretPosition();
				int endOffs = Utilities.getWordEnd(textArea, offs);
				if (select)
					textArea.moveCaretPosition(endOffs);
				else
					textArea.setCaretPosition(endOffs);
			} catch (BadLocationException bl) {
				UIManager.getLookAndFeel().provideErrorFeedback(textArea);
			}
		}

		public final String getMacroID() {
			return getName();
		}

	}


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


	/**
	 * The action for when the user hits the "Home" key.  This toggles the
	 * caret's position between the first non-whitespace character on the
	 * current line, and the beginning of the current line.
	 */
	public static class HomeAction extends RecordableTextAction {

		/**
	 * 
	 */
	private static final long serialVersionUID = 7202900978326860758L;
		private Segment currentLine = new Segment(); // For speed.

		public HomeAction() {
			super(rtaHomeAction);
		}

		public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

			try {
				// We use the elements instead of calling getLineOfOffset(),
				// etc. to speed things up just a tad (i.e. micro-optimize).
				int caretPosition = textArea.getCaretPosition();
				Document document = textArea.getDocument();
				Element map = document.getDefaultRootElement();
				int currentLineNum = map.getElementIndex(caretPosition);
				Element currentLineElement = map.getElement(currentLineNum);
				int currentLineStart = currentLineElement.getStartOffset();
				int lineCount = map.getElementCount();
				int currentLineEnd = (currentLineNum==lineCount-1) ?
							currentLineElement.getEndOffset()-1 :
							currentLineElement.getEndOffset();
				int count = currentLineEnd - currentLineStart;
				if (count>0) { // If there are chars in the line...
					document.getText(currentLineStart, count, currentLine);
					int firstNonWhitespace = getFirstNonWhitespacePos();
					firstNonWhitespace = currentLineStart +
							(firstNonWhitespace - currentLine.offset);
					if (caretPosition!=firstNonWhitespace)
						textArea.setCaretPosition(firstNonWhitespace);
					else
						textArea.setCaretPosition(currentLineStart);
				}
				//e.consume();
			} catch (BadLocationException ble) {
				/* Shouldn't ever happen. */
				ble.printStackTrace();
			}

		}

		private final int getFirstNonWhitespacePos() {
			int offset = currentLine.offset;
			int end = offset + currentLine.count - 1;
			int pos = offset;
			char[] array = currentLine.array;
			char currentChar = array[pos];
			while ((currentChar=='\t' || currentChar==' ') && (++pos<end))
				currentChar = array[pos];
			return pos;
		}

		public final String getMacroID() {
			return rtaHomeAction;
		}

	}


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


	/**
	 * Action for increasing the font size.
	 */
	public static class IncreaseFontSizeAction extends RecordableTextAction {

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

		protected float increaseAmount;

		protected static final float MAXIMUM_SIZE	= 40.0f;

		public IncreaseFontSizeAction() {
			super(rtaIncreaseFontSizeAction);
			initialize();
		}

⌨️ 快捷键说明

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