import vnas.sm.*;
import javax.swing.*;
import java.awt.event.*;

/**
  Simple example to demonstrate how SMaker cooperates with Swing:
  The Application JFrame contains a menu bar with menu items.
  After clicking on a menu item, a dialog pops up.
*/

public class JExample4 implements ActionListener {

  private JMenuItem item1, item2, item3;
  private JDialog dialog1, dialog2, dialog3;

  /** Example Constructor
  */
  public JExample4() {

    // for example, select the system look and feel
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      // do nothing
    }

    XMLSMaker smaker = new XMLSMaker();
    try {
      // Load the XML-File into the SMaker
      smaker.load("JExample4.xml");

      // Create the Swing components
      smaker.create();
    }
    catch (Exception e) {
      System.err.println("XML file not found or error reading the XML file (" + e + ")");
      return;
    }

    // Get the Swing components from the SMaker
    JFrame frame;
    try {
      frame = (JFrame)smaker.getTarget("myFrame");
      item1 = (JMenuItem)smaker.getTarget("item1");
      item2 = (JMenuItem)smaker.getTarget("item2");
      item3 = (JMenuItem)smaker.getTarget("item3");
      dialog1 = (JDialog)smaker.getTarget("dialog1");
      dialog2 = (JDialog)smaker.getTarget("dialog2");
      dialog3 = (JDialog)smaker.getTarget("dialog3");
    }
    catch (Exception e) {
      System.err.println("target ID not found or invalid target");
      return;
    }

    // This is the usual AWT/Swing stuff
    ////////////////////////////////////

      item1.addActionListener(this);
      item2.addActionListener(this);
      item3.addActionListener(this);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );

    frame.setVisible(true);
  }

  /** After clicking on a menu item, a dialog is shown.
  */
  public void actionPerformed(ActionEvent evt) {
    JDialog currentDialog = null;
    if (evt.getSource().equals(item1)) {
      currentDialog = dialog1;
    }
    else if (evt.getSource().equals(item2)) {
      currentDialog = dialog2;
    }
    else if (evt.getSource().equals(item3)) {
      currentDialog = dialog3;
    }
    currentDialog.setVisible(true);
  }

  public static void main (String[] arg) {
    new JExample4();
  }

}