java.text.AttributedCharacterIterator.Attribute Java Examples

The following examples show how to use java.text.AttributedCharacterIterator.Attribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InternationalFormatter.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the <code>Format.Field</code> constants associated with
 * the text at <code>offset</code>. If <code>offset</code> is not
 * a valid location into the current text, this will return an
 * empty array.
 *
 * @param offset offset into text to be examined
 * @return Format.Field constants associated with the text at the
 *         given position.
 */
public Format.Field[] getFields(int offset) {
    if (getAllowsInvalid()) {
        // This will work if the currently edited value is valid.
        updateMask();
    }

    Map<Attribute, Object> attrs = getAttributes(offset);

    if (attrs != null && attrs.size() > 0) {
        ArrayList<Attribute> al = new ArrayList<Attribute>();

        al.addAll(attrs.keySet());
        return al.toArray(EMPTY_FIELD_ARRAY);
    }
    return EMPTY_FIELD_ARRAY;
}
 
Example #2
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private final void createRunAttributeDataVectors() {
    // use temporary variables so things remain consistent in case of an exception
    int newRunStarts[] = new int[ARRAY_SIZE_INCREMENT];

    @SuppressWarnings("unchecked")
    Vector<Attribute> newRunAttributes[] = (Vector<Attribute>[]) new Vector<?>[ARRAY_SIZE_INCREMENT];

    @SuppressWarnings("unchecked")
    Vector<Object> newRunAttributeValues[] = (Vector<Object>[]) new Vector<?>[ARRAY_SIZE_INCREMENT];

    runStarts = newRunStarts;
    runAttributes = newRunAttributes;
    runAttributeValues = newRunAttributeValues;
    runArraySize = ARRAY_SIZE_INCREMENT;
    runCount = 1; // assume initial run starting at index 0
}
 
Example #3
Source File: StyledParagraph.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Extract a GraphicAttribute or Font from the given attributes.
 * If attributes does not contain a GraphicAttribute, Font, or
 * Font family entry this method returns null.
 */
private static Object getGraphicOrFont(
        Map<? extends Attribute, ?> attributes) {

    Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
    if (value != null) {
        return value;
    }
    value = attributes.get(TextAttribute.FONT);
    if (value != null) {
        return value;
    }

    if (attributes.get(TextAttribute.FAMILY) != null) {
        return Font.getFont(attributes);
    }
    else {
        return null;
    }
}
 
Example #4
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public int getRunStart(Set<? extends Attribute> attributes) {
    if (currentRunStart == beginIndex || currentRunIndex == -1) {
        return currentRunStart;
    } else {
        int runStart = currentRunStart;
        int runIndex = currentRunIndex;
        while (runStart > beginIndex &&
                AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex - 1)) {
            runIndex--;
            runStart = runStarts[runIndex];
        }
        if (runStart < beginIndex) {
            runStart = beginIndex;
        }
        return runStart;
    }
}
 
Example #5
Source File: CInputMethod.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tell the component to commit all of the characters in the string to the current
 * text view. This effectively wipes out any text in progress.
 */
synchronized private void insertText(String aString) {
    AttributedString attribString = new AttributedString(aString);

    // Set locale information on the new string.
    attribString.addAttribute(Attribute.LANGUAGE, getLocale(), 0, aString.length());

    TextHitInfo theCaret = TextHitInfo.afterOffset(aString.length() - 1);
    InputMethodEvent event = new InputMethodEvent(fAwtFocussedComponent,
                                                  InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                                                  attribString.getIterator(),
                                                  aString.length(),
                                                  theCaret,
                                                  theCaret);
    LWCToolkit.postEvent(LWCToolkit.targetToAppContext(fAwtFocussedComponent), event);
    fCurrentText = null;
    fCurrentTextAsString = null;
    fCurrentTextLength = 0;
}
 
Example #6
Source File: StyledParagraph.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Extract a GraphicAttribute or Font from the given attributes.
 * If attributes does not contain a GraphicAttribute, Font, or
 * Font family entry this method returns null.
 */
private static Object getGraphicOrFont(
        Map<? extends Attribute, ?> attributes) {

    Object value = attributes.get(TextAttribute.CHAR_REPLACEMENT);
    if (value != null) {
        return value;
    }
    value = attributes.get(TextAttribute.FONT);
    if (value != null) {
        return value;
    }

    if (attributes.get(TextAttribute.FAMILY) != null) {
        return Font.getFont(attributes);
    }
    else {
        return null;
    }
}
 
Example #7
Source File: AttributedString.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets the attributes for the range from offset to the next run break
 * (typically the end of the text) to the ones specified in attrs.
 * This is only meant to be called from the constructor!
 */
