View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.plaf;
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 javax.swing.*;
28  import javax.swing.plaf.ComponentUI;
29  import javax.swing.plaf.basic.BasicComboPopup;
30  import javax.swing.plaf.basic.ComboPopup;
31  import javax.swing.plaf.synth.SynthComboBoxUI;
32  import java.awt.*;
33  
34  /**
35   * a SynthComboBoxUI specific that extends list popup even when combo itself it smaller than the items width
36   *
37   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
38   */
39  public class WiderSynthComboBoxUI extends SynthComboBoxUI {
40  
41      private BasicComboPopup popup;
42  
43      /**
44       * {@inheritDoc}
45       */
46      public static ComponentUI createUI(JComponent c) {
47          return new WiderSynthComboBoxUI();
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      protected ComboPopup createPopup() {
55          popup = new BasicComboPopup(comboBox) {
56  
57              @Override
58              @SuppressWarnings("unchecked")
59              protected void configureList() {
60                  list.setFont(comboBox.getFont());
61                  list.setCellRenderer(comboBox.getRenderer());
62                  list.setFocusable(false);
63                  list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
64                  int selectedIndex = comboBox.getSelectedIndex();
65                  if (selectedIndex == -1) {
66                      list.clearSelection();
67                  } else {
68                      list.setSelectedIndex(selectedIndex);
69                      list.ensureIndexIsVisible(selectedIndex);
70                  }
71                  installListListeners();
72              }
73  
74              @Override
75              protected Rectangle computePopupBounds(int px, int py, int pw, int ph) {
76                  return super.computePopupBounds(px, py, Math.max(comboBox.getPreferredSize().width, pw), ph);
77              }
78  
79          };
80          popup.getAccessibleContext().setAccessibleParent(comboBox);
81          return popup;
82      }
83  
84      public JList getPopupList() {
85          return popup.getList();
86      }
87  
88  //    TODO : active this code only if there is exceptions on UI event queue because JAXX combo force hide popup on reset, even if the popup is hidden
89  //      this cause unnecessary UI refresh... (see jaxx.runtime.swing.editor.bean.BeanFilterableComboBoxHandler.reset)
90  //    /**
91  //     * Override setPopupVisible to check visible state before action
92  //     *
93  //     * @param combobox
94  //     * @param visible
95  //     */
96  //    @Override
97  //    public void setPopupVisible(JComboBox combobox, boolean visible) {
98  //
99  //        if ( visible ) {
100 //            if (!popup.isVisible())
101 //                popup.show();
102 //        } else {
103 //            if (popup.isVisible())
104 //                popup.hide();
105 //        }
106 //
107 //    }
108 }