Ticket #19819: OSM-FlatLaf-plugin-outline.diff

File OSM-FlatLaf-plugin-outline.diff, 5.7 KB (added by DevCharly, 5 years ago)
  • src/org/openstreetmap/josm/plugins/flatlaf/FlatLaf.properties

     
     1#
     2# License: GPL. For details, see LICENSE file.
     3#
     4
     5ComboBoxUI=org.openstreetmap.josm.plugins.flatlaf.JosmFlatComboBoxUI
     6TextFieldUI=org.openstreetmap.josm.plugins.flatlaf.JosmFlatTextFieldUI
  • src/org/openstreetmap/josm/plugins/flatlaf/JosmFlatComboBoxUI.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.plugins.flatlaf;
     3
     4import java.awt.Color;
     5import java.awt.Component;
     6
     7import javax.swing.BorderFactory;
     8import javax.swing.ComboBoxEditor;
     9import javax.swing.JComponent;
     10import javax.swing.JTextField;
     11import javax.swing.border.LineBorder;
     12import javax.swing.plaf.ComponentUI;
     13
     14import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
     15
     16import com.formdev.flatlaf.ui.FlatComboBoxUI;
     17import com.formdev.flatlaf.ui.FlatTextBorder;
     18
     19/**
     20 * Special JOSM UI delegate for JComboBox that changes a {@link LineBorder},
     21 * set on the editor text field, into a FlatLaf outline.
     22 * Also assigns background color set on editor text field to combo box.
     23 * E.g. used by {@link AbstractTextComponentValidator}.
     24 */
     25public class JosmFlatComboBoxUI
     26    extends FlatComboBoxUI
     27{
     28    public static ComponentUI createUI(JComponent c) {
     29        return new JosmFlatComboBoxUI();
     30    }
     31
     32    @Override
     33    protected ComboBoxEditor createEditor() {
     34        ComboBoxEditor comboBoxEditor = super.createEditor();
     35
     36        Component editor = comboBoxEditor.getEditorComponent();
     37        if (editor instanceof JTextField) {
     38            JTextField textField = (JTextField) editor;
     39            editor.addPropertyChangeListener(e -> {
     40                String propertyName = e.getPropertyName();
     41                if ("border".equals(propertyName)) {
     42                    Object newBorder = e.getNewValue();
     43                    if (newBorder instanceof LineBorder) {
     44                        // change LineBorder to FlatLaf outline
     45                        Color borderColor = ((LineBorder)newBorder).getLineColor();
     46                        comboBox.putClientProperty("JComponent.outline", borderColor);
     47                        textField.setBorder(BorderFactory.createEmptyBorder());
     48                    } else if (newBorder instanceof FlatTextBorder) {
     49                        // change FlatTextBorder to empty border
     50                        textField.setBorder(BorderFactory.createEmptyBorder());
     51                    } else if (newBorder == null) {
     52                        // clear FlatLaf outline
     53                        comboBox.putClientProperty("JComponent.outline", null);
     54                    }
     55                } else if ("background".equals(propertyName)) {
     56                    // assign background color set on editor text field to combo box
     57                    comboBox.setBackground((Color) e.getNewValue());
     58                }
     59            });
     60        }
     61
     62        return comboBoxEditor;
     63    }
     64}
  • src/org/openstreetmap/josm/plugins/flatlaf/JosmFlatTextFieldUI.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.plugins.flatlaf;
     3
     4import java.awt.Color;
     5import java.beans.PropertyChangeEvent;
     6
     7import javax.swing.JComboBox;
     8import javax.swing.JComponent;
     9import javax.swing.UIManager;
     10import javax.swing.border.LineBorder;
     11import javax.swing.plaf.ComponentUI;
     12import javax.swing.text.JTextComponent;
     13
     14import org.openstreetmap.josm.gui.download.DownloadDialog;
     15import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
     16
     17import com.formdev.flatlaf.ui.FlatTextFieldUI;
     18
     19/**
     20 * Special JOSM UI delegate for JTextField that changes a {@link LineBorder} into a FlatLaf outline.
     21 * Line borders are used in several dialogs/components to indicate warnings/errors/etc.
     22 * E.g. used by {@link AbstractTextComponentValidator} or {@link DownloadDialog}.
     23 */
     24public class JosmFlatTextFieldUI
     25    extends FlatTextFieldUI
     26{
     27    public static ComponentUI createUI(JComponent c) {
     28        return new JosmFlatTextFieldUI();
     29    }
     30
     31    @Override
     32    protected void propertyChange(PropertyChangeEvent e) {
     33        super.propertyChange(e);
     34
     35        if ("border".equals(e.getPropertyName())) {
     36            Object newBorder = e.getNewValue();
     37            JTextComponent textField = getComponent();
     38            if (!(textField.getParent() instanceof JComboBox)) {
     39                if (newBorder instanceof LineBorder) {
     40                    // change LineBorder to FlatLaf outline
     41                    Color borderColor = ((LineBorder)newBorder).getLineColor();
     42                    textField.putClientProperty("JComponent.outline", borderColor);
     43                    textField.setBorder(UIManager.getBorder("TextField.border"));
     44                } else if (newBorder == null) {
     45                    // clear FlatLaf outline
     46                    textField.putClientProperty("JComponent.outline", null);
     47                }
     48            }
     49        }
     50    }
     51}