Menus.java



//: c14:Menus.java
// Podmenu, pozycje wyboru, zamiana menu,
// mnemoniki (akceleratory) i polecenia akcji.
// <applet code=Menus width=300 height=100></applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.bruceeckel.swing.*;

public class Menus extends JApplet {
  private String[] flavors = { 
    "Czekoladowe", "Truskawkowe", "Waniliowe", 
    "Miętowe", "Mocca", "Rumowe",
    "Karmelowe", "Bakaliowe" 
  };
  private JTextField t = new JTextField("Nie ma smaku", 30);
  private JMenuBar mb1 = new JMenuBar();
  private JMenu
    f = new JMenu("Pliki"),
    m = new JMenu("Smaki"),
    s = new JMenu("Bezpieczeństwo");
  // Alternatywne rozwiązanie:
  private JCheckBoxMenuItem[] safety = {
    new JCheckBoxMenuItem("Chroń"),
    new JCheckBoxMenuItem("Ukryj")
  };
  private JMenuItem[] file = { new JMenuItem("Otwórz") };
  // Drugi pasek menu:
  private JMenuBar mb2 = new JMenuBar();
  private JMenu fooBar = new JMenu("innyPasek");
  private JMenuItem[] other = {
    // Dodawanie do menu skrótów (mnemoników)  
    // jest bardzo proste ale tylko elementom menu
    // można przekazać je w konstruktorach:
    new JMenuItem("Ene", KeyEvent.VK_F),
    new JMenuItem("Due", KeyEvent.VK_A),
    // Bez skrótu:
    new JMenuItem("Rabe"),
  };
  private JButton b = new JButton("Zamień menu");
  class BL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JMenuBar m = getJMenuBar();
      setJMenuBar(m == mb1 ? mb2 : mb1);
      validate(); // Odśwież ramkę
    }
  }
  class ML implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JMenuItem target = (JMenuItem)e.getSource();
      String actionCommand = target.getActionCommand();
      if(actionCommand.equals("Open")) {
        String s = t.getText();
        boolean chosen = false;
        for(int i = 0; i < flavors.length; i++)
          if(s.equals(flavors[i])) chosen = true;
        if(!chosen)
          t.setText("Najpierw wybierz smak!");
        else
          t.setText("Otwieram " + s + ". Mniam, mniam!");
      }
    }
  }
  class FL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JMenuItem target = (JMenuItem)e.getSource();
      t.setText(target.getText());
    }
  }
  // Alternatywnie można stworzyć oddzielną klasę
  // dla każdego z elementów menu. Wtedy nie trzeba
  // zgadywać, który to jest element:
  class FooL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      t.setText("Wybrano ene");
    }
  }
  class BarL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      t.setText("Wybrano due");
    }
  }
  class BazL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      t.setText("Wybrano rabe");
    }
  }
  class CMIL implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
      JCheckBoxMenuItem target =
        (JCheckBoxMenuItem)e.getSource();
      String actionCommand = target.getActionCommand();
      if(actionCommand.equals("Chroń"))
        t.setText("Chroń lody! " +
          "Chronienie jest ustawione na " + target.getState());
      else if(actionCommand.equals("Ukryj"))
        t.setText("Ukryj lody! " +
          "Czy lody są ukryte? " + target.getState());
    }
  }
  public void init() {
    ML ml = new ML();
    CMIL cmil = new CMIL();
    safety[0].setActionCommand("Chroń");
    safety[0].setMnemonic(KeyEvent.VK_G);
    safety[0].addItemListener(cmil);
    safety[1].setActionCommand("Ukryj");
    safety[1].setMnemonic(KeyEvent.VK_H);
    safety[1].addItemListener(cmil);
    other[0].addActionListener(new FooL());
    other[1].addActionListener(new BarL());
    other[2].addActionListener(new BazL());
    FL fl = new FL();
    for(int i = 0; i < flavors.length; i++) {
      JMenuItem mi = new JMenuItem(flavors[i]);
      mi.addActionListener(fl);
      m.add(mi);
      // Dodaj separatory w równych odstępach:
      if((i+1) % 3 == 0)
        m.addSeparator();
    }
    for(int i = 0; i < safety.length; i++)
      s.add(safety[i]);
    s.setMnemonic(KeyEvent.VK_A);
    f.add(s);
    f.setMnemonic(KeyEvent.VK_F);
    for(int i = 0; i < file.length; i++) {
      file[i].addActionListener(fl);
      f.add(file[i]);
    }
    mb1.add(f);
    mb1.add(m);
    setJMenuBar(mb1);
    t.setEditable(false);
    Container cp = getContentPane();
    cp.add(t, BorderLayout.CENTER);
    // Ustaw system do zamiany menu:
    b.addActionListener(new BL());
    b.setMnemonic(KeyEvent.VK_S);
    cp.add(b, BorderLayout.NORTH);
    for(int i = 0; i < other.length; i++)
      fooBar.add(other[i]);
    fooBar.setMnemonic(KeyEvent.VK_B);
    mb2.add(fooBar);
  }
  public static void main(String[] args) {
    Console.run(new Menus(), 300, 100);
  }
} ///:~