| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.tools; |
| | 3 | |
| | 4 | import java.awt.Component; |
| | 5 | import java.awt.Graphics; |
| | 6 | import java.awt.Insets; |
| | 7 | |
| | 8 | import javax.swing.UIManager; |
| | 9 | import javax.swing.border.Border; |
| | 10 | |
| | 11 | public class GTKWorkaround { |
| | 12 | static class BorderDelegate implements Border{ |
| | 13 | private final Border delegate; |
| | 14 | |
| | 15 | public BorderDelegate(Border delegate) { |
| | 16 | this.delegate = delegate; |
| | 17 | } |
| | 18 | public Insets getBorderInsets(Component c) { |
| | 19 | return delegate.getBorderInsets(c); |
| | 20 | } |
| | 21 | public boolean isBorderOpaque() { |
| | 22 | return delegate.isBorderOpaque(); |
| | 23 | } |
| | 24 | public void paintBorder(Component c, Graphics g, int x, int y, |
| | 25 | int width, int height) { |
| | 26 | delegate.paintBorder(c, g, x, y, width, height); |
| | 27 | } |
| | 28 | } |
| | 29 | static class SafeBorder extends BorderDelegate{ |
| | 30 | public SafeBorder(Border delegate) { |
| | 31 | super(delegate); |
| | 32 | } |
| | 33 | @Override public Insets getBorderInsets(Component c) { |
| | 34 | Insets res = super.getBorderInsets(c); |
| | 35 | return res == null ? new Insets(0,0,0,0) : res; |
| | 36 | } |
| | 37 | } |
| | 38 | /** |
| | 39 | * This is a workaround for a JDK-Bug. See |
| | 40 | * http://josm.openstreetmap.de/ticket/1403 |
| | 41 | * |
| | 42 | * Bug: |
| | 43 | * DefaultListCellRenderer.getListCellRendererComponent calls |
| | 44 | * setBorder(UIManager.get(someBorder)) |
| | 45 | * JComponent.setBorder makes an unsafe check which throws a NPE if the border's |
| | 46 | * getBorderInsets returns null |
| | 47 | * see http://hg.openjdk.java.net/jdk7/swing/jdk/file/37a05a11f281/src/share/classes/javax/swing/JComponent.java line 1777 |
| | 48 | * |
| | 49 | * Workaround: |
| | 50 | * Wrap the problematic border and return standard insets in the problematic case. |
| | 51 | * @param borderName |
| | 52 | */ |
| | 53 | public static void makeBorderInsetsSafe(String borderName){ |
| | 54 | if ("GTK look and feel".equals(UIManager.getLookAndFeel().getName())){ |
| | 55 | Border b = UIManager.getBorder(borderName); |
| | 56 | if (!(b instanceof SafeBorder)) |
| | 57 | UIManager.put(borderName,new SafeBorder(b)); |
| | 58 | } |
| | 59 | } |
| | 60 | } |