1 package com.github.mygreen.cellformatter;
2
3 import java.util.List;
4 import java.util.Locale;
5 import java.util.concurrent.CopyOnWriteArrayList;
6
7 import com.github.mygreen.cellformatter.term.Term;
8
9
10
11
12
13
14
15
16
17 public class ConditionTextFormatter extends ConditionFormatter {
18
19
20
21
22 private List<Term<String>> terms = new CopyOnWriteArrayList<>();
23
24 public ConditionTextFormatter(final String pattern) {
25 super(pattern);
26 }
27
28 @Override
29 public FormatterType getType() {
30 return FormatterType.Text;
31 }
32
33
34
35
36 @Override
37 public boolean isMatch(final CommonCell cell) {
38 return cell.isText() || cell.isBoolean();
39 }
40
41 @Override
42 public CellFormatResult format(final CommonCell cell, final Locale runtimeLocale) {
43
44 final String value;
45 if(cell.isBoolean()) {
46 value = String.valueOf(cell.getBooleanCellValue()).toUpperCase();
47 } else {
48 value = cell.getTextCellValue();
49 }
50
51 final StringBuilder sb = new StringBuilder();
52
53 for(Term<String> term : terms) {
54 sb.append(term.format(value, getLocale(), runtimeLocale));
55 }
56
57 String text = sb.toString();
58
59 final CellFormatResultormatResult.html#CellFormatResult">CellFormatResult result = new CellFormatResult();
60 if(cell.isBoolean()) {
61 result.setValue(cell.getBooleanCellValue());
62 result.setCellType(FormatCellType.Boolean);
63 } else {
64 result.setValue(value);
65 result.setCellType(FormatCellType.Text);
66 }
67
68 result.setText(text);
69 result.setTextColor(getColor());
70 result.setSectionPattern(getPattern());
71
72 return result;
73
74 }
75
76
77
78
79
80 public List<Term<String>> getTerms() {
81 return terms;
82 }
83
84
85
86
87
88 public void addTerm(Term<String> term) {
89 this.terms.add(term);
90 }
91
92 }