Skip to content

Commit fb56876

Browse files
committed
Move Property from enum to a set of constants in order to allow more flexible customization of elements and renderers
It wasn't possible to add custom Property enum entries before, but now it is possible to use arbitrary integers as property ids DEVSIX-598
1 parent 580739d commit fb56876

File tree

17 files changed

+272
-232
lines changed

17 files changed

+272
-232
lines changed

layout/src/main/java/com/itextpdf/layout/ElementPropertyContainer.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,54 +73,54 @@ This file is part of the iText (R) project.
7373
*/
7474
public abstract class ElementPropertyContainer<Type extends ElementPropertyContainer> implements IPropertyContainer<Type> {
7575

76-
protected Map<Property, Object> properties = new HashMap<>();
76+
protected Map<Integer, Object> properties = new HashMap<>();
7777

7878
@Override
79-
public <T extends Type> T setProperty(Property property, Object value) {
79+
public <T extends Type> T setProperty(int property, Object value) {
8080
properties.put(property, value);
8181
return (T) this;
8282
}
8383

8484
@Override
85-
public boolean hasProperty(Property property) {
85+
public boolean hasProperty(int property) {
8686
return hasOwnProperty(property);
8787
}
8888

8989
@Override
90-
public boolean hasOwnProperty(Property property) {
90+
public boolean hasOwnProperty(int property) {
9191
return properties.containsKey(property);
9292
}
9393

9494
@Override
95-
public void deleteOwnProperty(Property property) {
95+
public void deleteOwnProperty(int property) {
9696
properties.remove(property);
9797
}
9898

9999
@Override
100-
public <T> T getProperty(Property property) {
100+
public <T> T getProperty(int property) {
101101
return getOwnProperty(property);
102102
}
103103

104104
@Override
105-
public <T> T getOwnProperty(Property property) {
105+
public <T> T getOwnProperty(int property) {
106106
return (T) properties.get(property);
107107
}
108108

109109
@Override
110-
public <T> T getDefaultProperty(Property property) {
110+
public <T> T getDefaultProperty(int property) {
111111
switch (property) {
112-
case MARGIN_TOP:
113-
case MARGIN_RIGHT:
114-
case MARGIN_BOTTOM:
115-
case MARGIN_LEFT:
116-
case PADDING_TOP:
117-
case PADDING_RIGHT:
118-
case PADDING_BOTTOM:
119-
case PADDING_LEFT:
112+
case Property.MARGIN_TOP:
113+
case Property.MARGIN_RIGHT:
114+
case Property.MARGIN_BOTTOM:
115+
case Property.MARGIN_LEFT:
116+
case Property.PADDING_TOP:
117+
case Property.PADDING_RIGHT:
118+
case Property.PADDING_BOTTOM:
119+
case Property.PADDING_LEFT:
120120
return (T) Float.valueOf(0);
121-
case POSITION:
121+
case Property.POSITION:
122122
return (T)Integer.valueOf(LayoutPosition.STATIC);
123-
case FORCED_PLACEMENT:
123+
case Property.FORCED_PLACEMENT:
124124
return (T) Boolean.FALSE;
125125
default:
126126
return null;

layout/src/main/java/com/itextpdf/layout/IPropertyContainer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@ This file is part of the iText (R) project.
5555
public interface IPropertyContainer<Type extends IPropertyContainer> {
5656

5757
/**
58-
* Checks if this entity has the specified property. Compared to {@link #hasOwnProperty(Property)},
58+
* Checks if this entity has the specified property. Compared to {@link #hasOwnProperty(int)},
5959
* this method can check parent's properties, styles, etc, depending on the origin of the instance
6060
* @param property the property to be checked
6161
* @return {@code true} if this instance has given property, {@code false} otherwise
6262
*/
63-
boolean hasProperty(Property property);
63+
boolean hasProperty(int property);
6464

6565
/**
6666
* Checks if this entity has the specified property, i.e. if it was set to this very element earlier
6767
* @param property the property to be checked
6868
* @return {@code true} if this instance has given own property, {@code false} otherwise
6969
*/
70-
boolean hasOwnProperty(Property property);
70+
boolean hasOwnProperty(int property);
7171

7272
/**
73-
* Gets the property from this entity. Compared to {@link #getOwnProperty(Property)},
73+
* Gets the property from this entity. Compared to {@link #getOwnProperty(int)},
7474
* this method can check parent's properties, styles, etc, depending on the origin of the instance
7575
* @param <T> the return type associated with the property
7676
* @param property the property to be retrieved
7777
* @return the value of the given property. {@code null} will be returned if the property value was not found
7878
*/
79-
<T> T getProperty(Property property);
79+
<T> T getProperty(int property);
8080

8181
/**
8282
* Gets own property from this entity. The property must have been set earlier to this entity.
@@ -85,15 +85,15 @@ public interface IPropertyContainer<Type extends IPropertyContainer> {
8585
* @param property the property to be retrieved
8686
* @return the value of the given own property. {@code null} will be returned if the property value was not found
8787
*/
88-
<T> T getOwnProperty(Property property);
88+
<T> T getOwnProperty(int property);
8989

9090
/**
9191
* Gets the default property from this entity.
9292
* @param <T> the return type associated with the property
9393
* @param property the property to be retrieved
9494
* @return the default property value. If the default property is not defined, {@code null} will be returned
9595
*/
96-
<T> T getDefaultProperty(Property property);
96+
<T> T getDefaultProperty(int property);
9797

9898
/**
9999
* Sets a property for this entity.
@@ -102,11 +102,11 @@ public interface IPropertyContainer<Type extends IPropertyContainer> {
102102
* @param value the value of the property
103103
* @return this element
104104
*/
105-
<T extends Type> T setProperty(Property property, Object value);
105+
<T extends Type> T setProperty(int property, Object value);
106106

107107
/**
108108
* Deletes the own property of this entity.
109109
* @param property the property to be deleted
110110
*/
111-
void deleteOwnProperty(Property property);
111+
void deleteOwnProperty(int property);
112112
}

layout/src/main/java/com/itextpdf/layout/RootElement.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ This file is part of the iText (R) project.
6868

6969
import java.io.IOException;
7070
import java.util.ArrayList;
71-
import java.util.EnumMap;
71+
import java.util.HashMap;
7272
import java.util.List;
7373
import java.util.Map;
7474

@@ -83,7 +83,7 @@ public abstract class RootElement<Type extends RootElement> implements IProperty
8383
protected PdfDocument pdfDocument;
8484

8585
protected List<IElement> childElements = new ArrayList<>();
86-
protected Map<Property, Object> properties = new EnumMap<>(Property.class);
86+
protected Map<Integer, Object> properties = new HashMap<>();
8787

8888
protected PdfFont defaultFont;
8989
protected ISplitCharacters defaultSplitCharacters;
@@ -115,50 +115,50 @@ public Type add(Image image) {
115115
}
116116

117117
@Override
118-
public boolean hasProperty(Property property) {
118+
public boolean hasProperty(int property) {
119119
return hasOwnProperty(property);
120120
}
121121

122122
@Override
123-
public boolean hasOwnProperty(Property property) {
123+
public boolean hasOwnProperty(int property) {
124124
return properties.containsKey(property);
125125
}
126126

127127
@Override
128-
public <T> T getProperty(Property property) {
128+
public <T> T getProperty(int property) {
129129
return getOwnProperty(property);
130130
}
131131

132132
@Override
133-
public <T> T getOwnProperty(Property property) {
133+
public <T> T getOwnProperty(int property) {
134134
return (T) properties.get(property);
135135
}
136136

137137
@Override
138-
public <T> T getDefaultProperty(Property property) {
138+
public <T> T getDefaultProperty(int property) {
139139
try {
140140
switch (property) {
141-
case FONT:
141+
case Property.FONT:
142142
if (defaultFont == null) {
143143
defaultFont = PdfFontFactory.createFont();
144144
}
145145
return (T) defaultFont;
146-
case SPLIT_CHARACTERS:
146+
case Property.SPLIT_CHARACTERS:
147147
if (defaultSplitCharacters == null) {
148148
defaultSplitCharacters = new DefaultSplitCharacters();
149149
}
150150
return (T) defaultSplitCharacters;
151-
case FONT_SIZE:
151+
case Property.FONT_SIZE:
152152
return (T) Integer.valueOf(12);
153-
case TEXT_RENDERING_MODE:
153+
case Property.TEXT_RENDERING_MODE:
154154
return (T) Integer.valueOf(PdfCanvasConstants.TextRenderingMode.FILL);
155-
case TEXT_RISE:
155+
case Property.TEXT_RISE:
156156
return (T) Float.valueOf(0);
157-
case SPACING_RATIO:
157+
case Property.SPACING_RATIO:
158158
return (T) Float.valueOf(0.75f);
159-
case FONT_KERNING:
159+
case Property.FONT_KERNING:
160160
return (T) FontKerning.NO;
161-
case BASE_DIRECTION:
161+
case Property.BASE_DIRECTION:
162162
return (T) BaseDirection.NO_BIDI;
163163
default:
164164
return null;
@@ -169,12 +169,12 @@ public <T> T getDefaultProperty(Property property) {
169169
}
170170

171171
@Override
172-
public void deleteOwnProperty(Property property) {
172+
public void deleteOwnProperty(int property) {
173173
properties.remove(property);
174174
}
175175

176176
@Override
177-
public Type setProperty(Property property, Object value) {
177+
public Type setProperty(int property, Object value) {
178178
properties.put(property, value);
179179
return (Type) this;
180180
}

layout/src/main/java/com/itextpdf/layout/element/AbstractElement.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.kernel.pdf.PdfName;
4848
import com.itextpdf.kernel.pdf.tagutils.IAccessibleElement;
4949
import com.itextpdf.layout.ElementPropertyContainer;
50-
import com.itextpdf.layout.property.Property;
5150
import com.itextpdf.layout.Style;
5251
import com.itextpdf.layout.renderer.IRenderer;
5352

@@ -93,7 +92,7 @@ public IRenderer createRendererSubTree() {
9392
}
9493

9594
@Override
96-
public boolean hasProperty(Property property) {
95+
public boolean hasProperty(int property) {
9796
boolean hasProperty = super.hasProperty(property);
9897
if (styles != null && styles.size() > 0 && !hasProperty) {
9998
for (Style style : styles) {
@@ -107,7 +106,7 @@ public boolean hasProperty(Property property) {
107106
}
108107

109108
@Override
110-
public <T> T getProperty(Property property) {
109+
public <T> T getProperty(int property) {
111110
Object result = super.getProperty(property);
112111
if (styles != null && styles.size() > 0 && result == null && !super.hasProperty(property)) {
113112
for (Style style : styles) {

layout/src/main/java/com/itextpdf/layout/element/BlockElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ public T setSpacingRatio(float ratio) {
272272
}
273273

274274
@Override
275-
public <T> T getDefaultProperty(Property property) {
275+
public <T> T getDefaultProperty(int property) {
276276
switch (property) {
277-
case KEEP_TOGETHER:
277+
case Property.KEEP_TOGETHER:
278278
return (T) Boolean.valueOf(false);
279279
default:
280280
return super.getDefaultProperty(property);

layout/src/main/java/com/itextpdf/layout/element/Cell.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ public Cell clone(boolean includeContent) {
199199
}
200200

201201
@Override
202-
public <T> T getDefaultProperty(Property property) {
202+
public <T> T getDefaultProperty(int property) {
203203
switch (property) {
204-
case BORDER:
204+
case Property.BORDER:
205205
return (T) DEFAULT_BORDER;
206-
case PADDING_BOTTOM:
207-
case PADDING_LEFT:
208-
case PADDING_RIGHT:
209-
case PADDING_TOP:
206+
case Property.PADDING_BOTTOM:
207+
case Property.PADDING_LEFT:
208+
case Property.PADDING_RIGHT:
209+
case Property.PADDING_TOP:
210210
return (T) Float.valueOf(2);
211211
default:
212212
return super.getDefaultProperty(property);

layout/src/main/java/com/itextpdf/layout/element/Image.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,12 @@ public Image setFixedPosition(int pageNumber, float x, float y) {
385385
}
386386

387387
@Override
388-
public <T> T getDefaultProperty(Property property) {
388+
public <T> T getDefaultProperty(int property) {
389389
switch (property) {
390-
case AUTO_SCALE:
390+
case Property.AUTO_SCALE:
391391
return (T) Boolean.valueOf(false);
392-
case HORIZONTAL_SCALING:
393-
case VERTICAL_SCALING:
392+
case Property.HORIZONTAL_SCALING:
393+
case Property.VERTICAL_SCALING:
394394
return (T) Float.valueOf(1);
395395
default:
396396
return super.getDefaultProperty(property);

layout/src/main/java/com/itextpdf/layout/element/List.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ public List(ListNumberingType listNumberingType) {
8282
}
8383

8484
@Override
85-
public <T> T getDefaultProperty(Property property) {
85+
public <T> T getDefaultProperty(int property) {
8686
switch (property) {
87-
case LIST_SYMBOL_PRE_TEXT:
87+
case Property.LIST_SYMBOL_PRE_TEXT:
8888
return (T) "";
89-
case LIST_SYMBOL_POST_TEXT:
89+
case Property.LIST_SYMBOL_POST_TEXT:
9090
return (T) ". ";
9191
default:
9292
return super.getDefaultProperty(property);

layout/src/main/java/com/itextpdf/layout/element/Paragraph.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,16 @@ public <T extends Paragraph> T removeTabStop(float tabStopPosition) {
163163
}
164164

165165
@Override
166-
public <T> T getDefaultProperty(Property property) {
166+
public <T> T getDefaultProperty(int property) {
167167
switch (property) {
168-
case LEADING:
168+
case Property.LEADING:
169169
return (T) new Leading(Leading.MULTIPLIED, childElements.size() == 1 && childElements.get(0) instanceof Image ? 1 : 1.35f);
170-
case FIRST_LINE_INDENT:
170+
case Property.FIRST_LINE_INDENT:
171171
return (T) Float.valueOf(0);
172-
case MARGIN_TOP:
173-
case MARGIN_BOTTOM:
172+
case Property.MARGIN_TOP:
173+
case Property.MARGIN_BOTTOM:
174174
return (T) Float.valueOf(4);
175-
case TAB_DEFAULT:
175+
case Property.TAB_DEFAULT:
176176
return (T) Float.valueOf(50);
177177
default:
178178
return super.getDefaultProperty(property);

layout/src/main/java/com/itextpdf/layout/element/Text.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ public Text(String text) {
7272
}
7373

7474
@Override
75-
public <T> T getDefaultProperty(Property property) {
75+
public <T> T getDefaultProperty(int property) {
7676
switch (property) {
77-
case HORIZONTAL_SCALING:
78-
case VERTICAL_SCALING:
77+
case Property.HORIZONTAL_SCALING:
78+
case Property.VERTICAL_SCALING:
7979
return (T) new Float(1);
8080
default:
8181
return super.getDefaultProperty(property);

0 commit comments

Comments
 (0)