Skip to content

Commit 3ca406d

Browse files
committed
refactoring
1 parent c0dd6fd commit 3ca406d

File tree

4 files changed

+79
-84
lines changed

4 files changed

+79
-84
lines changed

src/main/java/nl/digitalekabeltelevisie/controller/KVP.java

+53-58
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ public class KVP{
7070

7171
public record DetailView(DetailSource detailSource, String label) {}
7272

73+
public enum NUMBER_DISPLAY {
74+
DECIMAL,
75+
HEX,
76+
BOTH
77+
}
78+
79+
public enum STRING_DISPLAY{
80+
PLAIN, // plain
81+
JAVASCRIPT, // javascript escaped (quotes removed)
82+
HTML_FRAGMENTS, // HTML fragments '<' and '&' escaped,
83+
HTML_AWT // AWT HTML (html segments include <html> tag, otherwise plain text
84+
}
85+
86+
public enum FIELD_TYPE {
87+
STRING,
88+
BYTES,
89+
INT,
90+
LONG,
91+
92+
LABEL, //used for a node that has no value associated with it
93+
DVBSTRING,
94+
HTML, // used for a node that has no separate value associated with , but a HTML fragment as value for presentation where possible, has to have a plain text alternative
95+
96+
BIGINT
97+
}
98+
7399
/**
74100
* maximum length of byte[] to be shown in JTree, only has meaning in case data is array of byte.
75101
*/
@@ -86,43 +112,12 @@ public record DetailView(DetailSource detailSource, String label) {}
86112
private DVBString dvbStringValue;
87113
private BigInteger bigIntegerValue;
88114

89-
private static byte numberDisplay = 1; // 1 - decimal, 2 -
90-
// hex, 3 both
91-
// ,example "0xA0
92-
// (160)"
93-
public static final byte NUMBER_DISPLAY_DECIMAL = 1;
94-
public static final byte NUMBER_DISPLAY_HEX = 2;
95-
public static final byte NUMBER_DISPLAY_BOTH = 3;
96-
97-
private static byte stringDisplay = 1;
98-
// 1 - plain,
99-
// 2 - javascript escaped (quotes removed),
100-
// 3 - HTML fragments '<' and '&' escaped,
101-
// 4 - AWT HTML (html segments include <html> tag, otherwise plain text
102-
public static final byte STRING_DISPLAY_PLAIN = 1;
103-
public static final byte STRING_DISPLAY_JAVASCRIPT = 2;
104-
public static final byte STRING_DISPLAY_HTML_FRAGMENTS = 3;
105-
public static final byte STRING_DISPLAY_HTML_AWT = 4;
106-
107-
private FIELD_TYPE fieldType = FIELD_TYPE.STRING;
108-
109-
public enum FIELD_TYPE {
110-
STRING,
111-
BYTES,
112-
INT,
113-
LONG,
114-
115-
LABEL, //used for a node that has no value associated with it
116-
DVBSTRING,
117-
HTML, // used for a node that has no separate value associated with , but a HTML fragment as value for presentation where possible, has to have a plain text alternative
118-
119-
BIGINT
120-
121-
}
115+
private static NUMBER_DISPLAY numberDisplay = NUMBER_DISPLAY.DECIMAL; // 1 - decimal, 2 -
116+
private static STRING_DISPLAY stringDisplay = STRING_DISPLAY.PLAIN;
122117

118+
private FIELD_TYPE fieldType;
123119

124120
private List<DetailView> detailViews = new ArrayList<>();
125-
126121

127122
/**
128123
* crumb's are used to be able to jump to any place in the tree, based on a url-like
@@ -271,7 +266,7 @@ public String toString() {
271266
return toString(stringDisplay,numberDisplay);
272267
}
273268

274-
public String toString(byte stringFormat, byte numberFormat) {
269+
public String toString(STRING_DISPLAY stringFormat, NUMBER_DISPLAY numberFormat) {
275270
StringBuilder b = new StringBuilder(label);
276271
if(!labelAppend.isEmpty()) {
277272
b.append(labelAppend);
@@ -280,20 +275,20 @@ public String toString(byte stringFormat, byte numberFormat) {
280275
if ((fieldType != FIELD_TYPE.LABEL)&&(fieldType != FIELD_TYPE.HTML)) {
281276
appendValueAfterLabel(numberFormat, b);
282277
}
283-
if((fieldType==FIELD_TYPE.HTML)&&(STRING_DISPLAY_PLAIN!=stringFormat)){
278+
if((fieldType==FIELD_TYPE.HTML)&&(STRING_DISPLAY.PLAIN!=stringFormat)){
284279
b = replacePlainLabelWithHTML(stringFormat);
285280
}
286-
if (stringFormat == STRING_DISPLAY_JAVASCRIPT) {
281+
if (stringFormat == STRING_DISPLAY.JAVASCRIPT) {
287282
return b.toString().replace("\"", "\\\"").replace("\'", "\\\'");
288283
}
289284
return b.toString();
290285
}
291286

292287

293-
private StringBuilder replacePlainLabelWithHTML(byte stringFormat) {
294-
if(stringFormat==STRING_DISPLAY_HTML_AWT){
288+
private StringBuilder replacePlainLabelWithHTML(STRING_DISPLAY stringFormat) {
289+
if(stringFormat==STRING_DISPLAY.HTML_AWT){
295290
return new StringBuilder("<html>").append(value).append("</html>");
296-
}else if(stringFormat==STRING_DISPLAY_HTML_FRAGMENTS){
291+
}else if(stringFormat==STRING_DISPLAY.HTML_FRAGMENTS){
297292
return new StringBuilder(value);
298293
}
299294
return new StringBuilder();
@@ -303,7 +298,7 @@ private StringBuilder replacePlainLabelWithHTML(byte stringFormat) {
303298
* @param numberFormat
304299
* @param b
305300
*/
306-
private void appendValueAfterLabel(byte numberFormat, StringBuilder b) {
301+
private void appendValueAfterLabel(NUMBER_DISPLAY numberFormat, StringBuilder b) {
307302
b.append(": ");
308303
switch (fieldType){
309304
case STRING:
@@ -366,10 +361,10 @@ private void appendString(StringBuilder b) {
366361
* @param numberFormat
367362
* @param b
368363
*/
369-
private void appendLong(byte numberFormat, StringBuilder b) {
370-
if (numberFormat == NUMBER_DISPLAY_DECIMAL) {
364+
private void appendLong(NUMBER_DISPLAY numberFormat, StringBuilder b) {
365+
if (numberFormat == NUMBER_DISPLAY.DECIMAL) {
371366
b.append(longValue);
372-
} else if (numberFormat == NUMBER_DISPLAY_HEX) {
367+
} else if (numberFormat == NUMBER_DISPLAY.HEX) {
373368
b.append("0x").append(Long.toHexString(longValue).toUpperCase());
374369
} else { // assume both to be safe
375370
b.append("0x").append(Long.toHexString(longValue).toUpperCase()).append(" (").append(longValue)
@@ -381,50 +376,50 @@ private void appendLong(byte numberFormat, StringBuilder b) {
381376
* @param numberFormat
382377
* @param b
383378
*/
384-
private void appendInteger(byte numberFormat, StringBuilder b) {
385-
if (numberFormat == NUMBER_DISPLAY_DECIMAL) {
379+
private void appendInteger(NUMBER_DISPLAY numberFormat, StringBuilder b) {
380+
if (numberFormat == NUMBER_DISPLAY.DECIMAL) {
386381
b.append(intValue);
387-
} else if (numberFormat == NUMBER_DISPLAY_HEX) {
382+
} else if (numberFormat == NUMBER_DISPLAY.HEX) {
388383
b.append("0x").append(Integer.toHexString(intValue).toUpperCase());
389384
} else { // assume both to be safe
390385
b.append(getHexAndDecimalFormattedString(intValue));
391386
}
392387
}
393388

394-
private void appendBigInteger(byte numberFormat, StringBuilder b) {
395-
if (numberFormat == NUMBER_DISPLAY_DECIMAL) {
389+
private void appendBigInteger(NUMBER_DISPLAY numberFormat, StringBuilder b) {
390+
if (numberFormat == NUMBER_DISPLAY.DECIMAL) {
396391
b.append(bigIntegerValue.toString());
397-
} else if (numberFormat == NUMBER_DISPLAY_HEX) {
392+
} else if (numberFormat == NUMBER_DISPLAY.HEX) {
398393
b.append("0x").append(bigIntegerValue.toString(16).toUpperCase());
399394
} else { // assume both to be safe
400395
b.append(getHexAndDecimalFormattedString(bigIntegerValue));
401396
}
402397
}
403398

404399

405-
public static byte getNumberDisplay() {
400+
public static NUMBER_DISPLAY getNumberDisplay() {
406401
return numberDisplay;
407402
}
408403

409-
public static void setNumberDisplay(byte intDisplay) {
404+
public static void setNumberDisplay(NUMBER_DISPLAY intDisplay) {
410405
numberDisplay = intDisplay;
411406
}
412407

413408
public static String formatInt(int intValue) {
414-
if (numberDisplay == NUMBER_DISPLAY_DECIMAL) {
409+
if (numberDisplay == NUMBER_DISPLAY.DECIMAL) {
415410
return Integer.toString(intValue);
416-
} else if (numberDisplay == NUMBER_DISPLAY_HEX) {
411+
} else if (numberDisplay == NUMBER_DISPLAY.HEX) {
417412
return ("0x") + Integer.toHexString(intValue).toUpperCase();
418413
} else { // assume both to be safe
419-
return ("0x" + Integer.toHexString(intValue).toUpperCase()) + " (" + Integer.toString(intValue) + (")");
414+
return ("0x" + Integer.toHexString(intValue).toUpperCase()) + " (" + intValue + (")");
420415
}
421416
}
422417

423-
public static byte getStringDisplay() {
418+
public static STRING_DISPLAY getStringDisplay() {
424419
return stringDisplay;
425420
}
426421

427-
public static void setStringDisplay(byte stringDisplay) {
422+
public static void setStringDisplay(STRING_DISPLAY stringDisplay) {
428423
KVP.stringDisplay = stringDisplay;
429424
}
430425
/**
@@ -436,7 +431,7 @@ public void setImageSource(ImageSource imageSource) {
436431
}
437432

438433
public String getPlainText(){
439-
return toString(STRING_DISPLAY_PLAIN, NUMBER_DISPLAY_BOTH);
434+
return toString(STRING_DISPLAY.PLAIN, NUMBER_DISPLAY.BOTH);
440435
}
441436

442437

src/main/java/nl/digitalekabeltelevisie/main/DVBinspector.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ public static Map<Integer, GeneralPidHandler> determinePidHandlers(final String[
216216

217217
public void run() {
218218

219-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
220-
KVP.setStringDisplay(KVP.STRING_DISPLAY_HTML_AWT);
219+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
220+
KVP.setStringDisplay(KVP.STRING_DISPLAY.HTML_AWT);
221221
javax.swing.SwingUtilities.invokeLater(() -> createAndShowGUI(transportStream));
222222

223223
}
@@ -661,8 +661,8 @@ public void setTreeView(final DVBtree treeView) {
661661
private void updatePIDLists(final TransportStream tStream, final PIDDialog pDialog){
662662

663663
final ViewContext viewConfig = new ViewContext();
664-
final ArrayList<ChartLabel> used = new ArrayList<>();
665-
final ArrayList<ChartLabel> notUsed = new ArrayList<>();
664+
final List<ChartLabel> used = new ArrayList<>();
665+
final List<ChartLabel> notUsed = new ArrayList<>();
666666

667667
if(tStream!=null){
668668
final short[] used_pids=tStream.getUsedPids();

src/main/java/nl/digitalekabeltelevisie/util/Utils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ public static StringBuilder getChildrenAsHTML(DefaultMutableTreeNode dmtn) {
20012001
Object next = children.nextElement();
20022002
if(next instanceof DefaultMutableTreeNode child){
20032003
KVP chKVP = (KVP)child.getUserObject();
2004-
res.append(chKVP.toString(KVP.STRING_DISPLAY_HTML_FRAGMENTS, KVP.NUMBER_DISPLAY_BOTH)).append(lineSep);
2004+
res.append(chKVP.toString(KVP.STRING_DISPLAY.HTML_FRAGMENTS, KVP.NUMBER_DISPLAY.BOTH)).append(lineSep);
20052005
if(!child.isLeaf()){
20062006
res.append(getChildrenAsHTML(child));
20072007
}

src/test/java/nl/digitalekabeltelevisie/controller/KVPTest.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public void testKVPLabelValueDescription() {
7171
@Test
7272
public void testKVPStringIntString() {
7373
KVP kvp = new KVP("LabelForInt",42,"Explanation");
74-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
74+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
7575
assertEquals("LabelForInt: 42 => Explanation",kvp.toString());
76-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
76+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
7777
assertEquals("LabelForInt: 0x2A (42) => Explanation",kvp.toString());
78-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
78+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
7979
assertEquals("LabelForInt: 0x2A => Explanation",kvp.toString());
8080

8181
assertEquals("LabelForInt:42",kvp.getCrumb());
@@ -84,44 +84,44 @@ public void testKVPStringIntString() {
8484
@Test
8585
public void testKVPStringLongString() {
8686
KVP kvp = new KVP("LabelForLong",142L,"Loooooong Explanation");
87-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
87+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
8888
assertEquals("LabelForLong: 142 => Loooooong Explanation",kvp.toString());
89-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
89+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
9090
assertEquals("LabelForLong: 0x8E (142) => Loooooong Explanation",kvp.toString());
91-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
91+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
9292
assertEquals("LabelForLong: 0x8E => Loooooong Explanation",kvp.toString());
9393
kvp = new KVP("LabelForLong",142L,null);
94-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
94+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
9595
assertEquals("LabelForLong: 142",kvp.toString());
96-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
96+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
9797
assertEquals("LabelForLong: 0x8E (142)",kvp.toString());
98-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
98+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
9999
assertEquals("LabelForLong: 0x8E",kvp.toString());
100100
}
101101

102102
@Test
103103
public void testKVPStringBooleanString() {
104104
KVP kvp = new KVP("BooleanLabel",true,"Boooolean Explanation");
105-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
105+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
106106
assertEquals("BooleanLabel: 0x1 => Boooolean Explanation",kvp.toString());
107-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
107+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
108108
assertEquals("BooleanLabel: 0x1 (1) => Boooolean Explanation",kvp.toString());
109109
kvp = new KVP("BooleanLabel",false,"Boooolean false Explanation");
110110
assertEquals("BooleanLabel: 0x0 (0) => Boooolean false Explanation",kvp.toString());
111-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
111+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
112112
assertEquals("BooleanLabel: 0 => Boooolean false Explanation",kvp.toString());
113113
}
114114

115115
@Test
116116
public void testHtmlLabelString() {
117117
KVP kvp = new KVP("<h1>Head</h1>","<b>bold label?</b>");
118-
KVP.setStringDisplay(KVP.STRING_DISPLAY_HTML_AWT);
118+
KVP.setStringDisplay(KVP.STRING_DISPLAY.HTML_AWT);
119119
assertEquals("<html><h1>Head</h1></html>",kvp.toString());
120-
KVP.setStringDisplay(KVP.STRING_DISPLAY_PLAIN);
120+
KVP.setStringDisplay(KVP.STRING_DISPLAY.PLAIN);
121121
assertEquals("<b>bold label?</b>",kvp.toString());
122-
KVP.setStringDisplay(KVP.STRING_DISPLAY_HTML_FRAGMENTS);
122+
KVP.setStringDisplay(KVP.STRING_DISPLAY.HTML_FRAGMENTS);
123123
assertEquals("<h1>Head</h1>",kvp.toString());
124-
KVP.setStringDisplay(KVP.STRING_DISPLAY_JAVASCRIPT);
124+
KVP.setStringDisplay(KVP.STRING_DISPLAY.JAVASCRIPT);
125125
assertEquals("",kvp.toString());
126126
}
127127

@@ -200,18 +200,18 @@ public void testKVPStringBigIntegerString() {
200200
BigInteger bg = new BigInteger(biggy);
201201

202202
KVP kvp = new KVP("Big Number",bg,null);
203-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
203+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
204204

205205
assertEquals("Big Number: "+biggy, kvp.toString());
206206

207207
// test non global formatting
208-
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600", kvp.toString(KVP.STRING_DISPLAY_PLAIN,KVP.NUMBER_DISPLAY_HEX));
209-
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600 ("+biggy +")" , kvp.toString(KVP.STRING_DISPLAY_PLAIN,KVP.NUMBER_DISPLAY_BOTH));
208+
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600", kvp.toString(KVP.STRING_DISPLAY.PLAIN,KVP.NUMBER_DISPLAY.HEX));
209+
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600 ("+biggy +")" , kvp.toString(KVP.STRING_DISPLAY.PLAIN,KVP.NUMBER_DISPLAY.BOTH));
210210

211-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
211+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
212212
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600", kvp.toString());
213213

214-
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
214+
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
215215
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600 ("+biggy +")" , kvp.toString());
216216
}
217217

0 commit comments

Comments
 (0)