/** * Updates the interal bitset from <code>iterator</code>. This will set <code>validMask</code> to * true if <code>iterator</code> is non-null. */ private void updateMask(AttributedCharacterIterator iterator) { if (iterator != null) { validMask = true; this.iterator = iterator; // Update the literal mask if (literalMask == null) { literalMask = new BitSet(); } else { for (int counter = literalMask.length() - 1; counter >= 0; counter--) { literalMask.clear(counter); } } iterator.first(); while (iterator.current() != CharacterIterator.DONE) { Map attributes = iterator.getAttributes(); boolean set = isLiteral(attributes); int start = iterator.getIndex(); int end = iterator.getRunLimit(); while (start < end) { if (set) { literalMask.set(start); } else { literalMask.clear(start); } start++; } iterator.setIndex(start); } } }
/** * Called when a user processing input characters and select candidates from input method. * * @param text Text from InputMethodEvent. * @param commited_count Numbers of committed characters in text. */ public void processCompositionText(AttributedCharacterIterator text, int committed_count) { int layoutCaretPosition = initialCaretPosition + committed_count; CompositionTextPainter compositionPainter = textArea.getPainter().getCompositionTextpainter(); compositionPainter.setComposedTextLayout( getTextLayout(text, committed_count), layoutCaretPosition); int textLength = text.getEndIndex() - text.getBeginIndex() - committed_count; StringBuffer unCommitedStringBuf = new StringBuffer(textLength); char c; for (c = text.setIndex(committed_count); c != AttributedCharacterIterator.DONE && textLength > 0; c = text.next(), --textLength) { unCommitedStringBuf.append(c); } String unCommittedString = unCommitedStringBuf.toString(); try { if (canRemovePreviousInput(committed_count)) { textArea.getDocument().remove(layoutCaretPosition, prevComposeString.length()); } textArea.getDocument().insertString(layoutCaretPosition, unCommittedString, null); if (committed_count > 0) { initialCaretPosition = initialCaretPosition + committed_count; } prevComposeString = unCommittedString; prevCommittedCount = committed_count; } catch (BadLocationException e) { e.printStackTrace(); } }
/** * Return a StyledParagraph reflecting the insertion of a single character into the text. This * method will attempt to reuse the given paragraph, but may create a new paragraph. * * @param aci an iterator over the text. The text should be the same as the text used to create * (or most recently update) oldParagraph, with the exception of inserting a single character * at insertPos. * @param chars the characters in aci * @param insertPos the index of the new character in aci * @param oldParagraph a StyledParagraph for the text in aci before the insertion */ public static StyledParagraph insertChar( AttributedCharacterIterator aci, char[] chars, int insertPos, StyledParagraph oldParagraph) { // If the styles at insertPos match those at insertPos-1, // oldParagraph will be reused. Otherwise we create a new // paragraph. char ch = aci.setIndex(insertPos); int relativePos = Math.max(insertPos - aci.getBeginIndex() - 1, 0); Map attributes = addInputMethodAttrs(aci.getAttributes()); Decoration d = Decoration.getDecoration(attributes); if (!oldParagraph.getDecorationAt(relativePos).equals(d)) { return new StyledParagraph(aci, chars); } Object f = getGraphicOrFont(attributes); if (f == null) { FontResolver resolver = FontResolver.getInstance(); int fontIndex = resolver.getFontIndex(ch); f = resolver.getFont(fontIndex, attributes); } if (!oldParagraph.getFontOrGraphicAt(relativePos).equals(f)) { return new StyledParagraph(aci, chars); } // insert into existing paragraph oldParagraph.length += 1; if (oldParagraph.decorations != null) { insertInto(relativePos, oldParagraph.decorationStarts, oldParagraph.decorations.size()); } if (oldParagraph.fonts != null) { insertInto(relativePos, oldParagraph.fontStarts, oldParagraph.fonts.size()); } return oldParagraph; }
public int findMaxFontSize(AttributedCharacterIterator line, int defaultFontSize) { line.setIndex(0); Float maxFontSize = ZERO; int runLimit = 0; while (runLimit < line.getEndIndex() && (runLimit = line.getRunLimit(TextAttribute.SIZE)) <= line.getEndIndex()) { Float size = (Float) line.getAttribute(TextAttribute.SIZE); if (maxFontSize.compareTo(size) < 0) { maxFontSize = size; } line.setIndex(runLimit); } return maxFontSize.intValue(); }
public Format.Field[] getFields(final int offset) { if (format == null) { return null; } Object value = getFormattedTextField().getValue(); if (value == null) { return null; } AttributedCharacterIterator iterator = format.formatToCharacterIterator(value); if (offset < iterator.getBeginIndex() || offset > iterator.getEndIndex()) { return new Format.Field[0]; } iterator.setIndex(offset); Set keys = iterator.getAttributes().keySet(); Set result = new HashSet(); Iterator iter = keys.iterator(); while (iter.hasNext()) { Object key = iter.next(); if (key instanceof Format.Field) { result.add(key); } } return (Format.Field[]) result.toArray(new Format.Field[result.size()]); }
protected void clicked(CharPos pos) { AttributedCharacterIterator inf = pos.part.ti(); inf.setIndex(pos.ch.getCharIndex()); FuckMeGentlyWithAChainsaw url = (FuckMeGentlyWithAChainsaw) inf.getAttribute(ChatAttribute.HYPERLINK); if ((url != null) && (WebBrowser.self != null)) WebBrowser.self.show(url.url); }
/** Returns a Set of the attribute identifiers at <code>index</code>. */ Map getAttributes(int index) { if (isValidMask()) { AttributedCharacterIterator iterator = getIterator(); if (index >= 0 && index <= iterator.getEndIndex()) { iterator.setIndex(index); return iterator.getAttributes(); } } return null; }
public void actionPerformed(ActionEvent ae) { if (getFormattedTextField().isEditable()) { if (getAllowsInvalid()) { // This will work if the currently edited value is valid. updateMask(); } boolean validEdit = false; if (isValidMask()) { int start = getFormattedTextField().getSelectionStart(); if (start != -1) { AttributedCharacterIterator iterator = getIterator(); iterator.setIndex(start); Map attributes = iterator.getAttributes(); Object field = getAdjustField(start, attributes); if (canIncrement(field, start)) { try { Object value = stringToValue(getFormattedTextField().getText()); int fieldTypeCount = getFieldTypeCountTo(field, start); value = adjustValue(value, attributes, field, direction); if (value != null && isValidValue(value, false)) { resetValue(value); updateMask(); if (isValidMask()) { selectField(field, fieldTypeCount); } validEdit = true; } } catch (ParseException pe) { } catch (BadLocationException ble) { } } } } if (!validEdit) { invalidEdit(); } } }
protected void exportStyledText(JRStyledText styledText, Locale locale, boolean startedHyperlink) throws IOException { String text = styledText.getText(); int runLimit = 0; AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator(); while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) { exportStyledTextRun( iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), locale, startedHyperlink); iterator.setIndex(runLimit); } }
/** * finds attributes with regards to char index in this AttributedCharacterIterator, and puts them * in a vector * * @param iterator * @return a vector, each entry in this vector are of type FieldContainer , which stores start and * end indexes and an attribute this range has */ private static List<FieldContainer> findFields(AttributedCharacterIterator iterator) { List<FieldContainer> result = new ArrayList<FieldContainer>(); while (iterator.getIndex() != iterator.getEndIndex()) { int start = iterator.getRunStart(); int end = iterator.getRunLimit(); Iterator it = iterator.getAttributes().keySet().iterator(); while (it.hasNext()) { AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) it.next(); Object value = iterator.getAttribute(attribute); result.add(new FieldContainer(start, end, attribute, value)); // System.out.println(start + " " + end + ": " + attribute + ", // " + value ); // System.out.println("v.add(new FieldContainer(" + start +"," + // end +"," + attribute+ "," + value+ "));"); } iterator.setIndex(end); } return result; }
/** * ************************************************************************ Return a Iterator with * only the relevant attributes. Fixes implementation in AttributedString, which returns * everything * * @param aString attributed string * @param relevantAttributes relevant attributes * @return iterator */ public static AttributedCharacterIterator getIterator( AttributedString aString, AttributedCharacterIterator.Attribute[] relevantAttributes) { AttributedCharacterIterator iter = aString.getIterator(); Set set = iter.getAllAttributeKeys(); // System.out.println("AllAttributeKeys=" + set); if (set.size() == 0) return iter; // Check, if there are unwanted attributes Set<AttributedCharacterIterator.Attribute> unwanted = new HashSet<AttributedCharacterIterator.Attribute>(iter.getAllAttributeKeys()); for (int i = 0; i < relevantAttributes.length; i++) unwanted.remove(relevantAttributes[i]); if (unwanted.size() == 0) return iter; // Create new String StringBuffer sb = new StringBuffer(); for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) sb.append(c); aString = new AttributedString(sb.toString()); // copy relevant attributes Iterator it = iter.getAllAttributeKeys().iterator(); while (it.hasNext()) { AttributedCharacterIterator.Attribute att = (AttributedCharacterIterator.Attribute) it.next(); if (!unwanted.contains(att)) { for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) { Object value = iter.getAttribute(att); if (value != null) { int start = iter.getRunStart(att); int limit = iter.getRunLimit(att); // System.out.println("Attribute=" + att + " Value=" + value + " Start=" + start + " // Limit=" + limit); aString.addAttribute(att, value, start, limit); iter.setIndex(limit); } } } // else // System.out.println("Unwanted: " + att); } return aString.getIterator(); } // getIterator
/** * Create a new StyledParagraph over the given styled text. * * @param aci an iterator over the text * @param chars the characters extracted from aci */ public StyledParagraph(AttributedCharacterIterator aci, char[] chars) { int start = aci.getBeginIndex(); int end = aci.getEndIndex(); length = end - start; int index = start; aci.first(); do { final int nextRunStart = aci.getRunLimit(); final int localIndex = index - start; Map attributes = aci.getAttributes(); attributes = addInputMethodAttrs(attributes); Decoration d = Decoration.getDecoration(attributes); addDecoration(d, localIndex); Object f = getGraphicOrFont(attributes); if (f == null) { addFonts(chars, attributes, localIndex, nextRunStart - start); } else { addFont(f, localIndex); } aci.setIndex(nextRunStart); index = nextRunStart; } while (index < end); // Add extra entries to starts arrays with the length // of the paragraph. 'this' is used as a dummy value // in the Vector. if (decorations != null) { decorationStarts = addToVector(this, length, decorations, decorationStarts); } if (fonts != null) { fontStarts = addToVector(this, length, fonts, fontStarts); } }
/** * Returns the number of occurences 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; int index = 0; 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; }
/** Selects the fields identified by <code>attributes</code>. */ void selectField(Object f, int count) { AttributedCharacterIterator iterator = getIterator(); if (iterator != null && (f instanceof AttributedCharacterIterator.Attribute)) { AttributedCharacterIterator.Attribute field = (AttributedCharacterIterator.Attribute) f; iterator.first(); while (iterator.current() != CharacterIterator.DONE) { while (iterator.getAttribute(field) == null && iterator.next() != CharacterIterator.DONE) ; if (iterator.current() != CharacterIterator.DONE) { int limit = iterator.getRunLimit(field); if (--count <= 0) { getFormattedTextField().select(iterator.getIndex(), limit); break; } iterator.setIndex(limit); iterator.next(); } } } }
/** * Updates the <code>TextMeasurer</code> after a single character has been inserted into the * paragraph currently represented by this <code>TextMeasurer</code>. After this call, this <code> * TextMeasurer</code> is equivalent to a new <code>TextMeasurer</code> created from the text; * however, it will usually be more efficient to update an existing <code>TextMeasurer</code> than * to create a new one from scratch. * * @param newParagraph the text of the paragraph after performing the insertion. Cannot be null. * @param insertPos the position in the text where the character was inserted. Must not be less * than the start of <code>newParagraph</code>, and must be less than the end of <code> * newParagraph</code>. * @throws IndexOutOfBoundsException if <code>insertPos</code> is less than the start of <code> * newParagraph</code> or greater than or equal to the end of <code>newParagraph</code> * @throws NullPointerException if <code>newParagraph</code> is <code>null</code> */ public void insertChar(AttributedCharacterIterator newParagraph, int insertPos) { if (collectStats) { printStats(); } if (wantStats) { collectStats = true; } fStart = newParagraph.getBeginIndex(); int end = newParagraph.getEndIndex(); if (end - fStart != fChars.length + 1) { initAll(newParagraph); } char[] newChars = new char[end - fStart]; int newCharIndex = insertPos - fStart; System.arraycopy(fChars, 0, newChars, 0, newCharIndex); char newChar = newParagraph.setIndex(insertPos); newChars[newCharIndex] = newChar; System.arraycopy(fChars, newCharIndex, newChars, newCharIndex + 1, end - insertPos - 1); fChars = newChars; if (fBidi != null || Bidi.requiresBidi(newChars, newCharIndex, newCharIndex + 1) || newParagraph.getAttribute(TextAttribute.BIDI_EMBEDDING) != null) { fBidi = new Bidi(newParagraph); if (fBidi.isLeftToRight()) { fBidi = null; } } fParagraph = StyledParagraph.insertChar(newParagraph, fChars, insertPos, fParagraph); invalidateComponents(); }