Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBerendsen committed Sep 8, 2024
1 parent c0dd6fd commit 3ca406d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 84 deletions.
111 changes: 53 additions & 58 deletions src/main/java/nl/digitalekabeltelevisie/controller/KVP.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ public class KVP{

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

public enum NUMBER_DISPLAY {
DECIMAL,
HEX,
BOTH
}

public enum STRING_DISPLAY{
PLAIN, // plain
JAVASCRIPT, // javascript escaped (quotes removed)
HTML_FRAGMENTS, // HTML fragments '<' and '&' escaped,
HTML_AWT // AWT HTML (html segments include <html> tag, otherwise plain text
}

public enum FIELD_TYPE {
STRING,
BYTES,
INT,
LONG,

LABEL, //used for a node that has no value associated with it
DVBSTRING,
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

BIGINT
}

/**
* maximum length of byte[] to be shown in JTree, only has meaning in case data is array of byte.
*/
Expand All @@ -86,43 +112,12 @@ public record DetailView(DetailSource detailSource, String label) {}
private DVBString dvbStringValue;
private BigInteger bigIntegerValue;

private static byte numberDisplay = 1; // 1 - decimal, 2 -
// hex, 3 both
// ,example "0xA0
// (160)"
public static final byte NUMBER_DISPLAY_DECIMAL = 1;
public static final byte NUMBER_DISPLAY_HEX = 2;
public static final byte NUMBER_DISPLAY_BOTH = 3;

private static byte stringDisplay = 1;
// 1 - plain,
// 2 - javascript escaped (quotes removed),
// 3 - HTML fragments '<' and '&' escaped,
// 4 - AWT HTML (html segments include <html> tag, otherwise plain text
public static final byte STRING_DISPLAY_PLAIN = 1;
public static final byte STRING_DISPLAY_JAVASCRIPT = 2;
public static final byte STRING_DISPLAY_HTML_FRAGMENTS = 3;
public static final byte STRING_DISPLAY_HTML_AWT = 4;

private FIELD_TYPE fieldType = FIELD_TYPE.STRING;

public enum FIELD_TYPE {
STRING,
BYTES,
INT,
LONG,

LABEL, //used for a node that has no value associated with it
DVBSTRING,
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

BIGINT

}
private static NUMBER_DISPLAY numberDisplay = NUMBER_DISPLAY.DECIMAL; // 1 - decimal, 2 -
private static STRING_DISPLAY stringDisplay = STRING_DISPLAY.PLAIN;

private FIELD_TYPE fieldType;

private List<DetailView> detailViews = new ArrayList<>();


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

public String toString(byte stringFormat, byte numberFormat) {
public String toString(STRING_DISPLAY stringFormat, NUMBER_DISPLAY numberFormat) {
StringBuilder b = new StringBuilder(label);
if(!labelAppend.isEmpty()) {
b.append(labelAppend);
Expand All @@ -280,20 +275,20 @@ public String toString(byte stringFormat, byte numberFormat) {
if ((fieldType != FIELD_TYPE.LABEL)&&(fieldType != FIELD_TYPE.HTML)) {
appendValueAfterLabel(numberFormat, b);
}
if((fieldType==FIELD_TYPE.HTML)&&(STRING_DISPLAY_PLAIN!=stringFormat)){
if((fieldType==FIELD_TYPE.HTML)&&(STRING_DISPLAY.PLAIN!=stringFormat)){
b = replacePlainLabelWithHTML(stringFormat);
}
if (stringFormat == STRING_DISPLAY_JAVASCRIPT) {
if (stringFormat == STRING_DISPLAY.JAVASCRIPT) {
return b.toString().replace("\"", "\\\"").replace("\'", "\\\'");
}
return b.toString();
}


private StringBuilder replacePlainLabelWithHTML(byte stringFormat) {
if(stringFormat==STRING_DISPLAY_HTML_AWT){
private StringBuilder replacePlainLabelWithHTML(STRING_DISPLAY stringFormat) {
if(stringFormat==STRING_DISPLAY.HTML_AWT){
return new StringBuilder("<html>").append(value).append("</html>");
}else if(stringFormat==STRING_DISPLAY_HTML_FRAGMENTS){
}else if(stringFormat==STRING_DISPLAY.HTML_FRAGMENTS){
return new StringBuilder(value);
}
return new StringBuilder();
Expand All @@ -303,7 +298,7 @@ private StringBuilder replacePlainLabelWithHTML(byte stringFormat) {
* @param numberFormat
* @param b
*/
private void appendValueAfterLabel(byte numberFormat, StringBuilder b) {
private void appendValueAfterLabel(NUMBER_DISPLAY numberFormat, StringBuilder b) {
b.append(": ");
switch (fieldType){
case STRING:
Expand Down Expand Up @@ -366,10 +361,10 @@ private void appendString(StringBuilder b) {
* @param numberFormat
* @param b
*/
private void appendLong(byte numberFormat, StringBuilder b) {
if (numberFormat == NUMBER_DISPLAY_DECIMAL) {
private void appendLong(NUMBER_DISPLAY numberFormat, StringBuilder b) {
if (numberFormat == NUMBER_DISPLAY.DECIMAL) {
b.append(longValue);
} else if (numberFormat == NUMBER_DISPLAY_HEX) {
} else if (numberFormat == NUMBER_DISPLAY.HEX) {
b.append("0x").append(Long.toHexString(longValue).toUpperCase());
} else { // assume both to be safe
b.append("0x").append(Long.toHexString(longValue).toUpperCase()).append(" (").append(longValue)
Expand All @@ -381,50 +376,50 @@ private void appendLong(byte numberFormat, StringBuilder b) {
* @param numberFormat
* @param b
*/
private void appendInteger(byte numberFormat, StringBuilder b) {
if (numberFormat == NUMBER_DISPLAY_DECIMAL) {
private void appendInteger(NUMBER_DISPLAY numberFormat, StringBuilder b) {
if (numberFormat == NUMBER_DISPLAY.DECIMAL) {
b.append(intValue);
} else if (numberFormat == NUMBER_DISPLAY_HEX) {
} else if (numberFormat == NUMBER_DISPLAY.HEX) {
b.append("0x").append(Integer.toHexString(intValue).toUpperCase());
} else { // assume both to be safe
b.append(getHexAndDecimalFormattedString(intValue));
}
}

private void appendBigInteger(byte numberFormat, StringBuilder b) {
if (numberFormat == NUMBER_DISPLAY_DECIMAL) {
private void appendBigInteger(NUMBER_DISPLAY numberFormat, StringBuilder b) {
if (numberFormat == NUMBER_DISPLAY.DECIMAL) {
b.append(bigIntegerValue.toString());
} else if (numberFormat == NUMBER_DISPLAY_HEX) {
} else if (numberFormat == NUMBER_DISPLAY.HEX) {
b.append("0x").append(bigIntegerValue.toString(16).toUpperCase());
} else { // assume both to be safe
b.append(getHexAndDecimalFormattedString(bigIntegerValue));
}
}


public static byte getNumberDisplay() {
public static NUMBER_DISPLAY getNumberDisplay() {
return numberDisplay;
}

public static void setNumberDisplay(byte intDisplay) {
public static void setNumberDisplay(NUMBER_DISPLAY intDisplay) {
numberDisplay = intDisplay;
}

public static String formatInt(int intValue) {
if (numberDisplay == NUMBER_DISPLAY_DECIMAL) {
if (numberDisplay == NUMBER_DISPLAY.DECIMAL) {
return Integer.toString(intValue);
} else if (numberDisplay == NUMBER_DISPLAY_HEX) {
} else if (numberDisplay == NUMBER_DISPLAY.HEX) {
return ("0x") + Integer.toHexString(intValue).toUpperCase();
} else { // assume both to be safe
return ("0x" + Integer.toHexString(intValue).toUpperCase()) + " (" + Integer.toString(intValue) + (")");
return ("0x" + Integer.toHexString(intValue).toUpperCase()) + " (" + intValue + (")");
}
}

public static byte getStringDisplay() {
public static STRING_DISPLAY getStringDisplay() {
return stringDisplay;
}

public static void setStringDisplay(byte stringDisplay) {
public static void setStringDisplay(STRING_DISPLAY stringDisplay) {
KVP.stringDisplay = stringDisplay;
}
/**
Expand All @@ -436,7 +431,7 @@ public void setImageSource(ImageSource imageSource) {
}

public String getPlainText(){
return toString(STRING_DISPLAY_PLAIN, NUMBER_DISPLAY_BOTH);
return toString(STRING_DISPLAY.PLAIN, NUMBER_DISPLAY.BOTH);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ public static Map<Integer, GeneralPidHandler> determinePidHandlers(final String[

public void run() {

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

}
Expand Down Expand Up @@ -661,8 +661,8 @@ public void setTreeView(final DVBtree treeView) {
private void updatePIDLists(final TransportStream tStream, final PIDDialog pDialog){

final ViewContext viewConfig = new ViewContext();
final ArrayList<ChartLabel> used = new ArrayList<>();
final ArrayList<ChartLabel> notUsed = new ArrayList<>();
final List<ChartLabel> used = new ArrayList<>();
final List<ChartLabel> notUsed = new ArrayList<>();

if(tStream!=null){
final short[] used_pids=tStream.getUsedPids();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nl/digitalekabeltelevisie/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ public static StringBuilder getChildrenAsHTML(DefaultMutableTreeNode dmtn) {
Object next = children.nextElement();
if(next instanceof DefaultMutableTreeNode child){
KVP chKVP = (KVP)child.getUserObject();
res.append(chKVP.toString(KVP.STRING_DISPLAY_HTML_FRAGMENTS, KVP.NUMBER_DISPLAY_BOTH)).append(lineSep);
res.append(chKVP.toString(KVP.STRING_DISPLAY.HTML_FRAGMENTS, KVP.NUMBER_DISPLAY.BOTH)).append(lineSep);
if(!child.isLeaf()){
res.append(getChildrenAsHTML(child));
}
Expand Down
42 changes: 21 additions & 21 deletions src/test/java/nl/digitalekabeltelevisie/controller/KVPTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void testKVPLabelValueDescription() {
@Test
public void testKVPStringIntString() {
KVP kvp = new KVP("LabelForInt",42,"Explanation");
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
assertEquals("LabelForInt: 42 => Explanation",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
assertEquals("LabelForInt: 0x2A (42) => Explanation",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
assertEquals("LabelForInt: 0x2A => Explanation",kvp.toString());

assertEquals("LabelForInt:42",kvp.getCrumb());
Expand All @@ -84,44 +84,44 @@ public void testKVPStringIntString() {
@Test
public void testKVPStringLongString() {
KVP kvp = new KVP("LabelForLong",142L,"Loooooong Explanation");
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
assertEquals("LabelForLong: 142 => Loooooong Explanation",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
assertEquals("LabelForLong: 0x8E (142) => Loooooong Explanation",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
assertEquals("LabelForLong: 0x8E => Loooooong Explanation",kvp.toString());
kvp = new KVP("LabelForLong",142L,null);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
assertEquals("LabelForLong: 142",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
assertEquals("LabelForLong: 0x8E (142)",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
assertEquals("LabelForLong: 0x8E",kvp.toString());
}

@Test
public void testKVPStringBooleanString() {
KVP kvp = new KVP("BooleanLabel",true,"Boooolean Explanation");
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
assertEquals("BooleanLabel: 0x1 => Boooolean Explanation",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_BOTH);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.BOTH);
assertEquals("BooleanLabel: 0x1 (1) => Boooolean Explanation",kvp.toString());
kvp = new KVP("BooleanLabel",false,"Boooolean false Explanation");
assertEquals("BooleanLabel: 0x0 (0) => Boooolean false Explanation",kvp.toString());
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);
assertEquals("BooleanLabel: 0 => Boooolean false Explanation",kvp.toString());
}

@Test
public void testHtmlLabelString() {
KVP kvp = new KVP("<h1>Head</h1>","<b>bold label?</b>");
KVP.setStringDisplay(KVP.STRING_DISPLAY_HTML_AWT);
KVP.setStringDisplay(KVP.STRING_DISPLAY.HTML_AWT);
assertEquals("<html><h1>Head</h1></html>",kvp.toString());
KVP.setStringDisplay(KVP.STRING_DISPLAY_PLAIN);
KVP.setStringDisplay(KVP.STRING_DISPLAY.PLAIN);
assertEquals("<b>bold label?</b>",kvp.toString());
KVP.setStringDisplay(KVP.STRING_DISPLAY_HTML_FRAGMENTS);
KVP.setStringDisplay(KVP.STRING_DISPLAY.HTML_FRAGMENTS);
assertEquals("<h1>Head</h1>",kvp.toString());
KVP.setStringDisplay(KVP.STRING_DISPLAY_JAVASCRIPT);
KVP.setStringDisplay(KVP.STRING_DISPLAY.JAVASCRIPT);
assertEquals("",kvp.toString());
}

Expand Down Expand Up @@ -200,18 +200,18 @@ public void testKVPStringBigIntegerString() {
BigInteger bg = new BigInteger(biggy);

KVP kvp = new KVP("Big Number",bg,null);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_DECIMAL);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.DECIMAL);

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

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

KVP.setNumberDisplay(KVP.NUMBER_DISPLAY_HEX);
KVP.setNumberDisplay(KVP.NUMBER_DISPLAY.HEX);
assertEquals("Big Number: 0x18EE90FF6C4FE9FCDC157A600", kvp.toString());

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

Expand Down

0 comments on commit 3ca406d

Please sign in to comment.