use of java.text.AttributedCharacterIterator in project eclipse.platform.swt by eclipse.
the class DateTime method getNextField.
/**
* Given a {@link FieldPosition} searches the next field in the format string
*
* @param field
* the Field to start from
* @return the next {@link FieldPosition}
*/
private FieldPosition getNextField(FieldPosition field) {
AttributedCharacterIterator iterator = dateFormat.formatToCharacterIterator(calendar.getTime());
FieldPosition first = null;
boolean found = false;
while (iterator.current() != CharacterIterator.DONE) {
FieldPosition current = getFieldPosition(iterator);
iterator.setIndex(iterator.getRunLimit());
if (current.getFieldAttribute() == null) {
continue;
}
if (found) {
return current;
}
if (first == null) {
first = current;
}
if (isSameField(current, field)) {
found = true;
}
}
return first;
}
use of java.text.AttributedCharacterIterator in project eclipse.platform.swt by eclipse.
the class DateTime method onNumberKeyInput.
boolean onNumberKeyInput(int key) {
if (currentField == null) {
return false;
}
int fieldName = getCalendarField(currentField);
StringBuffer prefix = new StringBuffer();
StringBuffer current = new StringBuffer();
StringBuffer suffix = new StringBuffer();
AttributedCharacterIterator iterator = dateFormat.formatToCharacterIterator(calendar.getTime());
char c = iterator.first();
do {
if (isSameField(currentField, getFieldPosition(iterator))) {
current.append(c);
} else if (current.length() == 0) {
prefix.append(c);
} else {
suffix.append(c);
}
} while ((c = iterator.next()) != CharacterIterator.DONE);
if (typeBufferPos < 0) {
typeBuffer.setLength(0);
typeBuffer.append(current);
typeBufferPos = 0;
}
if (key == GDK.GDK_BackSpace) {
if (typeBufferPos > 0 && typeBufferPos <= typeBuffer.length()) {
typeBuffer.deleteCharAt(typeBufferPos - 1);
typeBufferPos--;
}
} else if (key == GDK.GDK_Delete) {
if (typeBufferPos >= 0 && typeBufferPos < typeBuffer.length()) {
typeBuffer.deleteCharAt(typeBufferPos);
}
} else {
char newText = keyToString(key);
if (!Character.isAlphabetic(newText) && !Character.isDigit(newText)) {
return false;
}
if (fieldName == Calendar.AM_PM) {
if (dateFormat instanceof SimpleDateFormat) {
String[] amPmStrings = ((SimpleDateFormat) dateFormat).getDateFormatSymbols().getAmPmStrings();
if (amPmStrings[Calendar.AM].charAt(0) == newText) {
setTextField(currentField, Calendar.AM);
return false;
} else if (amPmStrings[Calendar.PM].charAt(0) == newText) {
setTextField(currentField, Calendar.PM);
return false;
}
}
}
if (typeBufferPos < typeBuffer.length()) {
typeBuffer.replace(typeBufferPos, typeBufferPos + 1, Character.toString(newText));
} else {
typeBuffer.append(newText);
}
typeBufferPos++;
}
StringBuffer newText = new StringBuffer(prefix);
newText.append(typeBuffer);
newText.append(suffix);
setText(newText.toString());
setSelection(prefix.length() + typeBufferPos, prefix.length() + typeBuffer.length());
currentField.setBeginIndex(prefix.length());
currentField.setEndIndex(prefix.length() + typeBuffer.length());
return false;
}
use of java.text.AttributedCharacterIterator in project OpenNotebook by jaltekruse.
the class AnswerBoxGUI method drawMathObject.
public void drawMathObject(AnswerBoxObject object, Graphics g, Point pageOrigin, float zoomLevel) {
ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
// TODO - decide how extra whitespace should be handled, should it always be stored?
// students may use it to format a multi-line answer
// although useful whitespace will likely not coming at the very beginning or very end
// of an answer
List<? extends MathObjectAttribute> correctAnswers = object.getListWithName(AnswerBoxObject.CORRECT_ANSWERS).getValues();
if (!object.getStudentAnswer().trim().equals("") || !correctAnswers.isEmpty()) {
Font f = g.getFont();
g.setColor(new Color(150, 210, 255));
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
String message = object.getStudentAnswer();
for (MathObjectAttribute mAtt : correctAnswers) {
message += mAtt.getValue().toString() + ";";
}
message = message.substring(0, message.length() - 1);
if (message.isEmpty()) {
// cannot have an empty string in AttributedString
message = " ";
}
g.setFont(f.deriveFont(sap.getFontSize()));
g.setColor(Color.BLACK);
Graphics2D graphics2D = (Graphics2D) g;
GraphicsEnvironment.getLocalGraphicsEnvironment();
AttributedString messageAS = new AttributedString(message);
messageAS.addAttribute(TextAttribute.FONT, g.getFont());
AttributedCharacterIterator messageIterator = messageAS.getIterator();
FontRenderContext messageFRC = graphics2D.getFontRenderContext();
LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
Insets insets = new Insets(2, 2, 2, 2);
float wrappingWidth = sap.getWidth() - insets.left - insets.right;
float x = sap.getxOrigin() + insets.left;
float y = sap.getyOrigin() + insets.top;
while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
y += textLayout.getAscent();
textLayout.draw(graphics2D, x, y);
y += textLayout.getDescent() + textLayout.getLeading();
x = sap.getxOrigin() + insets.left;
}
g.setFont(f);
} else {
g.setColor(new Color(230, 230, 230));
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
g.setColor(Color.BLACK);
g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
use of java.text.AttributedCharacterIterator in project j2objc by google.
the class DecimalFormatTest method test_formatToCharacterIterator.
public void test_formatToCharacterIterator() throws Exception {
AttributedCharacterIterator iterator;
int[] runStarts;
int[] runLimits;
String result;
char current;
// BigInteger.
iterator = new DecimalFormat().formatToCharacterIterator(new BigInteger("123456789"));
runStarts = new int[] { 0, 0, 0, 3, 4, 4, 4, 7, 8, 8, 8 };
runLimits = new int[] { 3, 3, 3, 4, 7, 7, 7, 8, 11, 11, 11 };
result = "123,456,789";
current = iterator.current();
for (int i = 0; i < runStarts.length; i++) {
assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
assertEquals("wrong char @" + i, result.charAt(i), current);
current = iterator.next();
}
assertEquals(0, iterator.getBeginIndex());
assertEquals(11, iterator.getEndIndex());
// For BigDecimal with multiplier test.
DecimalFormat df = new DecimalFormat();
df.setMultiplier(10);
iterator = df.formatToCharacterIterator(new BigDecimal("12345678901234567890"));
result = "123,456,789,012,345,678,900";
current = iterator.current();
for (int i = 0; i < result.length(); i++) {
assertEquals("wrong char @" + i, result.charAt(i), current);
current = iterator.next();
}
// For BigDecimal with multiplier test.
df = new DecimalFormat();
df.setMultiplier(-1);
df.setMaximumFractionDigits(20);
iterator = df.formatToCharacterIterator(new BigDecimal("1.23456789012345678901"));
result = "-1.23456789012345678901";
current = iterator.current();
for (int i = 0; i < result.length(); i++) {
assertEquals("wrong char @" + i, result.charAt(i), current);
current = iterator.next();
}
iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.23456789E301"));
runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 };
runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 };
// 000,000,000,000....
result = "12,345,678,900,";
current = iterator.current();
for (int i = 0; i < runStarts.length; i++) {
assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
assertEquals("wrong char @" + i, result.charAt(i), current);
current = iterator.next();
}
assertEquals(0, iterator.getBeginIndex());
assertEquals(402, iterator.getEndIndex());
iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.2345678E4"));
runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7 };
runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10 };
result = "12,345.678";
current = iterator.current();
for (int i = 0; i < runStarts.length; i++) {
assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
assertEquals("wrong char @" + i, result.charAt(i), current);
current = iterator.next();
}
assertEquals(0, iterator.getBeginIndex());
assertEquals(10, iterator.getEndIndex());
}
use of java.text.AttributedCharacterIterator in project j2objc by google.
the class Support_Format method findFields.
/**
* 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
*/
protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
Vector<FieldContainer> result = new Vector<FieldContainer>();
while (iterator.getIndex() != iterator.getEndIndex()) {
int start = iterator.getRunStart();
int end = iterator.getRunLimit();
Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
while (it.hasNext()) {
AttributedCharacterIterator.Attribute 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;
}
Aggregations