Skip to content

Commit

Permalink
Make AutoComplete description window look better (smaller empty borde…
Browse files Browse the repository at this point in the history
…r) in Flat LookAndFeel
  • Loading branch information
bobbylight committed Jan 22, 2022
1 parent 5501ace commit 63300b5
Showing 1 changed file with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
*/
package org.fife.ui.autocomplete;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Window;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -126,6 +123,8 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener,
private static final String MSG =
"org.fife.ui.autocomplete.AutoCompleteDescWindow";

private static final String FLAT_LAF_BORDER_PREFIX = "com.formdev.flatlaf.ui.Flat";


/**
* Constructor.
Expand All @@ -141,7 +140,7 @@ class AutoCompleteDescWindow extends JWindow implements HyperlinkListener,
ComponentOrientation o = ac.getTextComponentOrientation();

JPanel cp = new JPanel(new BorderLayout());
cp.setBorder(TipUtil.getToolTipBorder());
cp.setBorder(getToolTipBorder());

descArea = new JEditorPane("text/html", null);
TipUtil.tweakTipEditorPane(descArea);
Expand Down Expand Up @@ -256,6 +255,42 @@ public boolean copy() {
}


/**
* FlatLaf adds insets to tool tips, and for some themes (usually light ones)
* also uses a line border, whereas for other themes (usually dark ones)
* there is no line border. We need to ensure our border has no insets
* so our draggable bottom component looks good, but we'd like to preserve
* the color of the line border, if any. This method allows us to do so
* without a compile-time dependency on flatlaf.
*
* @param border The default tool tip border for the current Look and Feel.
* @return The border to use for this window.
*/
private static Border getReplacementForFlatLafBorder(Border border) {

Class<?> clazz = border.getClass();

// If it's a FlatLineBorder, get its color.
// If it's a FlatEmptyBorder, just return a 0-sized regular EmptyBorder.
Color color = null;
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if ("getLineColor".equals(method.getName())) {
try {
color = (Color)method.invoke(border);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace(); // Never happens
}
}
}

if (color != null) {
return BorderFactory.createLineBorder(color);
}
return BorderFactory.createEmptyBorder();
}


/**
* Returns the localized message for the specified key.
*
Expand All @@ -270,6 +305,21 @@ private String getString(String key) {
}


private static Border getToolTipBorder() {

Border border = org.fife.ui.rsyntaxtextarea.focusabletip.TipUtil.getToolTipBorder();


// Special case for FlatDarkLaf and FlatLightLaf, since they add an
// empty border to tool tips that messes up our floating-window appearance
if (isFlatLafBorder(border)) {
border = getReplacementForFlatLafBorder(border);
}

return border;
}


/**
* Called when a hyperlink is clicked.
*
Expand Down Expand Up @@ -339,6 +389,11 @@ public void hyperlinkUpdate(HyperlinkEvent e) {
}


private static boolean isFlatLafBorder(Border border) {
return border != null && border.getClass().getName().startsWith(FLAT_LAF_BORDER_PREFIX);
}


/**
* Enables or disables the back and forward actions as appropriate.
*/
Expand Down

0 comments on commit 63300b5

Please sign in to comment.