private void addSelections(boolean all) {

    List<Object[]> selected = new ArrayList<Object[]>();
    int[] rows;

    if (all) {
      int rowCount = individualsSTP.getTable().getModel().getRowCount();
      rows = new int[rowCount];
      for (int i = 0; i < rowCount; i++) {
        rows[i] = i;
      }
    } else {
      rows = individualsSTP.getTable().getSelectedRows();
    }

    for (int r : rows) {
      int realRow = individualsSTP.getActualRowAt(r);
      Object[] o = individualsRetriever.getIndividuals().get(realRow);
      selected.add(o);
    }

    for (int i : rows) {
      selectedRows.add(individualsSTP.getActualRowAt(i));
    }

    for (Object[] s : selected) {
      selectedHospitalIDs.add(s[INDEX_OF_HOSPITAL_ID].toString());
    }

    refreshSelectionIndicator();
  }
  private void initUI() {

    JPanel p = new JPanel();
    ViewUtil.applyVerticalBoxLayout(p);
    this.add(p);

    topPanel = ViewUtil.getClearPanel();
    middlePanel = ViewUtil.getClearPanel();
    bottomPanel = ViewUtil.getClearPanel();

    p.add(topPanel);
    p.add(middlePanel);
    p.add(bottomPanel);

    // Only display the bottom panel for multiple patient selection
    if (onlyOnePatient) {
      bottomPanel.setVisible(false);
    }

    // middle
    middlePanel.setLayout(new BorderLayout());

    individualsRetriever = new IndividualsReceiver();
    individualsSTP =
        new SearchableTablePanel(
            "Individuals",
            COLUMN_NAMES,
            COLUMN_CLASSES,
            HIDDEN_COLUMNS,
            true,
            true,
            Integer.MAX_VALUE,
            false,
            SearchableTablePanel.TableSelectionType.ROW,
            Integer.MAX_VALUE,
            individualsRetriever);
    individualsSTP.setExportButtonVisible(false);

    // If patients or cohorts are edited, update the searchabletable.
    CacheController.getInstance()
        .addListener(
            new Listener<ModificationType>() {
              @Override
              public void handleEvent(ModificationType event) {
                if (event == ModificationType.PATIENT || event == ModificationType.COHORT) {
                  if (!individualsSTP.isUpdating()) {
                    forceRefresh = true;
                    individualsSTP.forceRefreshData();
                  }
                }
              }
            });

    middlePanel.add(individualsSTP, BorderLayout.CENTER);

    // bottom
    ViewUtil.applyVerticalBoxLayout(bottomPanel);

    numselections = ViewUtil.getTitleLabel("0 individual(s) selected");

    JPanel text = ViewUtil.getClearPanel();
    ViewUtil.applyHorizontalBoxLayout(text);

    JButton clearIndividuals =
        ViewUtil.getIconButton(IconFactory.getInstance().getIcon(IconFactory.StandardIcon.CLOSE));
    clearIndividuals.setToolTipText("Clear selections");
    clearIndividuals.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            clearSelections();
          }
        });

    text.add(numselections);
    text.add(Box.createHorizontalStrut(5));
    text.add(clearIndividuals);
    bottomPanel.add(ViewUtil.centerHorizontally(text));

    final JButton addAllIndividuals = ViewUtil.getSoftButton("Add All");
    bottomPanel.add(addAllIndividuals);

    final JButton addIndividuals = ViewUtil.getSoftButton("Add Selected");
    bottomPanel.add(addIndividuals);

    final JButton removeIndividuals = ViewUtil.getSoftButton("Remove Selected");
    bottomPanel.add(removeIndividuals);

    final JButton removeAllIndividuals = ViewUtil.getSoftButton("Remove All");
    bottomPanel.add(removeAllIndividuals);

    JPanel buttons = ViewUtil.getClearPanel();
    ViewUtil.applyHorizontalBoxLayout(buttons);

    buttons.add(addAllIndividuals);
    buttons.add(addIndividuals);
    buttons.add(removeIndividuals);
    buttons.add(removeAllIndividuals);

    JPanel windowControlPanel = ViewUtil.getClearPanel();
    ViewUtil.applyHorizontalBoxLayout(windowControlPanel);
    windowControlPanel.add(Box.createHorizontalGlue());

    JButton cancel = new JButton("Cancel");
    bottomPanel.add(ViewUtil.centerHorizontally(buttons));

    ok = new JButton("OK");
    bottomPanel.add(ViewUtil.alignRight(ok));
    ok.setEnabled(false);

    windowControlPanel.add(cancel);
    windowControlPanel.add(ok);

    bottomPanel.add(windowControlPanel);

    final JDialog instance = this;

    ok.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            instance.setVisible(false);
            setIndividualsChosen(true);
          }
        });

    cancel.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            instance.setVisible(false);
            setIndividualsChosen(false || hasMadeSelections);
          }
        });

    addIndividuals.setEnabled(false);
    removeIndividuals.setEnabled(false);

    individualsSTP
        .getTable()
        .getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {
              @Override
              public void valueChanged(ListSelectionEvent lse) {
                if (!lse.getValueIsAdjusting()) {

                  int rows[] = individualsSTP.getTable().getSelectedRows();

                  boolean someSelection = rows.length > 0;

                  addIndividuals.setEnabled(someSelection);
                  removeIndividuals.setEnabled(someSelection);

                  if (someSelection) {
                    addIndividuals.setText("Add Selected (" + rows.length + ")");
                    removeIndividuals.setText("Remove Selected (" + rows.length + ")");
                  } else {
                    addIndividuals.setText("Add Selected");
                    removeIndividuals.setText("Remove Selected");
                  }

                  /* Close the dialog if only a single individual is requested. */
                  if (onlyOnePatient && rows.length == 1) {
                    selectedRows.clear();
                    selectedHospitalIDs.clear();

                    int realRow = individualsSTP.getActualRowAt(rows[0]);
                    selectedRows.add(realRow);
                    Object[] o = individualsRetriever.getIndividuals().get(realRow);
                    selectedHospitalIDs.add(o[INDEX_OF_HOSPITAL_ID].toString());

                    instance.setVisible(false);
                    setIndividualsChosen(true);

                    individualsSTP
                        .getTable()
                        .clearSelection(); // if errors crop up, this line may be causing
                    // ListSelectionEvents - can be removed
                  }
                }
              }
            });

    individualsSTP
        .getTable()
        .getModel()
        .addTableModelListener(
            new TableModelListener() {
              @Override
              public void tableChanged(TableModelEvent tme) {
                // addAllIndividuals.setText("Add All (" + stp.getTable().getModel().getRowCount() +
                // ")");
                // removeAllIndividuals.setText("Remove All (" +
                // stp.getTable().getModel().getRowCount() + ")");
              }
            });

    ActionListener addAction =
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            addSelections(false);
          }
        };

    ActionListener addAllAction =
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            addSelections(true);
          }
        };

    ActionListener removeAction =
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            removeSelections(false);
          }
        };

    ActionListener removeAllAction =
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            removeSelections(true);
          }
        };

    addIndividuals.addActionListener(addAction);
    addAllIndividuals.addActionListener(addAllAction);
    removeIndividuals.addActionListener(removeAction);
    removeAllIndividuals.addActionListener(removeAllAction);

    this.pack();
    this.setLocationRelativeTo(MedSavantFrame.getInstance());
  }