-
Notifications
You must be signed in to change notification settings - Fork 5
/
TextBindingCustom.java
149 lines (128 loc) · 5.23 KB
/
TextBindingCustom.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package org.genericsystem.ir.app.gui.utils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.Doc.DocTimestamp;
import org.genericsystem.cv.model.Doc.RefreshTimestamp;
import org.genericsystem.cv.model.ImgFilter.ImgFilterInstance;
import org.genericsystem.cv.model.MeanLevenshtein;
import org.genericsystem.cv.model.ModelTools;
import org.genericsystem.cv.model.Score.ScoreInstance;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
import org.genericsystem.defaults.tools.RxJavaHelpers;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.context.TextBinding;
import io.reactivex.Observable;
import javafx.beans.property.SimpleObjectProperty;
/**
* This class contains all the {@link TextBinding} needed across the app.
*
* @author Pierrik Lassalas
*/
public class TextBindingCustom {
/*
* === DOC CLASS AND DOC ===
*/
public static class DOC_CLASS_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
return Observable.just("Doc class: " + String.valueOf(context.getGeneric().getValue()));
}
}
/*
* === TIMESTAMP ===
*/
public static class LAST_DOC_UPDATE_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
DocInstance currentDoc = (DocInstance) context.getGeneric();
Root root = currentDoc.getRoot();
DocTimestamp docTimestamp = root.find(DocTimestamp.class);
SimpleObjectProperty<Generic> timeStamp = new SimpleObjectProperty<>(docTimestamp.getDocTimestamp(currentDoc));
return Observable.just(null == timeStamp || null == timeStamp.get() ? "n/a" : ModelTools.formatDate((Long) timeStamp.get().getValue()));
}
}
public static class LAST_REFRESH_UPDATE_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
DocInstance currentDoc = (DocInstance) context.getGeneric();
Root root = currentDoc.getRoot();
RefreshTimestamp refreshTimestamp = root.find(RefreshTimestamp.class);
return refreshTimestamp.getInstances(currentDoc).getAddsObservable().map(timeStamp -> formatTimeStamp(timeStamp))
.startWith(formatTimeStamp(refreshTimestamp.getRefreshTimestamp(currentDoc)));
}
private String formatTimeStamp(Generic timeStamp) {
return timeStamp == null ? "Last update: none" : "Last update: " + ModelTools.formatDate((Long) timeStamp.getValue());
}
}
/*
* === ZONES ===
*/
public static class ZONE_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
return Observable.just("Zone " + ((ZoneTextInstance) context.getGeneric()).getZone().getZoneNum());
}
}
public static class ZONE_LABEL2 implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
ScoreInstance score = (ScoreInstance) context.getGeneric();
return Observable.just(String.valueOf(score.getZone().getZoneNum().getValue()));
}
}
public static class ZONE_TEXT implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
return Observable.just(StringEscapeUtils.escapeHtml4(String.valueOf(context.getGeneric().getValue())));
}
}
/*
* === OCR ===
*/
public static class OCR_TEXT implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
ZoneText zt = (ZoneText) context.getGeneric().getRoot().find(ZoneText.class);
ImgFilterInstance ifi = (ImgFilterInstance) context.getGeneric();
ZoneTextInstance zti = (ZoneTextInstance) context.getGenerics()[1];
DocInstance doc = zti.getDoc();
ZoneTextInstance text = zt.getZoneText(doc, zti.getZone(), ifi);
return Observable.just(text == null ? "" : StringEscapeUtils.escapeHtml4(String.valueOf(text.getValue())));
}
}
/*
* === SCORES ===
*/
public static class SCORE_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
ScoreInstance score = (ScoreInstance) context.getGeneric();
SimpleObjectProperty<Generic> scoreProperty = new SimpleObjectProperty<>(score);
return RxJavaHelpers.valuesOf(scoreProperty).map(scoreInstance -> String.valueOf(scoreInstance.getValue()));
}
}
public static class MEAN_LEV_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
ScoreInstance score = (ScoreInstance) context.getGeneric();
MeanLevenshtein meanLevenshtein = score.getRoot().find(MeanLevenshtein.class);
SimpleObjectProperty<Generic> mlProperty = new SimpleObjectProperty<>(meanLevenshtein.getMeanLev(score));
return RxJavaHelpers.valuesOf(mlProperty).map(ml -> String.valueOf(ml.getValue()));
}
}
/*
* === IMG FILTERS ===
*/
public static class IMG_FILTER_LABEL implements TextBinding {
@Override
public Observable<String> apply(Context context, Tag tag) {
ScoreInstance score = (ScoreInstance) context.getGeneric();
ImgFilterInstance imgFilterInstance = score.getImgFilter();
return Observable.just(String.valueOf(imgFilterInstance.getValue()));
}
}
}