View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.component;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import org.jdesktop.swingx.util.PaintUtils;
28  
29  import javax.swing.*;
30  import javax.swing.border.EmptyBorder;
31  import java.awt.*;
32  
33  /**
34   * <p>ActionListCellRenderer class.</p>
35   *
36   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
37   */
38  public class ActionListCellRenderer extends DefaultListCellRenderer {
39  
40      private final JComboBox comboBox;
41  
42      protected final EmptyBorder border = new EmptyBorder(4, 8, 4, 8);
43  
44      /**
45       * <p>Constructor for ActionListCellRenderer.</p>
46       *
47       * @param comboBox a {@link javax.swing.JComboBox} object.
48       */
49      public ActionListCellRenderer(JComboBox comboBox) {
50          super();
51          this.comboBox = comboBox;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
59  
60          // by precaution
61          if (list == null) {
62              list = new JList<>();
63          }
64  
65          DefaultListCellRenderer result = (DefaultListCellRenderer) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
66  
67          if (value instanceof JButton) {
68              JButton button = (JButton) value;
69              result.setEnabled(comboBox.isEnabled() && button.isEnabled() && (button.getAction() == null || button.getAction().isEnabled()));
70              Action action = button.getAction();
71              if (action != null) {
72                  result.setIcon((Icon) action.getValue(Action.LARGE_ICON_KEY));
73                  result.setText((String) action.getValue(Action.NAME));
74                  result.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
75              } else {
76                  result.setIcon(button.getIcon());
77                  result.setText(button.getText());
78                  result.setToolTipText(button.getToolTipText());
79              }
80              if (isSelected) {
81                  result.setBackground(UIManager.getColor("nimbusSelection"));
82                  result.setForeground(PaintUtils.computeForeground(UIManager.getColor("nimbusSelection")));
83              }
84              result.setBorder(border);
85              return result;
86          }
87          throw new ClassCastException("the value is not an instance of JButton");
88      }
89  }