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
| public class DemoMenus {
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionManager actionManager = new ActionManager();
JMenuBar menu = createMenu(actionManager);
frame.setJMenuBar(menu);
JComboBox<Role> roleCombo = new JComboBox<>(Role.values());
roleCombo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if( value instanceof Role) {
value = ((Role)value).getLabel();
}
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
});
roleCombo.addActionListener(e-> actionManager.setRole((Role)roleCombo.getSelectedItem()));
actionManager.setRole(Role.ADMIN);
JPanel panel = new JPanel(new GridBagLayout());
panel.add(roleCombo);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JMenuBar createMenu(ActionManager actionManager) {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile=new JMenu("File");
menuFile.add(new MMenuItem(actionManager.getAction(ActionId.OPEN)));
menuFile.add(new MMenuItem(actionManager.getAction(ActionId.SAVE)));
menuFile.add(new MMenuItem(actionManager.getAction(ActionId.QUIT)));
menuBar.add(menuFile);
return menuBar;
}
// un menu qui se cache lorsque l'action est disabled
public static class MMenuItem extends JMenuItem {
public MMenuItem(Action action) {
super(action);
action.addPropertyChangeListener(e-> {
if ( "enabled".equals(e.getPropertyName()) ) {
setVisible(action.isEnabled());
}
});
}
}
private enum Role {
ADMIN("Administrateur"),
USER("Utilisateur");
private final String label;
private Role(String label) {
this.label=label;
}
public String getLabel() {
return label;
}
}
private enum ActionId {
OPEN,
SAVE,
QUIT;
}
private static class ActionManager {
private final EnumMap<ActionId,Action> actions;
private final EnumSet<ActionId> adminActions;
public ActionManager() {
actions=new EnumMap<>(ActionId.class);
actions.put(ActionId.OPEN, new AbstractAction("Ouvrir") {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
actions.put(ActionId.SAVE, new AbstractAction("Sauver") {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
actions.put(ActionId.QUIT, new AbstractAction("Quitter") {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
adminActions = EnumSet.of(ActionId.OPEN, ActionId.SAVE);
}
public void setRole(Role role) {
adminActions.stream().map(this::getAction).forEach(a->a.setEnabled(role==Role.ADMIN));
}
public Action getAction(ActionId actionId) {
return actions.get(actionId);
}
}
} |
Partager