use of org.apache.pivot.wtk.Form 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);
}
}
}
}
}
}
use of org.apache.pivot.wtk.Form in project pivot by apache.
the class TerraFormSkin method removeSections.
private void removeSections(int index, Sequence<Form.Section> removed) {
Form form = (Form) getComponent();
int count = removed.getLength();
// Remove fields
for (int i = 0; i < count; i++) {
removeFields(index + i, 0, removed.get(i));
}
// Remove labels
labels.remove(index, count);
// Remove separators
Sequence<Separator> removedSeparators = separators.remove(index, count);
for (int i = 0; i < count; i++) {
form.remove(removedSeparators.get(i));
}
invalidateComponent();
}
use of org.apache.pivot.wtk.Form 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);
}
}
}
}
use of org.apache.pivot.wtk.Form 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;
}
use of org.apache.pivot.wtk.Form in project pivot by apache.
the class TerraFormSkin method insertSection.
private void insertSection(Form.Section section, int index) {
Form form = (Form) getComponent();
// Insert separator
Separator separator = new Separator(section.getHeading());
separator.getStyles().put(Style.color, separatorColor);
separator.getStyles().put(Style.headingColor, separatorHeadingColor);
separators.insert(separator, index);
form.add(separator);
// Insert label list
ArrayList<Label> sectionLabels = new ArrayList<>();
labels.insert(sectionLabels, index);
// Insert fields
for (int i = 0, n = section.getLength(); i < n; i++) {
insertField(section, section.get(i), i);
}
invalidateComponent();
}
Aggregations