36
36
import java .util .List ;
37
37
38
38
import javax .swing .JMenuItem ;
39
+ import javax .swing .tree .DefaultMutableTreeNode ;
39
40
40
41
import nl .digitalekabeltelevisie .gui .*;
41
42
65
66
* @author Eric Berendsen
66
67
*
67
68
*/
68
- public class KVP {
69
+ public class KVP extends DefaultMutableTreeNode {
69
70
70
71
71
72
public record DetailView (DetailSource detailSource , String label ) {}
@@ -91,7 +92,6 @@ public enum FIELD_TYPE {
91
92
92
93
LABEL , //used for a node that has no value associated with it
93
94
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
95
96
96
BIGINT
97
97
}
@@ -101,7 +101,7 @@ public enum FIELD_TYPE {
101
101
*/
102
102
private static final int BYTE_DATA_MAX_LEN = 100 ;
103
103
private String label ;
104
- private String value ;
104
+ private String stringValue ;
105
105
private String description ;
106
106
107
107
private int intValue ;
@@ -132,163 +132,155 @@ public enum FIELD_TYPE {
132
132
private JMenuItem subMenu ;
133
133
private Object owner ;
134
134
private String labelAppend = "" ;
135
+ private String htmlLabel ;
135
136
136
137
public KVP (String label ) {
137
138
this .label = label ;
138
139
this .fieldType = FIELD_TYPE .LABEL ;
139
140
}
140
141
141
- public KVP (String label , ImageSource imageSource ) {
142
+ public KVP (String label , String stringValue ) {
142
143
this .label = label ;
143
- this .fieldType = FIELD_TYPE .LABEL ;
144
- detailViews .add (new DetailView (imageSource ,"" ));
145
- }
146
- public KVP (String label , String value , String description ) {
147
- this .label = label ;
148
- if (value ==null ){ // just a label
149
- this .fieldType = FIELD_TYPE .LABEL ;
150
- }else {
151
- this .value = value ;
152
- this .description = description ;
153
- this .fieldType = FIELD_TYPE .STRING ;
144
+ this .stringValue = stringValue ;
145
+ this .fieldType = FIELD_TYPE .STRING ;
154
146
155
- }
156
147
}
157
148
158
- public KVP (String label , int value , String description ) {
149
+ public KVP (String label , String stringValue , String description ) {
150
+ this (label ,stringValue );
151
+ setDescription (description );
152
+
153
+ }
154
+
155
+ public KVP (String label , int intValue ) {
159
156
this .label = label ;
160
- this .intValue = value ;
161
- this .description = description ;
157
+ this .intValue = intValue ;
162
158
this .fieldType = FIELD_TYPE .INT ;
163
159
}
164
160
165
- public KVP (String label , long value , String description ) {
166
- this .label = label ;
167
- this .longValue = value ;
168
- this .description = description ;
169
- this .fieldType = FIELD_TYPE .LONG ;
161
+ public KVP (String label , int intValue , String description ) {
162
+ this (label ,intValue );
163
+ setDescription (description );
170
164
}
171
165
172
- public KVP (String label , boolean value , String description ) {
173
- this (label , value ? 1 : 0 , description );
166
+ public KVP (String label , long longValue ) {
167
+ this .label = label ;
168
+ this .longValue = longValue ;
169
+ this .fieldType = FIELD_TYPE .LONG ;
174
170
}
175
171
176
- public KVP (String html , String label ) {
177
- this .value = html ;
178
- this .label = label ; // text representation of the HTML string
179
- this .fieldType = FIELD_TYPE .HTML ;
172
+ public KVP (String label , long longValue , String description ) {
173
+ this (label ,longValue );
174
+ setDescription (description );
180
175
}
181
176
182
-
183
-
184
- /**
185
- * @param label
186
- * @param value
187
- * @param description
188
- */
189
- public KVP (String label , byte [] value , String description ) {
190
- this .label = label ;
191
- this .byteValue = value ;
177
+ public KVP (String label , byte [] byteArray ) {
178
+ this .label = label ;
179
+ this .byteValue = byteArray ;
192
180
this .byteStart = 0 ;
193
- this .byteLen = value .length ;
194
- this .description = description ;
181
+ this .byteLen = byteArray .length ;
195
182
this .fieldType = FIELD_TYPE .BYTES ;
196
- detailViews . add ( new DetailView (( HTMLSource )() -> getHTMLHexview (byteValue , byteStart , byteLen ),"Hex View" ) );
183
+ addHTMLSource (() -> getHTMLHexview (byteValue , byteStart , byteLen ), "Hex View" );
197
184
}
198
185
199
- public KVP (String label , byte [] value , int offset , int len , String description ) {
200
- this .label = label ;
201
- this .byteValue = value ;
186
+ public KVP (String label , byte [] byteArray , String description ) {
187
+ this (label , byteArray );
188
+ setDescription (description );
189
+ }
190
+
191
+ public KVP (String label , byte [] byteArray , int offset , int len ) {
192
+ this .label = label ;
193
+ this .byteValue = byteArray ;
202
194
this .byteStart = offset ;
203
195
this .byteLen = len ;
204
- this .description = description ;
205
196
this .fieldType = FIELD_TYPE .BYTES ;
206
- detailViews . add ( new DetailView (( HTMLSource )() -> getHTMLHexview (byteValue , byteStart , byteLen ),"Hex View" ) );
197
+ addHTMLSource (() -> getHTMLHexview (byteValue , byteStart , byteLen ), "Hex View" );
207
198
}
208
199
209
- public KVP (String label , DVBString value , String description ) {
210
- this .label = label ;
211
- this .dvbStringValue = value ;
212
- this .description = description ;
213
- this .fieldType = FIELD_TYPE .DVBSTRING ;
200
+ public KVP (String label , byte [] byteArray , int offset , int len , String description ) {
201
+ this (label , byteArray , offset , len );
202
+ setDescription (description );
214
203
}
215
204
216
- public KVP (String label , HTMLSource htmlSource ) {
205
+ public KVP (String label , DVBString dvbStringValue ) {
217
206
this .label = label ;
218
- this .fieldType = FIELD_TYPE .LABEL ;
219
- detailViews .add (new DetailView (htmlSource ,"" ));
207
+ this .dvbStringValue = dvbStringValue ;
208
+ this .fieldType = FIELD_TYPE .DVBSTRING ;
209
+ this .add (new KVP ("encoding" , dvbStringValue .getEncodingString ()));
210
+ this .add (new KVP ("length" , dvbStringValue .getLength ()));
211
+ this .addHTMLSource (
212
+ () ->"<b>Encoding:</b> " + dvbStringValue .getEncodingString ()
213
+ + "<br><br><b>Data:</b><br>" + getHTMLHexview (dvbStringValue .getData (), dvbStringValue .getOffset () + 1 , dvbStringValue .getLength ())
214
+ + "<br><b>Formatted:</b><br>" + dvbStringValue .toEscapedHTML (),
215
+ "DVB String" );
220
216
}
221
217
222
- public KVP (String label , BigInteger value , String description ) {
218
+
219
+ public KVP (String label , BigInteger bigIntegerValue ) {
223
220
224
221
this .label = label ;
225
- this .bigIntegerValue = value ;
222
+ this .bigIntegerValue = bigIntegerValue ;
226
223
this .fieldType = FIELD_TYPE .BIGINT ;
227
- this .description = description ;
228
224
}
229
225
226
+ public KVP (String label , BigInteger bigIntegerValue , String description ) {
227
+ this (label , bigIntegerValue );
228
+ setDescription (description );
229
+ }
230
+
230
231
public String getDescription () {
231
232
return description ;
232
233
}
233
234
234
- public void setDescription (String description ) {
235
+ public KVP setDescription (String description ) {
235
236
this .description = description ;
237
+ return this ;
236
238
}
237
239
238
240
public String getLabel () {
239
241
return label ;
240
242
}
241
243
242
- public void setLabel (String label ) {
244
+ public KVP setLabel (String label ) {
243
245
this .label = label ;
246
+ return this ;
244
247
}
245
248
246
249
// put appends in separate String, so original label is constant and available for path
247
250
public void appendLabel (String labelAppend ) {
248
251
this .labelAppend = this .labelAppend + labelAppend ;
249
252
}
250
253
251
- public String getValue () {
252
- return value ;
253
- }
254
-
255
- public void setValue (String value ) {
256
- this .value = value ;
257
- }
258
254
259
255
@ Override
260
256
public String toString () {
261
257
return toString (stringDisplay ,numberDisplay );
262
258
}
263
259
264
260
public String toString (STRING_DISPLAY stringFormat , NUMBER_DISPLAY numberFormat ) {
265
- StringBuilder b = new StringBuilder (label );
261
+ StringBuilder b = new StringBuilder ();
262
+ if ((htmlLabel != null ) && (STRING_DISPLAY .PLAIN != stringFormat )) {
263
+ b .append (htmlLabel );
264
+ } else {
265
+ b .append (label );
266
+ }
266
267
if (!labelAppend .isEmpty ()) {
267
268
b .append (labelAppend );
268
269
}
269
270
270
- if (( fieldType != FIELD_TYPE .LABEL )&&( fieldType != FIELD_TYPE . HTML ) ) {
271
+ if (fieldType != FIELD_TYPE .LABEL ) {
271
272
appendValueAfterLabel (numberFormat , b );
272
273
}
273
- if ((fieldType ==FIELD_TYPE .HTML )&&(STRING_DISPLAY .PLAIN !=stringFormat )){
274
- b = replacePlainLabelWithHTML (stringFormat );
275
- }
276
274
if (stringFormat == STRING_DISPLAY .JAVASCRIPT ) {
277
- return b .toString ().replace ("\" " , "\\ \" " ).replace ("\' " , "\\ \' " );
275
+ return b .toString ().replace ("\" " , "\\ \" " ).replace ("\' " , "\\ \' " );
276
+ }
277
+ if ((htmlLabel != null ) && (stringFormat == STRING_DISPLAY .HTML_AWT )) {
278
+ return new StringBuilder ("<html>" ).append (b ).append ("</html>" ).toString ();
278
279
}
279
280
return b .toString ();
280
281
}
281
282
282
283
283
- private StringBuilder replacePlainLabelWithHTML (STRING_DISPLAY stringFormat ) {
284
- if (stringFormat ==STRING_DISPLAY .HTML_AWT ){
285
- return new StringBuilder ("<html>" ).append (value ).append ("</html>" );
286
- }else if (stringFormat ==STRING_DISPLAY .HTML_FRAGMENTS ){
287
- return new StringBuilder (value );
288
- }
289
- return new StringBuilder ();
290
- }
291
-
292
284
/**
293
285
* @param numberFormat
294
286
* @param b
@@ -349,7 +341,7 @@ private void appendHexBytes(StringBuilder b) {
349
341
* @param b
350
342
*/
351
343
private void appendString (StringBuilder b ) {
352
- b .append (value );
344
+ b .append (stringValue );
353
345
}
354
346
355
347
/**
@@ -435,16 +427,18 @@ public JMenuItem getSubMenu() {
435
427
* @param subMenu the subMenu to set
436
428
* @param owner the owner to set
437
429
*/
438
- public void setSubMenuAndOwner (JMenuItem subMenu , Object owner ) {
430
+ public KVP setSubMenuAndOwner (JMenuItem subMenu , Object owner ) {
439
431
this .subMenu = subMenu ;
440
432
this .owner = owner ;
433
+ return this ;
441
434
}
442
435
443
436
/**
444
437
* @param subMenu the subMenu to set
445
438
*/
446
- public void setSubMenu (JMenuItem subMenu ) {
439
+ public KVP setSubMenu (JMenuItem subMenu ) {
447
440
this .subMenu = subMenu ;
441
+ return this ;
448
442
}
449
443
450
444
/**
@@ -485,28 +479,45 @@ public String getCrumb() {
485
479
486
480
487
481
488
- public void setCrumb (String path ) {
482
+ public KVP setCrumb (String path ) {
489
483
this .crumb = path ;
484
+ return this ;
490
485
}
491
486
492
487
public List <DetailView > getDetailViews () {
493
488
return detailViews ;
494
489
}
495
490
496
- public void addHTMLSource (HTMLSource htmlSource , String label ) {
491
+ public KVP addHTMLSource (HTMLSource htmlSource , String label ) {
497
492
detailViews .add (new DetailView (htmlSource , label ));
493
+ return this ;
498
494
}
499
495
500
- public void addImageSource (ImageSource imageSource , String label ) {
496
+ public KVP addImageSource (ImageSource imageSource , String label ) {
501
497
detailViews .add (new DetailView (imageSource , label ));
498
+ return this ;
502
499
}
503
500
504
- public void addTableSource (TableSource tableSource , String label ) {
501
+ public KVP addTableSource (TableSource tableSource , String label ) {
505
502
detailViews .add (new DetailView (tableSource , label ));
503
+ return this ;
506
504
}
507
505
508
- public void addXMLSource (XMLSource xmlSource , String label ) {
506
+ public KVP addXMLSource (XMLSource xmlSource , String label ) {
509
507
detailViews .add (new DetailView (xmlSource , label ));
508
+ return this ;
510
509
}
511
510
511
+ @ Override
512
+ public Object getUserObject () {
513
+ return this ;
514
+ }
515
+
516
+ public KVP setHtmlLabel (String htmlLabel ) {
517
+ this .htmlLabel = htmlLabel ;
518
+ return this ;
519
+ }
520
+
521
+
522
+
512
523
}
0 commit comments