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

📄 rtextareaui.java~1~

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

		// Paint the caret.
		Caret caret = textArea.getCaret();
		if (caret != null)
			caret.paint(g);

	}


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


	/**
	 * This method gets called when a bound property is changed
	 * on the associated RTextArea.  This is a hook
	 * which UI implementations may change to reflect how the
	 * UI displays bound properties of JTextComponent subclasses.<p>
	 *
	 * These events are forwarded to us from
	 * {@link org.fife.ui.rtextarea.RTAUpdateHandler}.
	 *
	 * @param e The property change event.
	 */
	protected void propertyChange(PropertyChangeEvent e) {

		String propertyName = e.getPropertyName();

		if (propertyName.equals("lineWrap") ||
			propertyName.equals("wrapStyleWord") ||
			propertyName.equals("tabSize")) {
				// rebuild the view
				modelChanged();
		}
		else if ("editable".equals(propertyName)) {
			updateFocusTraversalKeys();
		}

	}


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


	/**
	 * Sets the current root of the view hierarchy and calls invalidate().
	 * If there were any child components, they will be removed (i.e.
	 * there are assumed to have come from components embedded in views).
	 *
	 * @param v the root view
	 */
	protected final void setView(View v) {
		rootView.setView(v);
		painted = false;
		textArea.revalidate();
		textArea.repaint();
	}


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


	/**
	 * Sets the component properties that haven't been explicitly overridden to 
	 * null.  A property is considered overridden if its current value
	 * is not a UIResource.
	 * 
	 * @see #installDefaults
	 * @see #uninstallUI
	 */
	protected void uninstallDefaults()  {

		if (textArea.getCaretColor() instanceof UIResource) {
			textArea.setCaretColor(null);
		}
                                                                                         
		if (textArea.getSelectionColor() instanceof UIResource) {
			textArea.setSelectionColor(null);
		}

		if (textArea.getDisabledTextColor() instanceof UIResource) {
			textArea.setDisabledTextColor(null);
		}

		if (textArea.getSelectedTextColor() instanceof UIResource) {
			textArea.setSelectedTextColor(null);
		}

		if (textArea.getBorder() instanceof UIResource) {
			textArea.setBorder(null);
		}

		if (textArea.getMargin() instanceof UIResource) {
			textArea.setMargin(null);
		}

		if (textArea.getCaret() instanceof UIResource) {
			textArea.setCaret(null);
		}

	}


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


	/**
	 * Uninstalls the registered keyboard actions.
	 */
	protected void uninstallKeyboardActions() {
		textArea.setKeymap(null);
		SwingUtilities.replaceUIInputMap(textArea, JComponent.
										WHEN_IN_FOCUSED_WINDOW, null);
		SwingUtilities.replaceUIActionMap(textArea, null);
	}


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


	/**
	 * Uninstalls listeners for the UI.
	 */
	protected void uninstallListeners() {
	}


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


	/**
	 * Uninstalls the private defaults of the UI.
	 */
	protected void uninstallPrivateDefaults() {

		textArea.removeMouseListener(defaultDragRecognizer);
		textArea.removeMouseMotionListener(defaultDragRecognizer);

		if (textArea.getHighlighter() instanceof UIResource) {
			textArea.setHighlighter(null);
		}

		if (textArea.getTransferHandler() instanceof UIResource) {
			textArea.setTransferHandler(null);
		}

		DropTarget dropTarget = textArea.getDropTarget();
		if (dropTarget instanceof UIResource) {
			dropTarget.removeDropTargetListener(dropTargetListener);
		}

	}


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


	/**
	 * Uninstalls the UI for a component.  This removes the listeners,
	 * uninstalls the highlighter, removes views, and nulls out the keymap.
	 *
	 * @param c the editor component
	 * @see ComponentUI#uninstallUI
	 */
	public void uninstallUI(JComponent c) {

		// detach from the model
		textArea.removePropertyChangeListener(updateHandler);
		textArea.getDocument().removeDocumentListener(updateHandler);

		// view part
		painted = false;
		uninstallDefaults();
		uninstallPrivateDefaults();
		rootView.setView(null);
		c.removeAll();
		LayoutManager lm = c.getLayout();
		if (lm instanceof UIResource) {
			c.setLayout(null);
		}

		// controller part
		uninstallKeyboardActions();
		uninstallListeners();

    }


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


	/**
	 * The superclass </code>ComponentUI</code>'s <code>update</code> method
	 * always checks the <code>isOpaque</code> property and fills the
	 * background with its appropriate color accordingly.  We don't want this
	 * behavior, as the background may be an image, so we simply call
	 * <code>paint</code>.
	 * @param g The graphics context with which to paint.
	 * @param c The component to paint.
	 */
	public void update(Graphics g, JComponent c) {
		paint(g, c);
	}


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


	/**
	 * Invoked when the focus accelerator changes, this will update the
	 * key bindings as necessary.
	 */
	void updateFocusAcceleratorBinding(boolean changed) {

		char accelerator = textArea.getFocusAccelerator();

		if (changed || accelerator != '\0') {
			InputMap km = SwingUtilities.getUIInputMap
						(textArea, JComponent.WHEN_IN_FOCUSED_WINDOW);

			if (km == null && accelerator != '\0') {
				km = new ComponentInputMapUIResource(textArea);
				SwingUtilities.replaceUIInputMap(textArea, JComponent.
										WHEN_IN_FOCUSED_WINDOW, km);
				ActionMap am = getActionMap();
				SwingUtilities.replaceUIActionMap(textArea, am);
			}
			if (km != null) {
				km.clear();
				if (accelerator != '\0') {
					km.put(KeyStroke.getKeyStroke(accelerator,
											ActionEvent.ALT_MASK),
											"requestFocus");
				}
			}
		}

	}


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


	/**
	 * Invoked when editable property is changed.
	 *
	 * removing 'TAB' and 'SHIFT-TAB' from traversalKeysSet in case 
	 * editor is editable
	 * adding 'TAB' and 'SHIFT-TAB' to traversalKeysSet in case 
	 * editor is non editable
	 */ 
	void updateFocusTraversalKeys() {
		/*
		 * Fix for 4514331 Non-editable JTextArea and similar 
		 * should allow Tab to keyboard - accessibility 
		 */
		EditorKit editorKit = getEditorKit(textArea);
		if (editorKit!=null && editorKit instanceof DefaultEditorKit) {
			Set storedForwardTraversalKeys = textArea.getFocusTraversalKeys(
						KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
			Set storedBackwardTraversalKeys = textArea.getFocusTraversalKeys(
						KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
			Set forwardTraversalKeys = new HashSet(storedForwardTraversalKeys);
			Set backwardTraversalKeys = new HashSet(storedBackwardTraversalKeys);
			if (textArea.isEditable()) {
				forwardTraversalKeys.remove(
						KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				backwardTraversalKeys.remove(
						KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
			}
			else {
				forwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				backwardTraversalKeys.add(KeyStroke.getKeyStroke(
							KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
			}
			textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 
									forwardTraversalKeys);
			textArea.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 
									backwardTraversalKeys);
		}

	}


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


	/**
	 * Converts the given place in the view coordinate system
	 * to the nearest representative location in the model.
	 * The component must have a non-zero positive size for 
	 * this translation to be computed.
	 *
	 * @param tc the text component for which this UI is installed
	 * @param pt the location in the view to translate.  This
	 *  should be in the same coordinate system as the mouse events.
	 * @return the offset from the start of the document >= 0,
	 *   -1 if not painted
	 * @see TextUI#viewToModel
	 */
	public int viewToModel(JTextComponent tc, Point pt) {
		return viewToModel(tc, pt, discardBias);
	}


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


	/**
	 * Converts the given place in the view coordinate system
	 * to the nearest representative location in the model.
	 * The component must have a non-zero positive size for 
	 * this translation to be computed.
	 *
	 * @param tc the text component for which this UI is installed
	 * @param pt the location in the view to translate.  This
	 *  should be in the same coordinate system as the mouse events.
	 * @return the offset from the start of the document >= 0,
	 *   -1 if the component doesn't yet have a positive size.
	 * @see TextUI#viewToModel
	 */
	public int viewToModel(JTextComponent tc, Point pt,
								Position.Bias[] biasReturn) {

		int offs = -1;
		RTextAreaDocument doc = (RTextAreaDocument)textArea.getDocument();
		doc.readLock();
		try {
			Rectangle alloc = getVisibleEditorRect();
			if (alloc != null) {
				rootView.setSize(alloc.width, alloc.height);
				offs = rootView.viewToModel(pt.x, pt.y, alloc, biasReturn);
			}
		} finally {
			doc.readUnlock();
		}

		return offs;

	}


/*****************************************************************************/
/********************* PRIVATE INNER CLASSES *********************************/
/*****************************************************************************/


	/**
	 * Registered in the ActionMap.
	 */
	class FocusAction extends AbstractAction {

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

		public void actionPerformed(ActionEvent e) {
			textArea.requestFocus();
		}

		public boolean isEnabled() {
			return textArea.isEditable();
		}

	}


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


	/**
	 * Listens to mouse events, and decides when the user is dragging some
	 * text.
	 */
	static class TextDragGestureRecognizer implements MouseListener,
											MouseMotionListener {

		private MouseEvent dndArmedEvent = null;
    
		protected JComponent getComponent(MouseEvent e) {
			Object src = e.getSource();
			if (src instanceof JComponent)
				return (JComponent)src;
			return null;
		}

		private static int getMotionThreshold() {
			return 3;//DragSource.getDragThreshold();
		}
    
		protected int mapDragOperationFromModifiers(MouseEvent e) {
			int mods = e.getModifiersEx();
			if ((mods & InputEvent.BUTTON1_DOWN_MASK) !=
					InputEvent.BUTTON1_DOWN_MASK)
				return TransferHandler.NONE;
			JComponent c = getComponent(e);
			TransferHandler th = c.getTransferHandler();
			int sourceActions = th.getSourceActions(c);
			switch (sourceActions) {
				case TransferHandler.NONE:
				case TransferHandler.COPY:
				case TransferHandler.MOVE:
					return sourceActions;
				case TransferHandler.COPY_OR_MOVE:
					return ((mods&InputEvent.CTRL_MASK)==InputEvent.CTRL_MASK) ?
								TransferHandler.COPY : TransferHandler.MOVE;
			}
			return TransferHandler.NONE; // Should never happen.
		}
    
		public void mouseClicked(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {
			dndArmedEvent = null;
			if (isDragPossible(e) && mapDragOperationFromModifiers(e) != TransferHandler.NONE) {
				dndArmedEvent = e;
				e.consume();
			}
		}

		public void mouseReleased(MouseEvent e) {
			dndArmedEvent = null;
		}
    
		public void mouseEntered(MouseEvent e) {
			//dndArmedEvent = null;
		}
    
		public void mouseExited(MouseEvent e) {
			//if (dndArmedEvent != null && mapDragOperationFromModifiers(e) == TransferHandler.NONE) {
			//    dndArmedEvent = null;
			//}
		}

		public void mouseDragged(MouseEvent e) {
			if (dndArmedEvent != null) {
				e.consume();
				int action = mapDragOperationFromModifiers(e);
				if (action == TransferHandler.NONE)
					return;
				int dx = Math.abs(e.getX() - dndArmedEvent.getX());
				int dy = Math.abs(e.getY() - dndArmedEvent.getY());
				if ((dx > getMotionThreshold()) || (dy > getMotionThreshold())) {
					// start transfer... shouldn't be a click at this point
					JComponent c = getComponent(e);
					TransferHandler th = c.getTransferHandler();
					th.exportAsDrag(c, dndArmedEvent, action);
					dndArmedEvent = null;
				}
			}
		}
    
		public void mouseMoved(MouseEvent e) {
		}
    
		private TransferHandler getTransferHandler(MouseEvent e) {
			JComponent c = getComponent(e);
			return c == null ? null : c.getTransferHandler();
		}
    
		/**
		 * Determines if the following are true:
		 * <ul>
		 * <li>the press event is located over a selection
		 * <li>the dragEnabled property is true
		 * <li>A TranferHandler is installed
		 * </ul>
		 * <p>
		 */
		protected boolean isDragPossible(MouseEvent e) {
			JTextComponent c = (JTextComponent)this.getComponent(e);
			if (c.getTransferHandler()!=null) {
				if (c.getDragEnabled()) {
					Caret caret = c.getCaret();
					int dot = caret.getDot();
					int mark = caret.getMark();
					if (dot != mark) {
						Point p = new Point(e.getX(), e.getY());
						int pos = c.viewToModel(p);
						int p0 = Math.min(dot, mark);
						int p1 = Math.max(dot, mark);
						if ((pos >= p0) && (pos < p1))
							return true;
					}
				} // End of if (c.getDragEnabled()).
			} // End of if (c.getTransferHandler()!=null).
			return false;
		}

    }


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

}

⌨️ 快捷键说明

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