Skip to content

Commit

Permalink
fix tooltip ui
Browse files Browse the repository at this point in the history
  • Loading branch information
akmsw committed Jul 5, 2024
1 parent 152df7b commit 8383ce6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
import armameeldoparti.models.Error;
import armameeldoparti.utils.common.CommonFunctions;
import armameeldoparti.utils.common.Constants;
import java.awt.AlphaComposite;
import armameeldoparti.utils.common.custom.graphical.ui.CustomToolTipUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.IllegalComponentStateException;
import java.awt.Insets;
import java.awt.geom.RoundRectangle2D;
import java.awt.Window;
import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ToolTipUI;

/**
* A custom tooltip that fits the overall program aesthetics.
Expand Down Expand Up @@ -54,18 +49,19 @@ public CustomToolTip(JComponent component) {
public void addNotify() {
super.addNotify();

setOpaque(false);

Component parent = this.getParent();

if (parent instanceof JComponent) {
((JComponent) parent).setOpaque(false);
}

try {
SwingUtilities.windowForComponent(this)
.setBackground(new Color(0, 0, 0, 0));
} catch (IllegalComponentStateException _) {
Window window = SwingUtilities.windowForComponent(this);

if (window != null) {
window.setBackground(new Color(0, 0, 0, 0));
}
} catch (IllegalComponentStateException | UnsupportedOperationException _) {
CommonFunctions.exitProgram(Error.ERROR_INTERNAL);
}
}
Expand All @@ -74,53 +70,4 @@ public void addNotify() {
public Insets getInsets() {
return Constants.INSETS_TOOLTIP;
}

// ---------- Private inner classes ----------------------------------------------------------------------------------------------------------------

/**
* Private, internal class that establishes the tooltip UI.
*/
private class CustomToolTipUI extends ToolTipUI {

// ---------- Public methods ---------------------------------------------------------------------------------------------------------------------

@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g.create();

RoundRectangle2D roundedRect = new RoundRectangle2D.Float(0,
0,
(c.getWidth() - 1),
(c.getHeight() - 1),
Constants.ROUNDED_BORDER_ARC_TOOLTIP,
Constants.ROUNDED_BORDER_ARC_TOOLTIP);

// Round rectangle configuration
g2.setRenderingHints(Constants.MAP_RENDERING_HINTS);
g2.setComposite(AlphaComposite.Clear);
g2.fill(roundedRect);

// Background painting
g2.setComposite(AlphaComposite.Src);
g2.setColor(Constants.COLOR_GREEN_DARK_MEDIUM);
g2.fill(roundedRect);

// Text painting
FontMetrics fm = g2.getFontMetrics();

String text = ((JToolTip) c).getTipText();

g2.setColor(Color.WHITE);
g2.drawString(text, (c.getWidth() - fm.stringWidth(text)) / 2, (c.getHeight() - fm.getHeight()) / 2 + fm.getAscent());
g2.dispose();
}

@Override
public Dimension getPreferredSize(JComponent c) {
FontMetrics fm = c.getFontMetrics(c.getFont());

return new Dimension(fm.stringWidth(((JToolTip) c).getTipText()) + getInsets().left + getInsets().right,
fm.getHeight() + getInsets().top + getInsets().bottom);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package armameeldoparti.utils.common.custom.graphical.ui;

import armameeldoparti.utils.common.Constants;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.ToolTipUI;

/**
* A custom tooltip UI that fits the overall program aesthetics.
*
* @author Bonino, Francisco Ignacio.
*
* @version 0.0.1
*
* @since 3.0
*/
public class CustomToolTipUI extends ToolTipUI {

// ---------- Public methods ---------------------------------------------------------------------------------------------------------------------

/**
* The "java:S1172" warning is suppressed since the argument is intentionally unused.
*
* @param component Intentionally unused argument.
*
* @return A new custom tooltip UI.
*/
@SuppressWarnings("java:S1172")
public static ComponentUI createUI(JComponent component) {
return new CustomToolTipUI();
}

@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g.create();

RoundRectangle2D roundedRect = new RoundRectangle2D.Float(0, 0, c.getWidth(), c.getHeight(),
Constants.ROUNDED_BORDER_ARC_TOOLTIP,
Constants.ROUNDED_BORDER_ARC_TOOLTIP);

// Round rectangle configuration
g2.setRenderingHints(Constants.MAP_RENDERING_HINTS);
g2.setComposite(AlphaComposite.Clear);
g2.fill(roundedRect);

// Background painting
g2.setComposite(AlphaComposite.Src);
g2.setColor(Constants.COLOR_GREEN_DARK_MEDIUM);
g2.fill(roundedRect);

// Text painting
FontMetrics fm = g2.getFontMetrics();

String text = ((JToolTip) c).getTipText();

g2.setColor(Color.WHITE);
g2.drawString(text, (c.getWidth() - fm.stringWidth(text)) / 2, (c.getHeight() - fm.getHeight()) / 2 + fm.getAscent());
g2.dispose();
}

@Override
public Dimension getPreferredSize(JComponent c) {
FontMetrics fm = c.getFontMetrics(c.getFont());

Insets insets = c.getInsets();

return new Dimension(fm.stringWidth(((JToolTip) c).getTipText()) + insets.left + insets.right,
fm.getHeight() + insets.top + insets.bottom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public List<Team> withAnchorages(List<Team> teams) {
teams.sort(comparingInt(Team::getTeamSkill));

distributeSubsets(teams, players, players.get(0)
.getPosition());
.getPosition());
} else {
for (Player player : players) {
teams.sort(comparingInt(Team::getTeamSkill));
Expand Down

0 comments on commit 8383ce6

Please sign in to comment.