use of org.apache.pivot.wtk.Label in project pivot by apache.
the class WindowTest method startup.
@Override
public void startup(final Display display, final Map<String, String> properties) {
window1.setTitle("Window 1");
window1.setPreferredSize(640, 480);
window1.setMaximumWidth(640);
window1.setMaximumHeight(480);
window1.setMinimumWidth(320);
window1.setMinimumHeight(240);
window1.getComponentListeners().add(new ComponentListener() {
@Override
public void sizeChanged(final Component component, final int previousWidth, final int previousHeight) {
window1.align(window1.getDisplay().getBounds(), HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
window1.getComponentListeners().remove(this);
}
});
display.getStyles().put(Style.backgroundColor, new Color(0, 127, 127));
window1.setContent(new Label("Hello Bar"));
window1.open(display);
ApplicationContext.queueCallback(() -> {
final Sheet sheet = new Sheet();
sheet.setPreferredSize(120, 60);
sheet.open(window1);
ApplicationContext.queueCallback(() -> {
Sheet sheet2 = new Sheet();
sheet2.setPreferredSize(60, 30);
sheet2.open(sheet);
});
});
Frame window1a = new Frame();
window1a.setTitle("Window 1 A");
window1a.setLocation(30, 280);
window1a.setPreferredSize(160, 120);
window1a.open(window1);
Frame window1ai = new Frame();
window1ai.setTitle("Window 1 A I");
window1ai.setLocation(150, 300);
window1ai.setPreferredSize(320, 200);
window1ai.open(window1a);
window1ai.getDecorators().update(0, new ReflectionDecorator());
Frame window1aii = new Frame();
window1aii.setTitle("Window 1 A II");
window1aii.setLocation(50, 400);
window1aii.setPreferredSize(320, 200);
window1aii.open(window1a);
Frame window1b = new Frame();
window1b.setTitle("Window 1 B");
window1b.setPreferredSize(160, 120);
window1b.setLocation(260, 60);
window1b.open(window1);
Frame window1bi = new Frame();
window1bi.setTitle("Window 1 B I");
window1bi.setPreferredSize(180, 60);
window1bi.setLocation(270, 160);
window1bi.setContent(new Label("This window is not enabled"));
// to test even a not enabled window ...
window1bi.setEnabled(false);
window1bi.open(window1b);
Frame window1bii = new Frame();
window1bii.setTitle("Window 1 B II");
window1bii.setPreferredSize(160, 60);
window1bii.setLocation(320, 10);
window1bii.open(window1b);
Palette palette1 = new Palette();
palette1.setTitle("Palette 1bii 1");
palette1.setPreferredSize(160, 60);
palette1.setLocation(290, 210);
palette1.open(window1bii);
Palette palette2 = new Palette();
palette2.setTitle("Palette 1bii 2");
palette2.setPreferredSize(160, 60);
palette2.setLocation(600, 200);
palette2.setContent(new Label("This palette is not enabled"));
// to test even a not enabled palette ...
palette2.setEnabled(false);
palette2.open(window1bii);
dialogOwner.setTitle("Dialog Owner");
dialogOwner.setPreferredSize(320, 120);
dialogOwner.open(display);
// window1bii.requestFocus();
ApplicationContext.queueCallback(() -> {
final Dialog dialog = new Dialog();
dialog.setTitle("Dialog 1");
dialog.setPreferredSize(280, 100);
dialog.open(dialogOwner);
ApplicationContext.queueCallback(() -> {
Dialog dialog2 = new Dialog();
dialog2.setTitle("Dialog 2");
dialog2.setPreferredSize(220, 80);
dialog2.open(dialog);
});
});
}
use of org.apache.pivot.wtk.Label in project pivot by apache.
the class Pivot811 method startup.
@Override
public void startup(final Display displayArgument, Map<String, String> properties) throws Exception {
this.display = displayArgument;
Frame listFrame = new Frame();
listFrame.setTitle("List Frame");
listFrame.setPreferredSize(400, 300);
listFrame.setLocation(20, 20);
listFrame.getStyles().put(Style.padding, Insets.NONE);
BoxPane boxPane = new BoxPane();
boxPane.getStyles().put(Style.fill, true);
boxPane.setOrientation(Orientation.VERTICAL);
listFrame.setContent(boxPane);
Label infoLabel = new Label("Double click on a list item to open a detail frame");
boxPane.add(infoLabel);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollBarPolicy.FILL);
scrollPane.setVerticalScrollBarPolicy(ScrollBarPolicy.FILL_TO_CAPACITY);
// workaround for pivot-738,
scrollPane.setRepaintAllViewport(true);
// needed only in in some cases
boxPane.add(scrollPane);
final ListView listView = new ListView();
List<String> listData = new ArrayList<>();
for (int i = 0; i < 50; ++i) {
listData.add("List Item " + i);
}
listView.setListData(listData);
scrollPane.setView(listView);
listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {
@Override
public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
System.out.println("selectedItemChanged : " + listViewArgument.getSelectedItem());
}
});
listView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
System.out.println("mouseClick : " + count);
if (count == 2) {
System.out.println("double click, now open a detail frame");
Frame detailFrame = new Frame();
detailFrame.setTitle("Detail Frame");
detailFrame.setPreferredSize(400, 300);
int selectedIndex = listView.getSelectedIndex();
detailFrame.setLocation(80 + (selectedIndex * 10), 80 + (selectedIndex * 10));
detailFrame.getStyles().put(Style.padding, Insets.NONE);
BoxPane boxPaneLocal = new BoxPane();
boxPaneLocal.getStyles().put(Style.fill, true);
boxPaneLocal.setOrientation(Orientation.VERTICAL);
detailFrame.setContent(boxPaneLocal);
String selectedItem = listView.getSelectedItem().toString();
Label label = new Label("Selected Item is \"" + selectedItem + "\"");
boxPaneLocal.add(label);
// spacer
boxPaneLocal.add(new Label(""));
boxPaneLocal.add(new Label("Click inside the text input to focus it"));
TextInput textInput = new TextInput();
textInput.setText("Focusable component");
// workaround for pivot-811:
boxPaneLocal.add(textInput);
// add a focusable element
// inside the frame
detailFrame.open(displayArgument);
// workaround for pivot-811: force the focus on the first
// focusable element inside the frame
detailFrame.requestFocus();
// textInput.requestFocus(); // or use this ...
}
return true;
}
});
listFrame.open(displayArgument);
listView.setSelectedIndex(0);
listView.requestFocus();
}
use of org.apache.pivot.wtk.Label in project pivot by apache.
the class LabelSkin method paint.
@Override
public void paint(Graphics2D graphics) {
Label label = (Label) this.getComponent();
int width = getWidth();
int height = getHeight();
// Draw the background
if (backgroundColor != null) {
graphics.setPaint(backgroundColor);
graphics.fillRect(0, 0, width, height);
}
// Draw the text
if (glyphVectors != null && glyphVectors.getLength() > 0) {
graphics.setFont(font);
if (label.isEnabled()) {
graphics.setPaint(color);
} else {
graphics.setPaint(disabledColor);
}
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
LineMetrics lm = font.getLineMetrics("", fontRenderContext);
float ascent = lm.getAscent();
float lineHeight = lm.getHeight();
float y = 0;
switch(verticalAlignment) {
case TOP:
{
y = padding.top;
break;
}
case BOTTOM:
{
y = height - (textHeight + padding.bottom);
break;
}
case CENTER:
{
y = (height - textHeight) / 2;
break;
}
default:
{
break;
}
}
for (int i = 0, n = glyphVectors.getLength(); i < n; i++) {
GlyphVector glyphVector = glyphVectors.get(i);
Rectangle2D textBounds = glyphVector.getLogicalBounds();
float lineWidth = (float) textBounds.getWidth();
float x = 0;
switch(horizontalAlignment) {
case LEFT:
{
x = padding.left;
break;
}
case RIGHT:
{
x = width - (lineWidth + padding.right);
break;
}
case CENTER:
{
x = (width - lineWidth) / 2;
break;
}
default:
{
break;
}
}
if (graphics instanceof PrintGraphics) {
// Work-around for printing problem in applets
String text = label.getText();
if (text != null && text.length() > 0) {
graphics.drawString(text, x, y + ascent);
}
} else {
graphics.drawGlyphVector(glyphVector, x, y + ascent);
}
// Draw the text decoration
if (textDecoration != null) {
graphics.setStroke(new BasicStroke());
float offset = 0;
switch(textDecoration) {
case UNDERLINE:
{
offset = y + ascent + 2;
break;
}
case STRIKETHROUGH:
{
offset = y + lineHeight / 2 + 1;
break;
}
default:
{
break;
}
}
Line2D line = new Line2D.Float(x, offset, x + lineWidth, offset);
graphics.draw(line);
}
y += textBounds.getHeight();
}
}
}
use of org.apache.pivot.wtk.Label in project pivot by apache.
the class LabelSkin method getPreferredHeight.
@Override
public int getPreferredHeight(int width) {
Label label = (Label) getComponent();
String text = label.getText();
float preferredHeight;
if (text != null) {
int widthUpdated = width;
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
LineMetrics lm = font.getLineMetrics("", fontRenderContext);
float lineHeight = lm.getHeight();
preferredHeight = lineHeight;
int n = text.length();
if (n > 0 && wrapText && widthUpdated != -1) {
// Adjust width for padding
widthUpdated -= (padding.left + padding.right);
float lineWidth = 0;
int lastWhitespaceIndex = -1;
int i = 0;
while (i < n) {
char c = text.charAt(i);
if (c == '\n') {
lineWidth = 0;
lastWhitespaceIndex = -1;
preferredHeight += lineHeight;
} else {
if (Character.isWhitespace(c)) {
lastWhitespaceIndex = i;
}
Rectangle2D characterBounds = font.getStringBounds(text, i, i + 1, fontRenderContext);
lineWidth += characterBounds.getWidth();
if (lineWidth > widthUpdated && lastWhitespaceIndex != -1) {
i = lastWhitespaceIndex;
lineWidth = 0;
lastWhitespaceIndex = -1;
preferredHeight += lineHeight;
}
}
i++;
}
}
} else {
preferredHeight = 0;
}
preferredHeight += (padding.top + padding.bottom);
return (int) Math.ceil(preferredHeight);
}
use of org.apache.pivot.wtk.Label in project pivot by apache.
the class LabelSkin method layout.
@Override
public void layout() {
Label label = (Label) getComponent();
String text = label.getText();
glyphVectors = new ArrayList<>();
textHeight = 0;
if (text != null) {
int n = text.length();
if (n > 0) {
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
if (wrapText) {
int width = getWidth() - (padding.left + padding.right);
int i = 0;
int start = 0;
float lineWidth = 0;
int lastWhitespaceIndex = -1;
// NOTE: We use a character iterator here only because it is the most
// efficient way to measure the character bounds (as of Java 6, the version
// of Font#getStringBounds() that takes a String performs a string copy,
// whereas the version that takes a character iterator does not).
StringCharacterIterator ci = new StringCharacterIterator(text);
while (i < n) {
char c = text.charAt(i);
if (c == '\n') {
appendLine(text, start, i, fontRenderContext);
start = i + 1;
lineWidth = 0;
lastWhitespaceIndex = -1;
} else {
if (Character.isWhitespace(c)) {
lastWhitespaceIndex = i;
}
Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1, fontRenderContext);
lineWidth += characterBounds.getWidth();
if (lineWidth > width && lastWhitespaceIndex != -1) {
appendLine(text, start, lastWhitespaceIndex, fontRenderContext);
i = lastWhitespaceIndex;
start = i + 1;
lineWidth = 0;
lastWhitespaceIndex = -1;
}
}
i++;
}
appendLine(text, start, i, fontRenderContext);
} else {
appendLine(text, 0, text.length(), fontRenderContext);
}
}
}
}
Aggregations