Search in sources :

Example 16 with Label

use of org.apache.pivot.wtk.Label in project pivot by apache.

the class LabelAntialiasTest method buildLabel.

private Label buildLabel(double rotation) {
    Label label = new Label();
    Font font = new Font("Arial", Font.BOLD, 64);
    AffineTransform fontAT = new AffineTransform();
    // Derive a new font using a rotation transform
    fontAT.rotate(rotation * java.lang.Math.PI / 180.0d);
    Font fontDerived = font.deriveFont(fontAT);
    label.setText("Hello at " + rotation + " degrees.");
    label.getStyles().put(Style.color, Color.RED);
    label.getStyles().put(Style.font, fontDerived);
    label.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
    label.getStyles().put(Style.verticalAlignment, VerticalAlignment.CENTER);
    return label;
}
Also used : Label(org.apache.pivot.wtk.Label) AffineTransform(java.awt.geom.AffineTransform) Font(java.awt.Font)

Example 17 with Label

use of org.apache.pivot.wtk.Label in project pivot by apache.

the class TerraFormSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    super.paint(graphics);
    GraphicsUtilities.setAntialiasingOn(graphics);
    Form form = (Form) getComponent();
    Form.SectionSequence sections = form.getSections();
    for (int sectionIndex = 0, sectionCount = sections.getLength(); sectionIndex < sectionCount; sectionIndex++) {
        Form.Section section = sections.get(sectionIndex);
        for (int fieldIndex = 0, fieldCount = section.getLength(); fieldIndex < fieldCount; fieldIndex++) {
            Component field = section.get(fieldIndex);
            if (field.isVisible()) {
                Form.Flag flag = Form.getFlag(field);
                if (flag != null) {
                    if (showFlagIcons) {
                        MessageType messageType = flag.getMessageType();
                        Image flagIcon = null;
                        switch(messageType) {
                            case ERROR:
                                {
                                    flagIcon = errorIcon;
                                    break;
                                }
                            case WARNING:
                                {
                                    flagIcon = warningIcon;
                                    break;
                                }
                            case QUESTION:
                                {
                                    flagIcon = questionIcon;
                                    break;
                                }
                            case INFO:
                                {
                                    flagIcon = infoIcon;
                                    break;
                                }
                            default:
                                {
                                    flagIcon = infoIcon;
                                    break;
                                }
                        }
                        Label label = labels.get(sectionIndex).get(fieldIndex);
                        int flagIconX = label.getX() - (flagIcon.getWidth() + flagIconOffset);
                        int flagIconY = label.getY() + (label.getHeight() - flagIcon.getHeight()) / 2;
                        graphics.translate(flagIconX, flagIconY);
                        flagIcon.paint(graphics);
                        graphics.translate(-flagIconX, -flagIconY);
                    }
                    if (showFlagHighlight) {
                        MessageType messageType = flag.getMessageType();
                        Color highlightColor = null;
                        switch(messageType) {
                            case ERROR:
                                {
                                    highlightColor = errorHighlightColor;
                                    break;
                                }
                            case WARNING:
                                {
                                    highlightColor = warningHighlightColor;
                                    break;
                                }
                            case QUESTION:
                                {
                                    highlightColor = questionHighlightColor;
                                    break;
                                }
                            case INFO:
                                {
                                    highlightColor = infoHighlightColor;
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                        Bounds fieldBounds = field.getBounds();
                        graphics.setColor(highlightColor);
                        graphics.setStroke(new BasicStroke(1));
                        graphics.drawRect(fieldBounds.x - FLAG_HIGHLIGHT_PADDING, fieldBounds.y - FLAG_HIGHLIGHT_PADDING, fieldBounds.width + FLAG_HIGHLIGHT_PADDING * 2 - 1, fieldBounds.height + FLAG_HIGHLIGHT_PADDING * 2 - 1);
                    }
                }
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Form(org.apache.pivot.wtk.Form) Color(java.awt.Color) Bounds(org.apache.pivot.wtk.Bounds) Label(org.apache.pivot.wtk.Label) Component(org.apache.pivot.wtk.Component) Image(org.apache.pivot.wtk.media.Image) Point(org.apache.pivot.wtk.Point) MessageType(org.apache.pivot.wtk.MessageType)

Example 18 with Label

use of org.apache.pivot.wtk.Label in project pivot by apache.

the class TerraFormSkin method layout.

@Override
public void layout() {
    Form form = (Form) getComponent();
    Form.SectionSequence sections = form.getSections();
    // Determine the maximum label and flag message width
    int maximumLabelWidth = 0;
    int maximumFlagMessageWidth = 0;
    for (int sectionIndex = 0, sectionCount = sections.getLength(); sectionIndex < sectionCount; sectionIndex++) {
        Form.Section section = sections.get(sectionIndex);
        for (int fieldIndex = 0, fieldCount = section.getLength(); fieldIndex < fieldCount; fieldIndex++) {
            Component field = section.get(fieldIndex);
            if (field.isVisible()) {
                Label label = labels.get(sectionIndex).get(fieldIndex);
                maximumLabelWidth = Math.max(maximumLabelWidth, label.getPreferredWidth());
                if (showFlagMessagesInline) {
                    // Calculate maximum flag message width
                    Form.Flag flag = Form.getFlag(field);
                    if (flag != null) {
                        String message = flag.getMessage();
                        if (message != null) {
                            flagMessageLabel.setText(message);
                            maximumFlagMessageWidth = Math.max(maximumFlagMessageWidth, flagMessageLabel.getPreferredWidth());
                        }
                    }
                }
            }
        }
    }
    // Determine the field width
    int width = getWidth();
    int fieldWidth = Math.max(0, width - (maximumLabelWidth + horizontalSpacing));
    if (showFlagIcons) {
        fieldWidth = Math.max(0, fieldWidth - (maximumFlagImageWidth + flagIconOffset));
    }
    if (showFlagMessagesInline) {
        fieldWidth = Math.max(0, fieldWidth - (maximumFlagMessageWidth + (INLINE_FIELD_INDICATOR_WIDTH - 2)));
    }
    fieldWidth = Math.max(0, fieldWidth - (padding.left + padding.right));
    // Lay out the components
    int rowY = padding.top;
    for (int sectionIndex = 0, sectionCount = sections.getLength(); sectionIndex < sectionCount; sectionIndex++) {
        Form.Section section = sections.get(sectionIndex);
        Separator separator = separators.get(sectionIndex);
        if (sectionIndex > 0 || section.getHeading() != null) {
            int separatorWidth = Math.max(width - (padding.left + padding.right), 0);
            separator.setVisible(true);
            separator.setSize(separatorWidth, separator.getPreferredHeight(separatorWidth));
            separator.setLocation(padding.left, rowY);
            rowY += separator.getHeight();
        } else {
            separator.setVisible(false);
        }
        for (int fieldIndex = 0, fieldCount = section.getLength(); fieldIndex < fieldCount; fieldIndex++) {
            Label label = labels.get(sectionIndex).get(fieldIndex);
            Component field = section.get(fieldIndex);
            if (field.isVisible()) {
                // Show the label
                label.setVisible(true);
                // Determine the label size and baseline
                Dimensions labelSize = label.getPreferredSize();
                label.setSize(labelSize);
                int labelAscent = label.getBaseline(labelSize.width, labelSize.height);
                int labelDescent = labelSize.height - labelAscent;
                // Determine the field size and baseline
                Dimensions fieldSize;
                if (fill) {
                    fieldSize = new Dimensions(fieldWidth, field.getPreferredHeight(fieldWidth));
                } else {
                    fieldSize = field.getPreferredSize();
                }
                field.setSize(fieldSize);
                int fieldAscent = field.getBaseline(fieldSize.width, fieldSize.height);
                if (fieldAscent == -1) {
                    fieldAscent = labelAscent;
                }
                int fieldDescent = fieldSize.height - fieldAscent;
                // Determine the baseline and row height
                int maximumAscent = Math.max(labelAscent, fieldAscent);
                int maximumDescent = Math.max(labelDescent, fieldDescent);
                int baseline = maximumAscent;
                int rowHeight = maximumAscent + maximumDescent;
                // Position the label
                int labelX = padding.left;
                if (!leftAlignLabels) {
                    labelX += maximumLabelWidth - label.getWidth();
                }
                if (showFlagIcons) {
                    labelX += (maximumFlagImageWidth + flagIconOffset);
                }
                int labelY = rowY + (baseline - labelAscent);
                label.setLocation(labelX, labelY);
                // Position the field
                int fieldX = padding.left + maximumLabelWidth + horizontalSpacing;
                if (showFlagIcons) {
                    fieldX += (maximumFlagImageWidth + flagIconOffset);
                }
                int fieldY = rowY + (baseline - fieldAscent);
                field.setLocation(fieldX, fieldY);
                // Update the row y-coordinate
                rowY += rowHeight + verticalSpacing;
            } else {
                // Hide the label
                label.setVisible(false);
            }
        }
    }
}
Also used : Form(org.apache.pivot.wtk.Form) Label(org.apache.pivot.wtk.Label) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component) Point(org.apache.pivot.wtk.Point) Separator(org.apache.pivot.wtk.Separator)

Example 19 with Label

use of org.apache.pivot.wtk.Label in project pivot by apache.

the class TerraFormSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    int baseline = -1;
    Form form = (Form) getComponent();
    Form.SectionSequence sections = form.getSections();
    // Determine the field width constraint
    int fieldWidth = (fill) ? getFieldWidth(width) : -1;
    int sectionCount = sections.getLength();
    int sectionIndex = 0;
    int rowY = 0;
    while (sectionIndex < sectionCount && baseline == -1) {
        Form.Section section = sections.get(sectionIndex);
        if (sectionIndex > 0 || section.getHeading() != null) {
            Separator separator = separators.get(sectionIndex);
            rowY += separator.getPreferredHeight(width);
            rowY += verticalSpacing;
        }
        int fieldCount = section.getLength();
        int fieldIndex = 0;
        while (fieldIndex < fieldCount && baseline == -1) {
            Component field = section.get(fieldIndex);
            if (field.isVisible()) {
                // Determine the label size and baseline
                Label label = labels.get(sectionIndex).get(fieldIndex);
                Dimensions labelSize = label.getPreferredSize();
                int labelAscent = label.getBaseline(labelSize.width, labelSize.height);
                // Determine the field size and baseline
                Dimensions fieldSize;
                if (fill && fieldWidth != -1) {
                    fieldSize = new Dimensions(fieldWidth, field.getPreferredHeight(fieldWidth));
                } else {
                    fieldSize = field.getPreferredSize();
                }
                int fieldAscent = field.getBaseline(fieldSize.width, fieldSize.height);
                if (fieldAscent == -1) {
                    fieldAscent = labelAscent;
                }
                // Determine the baseline
                int maximumAscent = Math.max(labelAscent, fieldAscent);
                baseline = rowY + maximumAscent;
            }
            fieldIndex++;
        }
        sectionIndex++;
    }
    baseline += padding.top;
    return baseline;
}
Also used : Form(org.apache.pivot.wtk.Form) Label(org.apache.pivot.wtk.Label) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component) Point(org.apache.pivot.wtk.Point) Separator(org.apache.pivot.wtk.Separator)

Example 20 with Label

use of org.apache.pivot.wtk.Label in project pivot by apache.

the class SheetTest method startup.

@SuppressWarnings("unused")
@Override
public void startup(final Display display, Map<String, String> properties) throws Exception {
    Picture picture = (Picture) Image.load(getClass().getResource("IMG_0767_2.jpg"));
    picture.resample(120);
    BoxPane windowContent = new BoxPane();
    PushButton button = new PushButton(picture);
    button.getStyles().put(Style.toolbar, true);
    windowContent.add(button);
    frame = new Frame(windowContent);
    frame.setPreferredSize(480, 360);
    frame.getStyles().put(Style.padding, 0);
    frame.open(display);
    final TablePane tablePane = new TablePane();
    tablePane.setPreferredSize(320, 240);
    new TablePane.Column(tablePane, 1, true);
    TablePane.Row row0 = new TablePane.Row(tablePane, 1, true);
    TablePane.Row row1 = new TablePane.Row(tablePane, -1);
    final Label sheetContent = new Label("Sheet Content");
    sheetContent.getStyles().put(Style.wrapText, true);
    sheetContent.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
    sheetContent.getStyles().put(Style.verticalAlignment, VerticalAlignment.CENTER);
    row0.add(sheetContent);
    Label promptBody = new Label("Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
    promptBody.getStyles().put(Style.wrapText, true);
    final Prompt prompt = new Prompt(MessageType.INFO, "Prompt", new ArrayList<>("OK"), promptBody);
    prompt.setTitle("Prompt");
    prompt.getStyles().put(Style.resizable, true);
    prompt.getComponentMouseListeners().add(new ComponentMouseListener() {

        @Override
        public void mouseOver(Component component) {
            System.out.println("Mouse Over");
        }

        @Override
        public void mouseOut(Component component) {
            System.out.println("Mouse out");
        }
    });
    Label alertBody = new Label("Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
    alertBody.getStyles().put(Style.wrapText, true);
    final Alert alert = new Alert(MessageType.INFO, "Alert", new ArrayList<>("OK"), alertBody);
    alert.setTitle("Alert");
    BoxPane boxPane = new BoxPane();
    row1.add(boxPane);
    boxPane.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.RIGHT);
    final PushButton closeButton = new PushButton("Close");
    closeButton.getStyles().put(Style.minimumAspectRatio, 3);
    boxPane.add(closeButton);
    sheet = new Sheet(tablePane);
    closeButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button buttonArgument) {
            buttonArgument.getWindow().close();
        }
    });
    button.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button buttonArgument) {
            prompt.open(frame);
            Display displayLocal = DesktopApplicationContext.createDisplay(640, 480, 100, 100, true, true, false, buttonArgument.getDisplay().getHostWindow(), null);
            Window window = new Window();
            window.setTitle("New Secondary Window");
            window.setMaximized(true);
            window.setContent(new Label("I am a secondary window!"));
            window.open(displayLocal);
        }
    });
    sheet.getWindowStateListeners().add(new WindowStateListener() {

        @Override
        public void windowOpened(Window window) {
            closeButton.requestFocus();
        }
    });
    DesktopApplicationContext.sizeHostToFit(frame);
}
Also used : Window(org.apache.pivot.wtk.Window) Frame(org.apache.pivot.wtk.Frame) WindowStateListener(org.apache.pivot.wtk.WindowStateListener) Label(org.apache.pivot.wtk.Label) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) BoxPane(org.apache.pivot.wtk.BoxPane) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) Picture(org.apache.pivot.wtk.media.Picture) Prompt(org.apache.pivot.wtk.Prompt) Alert(org.apache.pivot.wtk.Alert) PushButton(org.apache.pivot.wtk.PushButton) Component(org.apache.pivot.wtk.Component) ComponentMouseListener(org.apache.pivot.wtk.ComponentMouseListener) Sheet(org.apache.pivot.wtk.Sheet) TablePane(org.apache.pivot.wtk.TablePane) Display(org.apache.pivot.wtk.Display)

Aggregations

Label (org.apache.pivot.wtk.Label)44 Component (org.apache.pivot.wtk.Component)20 BoxPane (org.apache.pivot.wtk.BoxPane)15 Point (org.apache.pivot.wtk.Point)13 Form (org.apache.pivot.wtk.Form)10 TextInput (org.apache.pivot.wtk.TextInput)8 ComponentStateListener (org.apache.pivot.wtk.ComponentStateListener)7 FlowPane (org.apache.pivot.wtk.FlowPane)7 IntValidator (org.apache.pivot.wtk.validation.IntValidator)7 TablePane (org.apache.pivot.wtk.TablePane)6 FontRenderContext (java.awt.font.FontRenderContext)5 Rectangle2D (java.awt.geom.Rectangle2D)5 Dimensions (org.apache.pivot.wtk.Dimensions)5 Frame (org.apache.pivot.wtk.Frame)5 Separator (org.apache.pivot.wtk.Separator)5 Color (java.awt.Color)3 Font (java.awt.Font)3 LineMetrics (java.awt.font.LineMetrics)3 IOException (java.io.IOException)3 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)3