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