15
15
*/
16
16
package ghidra .framework .plugintool .dialog ;
17
17
18
+ import static ghidra .util .HTMLUtilities .*;
19
+
18
20
import java .awt .*;
19
- import java .util .StringTokenizer ;
20
21
21
22
import javax .swing .*;
22
23
import javax .swing .text .SimpleAttributeSet ;
23
24
import javax .swing .text .StyleConstants ;
24
25
25
- import org .apache .commons .lang3 .StringUtils ;
26
-
27
26
import docking .widgets .label .GDHtmlLabel ;
28
27
import ghidra .util .HTMLUtilities ;
29
28
34
33
*/
35
34
public abstract class AbstractDetailsPanel extends JPanel {
36
35
36
+ private static final int MIN_WIDTH = 700 ;
37
37
protected static final int LEFT_COLUMN_WIDTH = 150 ;
38
38
protected static final int RIGHT_MARGIN = 30 ;
39
39
40
40
// Font attributes for the title of each row.
41
41
protected static SimpleAttributeSet titleAttrSet ;
42
42
43
43
protected JLabel textLabel ;
44
- protected Font defaultFont ;
45
44
protected JScrollPane sp ;
46
45
47
46
/**
@@ -105,15 +104,25 @@ protected void clear() {
105
104
*/
106
105
protected void createMainPanel () {
107
106
setLayout (new BorderLayout ());
108
- textLabel = new GDHtmlLabel ("" );
107
+ textLabel = new GDHtmlLabel () {
108
+ @ Override
109
+ public Dimension getPreferredSize () {
110
+
111
+ // overridden to force word-wrapping by limiting the preferred size of the label
112
+ Dimension mySize = super .getPreferredSize ();
113
+ int rightColumnWidth = AbstractDetailsPanel .this .getWidth () - LEFT_COLUMN_WIDTH ;
114
+ mySize .width = Math .max (MIN_WIDTH , rightColumnWidth );
115
+ return mySize ;
116
+ }
117
+ };
118
+
109
119
textLabel .setVerticalAlignment (SwingConstants .TOP );
110
120
textLabel .setOpaque (true );
111
121
textLabel .setBackground (Color .WHITE );
112
122
sp = new JScrollPane (textLabel );
113
123
sp .getVerticalScrollBar ().setUnitIncrement (10 );
114
- sp .setPreferredSize (new Dimension (700 , 200 ));
124
+ sp .setPreferredSize (new Dimension (MIN_WIDTH , 200 ));
115
125
add (sp , BorderLayout .CENTER );
116
- defaultFont = new Font ("Tahoma" , Font .BOLD , 12 );
117
126
}
118
127
119
128
/**
@@ -126,7 +135,7 @@ protected void createMainPanel() {
126
135
protected void insertRowTitle (StringBuilder buffer , String rowName ) {
127
136
buffer .append ("<TR>" );
128
137
buffer .append ("<TD VALIGN=\" TOP\" >" );
129
- insertHTMLLine (rowName + ":" , titleAttrSet , buffer );
138
+ insertHTMLLine (buffer , rowName + ":" , titleAttrSet );
130
139
buffer .append ("</TD>" );
131
140
}
132
141
@@ -136,139 +145,67 @@ protected void insertRowTitle(StringBuilder buffer, String rowName) {
136
145
*
137
146
* @param buffer the string buffer to add to
138
147
* @param value the text to add
139
- * @param attrSet the structure containing formatting information
148
+ * @param attributes the structure containing formatting information
140
149
*/
141
- protected void insertRowValue (StringBuilder buffer , String value , SimpleAttributeSet attrSet ) {
142
- buffer .append ("<TD VALIGN=\" TOP\" >" );
143
- insertHTMLLine (value , attrSet , buffer );
150
+ protected void insertRowValue (StringBuilder buffer , String value ,
151
+ SimpleAttributeSet attributes ) {
152
+ buffer .append ("<TD VALIGN=\" TOP\" WIDTH=\" 80%\" >" );
153
+ insertHTMLLine (buffer , value , attributes );
144
154
buffer .append ("</TD>" );
145
155
buffer .append ("</TR>" );
146
156
}
147
157
148
158
/**
149
159
* Adds text to a string buffer as an html-formatted string, adding formatting information
150
160
* as specified.
151
- *
152
- * @param string the string to add
153
- * @param attributeSet the formatting instructions
154
161
* @param buffer the string buffer to add to
162
+ * @param string the string to add
163
+ * @param attributes the formatting instructions
155
164
*/
156
- protected void insertHTMLString (String string , SimpleAttributeSet attributeSet ,
157
- StringBuilder buffer ) {
165
+ protected void insertHTMLString (StringBuilder buffer , String string ,
166
+ SimpleAttributeSet attributes ) {
167
+
158
168
if (string == null ) {
159
169
return ;
160
170
}
161
- buffer .append ("<FONT COLOR=\" #" );
162
171
163
- Color foregroundColor = (Color ) attributeSet .getAttribute (StyleConstants .Foreground );
164
- buffer .append (createColorString (foregroundColor ));
172
+ buffer .append ("<FONT COLOR=\" " );
165
173
166
- buffer .append ("\" FACE=\" " );
174
+ Color foregroundColor = (Color ) attributes .getAttribute (StyleConstants .Foreground );
175
+ buffer .append (HTMLUtilities .toHexString (foregroundColor ));
167
176
168
- buffer .append (attributeSet .getAttribute (StyleConstants .FontFamily ).toString ());
177
+ buffer .append ("\" FACE=\" " );
178
+ buffer .append (attributes .getAttribute (StyleConstants .FontFamily ).toString ());
169
179
170
180
buffer .append ("\" >" );
171
181
172
- Boolean isBold = (Boolean ) attributeSet .getAttribute (StyleConstants .Bold );
182
+ Boolean isBold = (Boolean ) attributes .getAttribute (StyleConstants .Bold );
173
183
isBold = (isBold == null ) ? Boolean .FALSE : isBold ;
184
+ String text = HTMLUtilities .escapeHTML (string );
174
185
if (isBold ) {
175
- buffer . append ( "<B>" );
186
+ text = HTMLUtilities . bold ( text );
176
187
}
177
188
178
- buffer .append (HTMLUtilities .escapeHTML (string ));
179
-
180
- if (isBold ) {
181
- buffer .append ("</B>" );
182
- }
189
+ buffer .append (text );
183
190
184
191
buffer .append ("</FONT>" );
185
192
}
186
193
187
194
/**
188
195
* Inserts a single line of html into a {@link StringBuffer}, with the given attributes.
189
- *
190
- * @param string the string to insert
191
- * @param attributeSet the attributes to apply
192
196
* @param buffer the string buffer
197
+ * @param string the string to insert
198
+ * @param attributes the attributes to apply
193
199
*/
194
- protected void insertHTMLLine (String string , SimpleAttributeSet attributeSet ,
195
- StringBuilder buffer ) {
200
+ protected void insertHTMLLine (StringBuilder buffer , String string ,
201
+ SimpleAttributeSet attributes ) {
196
202
if (string == null ) {
197
203
return ;
198
204
}
199
205
200
- insertHTMLString (string , attributeSet , buffer );
206
+ insertHTMLString (buffer , string , attributes );
201
207
202
208
// row padding - newline space
203
- buffer .append ("<BR>" );
204
- }
205
-
206
- /**
207
- * Returns a stringified version of the {@link Color} provided; eg: "8c0000"
208
- *
209
- * @param color the color to parse
210
- * @return string version of the color
211
- */
212
- protected String createColorString (Color color ) {
213
-
214
- int red = color .getRed ();
215
- int green = color .getGreen ();
216
- int blue = color .getBlue ();
217
-
218
- return StringUtils .leftPad (Integer .toHexString (red ), 2 , "0" ) +
219
- StringUtils .leftPad (Integer .toHexString (green ), 2 , "0" ) +
220
- StringUtils .leftPad (Integer .toHexString (blue ), 2 , "0" );
221
- }
222
-
223
- /**
224
- * Returns a string with line breaks at the boundary of the window it's being displayed in.
225
- * Without this the description would just run on in one long line.
226
- *
227
- * @param descr the string to format
228
- * @return the formatted string
229
- */
230
- protected String formatDescription (String descr ) {
231
- if (descr == null ) {
232
- return "" ;
233
- }
234
- int maxWidth = getMaxStringWidth ();
235
- int remainingWidth = maxWidth ;
236
- FontMetrics fm = textLabel .getFontMetrics (defaultFont );
237
- int spaceSize = fm .charWidth (' ' );
238
- StringBuffer sb = new StringBuffer ();
239
- StringTokenizer st = new StringTokenizer (descr , " " );
240
- while (st .hasMoreTokens ()) {
241
- String str = st .nextToken ();
242
- if (str .endsWith ("." )) {
243
- str = str + " " ;
244
- }
245
- int strWidth = fm .stringWidth (str );
246
- if (strWidth + spaceSize <= remainingWidth ) {
247
- sb .append (" " );
248
- sb .append (str );
249
- remainingWidth -= strWidth + spaceSize ;
250
- }
251
- else {
252
- sb .append ("<BR>" );
253
- sb .append (str + " " );
254
- remainingWidth = maxWidth - strWidth ;
255
- }
256
- }
257
- return sb .toString ();
258
- }
259
-
260
- /**
261
- * Returns the maximum size that one line of text can be when formatting the description.
262
- *
263
- * @return the number of characters in the string
264
- */
265
- protected int getMaxStringWidth () {
266
-
267
- int width = textLabel .getWidth ();
268
- if (width == 0 ) {
269
- width = 700 ;
270
- }
271
- width -= LEFT_COLUMN_WIDTH + RIGHT_MARGIN ; // allow for tabs and right margin
272
- return width ;
209
+ buffer .append (BR );
273
210
}
274
211
}
0 commit comments