Exemple #1
0
  /** Create and layout the visual components. */
  private void initComponents() {
    setTitle("Chart Settings");
    setSize(new Dimension(450, 375));
    setResizable(false);

    addWindowListener(
        new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent evt) {
            closeDialog(evt);
          }
        });

    Box mainView = new Box(VERTICAL);
    getContentPane().add(mainView);

    Box row;

    Box yAxisView = new Box(VERTICAL);
    yAxisView.setBorder(border);
    mainView.add(yAxisView);

    yAutoScaleCheckbox = new JCheckBox("Auto Scale");
    yAutoScaleCheckbox.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            yAutoScaleCheckboxActionPerformed(evt);
          }
        });
    row = new Box(HORIZONTAL);
    row.add(Box.createHorizontalGlue());
    row.add(yAutoScaleCheckbox);
    yAxisView.add(row);

    yAxisMinValueField = new JTextField(10);
    yAxisMinValueField.setMaximumSize(yAxisMinValueField.getPreferredSize());
    yAxisMinValueField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    yAxisMinValueField.addFocusListener(
        new FocusAdapter() {
          public void focusGained(FocusEvent event) {
            yAxisMinValueField.selectAll();
          }

          public void focusLost(FocusEvent event) {
            yAxisMinValueField.setCaretPosition(0);
            yAxisMinValueField.moveCaretPosition(0);
          }
        });
    row = new Box(HORIZONTAL);
    row.add(Box.createHorizontalGlue());
    row.add(new JLabel("Min:"));
    row.add(yAxisMinValueField);
    yAxisView.add(row);

    yAxisMaxValueField = new JTextField(10);
    yAxisMaxValueField.setMaximumSize(yAxisMaxValueField.getPreferredSize());
    yAxisMaxValueField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    yAxisMaxValueField.addFocusListener(
        new FocusAdapter() {
          public void focusGained(FocusEvent event) {
            yAxisMaxValueField.selectAll();
          }

          public void focusLost(FocusEvent event) {
            yAxisMaxValueField.setCaretPosition(0);
            yAxisMaxValueField.moveCaretPosition(0);
          }
        });
    row = new Box(HORIZONTAL);
    row.add(Box.createHorizontalGlue());
    row.add(new JLabel("Max:"));
    row.add(yAxisMaxValueField);
    yAxisView.add(row);

    yAxisDivisionsField = new JTextField(10);
    yAxisDivisionsField.setMaximumSize(yAxisDivisionsField.getPreferredSize());
    yAxisDivisionsField.setHorizontalAlignment(JTextField.RIGHT);
    yAxisDivisionsField.addFocusListener(
        new FocusAdapter() {
          public void focusGained(FocusEvent event) {
            yAxisDivisionsField.selectAll();
          }

          public void focusLost(FocusEvent event) {
            yAxisDivisionsField.setCaretPosition(0);
            yAxisDivisionsField.moveCaretPosition(0);
          }
        });
    row = new Box(HORIZONTAL);
    row.add(Box.createHorizontalGlue());
    row.add(new JLabel("Major Divisions:"));
    row.add(yAxisDivisionsField);
    yAxisView.add(row);

    Box buttonView = new Box(HORIZONTAL);
    mainView.add(buttonView);
    buttonView.add(Box.createHorizontalGlue());

    JButton revertButton = new JButton("Revert");
    revertButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            revertButtonActionPerformed(event);
          }
        });
    buttonView.add(revertButton);

    JButton applyButton = new JButton("Apply");
    applyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            applyButtonActionPerformed(event);
          }
        });
    buttonView.add(applyButton);

    pack();
  }
  /**
   * Creates a new <code>AddPersonDialog</code> with a given owner <code>JFrame</code> and <code>
   * FamilyTree</code>.
   */
  public AddPersonDialog(JFrame owner, FamilyTree tree) {
    super(owner, "Add New Person", true /* modal */);

    Container pane = this.getContentPane();
    pane.setLayout(new BorderLayout());

    JPanel infoPanel = new JPanel();
    infoPanel.setLayout(new GridLayout(0, 2));
    Border infoBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
    infoPanel.setBorder(infoBorder);

    infoPanel.add(new JLabel("id:"));
    final JTextField idField = new JTextField();
    infoPanel.add(idField);

    final ButtonGroup group = new ButtonGroup();

    final JRadioButton male = new JRadioButton("male", true);
    group.add(male);
    infoPanel.add(male);

    final JRadioButton female = new JRadioButton("female");
    group.add(female);
    infoPanel.add(female);

    infoPanel.add(new JLabel("First name:"));
    final JTextField firstNameField = new JTextField();
    infoPanel.add(firstNameField);

    infoPanel.add(new JLabel("Middle name:"));
    final JTextField middleNameField = new JTextField();
    infoPanel.add(middleNameField);

    infoPanel.add(new JLabel("Last name:"));
    final JTextField lastNameField = new JTextField();
    infoPanel.add(lastNameField);

    infoPanel.add(new JLabel("Date of Birth:"));
    final JTextField dobField = new JTextField();
    infoPanel.add(dobField);

    infoPanel.add(new JLabel("Date of Death:"));
    final JTextField dodField = new JTextField();
    infoPanel.add(dodField);

    infoPanel.add(new JLabel("Father:"));
    JPanel fatherPanel = new JPanel();
    fatherPanel.setLayout(new FlowLayout());
    final JTextField fatherText = new JTextField("Click to choose");
    fatherText.setEditable(false);
    fatherText.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            // Pop up a PersonChooseDialog, place name in TextField
            System.out.println("Clicked father");
          }
        });
    fatherPanel.add(fatherText);
    infoPanel.add(fatherPanel);

    infoPanel.add(new JLabel("Mother:"));
    JPanel motherPanel = new JPanel();
    motherPanel.setLayout(new FlowLayout());
    final JTextField motherText = new JTextField("Click to choose");
    motherText.setEditable(false);
    motherText.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            // Pop up a PersonChooseDialog, place name in TextField
            System.out.println("Clicked mother");
          }
        });
    motherPanel.add(motherText);
    infoPanel.add(motherPanel);

    pane.add(infoPanel, BorderLayout.NORTH);

    // "Add" and "Cancel" buttons
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    buttonPanel.add(Box.createHorizontalGlue());

    JButton addButton = new JButton("Add");
    addButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Create a new person based on the information entered in
            // this dialog
            int id = 0;
            try {
              id = Integer.parseInt(idField.getText());

            } catch (NumberFormatException ex) {
              error("Invalid id: " + idField.getText());
              return;
            }

            String text = null;

            text = dobField.getText();
            Date dob = null;
            if (text != null && !text.equals("")) {
              dob = parseDate(dobField.getText());
              if (dob == null) {
                // Parse error
                return;
              }
            }

            text = dodField.getText();
            Date dod = null;
            if (text != null && !text.equals("")) {
              dod = parseDate(dodField.getText());
              if (dod == null) {
                // Parse error
                return;
              }
            }

            Person.Gender gender;
            if (group.getSelection().equals(male)) {
              gender = Person.MALE;

            } else {
              gender = Person.FEMALE;
            }

            // Okay, everything parsed alright
            newPerson = new Person(id, gender);
            newPerson.setFirstName(firstNameField.getText());
            newPerson.setMiddleName(middleNameField.getText());
            newPerson.setLastName(lastNameField.getText());
            newPerson.setDateOfBirth(dob);
            newPerson.setDateOfDeath(dod);

            if (mother != null) {
              newPerson.setMother(mother);
            }

            if (father != null) {
              newPerson.setFather(father);
            }

            // We're all happy
            AddPersonDialog.this.dispose();
          }
        });
    buttonPanel.add(addButton);

    buttonPanel.add(Box.createHorizontalGlue());

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Read my lips, no new Person!

            AddPersonDialog.this.newPerson = null;

            AddPersonDialog.this.dispose();
          }
        });
    buttonPanel.add(cancelButton);

    buttonPanel.add(Box.createHorizontalGlue());

    pane.add(buttonPanel, BorderLayout.SOUTH);
  }