Skip to content

Commit

Permalink
merge from 2.0.x: PIVOT-455
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1406206 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Sandro Martini committed Nov 6, 2012
1 parent 30f9e69 commit ba8a10a
Showing 1 changed file with 64 additions and 8 deletions.
Expand Up @@ -40,6 +40,7 @@
import org.apache.pivot.wtk.Dimensions; import org.apache.pivot.wtk.Dimensions;
import org.apache.pivot.wtk.FocusTraversalDirection; import org.apache.pivot.wtk.FocusTraversalDirection;
import org.apache.pivot.wtk.GraphicsUtilities; import org.apache.pivot.wtk.GraphicsUtilities;
import org.apache.pivot.wtk.HorizontalAlignment;
import org.apache.pivot.wtk.Insets; import org.apache.pivot.wtk.Insets;
import org.apache.pivot.wtk.Keyboard; import org.apache.pivot.wtk.Keyboard;
import org.apache.pivot.wtk.Keyboard.KeyCode; import org.apache.pivot.wtk.Keyboard.KeyCode;
Expand Down Expand Up @@ -148,6 +149,8 @@ public void run() {


private Insets padding; private Insets padding;


private HorizontalAlignment horizontalAlignment;

private Dimensions averageCharacterSize; private Dimensions averageCharacterSize;


private static final int SCROLL_RATE = 50; private static final int SCROLL_RATE = 50;
Expand All @@ -167,6 +170,7 @@ public TerraTextInputSkin() {
borderColor = theme.getColor(7); borderColor = theme.getColor(7);
disabledBorderColor = theme.getColor(7); disabledBorderColor = theme.getColor(7);
padding = new Insets(2); padding = new Insets(2);
horizontalAlignment = HorizontalAlignment.LEFT;


selectionColor = theme.getColor(4); selectionColor = theme.getColor(4);
selectionBackgroundColor = theme.getColor(14); selectionBackgroundColor = theme.getColor(14);
Expand Down Expand Up @@ -270,6 +274,30 @@ public void layout() {
&& textInput.getSelectionLength() == 0); && textInput.getSelectionLength() == 0);
} }


private int getAlignmentDeltaX() {
int alignmentDeltaX = 0;
switch (horizontalAlignment) {
case LEFT:
break;
case CENTER: {
TextInput textInput = (TextInput)getComponent();
double txtWidth = glyphVector == null ? 0 : glyphVector.getLogicalBounds().getWidth();
int availWidth = textInput.getWidth() - (padding.left + padding.right + 2);
alignmentDeltaX = (int)(availWidth - txtWidth) / 2;
break;
}
case RIGHT: {
TextInput textInput = (TextInput)getComponent();
double txtWidth = glyphVector == null ? 0 : glyphVector.getLogicalBounds().getWidth();
int availWidth = textInput.getWidth() - (padding.left + padding.right + 2 + caret.width);
alignmentDeltaX = (int)(availWidth - txtWidth);
break;
}
}

return alignmentDeltaX;
}

