Code example for JTextComponent

Methods: getCaretgetSelectionEndgetSelectionStartmoveCaretPositionsetCaretPosition

0
 
		int p0 = fElement.getStartOffset();
		int p1 = fElement.getEndOffset();
		if (fContainer instanceof JTextComponent) {
			JTextComponent textComp = (JTextComponent) fContainer;
			int start = textComp.getSelectionStart();
			int end = textComp.getSelectionEnd();
			if ((start <= p0) && (end >= p1)) {
				if ((start == p0) && (end == p1) && isEditable()) {
					return 2; 
				} 
				else { 
					return 1; 
				} 
			} 
		} 
		return 0; 
	} 
 
	/** 
	 * @return returns true if container is editable, false otherwise. 
	 */ 
	protected boolean isEditable() { 
 
		return ((fContainer instanceof JEditorPane) && ((JEditorPane) fContainer).isEditable());
	} 
 
	/** 
	 * @return returns the text editor's highlight color. 
	 */ 
	protected Color getHighlightColor() {
 
		JTextComponent textComp = (JTextComponent) fContainer;
		return textComp.getSelectionColor();
	} 
 
	// Progressive display ------------------------------------------------- 
 
	// This can come on any thread. If we are in the process of reloading 
	// the image and determining our state (loading == true) we don't fire 
	// preference changed, or repaint, we just reset the fWidth/fHeight as 
	// necessary and return. This is ok as we know when loading finishes 
	// it will pick up the new height/width, if necessary. 
 
	private static boolean	sIsInc		= true;
	private static int		sIncRate	= 100;
 
	/** 
	 * updates the image. 
	 *  
	 * @param img 
	 *            the image to update to 
	 * @param flags 
	 *            flags to set 
	 * @param x 
	 *            y-coordinate 
	 * @param y 
	 *            y-coordinate 
	 * @param width 
	 *            image width 
	 * @param height 
	 *            image height 
	 * @return returns true if update was successful, false otherwise. 
	 */ 
	public boolean imageUpdate(Image img, int flags, int x, int y, int width, int height) {
 
		if ((fImage == null) || (fImage != img)) {
			return false; 
		} 
 
		// Bail out if there was an error 
		if ((flags & (ABORT | ERROR)) != 0) {
			fImage = null;
			repaint(0); 
			return false; 
		} 
 
		// Resize image if necessary 
		short changed = 0;
		if ((flags & ImageObserver.HEIGHT) != 0) {
			if (!getElement().getAttributes().isDefined(HTML.Attribute.HEIGHT)) {
				changed |= 1;
			} 
		} 
 
		if ((flags & ImageObserver.WIDTH) != 0) {
			if (!getElement().getAttributes().isDefined(HTML.Attribute.WIDTH)) {
				changed |= 2;
			} 
		} 
 
		synchronized (this) {
			if ((changed & 1) == 1) {
				fWidth = width;
			} 
 
			if ((changed & 2) == 2) {
				fHeight = height;
			} 
 
			if (bLoading) {
				// No need to resize or repaint, still in the process of loading 
				return true; 
			} 
		} 
 
		if (changed != 0) {
			// May need to resize myself, asynchronously 
			Document doc = getDocument();
			try { 
				if (doc instanceof AbstractDocument) {
					((AbstractDocument) doc).readLock();
				} 
				preferenceChanged(this, true, true);
			} 
			finally { 
				if (doc instanceof AbstractDocument) {
					((AbstractDocument) doc).readUnlock();
				} 
			} 
			return true; 
		} 
 
		// Repaint when done or when new pixels arrive 
		if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
			repaint(0); 
		} 
		else if ((flags & SOMEBITS) != 0) {
			if (sIsInc) {
				repaint(sIncRate);
			} 
		} 
		return ((flags & ALLBITS) == 0);
	} 
 
	// Layout -------------------------------------------------------------- 
 
	/** 
	 * Determines the preferred span for this view along an axis. 
	 *  
	 * @param axis 
	 *            may be either X_AXIS or Y_AXIS 
	 * @return the span the view would like to be rendered into. Typically the 
	 *         view is told to render into the span that is returned, although 
	 *         there is no guarantee. The parent may choose to resize or break 
	 *         the view. 
	 */ 
	public float getPreferredSpan(int axis) {
 
		int extra = 2 * (getBorder() + getSpace(axis));
		switch (axis) {
		case View.X_AXIS:
			return fWidth + extra;
		case View.Y_AXIS:
			return fHeight + extra;
		default: 
			throw new IllegalArgumentException("Invalid axis in getPreferredSpan() : " + axis);
		} 
	} 
 
	/** 
	 * Determines the desired alignment for this view along an axis. This is 
	 * implemented to give the alignment to the bottom of the icon along the y 
	 * axis, and the default along the x axis. 
	 *  
	 * @param axis 
	 *            may be either X_AXIS or Y_AXIS 
	 * @return the desired alignment. This should be a value between 0.0 and 1.0 
	 *         where 0 indicates alignment at the origin and 1.0 indicates 
	 *         alignment to the full span away from the origin. An alignment of 
	 *         0.5 would be the center of the view. 
	 */ 
	public float getAlignment(int axis) {
 
		switch (axis) {
		case View.Y_AXIS:
			return getVerticalAlignment(); 
		default: 
			return super.getAlignment(axis);
		} 
	} 
 
	/** 
	 * Provides a mapping from the document model coordinate space to the 
	 * coordinate space of the view mapped to it. 
	 *  
	 * @param pos 
	 *            the position to convert 
	 * @param a 
	 *            the allocated region to render into 
	 * @param b 
	 *            Position Bias 
	 * @return the bounding box of the given position 
	 * @exception BadLocationException 
	 *                if the given position does not represent a valid location 
	 *                in the associated document 
	 * @see View#modelToView 
	 */ 
	public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
 
		int p0 = getStartOffset();
		int p1 = getEndOffset();
		if ((pos >= p0) && (pos <= p1)) {
			Rectangle r = a.getBounds();
			if (pos == p1) {
				r.x += r.width;
			} 
			r.width = 0;
			return r;
		} 
		return null; 
	} 
 
	/** 
	 * Provides a mapping from the view coordinate space to the logical 
	 * coordinate space of the model. 
	 *  
	 * @param x 
	 *            the X coordinate 
	 * @param y 
	 *            the Y coordinate 
	 * @param a 
	 *            the allocated region to render into 
	 * @param bias 
	 *            Position Bias[] 
	 * @return the location within the model that best represents the given 
	 *         point of view 
	 * @see View#viewToModel 
	 */ 
	public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
 
		Rectangle alloc = (Rectangle) a;
		if (x < (alloc.x + alloc.width)) {
			bias[0] = Position.Bias.Forward;
			return getStartOffset();
		} 
		bias[0] = Position.Bias.Backward;
		return getEndOffset();
	} 
 
	/** 
	 * Change the size of this image. This alters the HEIGHT and WIDTH 
	 * attributes of the Element and causes a re-layout. 
	 */ 
	protected void resize(int width, int height) {
 
		if ((width == fWidth) && (height == fHeight)) {
			return; 
		} 
		fWidth = width;
		fHeight = height;
		// Replace attributes in document 
		MutableAttributeSet myAttr = new SimpleAttributeSet();
		myAttr.addAttribute(HTML.Attribute.WIDTH, Integer.toString(width));
		myAttr.addAttribute(HTML.Attribute.HEIGHT, Integer.toString(height));
		((StyledDocument) getDocument())
				.setCharacterAttributes(fElement.getStartOffset(), fElement.getEndOffset(), myAttr, false);
	} 
 
	// Mouse event handling ------------------------------------------------ 
 
	/** 
	 * Select or grow image when clicked. 
	 *  
	 * @param e 
	 *            MouseEvent to handle 
	 */ 
	public void mousePressed(MouseEvent e) {
 
		Dimension size = fComponent.getSize();
		if ((e.getX() >= (size.width - 7)) && (e.getY() >= (size.height - 7)) && (getSelectionState() == 2)) {
			Point loc = fComponent.getLocationOnScreen();
			fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight);
			fGrowProportionally = e.isShiftDown();
		} 
		else { 
			fGrowBase = null;
			JTextComponent comp = (JTextComponent) fContainer;
			int start = fElement.getStartOffset();
			int end = fElement.getEndOffset();
			int mark = comp.getCaret().getMark();
			int dot = comp.getCaret().getDot();
			if (e.isShiftDown()) {
 
				if (mark <= start) {
					comp.moveCaretPosition(end);
				} 
				else { 
					comp.moveCaretPosition(start);
				} 
			} 
			else { 
				if (mark != start) {
					comp.setCaretPosition(start);
				} 
				if (dot != end) {
					comp.moveCaretPosition(end);
				} 
			} 
		} 
	} 
 
Connect your IDE to all the code out there  Get Codota for Java