Skip to content

Commit

Permalink
Merge pull request #185 from vincenzopalazzo/macros/title_pane
Browse files Browse the repository at this point in the history
title-pane: fixes button behavior
  • Loading branch information
vincenzopalazzo authored Apr 22, 2022
2 parents 5e9e001 + b1d539d commit aee3990
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 125 deletions.
8 changes: 5 additions & 3 deletions src/main/java/mdlaf/components/button/MaterialButtonUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ public void installUI(JComponent c) {
}

/**
* Inside the future version 1.2 this method will contain all component that work with listener,
* because this method is call before the installListener and I can insert the control of the
* listener inside the native method. TODO make the button installDefault change
* The installDefaults method is a good place to add the default setting, because it is called
* before to install methods and this give to the user the possibility to change the button style
* inside the installation method without worried about the order pre and post super.install call.
*
* <p>FIXME: starting the transition to the workflow described in the comment
*/
@Override
protected void installDefaults(AbstractButton b) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019-2020 Vincenzo Palazzo [email protected]
* Copyright (c) 2019-2022 Vincenzo Palazzo [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,7 +23,6 @@
*/
package mdlaf.components.button;

import java.awt.*;
import javax.swing.*;

/**
Expand All @@ -45,11 +44,4 @@ public void installUI(JComponent c) {
borderEnabled = false;
buttonBorderToAll = false;
}

@Override
protected void paintFocus(
Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {}

@Override
protected void paintBorderButton(Graphics graphics, JComponent b) {}
}
118 changes: 27 additions & 91 deletions src/main/java/mdlaf/components/titlepane/MaterialTitlePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import javax.accessibility.AccessibleContext;
import javax.swing.*;
import javax.swing.plaf.UIResource;
import mdlaf.components.button.MaterialButtonUI;
import mdlaf.components.button.MaterialButtonsComponentsUI;
import mdlaf.utils.MaterialDrawingUtils;
import mdlaf.utils.MaterialManagerListener;
import mdlaf.utils.WrapperSwingUtilities;
Expand Down Expand Up @@ -172,53 +172,9 @@ protected void initMaterialButtonClose() {
}

protected void determineColors() {
switch (getWindowDecorationStyle()) {
case JRootPane.FRAME:
myActiveBackground = UIManager.getColor("Material.activeCaption");
myActiveForeground = UIManager.getColor("Material.activeCaptionText");
myActiveShadow = UIManager.getColor("Material.activeCaptionBorder");
break;
case JRootPane.ERROR_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.errorDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.errorDialog.titlePane.foreground");
myActiveShadow = UIManager.getColor("OptionPane.errorDialog.titlePane.shadow");
break;
case JRootPane.QUESTION_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.questionDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.questionDialog.titlePane.foreground");
myActiveShadow = UIManager.getColor("OptionPane.questionDialog.titlePane.shadow");
break;
case JRootPane.COLOR_CHOOSER_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.questionDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.questionDialog.titlePane.foreground");
myActiveShadow = UIManager.getColor("OptionPane.questionDialog.titlePane.shadow");
break;
case JRootPane.FILE_CHOOSER_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.questionDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.questionDialog.titlePane.foreground");
myActiveShadow = myActiveBackground;
break;
case JRootPane.WARNING_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.warningDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.warningDialog.titlePane.foreground");
myActiveShadow = UIManager.getColor("OptionPane.warningDialog.titlePane.shadow");
break;
case JRootPane.PLAIN_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.questionDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.questionDialog.titlePane.foreground");
myActiveShadow = UIManager.getColor("OptionPane.questionDialog.titlePane.shadow");
break;
case JRootPane.INFORMATION_DIALOG:
myActiveBackground = UIManager.getColor("OptionPane.errorDialog.titlePane.background");
myActiveForeground = UIManager.getColor("OptionPane.errorDialog.titlePane.foreground");
myActiveShadow = UIManager.getColor("OptionPane.errorDialog.titlePane.shadow");
break;
default:
myActiveBackground = UIManager.getColor("Material.activeCaption");
myActiveForeground = UIManager.getColor("Material.activeCaptionText");
myActiveShadow = UIManager.getColor("Material.activeCaptionBorder");
break;
}
myActiveBackground = UIManager.getColor("Material.activeCaption");
myActiveForeground = UIManager.getColor("Material.activeCaptionText");
myActiveShadow = UIManager.getColor("Material.activeCaptionBorder");
}

protected void installDefaults() {
Expand Down Expand Up @@ -285,12 +241,13 @@ protected void addMenuItems(JMenu menu) {
menu.add(myCloseAction);
}

protected static JButton createButton(String accessibleName, Icon icon, Action action) {
JButton button = new TitlePaneButton(Color.BLACK, true);
protected JButton createButton(String accessibleName, Icon icon, Action action) {
JButton button = new TitlePaneButton();
button.putClientProperty("paintActive", Boolean.TRUE);
button.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, accessibleName);
button.setAction(action);
button.setIcon(icon);
button.setBackground(myActiveBackground);
return button;
}

Expand Down Expand Up @@ -319,10 +276,6 @@ protected void createButtons() {
myIconifyButton =
createButton("Iconify", UIManager.getIcon("InternalFrame.iconifyIcon"), myIconifyAction);
myToggleButton = createButton("Maximize", myMaximizeIcon, myRestoreAction);

myCloseButton.setBackground(myActiveBackground);
myIconifyButton.setBackground(myActiveBackground);
myToggleButton.setBackground(myActiveBackground);
}
}

Expand Down Expand Up @@ -555,30 +508,6 @@ public void actionPerformed(ActionEvent e) {
}
}

protected void activeChanged(boolean active) {
boolean hasEmbeddedMenuBar =
myRootPane.getJMenuBar() != null && myRootPane.getJMenuBar().isVisible();
Color background = active ? myActiveBackground : myInactiveBackground;
Color foreground = active ? myActiveForeground : myInactiveForeground;
Color titleForeground = foreground;

setBackground(background);
myWindow.setForeground(titleForeground);
// myIconifyButton.setForeground(foreground);
// maximizeButton.setForeground(foreground);
myIconifyButton.setForeground(foreground);
myCloseButton.setForeground(foreground);

// titleLabel.setHorizontalAlignment(hasEmbeddedMenuBar ? SwingConstants.CENTER :
// SwingConstants.LEADING);

// this is necessary because hover/pressed colors are derived from background color
myIconifyButton.setBackground(background);
// maximizeButton.setBackground(background);
myToggleButton.setBackground(background);
myCloseButton.setBackground(background);
}

protected class TitlePaneLayout implements LayoutManager {

public void addLayoutComponent(String name, Component c) {}
Expand Down Expand Up @@ -843,35 +772,42 @@ public void componentHidden(ComponentEvent e) {}

protected static class TitlePaneButton extends JButton {

protected Color background;
protected Boolean state;

public TitlePaneButton(Color background, Boolean state) {
this.background = background;
this.state = state;
}
public TitlePaneButton() {}

@Override
protected void init(String text, Icon icon) {
super.init(text, icon);
setUI(new TitlePaneButtonUI());
}

private static class TitlePaneButtonUI extends MaterialButtonUI {
@Override
public void updateUI() {
super.updateUI();
setUI(new TitlePaneButtonUI());
}

private static class TitlePaneButtonUI extends MaterialButtonsComponentsUI {
@Override
public void installUI(JComponent c) {
mouseHoverEnabled = false;
super.installUI(c);
c.setFocusable(false);
c.setOpaque(true);
c.setBackground(background);
c.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
super.background = UIManager.getColor("Material.activeCaption");
super.defaultBackground = UIManager.getColor("Material.activeCaption");
c.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

@Override
protected void paintButtonPressed(Graphics g, AbstractButton b) {}

@Override
protected void paintBorderButton(Graphics graphics, JComponent b) {}

@Override
protected void paintFocus(
Graphics g,
AbstractButton b,
Rectangle viewRect,
Rectangle textRect,
Rectangle iconRect) {}
}
}
}
30 changes: 10 additions & 20 deletions src/main/java/mdlaf/components/titlepane/MaterialTitlePaneUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import javax.accessibility.AccessibleContext;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.UIResource;
import mdlaf.components.button.MaterialButtonsComponentsUI;
import mdlaf.utils.MaterialDrawingUtils;
Expand All @@ -35,6 +34,7 @@
/**
* @author Konstantin Bulenkov this code is copyed by OpenJDK
* @author https://github.com/users/vincenzopalazzo
* @deprecated Use MaterialTitlePane
*/
public class MaterialTitlePaneUI extends JComponent {

Expand Down Expand Up @@ -245,15 +245,11 @@ protected void addMenuItems(JMenu menu) {
menu.add(myCloseAction);
}

protected static JButton createButton(String accessibleName, Icon icon, Action action) {
protected JButton createButton(String accessibleName, Icon icon, Action action) {
JButton button = new JButtonNoMouseHoverNative();
button.setFocusPainted(false);
button.setFocusable(false);
button.setOpaque(true);
button.setUI(new JButtonNoMouseHoverNative.JButtonNoMouseHoverUI());
button.putClientProperty("paintActive", Boolean.TRUE);
button.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, accessibleName);
button.setBorder(new EmptyBorder(0, 0, 0, 0));
button.setText(null);
button.setAction(action);
button.setIcon(icon);
return button;
Expand Down Expand Up @@ -666,7 +662,9 @@ public void windowDeactivated(WindowEvent ev) {

protected static class JButtonNoMouseHoverNative extends JButton {

public JButtonNoMouseHoverNative() {}
public JButtonNoMouseHoverNative() {
super();
}

public JButtonNoMouseHoverNative(Icon icon) {
super(icon);
Expand All @@ -684,12 +682,6 @@ public JButtonNoMouseHoverNative(String text, Icon icon) {
super(text, icon);
}

@Override
protected void init(String text, Icon icon) {
super.init(text, icon);
setUI(new JButtonNoMouseHoverUI());
}

@Override
public void updateUI() {
setUI(new JButtonNoMouseHoverUI());
Expand All @@ -700,13 +692,13 @@ private static class JButtonNoMouseHoverUI extends MaterialButtonsComponentsUI {
@Override
public void installUI(JComponent c) {
super.installUI(c);
super.background = UIManager.getColor("TitlePane.background");
super.defaultBackground = UIManager.getColor("TitlePane.background");
c.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

@Override
protected void paintButtonPressed(Graphics g, AbstractButton b) {
// doNothing
}
protected void paintButtonPressed(Graphics g, AbstractButton b) {}

@Override
protected void paintBorderButton(Graphics graphics, JComponent b) {}
Expand All @@ -717,9 +709,7 @@ protected void paintFocus(
AbstractButton b,
Rectangle viewRect,
Rectangle textRect,
Rectangle iconRect) {
// do nothing
}
Rectangle iconRect) {}
}
}
}
4 changes: 2 additions & 2 deletions src/test/java/integration/gui/mock/DemoGUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class DemoGUITest extends JFrame {
JDialog.setDefaultLookAndFeelDecorated(true);
// FIXME: finish support for the JFrame.setDefaultLookAndFeelDecorated
JFrame.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel(new MaterialLookAndFeel(new MaterialLiteTheme()));
// UIManager.setLookAndFeel(new MaterialLookAndFeel(new DarkStackOverflowTheme()));
// UIManager.setLookAndFeel(new MaterialLookAndFeel(new MaterialLiteTheme()));
UIManager.setLookAndFeel(new MaterialLookAndFeel(new DarkStackOverflowTheme()));
UIManager.put(
"Button.mouseHoverEnable",
true); // Because the test are more difficult with effect mouse hover
Expand Down

0 comments on commit aee3990

Please sign in to comment.