Skip to content

Commit

Permalink
Add tests and resolve some sonarqube issues for moved forms renderers…
Browse files Browse the repository at this point in the history
…, remove placeholders

DEVSIX-6294
  • Loading branch information
Eugene Bochilo committed Mar 7, 2023
1 parent d5cece9 commit 3b19a34
Show file tree
Hide file tree
Showing 71 changed files with 1,635 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ This file is part of the iText (R) project.

import com.itextpdf.commons.utils.MessageFormatUtil;
import com.itextpdf.forms.fields.borders.FormBorderFactory;
import com.itextpdf.forms.form.FormProperty;
import com.itextpdf.forms.form.element.CheckBox;
import com.itextpdf.forms.form.element.ComboBoxField;
import com.itextpdf.forms.form.element.IFormField;
import com.itextpdf.forms.form.element.InputButton;
import com.itextpdf.forms.form.element.InputField;
import com.itextpdf.forms.form.element.Radio;
import com.itextpdf.forms.logs.FormsLogMessageConstants;
import com.itextpdf.forms.util.DrawingUtil;
import com.itextpdf.io.logs.IoLogMessageConstant;
Expand Down Expand Up @@ -1274,80 +1267,6 @@ boolean regenerateWidget() {
}
return false;
}

boolean drawWidget() {
// TODO DEVSIX-7385 This method is excepted to be used instead of regenerateWidget method
final PdfName type = parent.getFormType();
IFormField formField;
if (PdfName.Tx.equals(type)) {
formField = createInputField();
} else if (PdfName.Ch.equals(type)) {
formField = createChoiceField();
} else if (PdfName.Btn.equals(type)) {
if (parent.getFieldFlag(PdfButtonFormField.FF_PUSH_BUTTON)) {
formField = createInputButton();
} else if (parent.getFieldFlag(PdfButtonFormField.FF_RADIO)) {
formField = createRadio();
} else {
formField = createCheckbox();
}
} else {
return false;
}
// TODO DEVSIX-7359 This method is a placeholder and is not expected to work correctly right now.
// Shall be changed.
drawFieldAndSaveAppearance(formField);
return true;
}

void drawFieldAndSaveAppearance(IFormField formField) {
Rectangle rectangle = getRect(this.getPdfObject());
// TODO DEVSIX-7359 rectangle for xObject is incorrect. The most close result appears with +5 instead of +1000,
// however several tests hang because of not enough space.
// It may be that rectangle should be unique for each element.
PdfFormXObject xObject = new PdfFormXObject(new Rectangle(0, 0, rectangle.getWidth() + 1000, rectangle.getHeight() + 1000));
Canvas canvas = new Canvas(xObject, this.getDocument());
// TODO DEVSIX-7359 setters for such values are expected to be implemented on the necessary level.
// For all properties, which are expected to be used by user,
// special setters shall present on model elements level.
formField.setProperty(FormProperty.FORM_FIELD_FLATTEN, true);
formField.setProperty(FormProperty.FORM_FIELD_VALUE, parent.getValueAsString());
canvas.add(formField);
PdfDictionary ap = new PdfDictionary();
ap.put(PdfName.N, xObject.getPdfObject());
ap.setModified();
put(PdfName.AP, ap);
}

IFormField createInputField() {
// TODO DEVSIX-7362 implement logic for text field.
InputField inputField = new InputField(parent.getFieldName().toUnicodeString());
return inputField;
}

IFormField createChoiceField() {
// TODO DEVSIX-7394 implement logic for choice form fields.
ComboBoxField comboBoxField = new ComboBoxField(parent.getFieldName().toUnicodeString());
return comboBoxField;
}

IFormField createInputButton() {
// TODO DEVSIX-7359 implement logic for push button.
InputButton inputButton = new InputButton(parent.getFieldName().toUnicodeString());
return inputButton;
}

IFormField createRadio() {
// TODO DEVSIX-7360 implement logic for radio button.
Radio radio = new Radio(parent.getFieldName().toUnicodeString());
return radio;
}

