FileDocCategorySizeDatePackage
ActiveClient.javaAPI DocExample13866Wed Apr 19 11:19:38 BST 2000None

ActiveClient

public class ActiveClient extends LWTextComponent implements InputMethodRequests, InputMethodListener
Implements an integrated text input user interface. This class is an active client of the input method framework, that is, actively uses its APIs to accomplish integration.

This class directly implements the two client interfaces of the input method framework, InputMethodListener and InputMethodRequests. This is not required. Especially in cases where the public interface matters (such as in a class library), it may be more appropriate to hide the implementations of these two interfaces in separate classes.

Fields Summary
private AttributedString
composedTextString
private AttributedCharacterIterator
composedText
private TextHitInfo
caret
private static final java.text.AttributedCharacterIterator.Attribute[]
IM_ATTRIBUTES
private static final AttributedCharacterIterator
EMPTY_TEXT
Constructors Summary
public ActiveClient(String name)
Constructs an ActiveClient. Input methods are always enabled for ActiveClient instances.

param
name the component name to be displayed above the text


                               
       
        super(name, true);
        enableInputMethods(true);
	addInputMethodListener(this);
    
Methods Summary
public java.text.AttributedCharacterIteratorcancelLatestCommittedText(java.text.AttributedCharacterIterator.Attribute[] attributes)
Returns null to indicate that the "Undo Commit" feature is not supported by this simple text component.

        return null;
    
public voidcaretPositionChanged(java.awt.event.InputMethodEvent event)
Handles changes to the caret within composed text. The caret defined in the event is saved and will be returned by getCaret if there is composed text. The component is redrawn.

	caret = event.getCaret();
	event.consume();
	repaint();
    
public java.awt.font.TextHitInfogetCaret()
Returns a text hit info indicating the current caret (insertion point). This override returns the caret provided by the input method while there is composed text; otherwise it returns a caret at the end of the committed text. The caret provided by the input method may be null.

        if (composedText == null) {
            return super.getCaret();
        } else if (caret == null) {
            return null;
        } else {
            // the caret provided by the input method is relative
            // to the composed text, so translate it to the entire text
            return caret.getOffsetHit(getCommittedTextLength());
        }
    
public java.text.AttributedCharacterIteratorgetCommittedText(int beginIndex, int endIndex, java.text.AttributedCharacterIterator.Attribute[] attributes)
Gets an iterator providing access to the entire text and attributes contained in the text editing component except for uncommitted text.

        return getCommittedText(beginIndex, endIndex);
    
public java.text.AttributedCharacterIteratorgetDisplayText()
Returns the text that the user has entered. This override returns the concatenation of committed text and composed text.

        if (composedText == null) {
            return super.getDisplayText();
        } else {
            // We don't want to copy all the text and attribute data here.
            // Instead, we return a CompositeIterator which iterates over
            // the concatenation of two iterators.
            return new CompositeIterator(super.getDisplayText(), composedText);
        }
    
public java.awt.im.InputMethodRequestsgetInputMethodRequests()
Implements getInputMethodRequests for ActiveClient by returning "this".

return
"this"

        return this;
    
public intgetInsertPositionOffset()
Gets the offset of the insert position in the committed text contained in the text editing component. In this simple component, that's always at the end of the committed text.

        return getCommittedTextLength();
    
public java.awt.font.TextHitInfogetLocationOffset(int x, int y)
Gets the offset within the composed text for the specified absolute x and y coordinates on the screen.


        // translate from screen coordinates to coordinates in the text layout
        Point location = getLocationOnScreen();
        Point textOrigin = getTextOrigin();
        x -= location.x + textOrigin.x;
        y -= location.y + textOrigin.y;
        
        // TextLayout maps locations far outside its bounds to locations within.
        // To avoid false hits, we use it only if it actually contains the location.
        // We also have to translate the TextHitInfo to be relative to composed text.
        TextLayout textLayout = getTextLayout();
        if (textLayout != null &&
                textLayout.getBounds().contains(x, y)) {
            return textLayout.hitTestChar(x, y).getOffsetHit(-getCommittedTextLength());
        } else {
            return null;
        }
    
public java.text.AttributedCharacterIteratorgetSelectedText(java.text.AttributedCharacterIterator.Attribute[] attributes)
Gets the currently selected text from the text editing component. Since this simple text component doesn't support selections, this is always an iterator over empty text.


                                   
        
        return EMPTY_TEXT;
    
public java.awt.RectanglegetTextLocation(java.awt.font.TextHitInfo offset)
Gets the location of a specified offset in the current composed text, or of the selection in committed text.


        // determine the text location in component coordinates
        Rectangle rectangle;
        if (offset == null) {
            // no composed text: return caret for committed text
            rectangle = getCaretRectangle();
        } else {
            // composed text: return caret within composed text
            TextHitInfo globalOffset = offset.getOffsetHit(getCommittedTextLength());
            rectangle = getCaretRectangle(globalOffset);
        }

        // translate to screen coordinates
        Point location = getLocationOnScreen();
        rectangle.translate(location.x, location.y);

	return rectangle;
    
public voidinputMethodTextChanged(java.awt.event.InputMethodEvent event)
Handles changes to the text entered through an input method. Committed text contained in the event is appended to the committed text of the text component. Composed text contained in the event replaces any existing composed text in the text component. The caret defined in the event is saved and will be returned by getCaret if there is composed text. The component is redrawn.

In this simple component, we only keep input method highlight attributes. Smarter components may want to keep language, reading, input method segment, and other attributes as well.


                                                                                                     
        
        int committedCharacterCount = event.getCommittedCharacterCount();
        AttributedCharacterIterator text = event.getText();
        composedText = null;
        char c;
        if (text != null) {
            // copy the committed text
            int toCopy = committedCharacterCount;
            c = text.first();
            while (toCopy-- > 0) {
                insertCharacter(c);
                c = text.next();
            }

            // copy the composed text
            if (text.getEndIndex() - (text.getBeginIndex() + committedCharacterCount) > 0) {
                composedTextString = new AttributedString(text,
                        text.getBeginIndex() + committedCharacterCount, // skip over committed text
                        text.getEndIndex(), IM_ATTRIBUTES);
                // add font information because TextLayout requires it
                composedTextString.addAttribute(TextAttribute.FONT, getFont());
                composedText = composedTextString.getIterator();
            }
        }
	event.consume();
	invalidateTextLayout();
	caret = event.getCaret();
	repaint();
    
public voidsetFontSize(int size)
Adjusts composed text for new font size.

        super.setFontSize(size);
        if (composedTextString != null) {
            composedTextString.addAttribute(TextAttribute.FONT, getFont());
        }