Skip to content

Commit 1e96f94

Browse files
Merge pull request #482 from twistedsquare/master
Added some documentation to some small classes
2 parents 45d262f + 0e8e607 commit 1e96f94

4 files changed

Lines changed: 185 additions & 1 deletion

File tree

richtextfx/src/main/java/org/fxmisc/richtext/TextExt.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
import javafx.scene.shape.StrokeLineCap;
1818
import javafx.scene.text.Text;
1919

20+
/**
21+
* A class which adds some more styleable properties to JavaFX's {@link Text} class.
22+
*
23+
* The extra items can be styled using the properties (and accessors/mutators) or via CSS.
24+
* Each property is documented with its CSS property. Each CSS property begins with the "-rtfx"
25+
* prefix.
26+
*
27+
* <p>Note that the underline properties specified here are orthogonal to the {@link #underlineProperty()} inherited
28+
* from {@link Text}. The underline properties defined here in {@link TextExt} will cause an underline to be
29+
* drawn if {@link #underlineWidthProperty()} is non-null and greater than zero, regardless of
30+
* the value of {@link #underlineProperty()}.</p>
31+
*/
2032
public class TextExt extends Text {
2133

2234
private final StyleableObjectProperty<Paint> backgroundColor = new StyleableObjectProperty<Paint>(null) {
@@ -133,28 +145,90 @@ public void setBackgroundColor(Paint fill) {
133145
backgroundColor.set(fill);
134146
}
135147

148+
/**
149+
* The background color of the section of text. By default, JavaFX doesn't
150+
* support a background for Text (as it is a Shape item), but RichTextFX
151+
* does support drawing a different background for different sections of text.
152+
*
153+
* <p>Note that this is actually a Paint type, so you can specify gradient or image fills
154+
* rather than a flat colour. But due to line wrapping, it's possible that
155+
* the fill may be used multiple times on separate lines even for the same
156+
* segment of text.</p>
157+
*
158+
* Can be styled from CSS using the "-rtfx-background-color" property.
159+
*/
136160
public ObjectProperty<Paint> backgroundColorProperty() {
137161
return backgroundColor;
138162
}
139163

140164
// Color of the text underline (-fx-underline is already defined by JavaFX)
141165
public Paint getUnderlineColor() { return underlineColor.get(); }
142166
public void setUnderlineColor(Paint fill) { underlineColor.set(fill); }
167+
168+
/**
169+
* The underline color of the section of text.
170+
*
171+
* <p>Note that this is actually a Paint type, so you can specify gradient or image fills
172+
* rather than a flat colour. But due to line wrapping, it's possible that
173+
* the fill may be used multiple times on separate lines even for the
174+
* same segment of text.</p>
175+
*
176+
* Can be styled from CSS using the "-rtfx-underline-color" property
177+
* (not to be confused with JavaFX's separate "-fx-underline" property).
178+
*
179+
* <p>Note that the underline properties specified here are orthogonal to the {@link #underlineProperty()} inherited
180+
* from {@link Text}. The underline properties defined here in {@link TextExt} will cause an underline to be
181+
* drawn if {@link #underlineWidthProperty()} is non-null and greater than zero, regardless of
182+
* the value of {@link #underlineProperty()}.</p>
183+
*/
143184
public ObjectProperty<Paint> underlineColorProperty() { return underlineColor; }
144185

145186
// Width of the text underline
146187
public Number getUnderlineWidth() { return underlineWidth.get(); }
147188
public void setUnderlineWidth(Number width) { underlineWidth.set(width); }
189+
190+
/**
191+
* The width of the underline for a section of text. If null or zero,
192+
* the underline will not be drawn.
193+
*
194+
* Can be styled from CSS using the "-rtfx-underline-width" property.
195+
*
196+
* <p>Note that the underline properties specified here are orthogonal to the {@link #underlineProperty()} inherited
197+
* from {@link Text}. The underline properties defined here in {@link TextExt} will cause an underline to be
198+
* drawn if {@link #underlineWidthProperty()} is non-null and greater than zero, regardless of
199+
* the value of {@link #underlineProperty()}.</p>
200+
*/
148201
public ObjectProperty<Number> underlineWidthProperty() { return underlineWidth; }
149202

150203
// Dash array for the text underline
151204
public Number[] getUnderlineDashArray() { return underlineDashArray.get(); }
152205
public void setUnderlineDashArray(Number[] dashArray) { underlineDashArray.set(dashArray); }
206+
207+
/**
208+
* The dash array used for drawing the underline for a section of text.
209+
*
210+
* Can be styled from CSS using the "-rtfx-underline-dash-array" property.
211+
*
212+
* <p>Note that the underline properties specified here are orthogonal to the {@link #underlineProperty()} inherited
213+
* from {@link Text}. The underline properties defined here in {@link TextExt} will cause an underline to be
214+
* drawn if {@link #underlineWidthProperty()} is non-null and greater than zero, regardless of
215+
* the value of {@link #underlineProperty()}.</p>
216+
*/
153217
public ObjectProperty<Number[]> underlineDashArrayProperty() { return underlineDashArray; }
154218

155219
// The end cap style of each dash in a dashed underline
156220
public StrokeLineCap getUnderlineCap() { return underlineCap.get(); }
157221
public void setUnderlineCap(StrokeLineCap cap) { underlineCap.set(cap); }
222+
/**
223+
* The end cap style used for drawing each dash in a dashed underline for a section of text.
224+
*
225+
* Can be styled from CSS using the "-rtfx-underline-cap" property.
226+
*
227+
* <p>Note that the underline properties specified here are orthogonal to the {@link #underlineProperty()} inherited
228+
* from {@link Text}. The underline properties defined here in {@link TextExt} will cause an underline to be
229+
* drawn if {@link #underlineWidthProperty()} is non-null and greater than zero, regardless of
230+
* the value of {@link #underlineProperty()}.</p>
231+
*/
158232
public ObjectProperty<StrokeLineCap> underlineCapProperty() { return underlineCap; }
159233

160234
private static class StyleableProperties {

richtextfx/src/main/java/org/fxmisc/richtext/model/Paragraph.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313

1414
import org.fxmisc.richtext.model.TwoDimensional.Position;
1515

16+
/**
17+
* This is one Paragraph of the document. Depending on whether the text is wrapped,
18+
* it corresponds to a single line or it can also span multiple lines. A Paragraph
19+
* contains of a list of SEG objects which make up the individual segments of the
20+
* Paragraph. By providing a specific segment object and an associated segment
21+
* operations object, all required data and the necessary operations on this data
22+
* for a single segment can be provided. For example, {@link StyledText} is a segment
23+
* type which is used in {@link org.fxmisc.richtext.StyledTextArea}, a text area which can render simple
24+
* text only (which is already sufficient to implement all kinds of code editors).
25+
*
26+
* <p>For more complex requirements (for example, when images shall be part of the
27+
* document) a different segment type must be provided (which can make use of
28+
* {@code StyledText<S>} for the text part and add another segment type for images).
29+
* <b>Note that Paragraph is an immutable class</b> - to modify a Paragraph, a new
30+
* Paragraph object must be created. Paragraph itself contains some methods which
31+
* take care of this, such as concat(), which appends some Paragraph to the current
32+
* one and returns a new Paragraph.</p>
33+
*
34+
* @param <PS> The type of the paragraph style.
35+
* @param <SEG> The type of the content segments in the paragraph (e.g. {@link StyledText}).
36+
* Every paragraph, even an empty paragraph, must have at least one SEG object
37+
* (even if that SEG object itself represents an empty segment).
38+
* @param <S> The type of the style of individual segments.
39+
*/
1640
public final class Paragraph<PS, SEG, S> {
1741

1842
@SafeVarargs
@@ -166,6 +190,15 @@ public Paragraph<PS, SEG, S> delete(int start, int end) {
166190
return trim(start).concat(subSequence(end));
167191
}
168192

193+
/**
194+
* Restyles every segment in the paragraph to have the given style.
195+
*
196+
* Note: because Paragraph is immutable, this method returns a new Paragraph.
197+
* The current Paragraph is unchanged.
198+
*
199+
* @param style The new style for each segment in the paragraph.
200+
* @return The new paragraph with the restyled segments.
201+
*/
169202
public Paragraph<PS, SEG, S> restyle(S style) {
170203
List<SEG> segs = new ArrayList<>();
171204
Iterator<SEG> it = segments.iterator();
@@ -218,6 +251,16 @@ public Paragraph<PS, SEG, S> restyle(int from, StyleSpans<? extends S> styleSpan
218251
return left.concat(newMiddle).concat(right);
219252
}
220253

254+
/**
255+
* Creates a new Paragraph which has the same contents as the current Paragraph,
256+
* but the given paragraph style.
257+
*
258+
* Note that because Paragraph is immutable, a new Paragraph is returned.
259+
* Despite the setX name, the current object is unchanged.
260+
*
261+
* @param paragraphStyle The new paragraph style
262+
* @return A new paragraph with the same segment contents, but a new paragraph style.
263+
*/
221264
public Paragraph<PS, SEG, S> setParagraphStyle(PS paragraphStyle) {
222265
return new Paragraph<>(paragraphStyle, segmentOps, segments);
223266
}
@@ -333,6 +376,10 @@ public String toString() {
333376
"]";
334377
}
335378

379+
/**
380+
* Two paragraphs are defined to be equal if they have the same style (as defined by
381+
* PS.equals) and the same list of segments (as defined by SEG.equals).
382+
*/
336383
@Override
337384
public boolean equals(Object other) {
338385
if(other instanceof Paragraph) {

richtextfx/src/main/java/org/fxmisc/richtext/model/StyledText.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@
66
import java.util.Objects;
77
import java.util.Optional;
88

9+
/**
10+
* A String with a single style of type S.
11+
*
12+
* This is a simple class suitable for use as the SEG type in the other classes
13+
* such as {@link Paragraph}, {@link org.fxmisc.richtext.GenericStyledArea}, {@link StyledDocument}, etc.
14+
*
15+
* This class is immutable.
16+
*
17+
* @param <S> The type of the style of the text.
18+
*/
919
public class StyledText<S> {
1020

21+
/**
22+
* An implementation of TextOps for StyledText. Useful for passing to the constructor
23+
* of {@link org.fxmisc.richtext.GenericStyledArea} and similar classes if you are using this class as the SEG type.
24+
*/
1125
public static <S> TextOps<StyledText<S>, S> textOps() {
1226
return new TextOps<StyledText<S>, S>() {
1327

@@ -67,6 +81,12 @@ public StyledText<S> create(String text, S style) {
6781
};
6882
}
6983

84+
/**
85+
* A codec which allows serialisation of this class to/from a data stream.
86+
*
87+
* Because S may be any type, you must pass a codec for it. If your style
88+
* is String or Color, you can use {@link Codec#STRING_CODEC}/{@link Codec#COLOR_CODEC} respectively.
89+
*/
7090
public static <S> Codec<StyledText<S>> codec(Codec<S> styleCodec) {
7191
return new Codec<StyledText<S>>() {
7292

@@ -94,15 +114,28 @@ public StyledText<S> decode(DataInputStream is) throws IOException {
94114

95115
private final String text;
96116

117+
/**
118+
* The text content of this piece of styled text.
119+
*/
97120
public String getText() { return text; }
98121

99122
private final S style;
123+
124+
/**
125+
* The style of this piece of styled text.
126+
*/
100127
public S getStyle() { return style; }
101128

129+
/**
130+
* Creates a new StyledText with the same content but the given style.
131+
*/
102132
public StyledText<S> setStyle(S style) {
103133
return new StyledText<>(text, style);
104134
}
105135

136+
/**
137+
* Creates a new StyledText with the given text content, and style.
138+
*/
106139
public StyledText(String text, S style) {
107140
this.text = text;
108141
this.style = style;

richtextfx/src/main/java/org/fxmisc/richtext/model/TwoDimensional.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
package org.fxmisc.richtext.model;
22

3-
3+
/**
4+
* TwoDimensional is an interface for any item which can be navigated
5+
* in two dimensions. A two dimensional position has a major dimension
6+
* (e.g. paragraph number in a document) and a minor dimension (e.g.
7+
* segment index or column number within a paragraph). Generally,
8+
* documents are not rectangular (paragraphs in a document are of differing lengths),
9+
* so the valid values of the minor dimension depends on the major dimension.
10+
*/
411
public interface TwoDimensional {
512

613
enum Bias {
714
Forward,
815
Backward,
916
}
1017

18+
/**
19+
* A two dimensional position, with a major offset (e.g. paragraph
20+
* number within a document) and a minor dimension (e.g. segment index or column
21+
* number within a paragraph). Major and minor positions begin at 0.
22+
*/
1123
interface Position {
1224

25+
/**
26+
* The TwoDimensional object that this position refers to.
27+
*/
1328
TwoDimensional getTargetObject();
1429

30+
/**
31+
* The major dimension, e.g. paragraph number within a document
32+
*/
1533
int getMajor();
1634

35+
/**
36+
* The minor dimension, e.g. segment index or column offset within a paragraph.
37+
*/
1738
int getMinor();
1839

1940
/**
@@ -23,10 +44,19 @@ interface Position {
2344
*/
2445
boolean sameAs(Position other);
2546

47+
/**
48+
* Returns a new position which clamps the minor position to be valid
49+
* given the major position. (i.e. if the position is beyond the
50+
* end of a given paragraph, moves the position back to the end of the paragraph).
51+
*/
2652
public Position clamp();
2753

2854
public Position offsetBy(int offset, Bias bias);
2955

56+
/**
57+
* Converts this position to an overall offset within the original
58+
* TwoDimensional item (which getTargetObject refers to)
59+
*/
3060
int toOffset();
3161

3262
}

0 commit comments

Comments
 (0)