1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
public class JComboCheckBox extends JPanel {
/** la factory pour la génération de popup */
private final PopupFactory factory = PopupFactory.getSharedInstance();
/** le panel qui contient ce qu'affiche la popup */
private final Box jpPopup = Box.createVerticalBox();
/** la popup de l'IHM */
private Popup popup = null;
/** le JButton de l'IHM */
private JButton jb = null;
/** le JLabel de l'IHM */
private JLabel jl = null;
/** contient tous les items */
private ArrayList<Item> listeItems = null;
/** constructeur */
public JComboCheckBox() {
super();
init();
listeItems = new ArrayList<Item>();
calculerPopup();
}
public JComboCheckBox(Collection<? extends Object> items) {
super();
init();
listeItems = new ArrayList<Item>();
for(Object o : items) {
listeItems.add(new Item(o));
}
calculerPopup();
}
private void init() {
jl = new JLabel("Cliquez ici pour changer");
jl.addMouseListener(new InversionPopupActionListener());
jl.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
@Override
public void ancestorMoved(HierarchyEvent e) {
if(popup!=null) {
popup.hide();
afficherPopup();
}
}
});
jb = new JButton("v");
jb.addMouseListener(new InversionPopupActionListener());
setLayout(new BorderLayout());
add(jl);
add(jb,BorderLayout.EAST);
}
private void calculerPopup() {
jpPopup.removeAll();
for(Item i : listeItems) {
jpPopup.add(i.getJCheckBox());
}
Dimension dim = jpPopup.getMinimumSize();
dim.width = Math.max(dim.width,this.getSize().width);
jpPopup.setMinimumSize(dim);
}
public void removeAllItems() {
listeItems.clear();
calculerPopup();
}
public void addItem(Object item) {
listeItems.add(new Item(item));
calculerPopup();
}
public void addItems(Collection<? extends Object> items) {
for(Object o : items) {
listeItems.add(new Item(o));
}
calculerPopup();
}
public Collection<Object> getSelectedObjects() {
ArrayList<Object> res = new ArrayList<Object>();
for(Item i : listeItems) {
if(i.isSelected()) {
res.add(i.getObject());
}
}
return res;
}
private void afficherPopup() {
popup = factory.getPopup(jl,jpPopup,jl.getLocationOnScreen().x,
jl.getLocationOnScreen().y+jl.getSize().height);
popup.show();
}
private class InversionPopupActionListener extends MouseAdapter {
private void inverserPopup() {
if(popup == null) {
afficherPopup();
} else {
popup.hide();
popup = null;
}
}
@Override
public void mouseClicked(MouseEvent e) {
inverserPopup();
}
}
/**
* Classe contenant un Object et le JCheckBox allant avec.
* @author mobi38
*/
private class Item {
private Object o = null;
private boolean selected = false;
public Item(Object o) {
if(o == null) {
throw new NullPointerException(
"Le paramètre \"o\" est null.");
}
this.o = o;
}
public Object getObject() {
return o;
}
public JCheckBox getJCheckBox() {
final JCheckBox jcb = new JCheckBox(o.toString());
jcb.setSelected(selected);
jcb.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
selected = jcb.isSelected();
}
});
return jcb;
}
public boolean isSelected() {
return selected;
}
}
} |
Partager