1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.gui; |
22 |
|
|
23 |
|
import jalview.util.MessageManager; |
24 |
|
|
25 |
|
import java.awt.BorderLayout; |
26 |
|
import java.awt.Color; |
27 |
|
import java.awt.Component; |
28 |
|
import java.awt.Font; |
29 |
|
import java.awt.GridLayout; |
30 |
|
import java.awt.Rectangle; |
31 |
|
import java.awt.event.ActionListener; |
32 |
|
import java.awt.event.MouseAdapter; |
33 |
|
import java.awt.event.MouseEvent; |
34 |
|
import java.util.List; |
35 |
|
import java.util.Objects; |
36 |
|
|
37 |
|
import javax.swing.AbstractButton; |
38 |
|
import javax.swing.BorderFactory; |
39 |
|
import javax.swing.JButton; |
40 |
|
import javax.swing.JComboBox; |
41 |
|
import javax.swing.JComponent; |
42 |
|
import javax.swing.JLabel; |
43 |
|
import javax.swing.JMenu; |
44 |
|
import javax.swing.JMenuItem; |
45 |
|
import javax.swing.JPanel; |
46 |
|
import javax.swing.JScrollBar; |
47 |
|
import javax.swing.SwingConstants; |
48 |
|
import javax.swing.border.Border; |
49 |
|
import javax.swing.border.TitledBorder; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@author |
55 |
|
|
56 |
|
|
|
|
| 45.6% |
Uncovered Elements: 80 (147) |
Complexity: 41 |
Complexity Density: 0.45 |
|
57 |
|
public final class JvSwingUtils |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
|
65 |
|
@param |
66 |
|
|
67 |
|
@return |
68 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 7 |
Complexity Density: 0.54 |
|
69 |
20872 |
public static String wrapTooltip(boolean enclose, String ttext)... |
70 |
|
{ |
71 |
20872 |
Objects.requireNonNull(ttext, |
72 |
|
"Tootip text to format must not be null!"); |
73 |
20872 |
ttext = ttext.trim(); |
74 |
20872 |
boolean maxLengthExceeded = false; |
75 |
|
|
76 |
20872 |
if (ttext.contains("<br>")) |
77 |
|
{ |
78 |
198 |
String[] htmllines = ttext.split("<br>"); |
79 |
198 |
for (String line : htmllines) |
80 |
|
{ |
81 |
203 |
maxLengthExceeded = line.length() > 60; |
82 |
203 |
if (maxLengthExceeded) |
83 |
|
{ |
84 |
193 |
break; |
85 |
|
} |
86 |
|
} |
87 |
|
} |
88 |
|
else |
89 |
|
{ |
90 |
20674 |
maxLengthExceeded = ttext.length() > 60; |
91 |
|
} |
92 |
|
|
93 |
20872 |
if (!maxLengthExceeded) |
94 |
|
{ |
95 |
13675 |
return enclose ? "<html>" + ttext + "</html>" : ttext; |
96 |
|
} |
97 |
|
|
98 |
7197 |
return (enclose ? "<html>" : "") |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
+ "<style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style><div class=\"ttip\">" |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
+ ttext |
108 |
|
|
109 |
|
+ " </div>" |
110 |
|
|
111 |
7197 |
+ ((enclose ? "</html>" : "")); |
112 |
|
|
113 |
|
} |
114 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
115 |
0 |
public static JButton makeButton(String label, String tooltip,... |
116 |
|
ActionListener action) |
117 |
|
{ |
118 |
0 |
JButton button = new JButton(); |
119 |
0 |
button.setText(label); |
120 |
|
|
121 |
0 |
button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10)); |
122 |
0 |
button.setForeground(Color.black); |
123 |
0 |
button.setHorizontalAlignment(SwingConstants.CENTER); |
124 |
0 |
button.setToolTipText(tooltip); |
125 |
0 |
button.addActionListener(action); |
126 |
0 |
return button; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
@param |
133 |
|
@param |
134 |
|
@return |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
136 |
7642 |
public static JMenu findOrCreateMenu(JMenu menu, String submenu)... |
137 |
|
{ |
138 |
7642 |
JMenu submenuinstance = null; |
139 |
39312 |
for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++) |
140 |
|
{ |
141 |
31670 |
if (menu.getMenuComponent(i) instanceof JMenu |
142 |
|
&& ((JMenu) menu.getMenuComponent(i)).getText() |
143 |
|
.equals(submenu)) |
144 |
|
{ |
145 |
7176 |
submenuinstance = (JMenu) menu.getMenuComponent(i); |
146 |
|
} |
147 |
|
} |
148 |
7642 |
if (submenuinstance == null) |
149 |
|
{ |
150 |
466 |
submenuinstance = new JMenu(submenu); |
151 |
466 |
menu.add(submenuinstance); |
152 |
|
} |
153 |
7642 |
return submenuinstance; |
154 |
|
|
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@param |
161 |
|
@param |
162 |
|
@param |
163 |
|
@return |
164 |
|
|
165 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
166 |
0 |
public static JPanel addtoLayout(JPanel panel, String tooltip,... |
167 |
|
JComponent label, JComponent valBox) |
168 |
|
{ |
169 |
0 |
JPanel laypanel = new JPanel(new GridLayout(1, 2)); |
170 |
0 |
JPanel labPanel = new JPanel(new BorderLayout()); |
171 |
0 |
JPanel valPanel = new JPanel(); |
172 |
0 |
labPanel.setBounds(new Rectangle(7, 7, 158, 23)); |
173 |
0 |
valPanel.setBounds(new Rectangle(172, 7, 270, 23)); |
174 |
0 |
labPanel.add(label, BorderLayout.WEST); |
175 |
0 |
valPanel.add(valBox); |
176 |
0 |
laypanel.add(labPanel); |
177 |
0 |
laypanel.add(valPanel); |
178 |
0 |
valPanel.setToolTipText(tooltip); |
179 |
0 |
labPanel.setToolTipText(tooltip); |
180 |
0 |
valBox.setToolTipText(tooltip); |
181 |
0 |
panel.add(laypanel); |
182 |
0 |
panel.validate(); |
183 |
0 |
return laypanel; |
184 |
|
} |
185 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
0 |
public static void mgAddtoLayout(JPanel cpanel, String tooltip,... |
187 |
|
JLabel jLabel, JComponent name) |
188 |
|
{ |
189 |
0 |
mgAddtoLayout(cpanel, tooltip, jLabel, name, null); |
190 |
|
} |
191 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
192 |
0 |
public static void mgAddtoLayout(JPanel cpanel, String tooltip,... |
193 |
|
JLabel jLabel, JComponent name, String params) |
194 |
|
{ |
195 |
0 |
cpanel.add(jLabel); |
196 |
0 |
if (params == null) |
197 |
|
{ |
198 |
0 |
cpanel.add(name); |
199 |
|
} |
200 |
|
else |
201 |
|
{ |
202 |
0 |
cpanel.add(name, params); |
203 |
|
} |
204 |
0 |
name.setToolTipText(tooltip); |
205 |
0 |
jLabel.setToolTipText(tooltip); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
@return |
212 |
|
|
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
57 |
public static Font getLabelFont()... |
215 |
|
{ |
216 |
57 |
return getLabelFont(false, false); |
217 |
|
} |
218 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 5 |
Complexity Density: 5 |
|
219 |
58 |
public static Font getLabelFont(boolean bold, boolean italic)... |
220 |
|
{ |
221 |
58 |
return new java.awt.Font("Verdana", |
222 |
58 |
(!bold && !italic) ? Font.PLAIN |
223 |
1 |
: (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0), |
224 |
|
11); |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
@return |
231 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
232 |
0 |
public static Font getTextAreaFont()... |
233 |
|
{ |
234 |
0 |
return getLabelFont(false, false); |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@param |
242 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
243 |
0 |
public static void cleanMenu(JMenu webService)... |
244 |
|
{ |
245 |
0 |
for (int i = 0; i < webService.getItemCount();) |
246 |
|
{ |
247 |
0 |
JMenuItem item = webService.getItem(i); |
248 |
0 |
if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0) |
249 |
|
{ |
250 |
0 |
webService.remove(i); |
251 |
|
} |
252 |
|
else |
253 |
|
{ |
254 |
0 |
i++; |
255 |
|
} |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
@see |
265 |
|
|
266 |
|
@param |
267 |
|
@return |
268 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
269 |
1 |
public static float getScrollBarProportion(JScrollBar scroll)... |
270 |
|
{ |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
1 |
int possibleRange = scroll.getMaximum() - scroll.getMinimum() |
276 |
|
- scroll.getModel().getExtent(); |
277 |
1 |
float valueInRange = scroll.getValue() |
278 |
|
- (scroll.getModel().getExtent() / 2f); |
279 |
1 |
float proportion = valueInRange / possibleRange; |
280 |
1 |
return proportion; |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
@param |
289 |
|
@param |
290 |
|
@return |
291 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
292 |
1 |
public static int getScrollValueForProportion(JScrollBar scrollbar,... |
293 |
|
float proportion) |
294 |
|
{ |
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
1 |
float fraction = proportion |
300 |
|
* (scrollbar.getMaximum() - scrollbar.getMinimum() |
301 |
|
- scrollbar.getModel().getExtent()) |
302 |
|
+ (scrollbar.getModel().getExtent() / 2f); |
303 |
1 |
return Math.min(Math.round(fraction), scrollbar.getMaximum()); |
304 |
|
} |
305 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
306 |
16 |
public static void jvInitComponent(AbstractButton comp, String i18nString)... |
307 |
|
{ |
308 |
16 |
setColorAndFont(comp); |
309 |
16 |
if (i18nString != null && !i18nString.isEmpty()) |
310 |
|
{ |
311 |
16 |
comp.setText(MessageManager.getString(i18nString)); |
312 |
|
} |
313 |
|
} |
314 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
315 |
6 |
public static void jvInitComponent(JComponent comp)... |
316 |
|
{ |
317 |
6 |
setColorAndFont(comp); |
318 |
|
} |
319 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
320 |
22 |
private static void setColorAndFont(JComponent comp)... |
321 |
|
{ |
322 |
22 |
comp.setBackground(Color.white); |
323 |
22 |
comp.setFont(JvSwingUtils.getLabelFont()); |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
@param |
331 |
|
@param |
332 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
333 |
0 |
public static JComboBox<String> buildComboWithTooltips(... |
334 |
|
List<String> entries, List<String> tooltips) |
335 |
|
{ |
336 |
0 |
JComboBox<String> combo = new JComboBox<>(); |
337 |
0 |
final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer(); |
338 |
0 |
combo.setRenderer(renderer); |
339 |
0 |
for (String attName : entries) |
340 |
|
{ |
341 |
0 |
combo.addItem(attName); |
342 |
|
} |
343 |
0 |
renderer.setTooltips(tooltips); |
344 |
0 |
final MouseAdapter mouseListener = new MouseAdapter() |
345 |
|
{ |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
346 |
0 |
@Override... |
347 |
|
public void mouseEntered(MouseEvent e) |
348 |
|
{ |
349 |
0 |
int j = combo.getSelectedIndex(); |
350 |
0 |
if (j > -1) |
351 |
|
{ |
352 |
0 |
combo.setToolTipText(tooltips.get(j)); |
353 |
|
} |
354 |
|
} |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
355 |
0 |
@Override... |
356 |
|
public void mouseExited(MouseEvent e) |
357 |
|
{ |
358 |
0 |
combo.setToolTipText(null); |
359 |
|
} |
360 |
|
}; |
361 |
0 |
for (Component c : combo.getComponents()) |
362 |
|
{ |
363 |
0 |
c.addMouseListener(mouseListener); |
364 |
|
} |
365 |
0 |
return combo; |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@param |
373 |
|
@param |
374 |
|
@param |
375 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
376 |
0 |
public static TitledBorder createTitledBorder(JComponent comp,... |
377 |
|
String title, boolean italic) |
378 |
|
{ |
379 |
0 |
Font font = comp.getFont(); |
380 |
0 |
if (italic) |
381 |
|
{ |
382 |
0 |
font = new Font(font.getName(), Font.ITALIC, font.getSize()); |
383 |
|
} |
384 |
0 |
Border border = BorderFactory.createTitledBorder(""); |
385 |
0 |
TitledBorder titledBorder = BorderFactory.createTitledBorder(border, |
386 |
|
title, TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, |
387 |
|
font); |
388 |
0 |
comp.setBorder(titledBorder); |
389 |
|
|
390 |
0 |
return titledBorder; |
391 |
|
} |
392 |
|
|
393 |
|
} |