private void setAttributes(Map<Attribute, Object> attrs, int offset) {
    if (runCount == 0) {
        createRunAttributeDataVectors();
    }

    int index = ensureRunBreak(offset, false);
    int size;

    if (attrs != null && (size = attrs.size()) > 0) {
        Vector<Attribute> runAttrs = new Vector<>(size);
        Vector<Object> runValues = new Vector<>(size);
        Iterator<Map.Entry<Attribute, Object>> iterator = attrs.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry<Attribute, Object> entry = iterator.next();

            runAttrs.add(entry.getKey());
            runValues.add(entry.getValue());
        }
        runAttributes[index] = runAttrs;
        runAttributeValues[index] = runValues;
    }
}
 
Example #8
Source File: AttributedString.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public int getRunLimit(Attribute attribute) {
    if (currentRunLimit == endIndex || currentRunIndex == -1) {
        return currentRunLimit;
    } else {
        Object value = getAttribute(attribute);
        int runLimit = currentRunLimit;
        int runIndex = currentRunIndex;
        while (runLimit < endIndex &&
                valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex + 1))) {
            runIndex++;
            runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
        }
        if (runLimit > endIndex) {
            runLimit = endIndex;
        }
        return runLimit;
    }
}
 
Example #9
Source File: AttributedString.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public int getRunStart(Attribute attribute) {
    if (currentRunStart == beginIndex || currentRunIndex == -1) {
        return currentRunStart;
    } else {
        Object value = getAttribute(attribute);
        int runStart = currentRunStart;
        int runIndex = currentRunIndex;
        while (runStart > beginIndex &&
                valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex - 1))) {
            runIndex--;
            runStart = runStarts[runIndex];
        }
        if (runStart < beginIndex) {
            runStart = beginIndex;
        }
        return runStart;
    }
}
 
Example #10
Source File: TextLayout.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and an attribute set.
 * <p>
 * All the text is styled using the provided attributes.
 * <p>
 * <code>string</code> must specify a single paragraph of text because an
 * entire paragraph is required for the bidirectional algorithm.
 * @param string the text to display
 * @param attributes the attributes used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Map<? extends Attribute,?> attributes,
                  FontRenderContext frc)
{
    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (attributes == null) {
        throw new IllegalArgumentException("Null map passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    char[] text = string.toCharArray();
    Font font = singleFont(text, 0, text.length, attributes);
    if (font != null) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = new AttributedString(string, attributes);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example #11
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public int getRunStart(Attribute attribute) {
    if (currentRunStart == beginIndex || currentRunIndex == -1) {
        return currentRunStart;
    } else {
        Object value = getAttribute(attribute);
        int runStart = currentRunStart;
        int runIndex = currentRunIndex;
        while (runStart > beginIndex &&
                valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex - 1))) {
            runIndex--;
            runStart = runStarts[runIndex];
        }
        if (runStart < beginIndex) {
            runStart = beginIndex;
        }
        return runStart;
    }
}
 
Example #12
Source File: InternationalFormatter.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the number of occurrences of <code>f</code> before
 * the location <code>start</code> in the current
 * <code>AttributedCharacterIterator</code>.
 */
private int getFieldTypeCountTo(Object f, int start) {
    AttributedCharacterIterator iterator = getIterator();
    int count = 0;

    if (iterator != null &&
                (f instanceof AttributedCharacterIterator.Attribute)) {
        AttributedCharacterIterator.Attribute field =
                               (AttributedCharacterIterator.Attribute)f;

        iterator.first();
        while (iterator.getIndex() < start) {
            while (iterator.getAttribute(field) == null &&
                   iterator.next() != CharacterIterator.DONE);
            if (iterator.current() != CharacterIterator.DONE) {
                iterator.setIndex(iterator.getRunLimit(field));
                iterator.next();
                count++;
            }
            else {
                break;
            }
        }
    }
    return count;
}
 
Example #13
Source File: AttributedString.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public Set<Map.Entry<Attribute, Object>> entrySet() {
    HashSet<Map.Entry<Attribute, Object>> set = new HashSet<>();
    synchronized (AttributedString.this) {
        int size = runAttributes[runIndex].size();
        for (int i = 0; i < size; i++) {
            Attribute key = runAttributes[runIndex].get(i);
            Object value = runAttributeValues[runIndex].get(i);
            if (value instanceof Annotation) {
                value = AttributedString.this.getAttributeCheckRange(key,
                                                     runIndex, beginIndex, endIndex);
                if (value == null) {
                    continue;
                }
            }

            Map.Entry<Attribute, Object> entry = new AttributeEntry(key, value);
            set.add(entry);
        }
    }
    return set;
}
 
Example #14
Source File: AquaSpinnerUI.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Selects the passed in field, returning true if it is found,
 * false otherwise.
 */
private boolean select(final JFormattedTextField ftf, final AttributedCharacterIterator iterator, final DateFormat.Field field) {
    final int max = ftf.getDocument().getLength();

    iterator.first();
    do {
        final Map<Attribute,Object> attrs = iterator.getAttributes();
        if (attrs == null || !attrs.containsKey(field)) continue;

        final int start = iterator.getRunStart(field);
        final int end = iterator.getRunLimit(field);
        if (start != -1 && end != -1 && start <= max && end <= max) {
            ftf.select(start, end);
        }

        return true;
    } while (iterator.next() != CharacterIterator.DONE);
    return false;
}
 
Example #15
Source File: AttributedString.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public Set<Map.Entry<Attribute, Object>> entrySet() {
    HashSet<Map.Entry<Attribute, Object>> set = new HashSet<>();
    synchronized (AttributedString.this) {
        int size = runAttributes[runIndex].size();
        for (int i = 0; i < size; i++) {
            Attribute key = runAttributes[runIndex].get(i);
            Object value = runAttributeValues[runIndex].get(i);
            if (value instanceof Annotation) {
                value = AttributedString.this.getAttributeCheckRange(key,
                                                     runIndex, beginIndex, endIndex);
                if (value == null) {
                    continue;
                }
            }

            Map.Entry<Attribute, Object> entry = new AttributeEntry(key, value);
            set.add(entry);
        }
    }
    return set;
}
 
Example #16
Source File: AttributedString.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public int getRunLimit(Set<? extends Attribute> attributes) {
    if (currentRunLimit == endIndex || currentRunIndex == -1) {
        return currentRunLimit;
    } else {
        int runLimit = currentRunLimit;
        int runIndex = currentRunIndex;
        while (runLimit < endIndex &&
                AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex + 1)) {
            runIndex++;
            runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
        }
        if (runLimit > endIndex) {
            runLimit = endIndex;
        }
        return runLimit;
    }
}
 