@Override @Override
public void paint(Graphics2D graphics) { public void paint(Graphics2D graphics) {
TextInput textInput = (TextInput)getComponent(); TextInput textInput = (TextInput)getComponent();
Expand Down Expand Up @@ -316,6 +344,10 @@ public void paint(Graphics2D graphics) {
String prompt = textInput.getPrompt(); String prompt = textInput.getPrompt();


Color caretColor; Color caretColor;

int alignmentDeltaX = getAlignmentDeltaX();
int xpos = padding.left - scrollLeft + 1 + alignmentDeltaX;

if (glyphVector == null if (glyphVector == null
&& prompt != null) { && prompt != null) {
graphics.setFont(font); graphics.setFont(font);
Expand All @@ -324,7 +356,7 @@ public void paint(Graphics2D graphics) {
fontRenderContext.getAntiAliasingHint()); fontRenderContext.getAntiAliasingHint());
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
fontRenderContext.getFractionalMetricsHint()); fontRenderContext.getFractionalMetricsHint());
graphics.drawString(prompt, padding.left - scrollLeft + 1, graphics.drawString(prompt, xpos,
(height - textHeight) / 2 + ascent); (height - textHeight) / 2 + ascent);


caretColor = color; caretColor = color;
Expand All @@ -350,7 +382,7 @@ public void paint(Graphics2D graphics) {
if (selection == null) { if (selection == null) {
// Paint the text // Paint the text
graphics.setColor(colorLocal); graphics.setColor(colorLocal);
graphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, graphics.drawGlyphVector(glyphVector, xpos,
(height - textHeight) / 2 + ascent); (height - textHeight) / 2 + ascent);
} else { } else {
// Paint the unselected text // Paint the unselected text
Expand All @@ -361,7 +393,7 @@ public void paint(Graphics2D graphics) {
Graphics2D textGraphics = (Graphics2D)graphics.create(); Graphics2D textGraphics = (Graphics2D)graphics.create();
textGraphics.setColor(colorLocal); textGraphics.setColor(colorLocal);
textGraphics.clip(unselectedArea); textGraphics.clip(unselectedArea);
textGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, textGraphics.drawGlyphVector(glyphVector, xpos,
(height - textHeight) / 2 + ascent); (height - textHeight) / 2 + ascent);
textGraphics.dispose(); textGraphics.dispose();


Expand All @@ -383,7 +415,7 @@ public void paint(Graphics2D graphics) {
Graphics2D selectedTextGraphics = (Graphics2D)graphics.create(); Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
selectedTextGraphics.setColor(selectionColorLocal); selectedTextGraphics.setColor(selectionColorLocal);
selectedTextGraphics.clip(selection.getBounds()); selectedTextGraphics.clip(selection.getBounds());
selectedTextGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1, selectedTextGraphics.drawGlyphVector(glyphVector, xpos,
(height - textHeight) / 2 + ascent); (height - textHeight) / 2 + ascent);
selectedTextGraphics.dispose(); selectedTextGraphics.dispose();
} }
Expand Down Expand Up @@ -411,7 +443,7 @@ public int getInsertionPoint(int x) {
offset = 0; offset = 0;
} else { } else {
// Translate to glyph coordinates // Translate to glyph coordinates
x -= (padding.left - scrollLeft + 1); x -= (padding.left - scrollLeft + 1 + getAlignmentDeltaX());


Rectangle2D textBounds = glyphVector.getLogicalBounds(); Rectangle2D textBounds = glyphVector.getLogicalBounds();


Expand Down Expand Up @@ -468,7 +500,7 @@ public Bounds getCharacterBounds(int index) {
width = 0; width = 0;
} }


characterBounds = new Bounds(x + padding.left - scrollLeft + 1, padding.top + 1, characterBounds = new Bounds(x + padding.left - scrollLeft + 1 + getAlignmentDeltaX(), padding.top + 1,
width, getHeight() - (padding.top + padding.bottom + 2)); width, getHeight() - (padding.top + padding.bottom + 2));
} }


Expand Down Expand Up @@ -919,6 +951,19 @@ public final void setPadding(String padding) {
setPadding(Insets.decode(padding)); setPadding(Insets.decode(padding));
} }


public HorizontalAlignment getHorizontalAlignment() {
return horizontalAlignment;
}

public final void setHorizontalAlignment(HorizontalAlignment alignment) {
if (alignment == null) {
throw new IllegalArgumentException("horizontalAlignment is null.");
}

this.horizontalAlignment = alignment;
invalidateComponent();
}

@Override @Override
public boolean mouseMove(Component component, int x, int y) { public boolean mouseMove(Component component, int x, int y) {
boolean consumed = super.mouseMove(component, x, y); boolean consumed = super.mouseMove(component, x, y);
Expand Down Expand Up @@ -1452,6 +1497,16 @@ public Vote previewRemoveText(TextInput textInput, int index, int count) {
return vote; return vote;
} }


@Override
public void setSize(int width, int height) {
boolean invalidate = (horizontalAlignment != HorizontalAlignment.LEFT) && (width != this.getWidth());
super.setSize(width, height);
if (invalidate) {
updateSelection();
invalidateComponent();
}
}

@Override @Override
public void removeTextVetoed(TextInput textInput, Vote reason) { public void removeTextVetoed(TextInput textInput, Vote reason) {
// No-op // No-op
Expand Down Expand Up @@ -1521,10 +1576,10 @@ private void updateSelection() {
// The insertion point is after the last character // The insertion point is after the last character
int x; int x;
if (n == 0) { if (n == 0) {
x = padding.left - scrollLeft + 1; x = padding.left - scrollLeft + 1 + getAlignmentDeltaX();
} else { } else {
Rectangle2D textBounds = glyphVector.getLogicalBounds(); Rectangle2D textBounds = glyphVector.getLogicalBounds();
x = (int)Math.ceil(textBounds.getWidth()) + (padding.left - scrollLeft + 1); x = (int)Math.ceil(textBounds.getWidth()) + (padding.left - scrollLeft + 1 + getAlignmentDeltaX());
} }


int y = padding.top + 1; int y = padding.top + 1;
Expand Down Expand Up @@ -1563,4 +1618,5 @@ public void showCaret(boolean show) {
scheduledBlinkCaretCallback = null; scheduledBlinkCaretCallback = null;
} }
} }

} }

0 comments on commit ba8a10a

Please sign in to comment.