| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.table; |
| 16 | |
| 17 | import java.awt.Container; |
| 18 | import java.awt.event.ActionEvent; |
| 19 | import java.awt.event.MouseAdapter; |
| 20 | import java.awt.event.MouseEvent; |
| 21 | import java.awt.event.MouseListener; |
| 22 | |
| 23 | import javax.swing.Action; |
| 24 | import javax.swing.JPopupMenu; |
| 25 | import javax.swing.JViewport; |
| 26 | |
| 27 | /** |
| 28 | * @author Jean-Francois Poilpret |
| 29 | */ |
| 30 | public class TablePopupHandler<T> |
| 31 | { |
| 32 | public TablePopupHandler(BeanTable<T> table, TableContribution contrib) |
| 33 | { |
| 34 | _table = table; |
| 35 | _popup = contrib.getPopup(); |
| 36 | _command = contrib.getDoubleClickCommand(); |
| 37 | _mouseListener = new TableMouseListener(); |
| 38 | _table.addMouseListener(_mouseListener); |
| 39 | } |
| 40 | |
| 41 | public void setDoubleClickCommand(Action command) |
| 42 | { |
| 43 | _command = command; |
| 44 | } |
| 45 | |
| 46 | public Action getDoubleClickCommand() |
| 47 | { |
| 48 | return _command; |
| 49 | } |
| 50 | |
| 51 | public void addNotify(Container parent) |
| 52 | { |
| 53 | if ( parent instanceof JViewport |
| 54 | && _mouseListener != null) |
| 55 | { |
| 56 | parent.addMouseListener(_mouseListener); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void removeNotify(Container parent) |
| 61 | { |
| 62 | if ( parent instanceof JViewport |
| 63 | && _mouseListener != null) |
| 64 | { |
| 65 | parent.removeMouseListener(_mouseListener); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private void managePopupTrigger(MouseEvent e) |
| 70 | { |
| 71 | if (e.isPopupTrigger() && _popup != null) |
| 72 | { |
| 73 | // First select clicked row |
| 74 | int index = _table.rowAtPoint(e.getPoint()); |
| 75 | if (index >= 0) |
| 76 | { |
| 77 | _table.setRowSelectionInterval(index, index); |
| 78 | } |
| 79 | _popup.show(_table, e.getX(), e.getY()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private class TableMouseListener extends MouseAdapter |
| 84 | { |
| 85 | @Override public void mousePressed(MouseEvent e) |
| 86 | { |
| 87 | managePopupTrigger(e); |
| 88 | } |
| 89 | |
| 90 | @Override public void mouseReleased(MouseEvent e) |
| 91 | { |
| 92 | managePopupTrigger(e); |
| 93 | } |
| 94 | |
| 95 | @Override public void mouseClicked(MouseEvent e) |
| 96 | { |
| 97 | if (e.getClickCount() == 2 && _command != null && _command.isEnabled()) |
| 98 | { |
| 99 | ActionEvent evt; |
| 100 | evt = new ActionEvent( _table, |
| 101 | ActionEvent.ACTION_PERFORMED, |
| 102 | (String) _command.getValue(Action.ACTION_COMMAND_KEY)); |
| 103 | _command.actionPerformed(evt); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private final BeanTable<T> _table; |
| 109 | private final JPopupMenu _popup; |
| 110 | private Action _command; |
| 111 | private MouseListener _mouseListener = null; |
| 112 | } |