Example #17
Source File: TextLayout.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and an attribute set.
 * <p>
 * All the text is styled using the provided attributes.
 * <p>
 * <code>string</code> must specify a single paragraph of text because an
 * entire paragraph is required for the bidirectional algorithm.
 * @param string the text to display
 * @param attributes the attributes used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Map<? extends Attribute,?> attributes,
                  FontRenderContext frc)
{
    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (attributes == null) {
        throw new IllegalArgumentException("Null map passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    char[] text = string.toCharArray();
    Font font = singleFont(text, 0, text.length, attributes);
    if (font != null) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = new AttributedString(string, attributes);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example #18
Source File: AttributedString.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public int getRunLimit(Set<? extends Attribute> attributes) {
    if (currentRunLimit == endIndex || currentRunIndex == -1) {
        return currentRunLimit;
    } else {
        int runLimit = currentRunLimit;
        int runIndex = currentRunIndex;
        while (runLimit < endIndex &&
                AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex + 1)) {
            runIndex++;
            runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
        }
        if (runLimit > endIndex) {
            runLimit = endIndex;
        }
        return runLimit;
    }
}
 
Example #19
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public int getRunLimit(Set<? extends Attribute> attributes) {
    if (currentRunLimit == endIndex || currentRunIndex == -1) {
        return currentRunLimit;
    } else {
        int runLimit = currentRunLimit;
        int runIndex = currentRunIndex;
        while (runLimit < endIndex &&
                AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex + 1)) {
            runIndex++;
            runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
        }
        if (runLimit > endIndex) {
            runLimit = endIndex;
        }
        return runLimit;
    }
}
 
Example #20
Source File: AttributedString.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public int getRunStart(Set<? extends Attribute> attributes) {
    if (currentRunStart == beginIndex || currentRunIndex == -1) {
        return currentRunStart;
    } else {
        int runStart = currentRunStart;
        int runIndex = currentRunIndex;
        while (runStart > beginIndex &&
                AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex - 1)) {
            runIndex--;
            runStart = runStarts[runIndex];
        }
        if (runStart < beginIndex) {
            runStart = beginIndex;
        }
        return runStart;
    }
}
 
Example #21
Source File: AttributeValues.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public AttributeValues merge(Map<? extends Attribute, ?>map,
                             int mask) {
    if (map instanceof AttributeMap &&
        ((AttributeMap) map).getValues() != null) {
        merge(((AttributeMap)map).getValues(), mask);
    } else if (map != null && !map.isEmpty()) {
        for (Map.Entry<? extends Attribute, ?> e: map.entrySet()) {
            try {
                EAttribute ea = EAttribute.forAttribute(e.getKey());
                if (ea!= null && (mask & ea.mask) != 0) {
                    set(ea, e.getValue());
                }
            } catch (ClassCastException cce) {
                // IGNORED
            }
        }
    }
    return this;
}
 
