OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Compare Revisions

Regard whitespace Rev 20 → Rev 21

/trunk/OpenConcerto/src/org/openconcerto/ui/component/MutableListComboPopupListener.java
14,17 → 14,77
package org.openconcerto.ui.component;
 
import static org.openconcerto.ui.component.ComboLockedMode.UNLOCKED;
import org.openconcerto.ui.PopupMouseListener;
import org.openconcerto.ui.SwingThreadUtils;
 
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashSet;
import java.util.Set;
 
import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
 
public class MutableListComboPopupListener extends MouseAdapter {
public class MutableListComboPopupListener {
 
static private final Set<JPopupMenu> toClose = new HashSet<JPopupMenu>();
static private final PropertyChangeListener propL = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
final JPopupMenu src = (JPopupMenu) evt.getSource();
if (!src.isVisible()) {
removePopup(src);
}
}
};
/**
* normally BasicPopupMenuUI.MouseGrabber.eventDispatched() closes popups, but BasicComboBoxUI
* sets HIDE_POPUP_KEY to indicate that it will take care of all popups, including ours, so we
* must mimic its behavior for JComboBox.
*/
static private final AWTEventListener l = new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
final MouseEvent me = (MouseEvent) event;
if (me.getID() != MouseEvent.MOUSE_PRESSED && me.getID() != MouseEvent.MOUSE_WHEEL)
return;
 
final Component src = me.getComponent();
final JPopupMenu menuClicked = SwingThreadUtils.getAncestorOrSelf(JPopupMenu.class, src);
final Set<JPopupMenu> sansClicked = new HashSet<JPopupMenu>(toClose);
if (menuClicked != null)
sansClicked.remove(menuClicked);
for (JPopupMenu menu : sansClicked) {
menu.setVisible(false);
}
}
};
 
static private void addPopup(JPopupMenu p) {
// ATTN the popup might not yet be visible
if (toClose.add(p) && toClose.size() == 1) {
Toolkit.getDefaultToolkit().addAWTEventListener(l, AWTEvent.MOUSE_EVENT_MASK);
}
// if the popup is closed for any reason (programmatically, menu item chosen), we no longer
// need to handle it
p.addPropertyChangeListener("visible", propL);
}
 
static private void removePopup(JPopupMenu p) {
p.removePropertyChangeListener("visible", propL);
if (toClose.remove(p) && toClose.size() == 0) {
Toolkit.getDefaultToolkit().removeAWTEventListener(l);
}
}
 
private final MutableListCombo combo;
private JPopupMenu popup;
 
35,15 → 95,24
}
 
public void listen() {
this.combo.getPopupComp().addMouseListener(this);
this.combo.getPopupComp().addMouseListener(new PopupMouseListener() {
@Override
protected JPopupMenu createPopup(MouseEvent e) {
return getPopup(e);
}
 
public final void mouseReleased(MouseEvent ev) {
// right-click only, respect enabled status
if (ev.getButton() != MouseEvent.BUTTON3 || !this.combo.getPopupComp().isEnabled()) {
return;
});
}
 
protected final JPopupMenu getPopup(MouseEvent ev) {
// respect enabled status
if (!this.combo.getPopupComp().isEnabled())
return null;
// Bouton droit en non locked
// On debloque le lock en faisant CTRL + bouton droit
final boolean displayPopup = this.combo.getMode() == UNLOCKED || (ev.isControlDown() && this.combo.getMode() == ComboLockedMode.ITEMS_LOCKED);
if (!displayPopup)
return null;
 
if (this.popup == null) {
this.popup = new JPopupMenu();
final JMenuItem menu1 = new JMenuItem("Ajouter à la liste");
62,10 → 131,9
}
});
}
// Bouton droit en non locked
// On debloque le lock en faisant CTRL + bouton droit
if (this.combo.getMode() == UNLOCKED || (ev.isControlDown() && this.combo.getMode() == ComboLockedMode.ITEMS_LOCKED)) {
this.popup.show(this.combo.getPopupComp(), ev.getX(), ev.getY());
// popups are never closed in a JComboBox (except when choosing a menu item)
if (SwingThreadUtils.getAncestorOrSelf(JComboBox.class, this.combo.getPopupComp()) != null)
addPopup(this.popup);
return this.popup;
}
}
}