public Set<Entry<Attribute, Object>> entrySet() { HashSet<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; } } Entry<Attribute, Object> entry = new AttributeEntry(key, value); set.add(entry); } } return set; }
private void populateText(Paragraph p, AttributedString as, boolean insertBreakBefore, Direction blockDirection) { if (as != null) { List<Serializable> content = p.getContent(); if (insertBreakBefore) content.add(ttmlFactory.createBr(ttmlFactory.createBreak())); AttributedCharacterIterator aci = as.getIterator(); aci.first(); StringBuffer sb = new StringBuffer(); while (aci.current() != CharacterIterator.DONE) { int i = aci.getRunStart(); int e = aci.getRunLimit(); Annotation annotation = (Annotation) aci.getAttribute(TextAttribute.ANNOTATION); while (i < e) { sb.append(aci.setIndex(i++)); } String text = sb.toString(); if (annotation != null) content.add(ttmlFactory.createSpan(createSpan(text, (Attribute) annotation.getValue(), blockDirection))); else content.add(text); sb.setLength(0); aci.setIndex(e); } } }
public static void main(String[] args) throws Exception { String text = "Hello world"; AttributedString as = new AttributedString(text); // add non-Annotation attributes as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_LIGHT, 0, 3); as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 3, 5); as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_EXTRABOLD, 5, text.length()); // add Annotation attributes as.addAttribute(TextAttribute.WIDTH, new Annotation(TextAttribute.WIDTH_EXTENDED), 0, 3); as.addAttribute(TextAttribute.WIDTH, new Annotation(TextAttribute.WIDTH_CONDENSED), 3, 4); AttributedCharacterIterator aci = as.getIterator(null, 2, 4); aci.first(); int runStart = aci.getRunStart(); if (runStart != 2) { throw new Exception("1st run start is wrong. ("+runStart+" should be 2.)"); } int runLimit = aci.getRunLimit(); if (runLimit != 3) { throw new Exception("1st run limit is wrong. ("+runLimit+" should be 3.)"); } Object value = aci.getAttribute(TextAttribute.WEIGHT); if (value != TextAttribute.WEIGHT_LIGHT) { throw new Exception("1st run attribute is wrong. (" +value+" should be "+TextAttribute.WEIGHT_LIGHT+".)"); } value = aci.getAttribute(TextAttribute.WIDTH); if (value != null) { throw new Exception("1st run annotation is wrong. (" +value+" should be null.)"); } aci.setIndex(runLimit); runStart = aci.getRunStart(); if (runStart != 3) { throw new Exception("2nd run start is wrong. ("+runStart+" should be 3.)"); } runLimit = aci.getRunLimit(); if (runLimit != 4) { throw new Exception("2nd run limit is wrong. ("+runLimit+" should be 4.)"); } value = aci.getAttribute(TextAttribute.WEIGHT); if (value != TextAttribute.WEIGHT_BOLD) { throw new Exception("2nd run attribute is wrong. (" +value+" should be "+TextAttribute.WEIGHT_BOLD+".)"); } value = aci.getAttribute(TextAttribute.WIDTH); if (!(value instanceof Annotation) || (((Annotation)value).getValue() != TextAttribute.WIDTH_CONDENSED)) { throw new Exception("2nd run annotation is wrong. (" + value + " should be " + new Annotation(TextAttribute.WIDTH_CONDENSED)+".)"); } }
/** * Adds InputMethodHighlight to the attributes * @param attrs - text attributes * @return patched text attributes */ Map<? extends Attribute, ?> unpackAttributes(Map<? extends Attribute, ?> attrs) { if (attrs.containsKey(TextAttribute.INPUT_METHOD_HIGHLIGHT)) { Map<TextAttribute, ?> styles = null; Object val = attrs.get(TextAttribute.INPUT_METHOD_HIGHLIGHT); if (val instanceof Annotation) { val = ((Annotation) val).getValue(); } // // if (val instanceof InputMethodHighlight) { // InputMethodHighlight ihl = ((InputMethodHighlight) val); // styles = ihl.getStyle(); // // if (styles == null) { // Toolkit tk = Toolkit.getDefaultToolkit(); // styles = tk.mapInputMethodHighlight(ihl); // } // } if (styles != null) { HashMap<Attribute, Object> newAttrs = new HashMap<Attribute, Object>(); newAttrs.putAll(attrs); newAttrs.putAll(styles); return newAttrs; } } return attrs; }
/** * Adds InputMethodHighlight to the attributes * @param attrs - text attributes * @return patched text attributes */ Map<? extends Attribute, ?> unpackAttributes(Map<? extends Attribute, ?> attrs) { if (attrs.containsKey(TextAttribute.INPUT_METHOD_HIGHLIGHT)) { Map<TextAttribute, ?> styles = null; Object val = attrs.get(TextAttribute.INPUT_METHOD_HIGHLIGHT); if (val instanceof Annotation) { val = ((Annotation) val).getValue(); } // if (val instanceof InputMethodHighlight) { // InputMethodHighlight ihl = ((InputMethodHighlight) val); // styles = ihl.getStyle(); // // if (styles == null) { // Toolkit tk = Toolkit.getDefaultToolkit(); // styles = tk.mapInputMethodHighlight(ihl); // } // } if (styles != null) { HashMap<Attribute, Object> newAttrs = new HashMap<Attribute, Object>(); newAttrs.putAll(attrs); newAttrs.putAll(styles); return newAttrs; } } return attrs; }
/** * Adds InputMethodHighlight to the attributes * @param attrs - text attributes * @return patched text attributes */ Map<? extends Attribute, ?> unpackAttributes(Map<? extends Attribute, ?> attrs) { if (attrs.containsKey(TextAttribute.INPUT_METHOD_HIGHLIGHT)) { Map<TextAttribute, ?> styles = null; Object val = attrs.get(TextAttribute.INPUT_METHOD_HIGHLIGHT); if (val instanceof Annotation) { val = ((Annotation) val).getValue(); } if (val instanceof InputMethodHighlight) { InputMethodHighlight ihl = ((InputMethodHighlight) val); styles = ihl.getStyle(); if (styles == null) { Toolkit tk = Toolkit.getDefaultToolkit(); styles = tk.mapInputMethodHighlight(ihl); } } if (styles != null) { HashMap<Attribute, Object> newAttrs = new HashMap<Attribute, Object>(); newAttrs.putAll(attrs); newAttrs.putAll(styles); return newAttrs; } } return attrs; }
/** * @tests java.text.Annotation.toString() */ public void testToString() { Annotation ant = new Annotation("HelloWorld"); assertEquals("toString error.", "java.text.Annotation[value=HelloWorld]",ant.toString()); assertNotNull(new Annotation(null).toString()); assertNotNull(new Annotation("value").toString()); }
public void setInputMethodHighlight(Annotation f) { this.imHighlight = f; update(EINPUT_METHOD_HIGHLIGHT); }
public AnnotatedRange(Annotation annotation, int start, int end) { this.annotation = annotation; this.start = start; this.end = end; }
/** * @tests java.text.Annotation(Object) */ public void testAnnotation() { assertNotNull(new Annotation(null)); assertNotNull(new Annotation("value")); }