IFormField createCheckbox() {
// TODO DEVSIX-7361 implement logic for checkbox.
CheckBox checkBox = new CheckBox(parent.getFieldName().toUnicodeString());
return checkBox;
}

private static double degreeToRadians(double angle) {
return Math.PI * angle / 180.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ This file is part of the iText (R) project.
package com.itextpdf.forms.form.element;

import com.itextpdf.layout.element.IBlockElement;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -51,7 +52,7 @@ This file is part of the iText (R) project.
*/
public abstract class AbstractSelectField extends FormField<AbstractSelectField> {

private List<IBlockElement> options = new ArrayList<>();
private final List<IBlockElement> options = new ArrayList<>();

protected AbstractSelectField(String id) {
super(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This file is part of the iText (R) project.
import com.itextpdf.layout.renderer.IRenderer;

/**
* Extension of the {@link FormField} class representing a button in html
* Extension of the {@link FormField} class representing a button in html.
*/
public class Button extends FormField<Button> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ This file is part of the iText (R) project.
public abstract class FormField<T extends IFormField> extends AbstractElement<T> implements IFormField {

/** The id. */
private String id;
private final String id;

/**
* Instantiates a new {@link FormField} instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This file is part of the iText (R) project.
import com.itextpdf.layout.element.IBlockElement;

/**
* Common interface for HTML form elements
* Common interface for HTML form elements.
*/
public interface IFormField extends IBlockElement {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This file is part of the iText (R) project.
import com.itextpdf.layout.element.Paragraph;

/**
* A marker interface that specifies that the layout object has placeholder
* A marker interface that specifies that the layout object has placeholder.
*/
public interface IPlaceholderable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This file is part of the iText (R) project.
import com.itextpdf.layout.renderer.IRenderer;

/**
* Extension of the {@link FormField} class representing a input with type button or submit in html
* Extension of the {@link FormField} class representing a input with type button or submit in html.
*/
public class InputButton extends FormField<InputButton> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ public InputField(String id) {
/**
* {@inheritDoc}
*/
@Override
public Paragraph getPlaceholder() {
return placeholder;
}

/**
* {@inheritDoc}
*/
@Override
public void setPlaceholder(Paragraph placeholder) {
this.placeholder = placeholder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public class ListBoxField extends AbstractSelectField {
/**
* Creates a new list box field.
*
* @param size the size of the list box, which will define the height of visible properties, shall be greater than zero
* @param allowMultipleSelection a boolean flag that defines whether multiple options are allowed to be selected at once
* @param size the size of the list box, which will define the height of visible properties,
* shall be greater than zero
* @param allowMultipleSelection a boolean flag that defines whether multiple options are allowed
* to be selected at once
* @param id the id
*/
public ListBoxField(String id, int size, boolean allowMultipleSelection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ public <T1> T1 getDefaultProperty(int property) {
/**
* {@inheritDoc}
*/
@Override
public Paragraph getPlaceholder() {
return placeholder;
}

/**
* {@inheritDoc}
*/
@Override
public void setPlaceholder(Paragraph placeholder) {
this.placeholder = placeholder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public abstract class AbstractFormFieldRenderer extends BlockRenderer {
*/
public boolean isFlatten() {
Boolean flatten = getPropertyAsBoolean(FormProperty.FORM_FIELD_FLATTEN);
return flatten != null ? (boolean) flatten : (boolean) modelElement.<Boolean>getDefaultProperty(FormProperty.FORM_FIELD_FLATTEN);
return flatten == null ?
(boolean) modelElement.<Boolean>getDefaultProperty(FormProperty.FORM_FIELD_FLATTEN) : (boolean) flatten;
}

/**
Expand All @@ -102,7 +103,8 @@ public boolean isFlatten() {
*/
public String getDefaultValue() {
String defaultValue = this.<String>getProperty(FormProperty.FORM_FIELD_VALUE);
return defaultValue != null ? defaultValue : modelElement.<String>getDefaultProperty(FormProperty.FORM_FIELD_VALUE);
return defaultValue == null ?
modelElement.<String>getDefaultProperty(FormProperty.FORM_FIELD_VALUE) : defaultValue;
}

/* (non-Javadoc)
Expand All @@ -125,7 +127,10 @@ public LayoutResult layout(LayoutContext layoutContext) {
layoutContext.getArea().setBBox(bBox);
LayoutResult result = super.layout(layoutContext);

if (!childRenderers.isEmpty()) {
if (childRenderers.isEmpty()) {
LoggerFactory.getLogger(getClass()).error(FormsLogMessageConstants.ERROR_WHILE_LAYOUT_OF_FORM_FIELD);
occupiedArea.getBBox().setWidth(0).setHeight(0);
} else {
flatRenderer = childRenderers.get(0);
processLangAttribute();
childRenderers.clear();
Expand All @@ -139,9 +144,6 @@ public LayoutResult layout(LayoutContext layoutContext) {
applyBorderBox(occupiedArea.getBBox(), true);
applyMargins(occupiedArea.getBBox(), true);
}
} else {
LoggerFactory.getLogger(getClass()).error(FormsLogMessageConstants.ERROR_WHILE_LAYOUT_OF_FORM_FIELD);
occupiedArea.getBBox().setWidth(0).setHeight(0);
}
if (!Boolean.TRUE.equals(getPropertyAsBoolean(Property.FORCED_PLACEMENT)) && !isRendererFit(parentWidth,
parentHeight)) {
Expand Down Expand Up @@ -238,7 +240,8 @@ protected boolean isRendererFit(float availableWidth, float availableHeight) {
}
return availableHeight >= occupiedArea.getBBox().getHeight() &&
((availableWidth >= occupiedArea.getBBox().getWidth()) ||
(this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X) == OverflowPropertyValue.VISIBLE));
(this.<OverflowPropertyValue>getProperty(Property.OVERFLOW_X)
== OverflowPropertyValue.VISIBLE));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ This file is part of the iText (R) project.
import com.itextpdf.layout.renderer.BlockRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -71,7 +71,7 @@ public abstract class AbstractSelectFieldRenderer extends BlockRenderer {
*
* @param modelElement the model element
*/
public AbstractSelectFieldRenderer(AbstractSelectField modelElement) {
protected AbstractSelectFieldRenderer(AbstractSelectField modelElement) {
super(modelElement);
addChild(createFlatRenderer());
if (!isFlatten()) {
Expand Down Expand Up @@ -187,27 +187,31 @@ protected float getFinalSelectFieldHeight(float availableHeight, float actualHei
protected List<IRenderer> getOptionsMarkedSelected(IRenderer optionsSubTree) {
List<IRenderer> selectedOptions = new ArrayList<>();
for (IRenderer option : optionsSubTree.getChildRenderers()) {
if (!isOptionRenderer(option)) {
if (isOptionRenderer(option)) {
if (Boolean.TRUE.equals(option.<Boolean>getProperty(FormProperty.FORM_FIELD_SELECTED))) {
selectedOptions.add(option);
}
} else {
List<IRenderer> subSelectedOptions = getOptionsMarkedSelected(option);
selectedOptions.addAll(subSelectedOptions);
} else if (Boolean.TRUE.equals(option.<Boolean>getProperty(FormProperty.FORM_FIELD_SELECTED))) {
selectedOptions.add(option);
}
}
return selectedOptions;
}

private LayoutResult makeLayoutResultFull(LayoutArea layoutArea, LayoutResult layoutResult) {
IRenderer splitRenderer = layoutResult.getSplitRenderer() != null ? layoutResult.getSplitRenderer() : this;
IRenderer splitRenderer = layoutResult.getSplitRenderer() == null ? this : layoutResult.getSplitRenderer();
if (occupiedArea == null) {
occupiedArea = new LayoutArea(layoutArea.getPageNumber(), new Rectangle(layoutArea.getBBox().getLeft(), layoutArea.getBBox().getTop(), 0, 0));
occupiedArea = new LayoutArea(layoutArea.getPageNumber(),
new Rectangle(layoutArea.getBBox().getLeft(), layoutArea.getBBox().getTop(), 0, 0));
}
layoutResult = new LayoutResult(LayoutResult.FULL, occupiedArea, splitRenderer, null);
return layoutResult;
}

static boolean isOptGroupRenderer(IRenderer renderer) {
return renderer.hasProperty(FormProperty.FORM_FIELD_LABEL) && !renderer.hasProperty(FormProperty.FORM_FIELD_SELECTED);
return renderer.hasProperty(FormProperty.FORM_FIELD_LABEL) &&
!renderer.hasProperty(FormProperty.FORM_FIELD_SELECTED);
}

static boolean isOptionRenderer(IRenderer child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,6 @@ IRenderer createParagraphRenderer(String defaultValue) {
return paragraph.createRendererSubTree();
}

/**
* Adjust number of content lines.
*
* @param lines the lines that need to be rendered
* @param bBox the bounding box
* @param rows the desired number of lines
*/
void adjustNumberOfContentLines(List<LineRenderer> lines, Rectangle bBox, int rows) {
if (lines.size() != rows) {
float rowsHeight = getHeightRowsBased(lines, bBox, rows);
adjustNumberOfContentLines(lines, bBox, rows, rowsHeight);
}
}

/**
* Adjust number of content lines.
*
* @param lines the lines that need to be rendered
* @param bBox the bounding box
* @param height the desired height of content
*/
void adjustNumberOfContentLines(List<LineRenderer> lines, Rectangle bBox, float height) {
float averageLineHeight = bBox.getHeight() / lines.size();
int visibleLinesNumber = (int) Math.ceil(height / averageLineHeight);
adjustNumberOfContentLines(lines, bBox, visibleLinesNumber, height);
}

/**
* Applies the default field properties.
*
Expand Down Expand Up @@ -167,7 +140,7 @@ void updatePdfFont(ParagraphRenderer renderer) {

//The width based on cols of textarea and size of input doesn't affected by box sizing, so we emulate it here
float updateHtmlColsSizeBasedWidth(float width) {
if (BoxSizingPropertyValue.BORDER_BOX.equals(this.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING))) {
if (BoxSizingPropertyValue.BORDER_BOX == this.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING)) {
Rectangle dummy = new Rectangle(width, 0);
applyBorderBox(dummy, true);
applyPaddings(dummy, true);
Expand All @@ -176,7 +149,35 @@ float updateHtmlColsSizeBasedWidth(float width) {
return width;
}

private static void adjustNumberOfContentLines(List<LineRenderer> lines, Rectangle bBox, int linesNumber, float height) {
/**
* Adjust number of content lines.
*
* @param lines the lines that need to be rendered
* @param bBox the bounding box
* @param rows the desired number of lines
*/
void adjustNumberOfContentLines(List<LineRenderer> lines, Rectangle bBox, int rows) {
if (lines.size() != rows) {
float rowsHeight = getHeightRowsBased(lines, bBox, rows);
adjustNumberOfContentLines(lines, bBox, rows, rowsHeight);
}
}

/**
* Adjust number of content lines.
*
* @param lines the lines that need to be rendered
* @param bBox the bounding box
* @param height the desired height of content
*/
void adjustNumberOfContentLines(List<LineRenderer> lines, Rectangle bBox, float height) {
float averageLineHeight = bBox.getHeight() / lines.size();
int visibleLinesNumber = (int) Math.ceil(height / averageLineHeight);
adjustNumberOfContentLines(lines, bBox, visibleLinesNumber, height);
}

private static void adjustNumberOfContentLines(List<LineRenderer> lines, Rectangle bBox,
int linesNumber, float height) {
bBox.moveUp(bBox.getHeight() - height);
bBox.setHeight(height);
if (lines.size() > linesNumber) {
Expand Down
Loading

0 comments on commit 3b19a34

Please sign in to comment.