mdlaf.components.combobox.MaterialComboBoxUI Maven / Gradle / Ivy
/**
* MIT License
*
* Copyright (c) 2018-2020 atharva washimkar, Vincenzo Palazzo [email protected]
* Copyright (c) 2021 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 in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package mdlaf.components.combobox;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import mdlaf.animation.MaterialUIMovement;
import mdlaf.components.button.MaterialButtonsComponentsUI;
import mdlaf.utils.MaterialBorders;
import mdlaf.utils.MaterialDrawingUtils;
/**
* @author https://github.com/vincenzopalazzo
* @author https://github.com/atarw
*/
public class MaterialComboBoxUI extends BasicComboBoxUI {
public static ComponentUI createUI(JComponent c) {
return new MaterialComboBoxUI();
}
protected JButton arrowButton;
protected Color background;
protected FocusListenerColor focusListener;
protected int arc = 10; // default value
protected Border disabledBorder;
@Override
public void installUI(JComponent c) {
super.installUI(c);
background = UIManager.getColor("ComboBox.background");
comboBox.setLightWeightPopupEnabled(true);
comboBox.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
comboBox.setFocusable(UIManager.getBoolean("ComboBox.focusable"));
this.disabledBorder =
MaterialBorders.roundedLineColorBorder(UIManager.getColor("ComboBox.disabledColor"), arc);
}
@Override
protected void installDefaults() {
super.installDefaults();
this.arc = UIManager.getInt("ComboBox.arc");
}
@Override
public void uninstallUI(JComponent c) {
super.uninstallUI(comboBox);
}
@Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
if (!c.isEnabled()) {
c.setBorder(this.disabledBorder);
}
}
@Override
protected JButton createArrowButton() {
this.arrowButton = new ArrowButtonComboBox();
return arrowButton;
}
@Override
protected ComboPopup createPopup() {
return new MaterialComboBoxPopup(comboBox);
}
@Override
public void unconfigureArrowButton() {
super.unconfigureArrowButton();
}
@Override
protected void installListeners() {
super.installListeners();
focusListener = new FocusListenerColor();
comboBox.addFocusListener(focusListener);
}
@Override
protected void uninstallListeners() {
if (focusListener != null) {
this.comboBox.removeFocusListener(focusListener);
}
super.uninstallListeners();
}
@Override
public void update(Graphics g, JComponent c) {
g = MaterialDrawingUtils.getAliasedGraphics(g);
g.setColor(c.getBackground());
g.fillRoundRect(0, 0, comboBox.getWidth(), comboBox.getHeight(), arc, arc);
paint(g, c);
}
@Override
public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) {
ListCellRenderer