Example #22
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
AttributedStringIterator(Attribute[] attributes, int beginIndex, int endIndex) {

            if (beginIndex < 0 || beginIndex > endIndex || endIndex > length()) {
                throw new IllegalArgumentException("Invalid substring range");
            }

            this.beginIndex = beginIndex;
            this.endIndex = endIndex;
            this.currentIndex = beginIndex;
            updateRunInfo();
            if (attributes != null) {
                relevantAttributes = attributes.clone();
            }
        }
 
Example #23
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean attributeValuesMatch(Set<? extends Attribute> attributes, int runIndex1, int runIndex2) {
    Iterator<? extends Attribute> iterator = attributes.iterator();
    while (iterator.hasNext()) {
        Attribute key = iterator.next();
       if (!valuesMatch(getAttribute(key, runIndex1), getAttribute(key, runIndex2))) {
            return false;
        }
    }
    return true;
}
 
Example #24
Source File: JTextComponent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public AttributedCharacterIterator getSelectedText(
                                        Attribute[] attributes) {
    String selection = JTextComponent.this.getSelectedText();
    if (selection != null) {
        return new AttributedString(selection).getIterator();
    } else {
        return null;
    }
}
 
Example #25
Source File: AttributedStringTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static final void checkIteratorAttributeKeys(AttributedCharacterIterator iterator, Attribute[] expectedKeys) throws Exception {
     Set iteratorKeys = iterator.getAllAttributeKeys();
     if (iteratorKeys.size() != expectedKeys.length) {
         throwException(iterator, "number of keys returned by iterator doesn't match expectation");
     }
     for (int i = 0; i < expectedKeys.length; i++) {
         if (!iteratorKeys.contains(expectedKeys[i])) {
             throwException(iterator, "expected key wasn't found in iterator's key set");
         }
     }
}
 
Example #26
Source File: InternationalFormatter.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the start of the first run that contains the attribute
 * <code>id</code>. This will return <code>-1</code> if the attribute
 * can not be found.
 */
int getAttributeStart(AttributedCharacterIterator.Attribute id) {
    if (isValidMask()) {
        AttributedCharacterIterator iterator = getIterator();

        iterator.first();
        while (iterator.current() != CharacterIterator.DONE) {
            if (iterator.getAttribute(id) != null) {
                return iterator.getIndex();
            }
            iterator.next();
        }
    }
    return -1;
}
 
Example #27
Source File: ReadResolve.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    testSerializationCycle(Attribute.LANGUAGE);
    testSerializationCycle(TextAttribute.INPUT_METHOD_HIGHLIGHT);

    boolean gotException = false;
    Attribute result = null;
    try {
        result = doSerializationCycle(FakeAttribute.LANGUAGE);
    } catch (Throwable e) {
        gotException = true;
    }
    if (!gotException) {
        throw new RuntimeException("Attribute should throw an exception when given a fake \"language\" attribute. Deserialized object: " + result);
    }
}
 
Example #28
Source File: AttributedString.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void addAttributeRunData(Attribute attribute, Object value,
        int beginRunIndex, int endRunIndex) {

    for (int i = beginRunIndex; i < endRunIndex; i++) {
        int keyValueIndex = -1; // index of key and value in our vectors; assume we don't have an entry yet
        if (runAttributes[i] == null) {
            Vector<Attribute> newRunAttributes = new Vector<>();
            Vector<Object> newRunAttributeValues = new Vector<>();
            runAttributes[i] = newRunAttributes;
            runAttributeValues[i] = newRunAttributeValues;
        } else {
            // check whether we have an entry already
            keyValueIndex = runAttributes[i].indexOf(attribute);
        }

        if (keyValueIndex == -1) {
            // create new entry
            int oldSize = runAttributes[i].size();
            runAttributes[i].addElement(attribute);
            try {
                runAttributeValues[i].addElement(value);
            }
            catch (Exception e) {
                runAttributes[i].setSize(oldSize);
                runAttributeValues[i].setSize(oldSize);
            }
        } else {
            // update existing entry
            runAttributeValues[i].set(keyValueIndex, value);
        }
    }
}
 
Example #29
Source File: CompositionAreaHandler.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public AttributedCharacterIterator cancelLatestCommittedText(Attribute[] attributes) {
    InputMethodRequests req = getClientInputMethodRequests();
    if(req != null) {
        return req.cancelLatestCommittedText(attributes);
    }

    // we don't have access to the client component's text.
    return null;
}
 
Example #30
Source File: AttributedStringTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static final void checkIteratorAttribute(AttributedCharacterIterator iterator, int index, Attribute key, Object expectedValue) throws Exception {
    iterator.setIndex(index);
    Object value = iterator.getAttribute(key);
    if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) {
        throwException(iterator, "iterator returns wrong attribute value - " + value + " instead of " + expectedValue);
    }
    value = iterator.getAttributes().get(key);
    if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) {
        throwException(iterator, "iterator's map returns wrong attribute value - " + value + " instead of " + expectedValue);
    }
}