View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.renderer;
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 fr.ifremer.quadrige3.ui.swing.table.editor.ButtonCellEditor;
28  
29  import javax.swing.AbstractAction;
30  import javax.swing.JMenuItem;
31  import javax.swing.JPopupMenu;
32  import javax.swing.JTable;
33  import java.awt.Component;
34  import java.awt.event.ActionEvent;
35  import java.awt.event.MouseAdapter;
36  import java.awt.event.MouseEvent;
37  import java.util.List;
38  
39  /**
40   * <p>Abstract DropDownButtonCellEditor class.</p>
41   *
42   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
43   * @param <E> bean type to display in drop down menu
44   */
45  public abstract class DropDownButtonCellEditor<E> extends ButtonCellEditor {
46  
47      private JPopupMenu popup = new JPopupMenu();
48  
49      /**
50       * abstract method to populate items
51       *
52       * @param table a {@link JTable} object.
53       * @param row a int.
54       * @param column a int.
55       * @return a {@link List} object.
56       */
57      public abstract List<E> getDropDownItems(JTable table, int row, int column);
58  
59      /**
60       * abstract method to decorator the item in the menu
61       *
62       * @param item a E object.
63       * @return a {@link String} object.
64       */
65      public abstract String decorateItem(E item);
66  
67      /**
68       * abstract method to intercept the action when the menu item is pressed
69       *
70       * @param item a E object.
71       * @param row a int.
72       * @param column a int.
73       */
74      public abstract void onItemMenuAction(E item, int row, int column);
75  
76      /**
77       * <p>Constructor for DropDownButtonCellEditor.</p>
78       */
79      public DropDownButtonCellEditor() {
80          super();
81  
82          button.addMouseListener(new MouseAdapter() {
83  
84              @Override
85              public void mousePressed(MouseEvent e) {
86                  // FIXME: when popup is shown, the button background is reset
87                  showPopup();
88              }
89          });
90  
91      }
92  
93      /**
94       * <p>showPopup.</p>
95       */
96      public void showPopup() {
97          popup.show(button, 0, button.getHeight());
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, final int column) {
103 
104         // add dropdown items
105         popup = new JPopupMenu();
106         List<E> items = getDropDownItems(table, row, column);
107 
108         if (items != null) {
109             for (final E item : items) {
110 
111                 AbstractAction action = new AbstractAction(decorateItem(item)) {
112 
113                     @Override
114                     public void actionPerformed(ActionEvent e) {
115                         onItemMenuAction(item, row, column);
116                     }
117                 };
118 
119                 popup.add(new JMenuItem(action));
120             }
121         }
122 
123         return super.getTableCellEditorComponent(table, value, isSelected, row, column);
124     }
125 
126 }