Example #1
0
  private void init(final EncodeTableModel model) {
    setModal(true);
    setTitle("Encode Production Data");

    table.setAutoCreateRowSorter(true);
    table.setModel(model);
    table.setRowSorter(model.getSorter());
    try {
      rowCountLabel.setText(numberFormatter.valueToString(table.getRowCount()) + " rows");
    } catch (ParseException e) {

    }

    table.setRowSelectionAllowed(false);
    table.setColumnSelectionAllowed(false);

    filterTextField
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              public void changedUpdate(DocumentEvent e) {
                updateFilter();
              }

              public void insertUpdate(DocumentEvent e) {
                updateFilter();
              }

              public void removeUpdate(DocumentEvent e) {
                updateFilter();
              }
            });
  }
 /** Constructor */
 public ServiceFilterPanel(String text, Vector list) {
   empty_border = new EmptyBorder(5, 5, 0, 5);
   indent_border = new EmptyBorder(5, 25, 5, 5);
   service_box = new JCheckBox(text);
   service_box.addActionListener(this);
   service_data = new Vector();
   if (list != null) {
     service_box.setSelected(true);
     service_data = (Vector) list.clone();
   }
   service_list = new JList(service_data);
   service_list.setBorder(new EtchedBorder());
   service_list.setVisibleRowCount(5);
   service_list.addListSelectionListener(this);
   service_list.setEnabled(service_box.isSelected());
   service_scroll = new JScrollPane(service_list);
   service_scroll.setBorder(new EtchedBorder());
   remove_service_button = new JButton("Remove");
   remove_service_button.addActionListener(this);
   remove_service_button.setEnabled(false);
   remove_service_panel = new JPanel();
   remove_service_panel.setLayout(new FlowLayout());
   remove_service_panel.add(remove_service_button);
   service_area = new JPanel();
   service_area.setLayout(new BorderLayout());
   service_area.add(service_scroll, BorderLayout.CENTER);
   service_area.add(remove_service_panel, BorderLayout.EAST);
   service_area.setBorder(indent_border);
   add_service_field = new JTextField();
   add_service_field.addActionListener(this);
   add_service_field.getDocument().addDocumentListener(this);
   add_service_field.setEnabled(service_box.isSelected());
   add_service_button = new JButton("Add");
   add_service_button.addActionListener(this);
   add_service_button.setEnabled(false);
   add_service_panel = new JPanel();
   add_service_panel.setLayout(new BorderLayout());
   JPanel dummy = new JPanel();
   dummy.setBorder(empty_border);
   add_service_panel.add(dummy, BorderLayout.WEST);
   add_service_panel.add(add_service_button, BorderLayout.EAST);
   add_service_area = new JPanel();
   add_service_area.setLayout(new BorderLayout());
   add_service_area.add(add_service_field, BorderLayout.CENTER);
   add_service_area.add(add_service_panel, BorderLayout.EAST);
   add_service_area.setBorder(indent_border);
   setLayout(new BorderLayout());
   add(service_box, BorderLayout.NORTH);
   add(service_area, BorderLayout.CENTER);
   add(add_service_area, BorderLayout.SOUTH);
   setBorder(empty_border);
 }
  public WizStepManyTextFields(Wizard w, String instr, Vector strings) {
    // store wizard?
    _instructions.setText(instr);
    _instructions.setWrapStyleWord(true);
    _instructions.setEditable(false);
    _instructions.setBorder(null);
    _instructions.setBackground(_mainPanel.getBackground());

    _mainPanel.setBorder(new EtchedBorder());

    GridBagLayout gb = new GridBagLayout();
    _mainPanel.setLayout(gb);

    GridBagConstraints c = new GridBagConstraints();
    c.ipadx = 3;
    c.ipady = 3;
    c.weightx = 0.0;
    c.weighty = 0.0;
    c.anchor = GridBagConstraints.EAST;

    JLabel image = new JLabel("");
    // image.setMargin(new Insets(0, 0, 0, 0));
    image.setIcon(WIZ_ICON);
    image.setBorder(null);
    c.gridx = 0;
    c.gridheight = GridBagConstraints.REMAINDER;
    c.gridy = 0;
    c.anchor = GridBagConstraints.NORTH;
    gb.setConstraints(image, c);
    _mainPanel.add(image);

    c.weightx = 0.0;
    c.gridx = 2;
    c.gridheight = 1;
    c.gridwidth = 3;
    c.gridy = 0;
    c.fill = GridBagConstraints.NONE;
    gb.setConstraints(_instructions, c);
    _mainPanel.add(_instructions);

    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 0.0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    SpacerPanel spacer = new SpacerPanel();
    gb.setConstraints(spacer, c);
    _mainPanel.add(spacer);

    c.gridx = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    c.gridwidth = 1;
    int size = strings.size();
    for (int i = 0; i < size; i++) {
      c.gridy = 2 + i;
      String s = (String) strings.elementAt(i);
      JTextField tf = new JTextField(s, 50);
      tf.setMinimumSize(new Dimension(200, 20));
      tf.getDocument().addDocumentListener(this);
      _fields.addElement(tf);
      gb.setConstraints(tf, c);
      _mainPanel.add(tf);
    }

    c.gridx = 1;
    c.gridy = 3 + strings.size();
    c.weightx = 0.0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    SpacerPanel spacer2 = new SpacerPanel();
    gb.setConstraints(spacer2, c);
    _mainPanel.add(spacer2);
  }
  /** Creates the view's listeners */
  public void createListeners() {

    // If the table selection changes, set the currentInsuranceCompany accordingly (Listens to
    // keyboard and mouse)
    insuranceCompaniesTable
        .getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {
              public void valueChanged(ListSelectionEvent e) {
                if ((e.getValueIsAdjusting() == false)
                    && (insuranceCompaniesTable.getSelectedRow() != -1)) {
                  int selectedCompanyId =
                      Integer.valueOf(
                          (String)
                              insuranceCompaniesTable.getValueAt(
                                  insuranceCompaniesTable.getSelectedRow(), 0));
                  controller.selectInsuranceCompany(selectedCompanyId);
                }
              }
            });

    // Add a new InsuranceCompany
    addInsuranceCompanyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            controller.addInsuranceCompany();
          }
        });

    // Save an InsuranceCompany
    saveInsuranceCompanyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Check if a row is selected
            if (insuranceCompaniesTable.getSelectedRow() != -1) {
              int selectedCompanyId =
                  Integer.parseInt(
                      (String)
                          insuranceCompaniesTable.getValueAt(
                              insuranceCompaniesTable.getSelectedRow(), 0));
              String[] newData = {
                companyNameTextField.getText(),
                telephoneTextField.getText(),
                urlTextField.getText(),
                insuranceTypesTextField.getText(),
                percentageTextField.getText(),
                generalDescriptionTextField.getText()
              };

              // Input validation
              boolean checkInput = true;
              for (String input : newData) {
                if ((input.equals("")) || (input.equals(":"))) {
                  checkInput = false;
                }
              }
              // Use regex pattern to check double values
              if (!percentageTextField.getText().matches("(-|\\+)?[0-9]+(\\.[0-9]+)?")) {
                checkInput = false;
              }

              if (checkInput) {
                recordEdited = insuranceCompaniesTable.getSelectedRow();
                controller.updateInsuranceCompany(selectedCompanyId, newData);
              } else {
                showErrorMsg(
                    "Error Saving Insurance Company",
                    "Please enter proper values:\n(Fields cannot be blank or contain \":\")");
              }

            } else {
              showErrorMsg("Error Saving Insurance Company", "Please select a valid table row.");
            }
          }
        });

    // Delete an InsuranceCompany
    deleteInsuranceCompanyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // Check if a row is selected
            if (insuranceCompaniesTable.getSelectedRow() != -1) {
              int i =
                  JOptionPane.showConfirmDialog(
                      null,
                      "<html>Are you sure you want to delete following Insurance Company:<br><br><b>ID: "
                          + (String)
                              insuranceCompaniesTable.getValueAt(
                                  insuranceCompaniesTable.getSelectedRow(), 0)
                          + " - "
                          + (String)
                              insuranceCompaniesTable.getValueAt(
                                  insuranceCompaniesTable.getSelectedRow(), 1)
                          + "</b></html>",
                      "Delete Insurance Company",
                      JOptionPane.YES_NO_OPTION);

              if (i == JOptionPane.YES_OPTION) {
                int selectedCompanyId =
                    Integer.parseInt(
                        (String)
                            insuranceCompaniesTable.getValueAt(
                                insuranceCompaniesTable.getSelectedRow(), 0));
                controller.deleteInsuranceCompany(selectedCompanyId);
                searchTextField.setText("");
              }
            } else {
              showErrorMsg("Error Deleting Insurance Company", "Please select a valid table row.");
            }
          }
        });

    // Enable dynamic searching, by just typing into the searchTextField
    searchTextField
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              public void changedUpdate(DocumentEvent e) {}

              public void removeUpdate(DocumentEvent e) {
                searchInsuranceCompany();
              }

              public void insertUpdate(DocumentEvent e) {
                searchInsuranceCompany();
              }
            });

    // Clear the searchTextField
    clearSearchButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            searchTextField.setText("");
          }
        });

    // Enable sorting
    sortComboBox.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String sortStrategy = (String) sortComboBox.getSelectedItem();

            if (sortStrategy.equals("ID")) {
              model.setSortingStrategy(null);
            } else if (sortStrategy.equals("Company Name")) {
              model.setSortingStrategy(new InsuranceCompanyNameComparator());
            } else {
              model.setSortingStrategy(new InsuranceCompanyPercentageComparator());
            }

            updateTable();
          }
        });
  }