/** Builds the ui. */
  private void buildUI() {

    panel.removeAll();
    GridBagConstraints panelGC = userInterfaceFactory.createGridBagConstraints();

    String currentSelectionAreaTitleText =
        RIFJobSubmissionToolMessages.getMessage("mapAreaListSelectionPanel.title");
    JLabel currentSelectionAreaTitleLabel =
        userInterfaceFactory.createLabel(currentSelectionAreaTitleText);
    String currentSelectionAreaTitleToolTipText =
        RIFJobSubmissionToolMessages.getMessage("mapAreaListSelectionPanel.title.toolTip");
    currentSelectionAreaTitleLabel.setToolTipText(currentSelectionAreaTitleToolTipText);
    userInterfaceFactory.setBoldFont(currentSelectionAreaTitleLabel);
    panel.add(currentSelectionAreaTitleLabel, panelGC);

    panelGC.gridy++;
    panelGC.fill = GridBagConstraints.BOTH;
    panelGC.weightx = 1;
    panelGC.weighty = 0.8;
    panel.add(createMainPanel(), panelGC);

    panelGC.gridy++;
    panelGC.fill = GridBagConstraints.HORIZONTAL;
    panelGC.weightx = 1;
    panelGC.weighty = 0;
    panel.add(createMapAreaBasketPanel(), panelGC);

    updateBasketStatusPanel();
  }
  /** Update basket status panel. */
  private void updateBasketStatusPanel() {

    try {
      RIFStudySubmissionAPI service = rifSession.getRIFStudySubmissionService();
      User currentUser = rifSession.getUser();
      Geography currentGeography = rifSession.getCurrentGeography();
      GeoLevelSelect currentGeoLevelSelect = currentGeographicalArea.getGeoLevelSelect();
      GeoLevelArea currentGeoLevelArea = currentGeographicalArea.getGeoLevelArea();
      GeoLevelToMap currentGeoLevelToMap = currentGeographicalArea.getGeoLevelToMap();

      // Sensitise the control buttons appropriately to reflect
      // whether basket is empty or not
      if (currentMapAreaSelectionBasket.getSize() == 0) {
        String basketIsEmptyMessage =
            RIFJobSubmissionToolMessages.getMessage("mapAreaListSelectionPanel.basketIsEmpty");
        selectedItemsStatusTextArea.setText(basketIsEmptyMessage);

        // basket is empty
        basketControlButtonPanel.indicateEmptyState();
      } else {
        // current geo level select should only be null
        // when the application is first starting.  By this point,
        // GeoLevelSelect will not have been selected
        MapAreaSummaryData mapAreaSummaryData =
            service.getMapAreaSummaryInformation(
                currentUser,
                currentGeography,
                currentGeoLevelSelect,
                currentGeoLevelArea,
                currentGeoLevelToMap,
                currentMapAreaSelectionBasket.getMapAreas());

        // Display information about the areas currently in the basket
        StringBuilder selectedStatusMessage = new StringBuilder();
        String selectedAreasMessage =
            RIFJobSubmissionToolMessages.getMessage(
                "mapAreaListSelectionPanel.basketAreasMessage",
                String.valueOf(mapAreaSummaryData.getTotalViewAreas()),
                String.valueOf(mapAreaSummaryData.getTotalNumberToMapAreas()),
                currentGeoLevelToMap.getDisplayName());
        selectedStatusMessage.append(selectedAreasMessage);
        String totalAreaTotalPopulationMessage =
            RIFJobSubmissionToolMessages.getMessage(
                "mapAreaListSelectionPanel.totalAreaTotalPopulationMessage",
                String.valueOf(mapAreaSummaryData.getTotalPopulation()),
                String.valueOf(mapAreaSummaryData.getTotalArea()));
        selectedStatusMessage.append(totalAreaTotalPopulationMessage);
        selectedItemsStatusTextArea.setText(selectedStatusMessage.toString());

        basketControlButtonPanel.indicatePopulatedState();
      }
    } catch (RIFServiceException rifServiceException) {
      ErrorDialog.showError(parentDialog, rifServiceException.getErrorMessages());
    }
  }
  /**
   * Creates the map area basket panel.
   *
   * @return the j panel
   */
  private JPanel createMapAreaBasketPanel() {
    JPanel panel = userInterfaceFactory.createPanel();
    GridBagConstraints panelGC = userInterfaceFactory.createGridBagConstraints();

    String mapAreaBasketLabelText =
        RIFJobSubmissionToolMessages.getMessage("mapAreaListSelectionPanel.mapAreaBasket.label");
    JLabel mapAreaBasketLabel = userInterfaceFactory.createLabel(mapAreaBasketLabelText);
    userInterfaceFactory.setBoldFont(mapAreaBasketLabel);
    panel.add(mapAreaBasketLabel, panelGC);

    panelGC.gridy++;
    panelGC.fill = GridBagConstraints.BOTH;
    panelGC.weightx = 1.0;
    panelGC.weighty = 1.0;

    JScrollPane selectedItemsStatusScrollPane =
        userInterfaceFactory.createScrollPane(selectedItemsStatusTextArea);
    panel.add(selectedItemsStatusScrollPane, panelGC);

    panelGC.gridy++;
    panelGC.fill = GridBagConstraints.NONE;
    panelGC.weightx = 0;
    panelGC.weighty = 0;
    panelGC.anchor = GridBagConstraints.SOUTHEAST;
    panel.add(createBasketControlButtonsPanel(), panelGC);

    return panel;
  }
  /**
   * Creates the basket control buttons panel.
   *
   * @return the j panel
   */
  private JPanel createBasketControlButtonsPanel() {

    basketControlButtonPanel = new ListEditingButtonPanel(userInterfaceFactory);
    basketControlButtonPanel.includeAddButton(null);
    basketControlButtonPanel.includeEditButton(null);
    basketControlButtonPanel.includeDeleteButton(null);
    basketControlButtonPanel.includeClearButton(null);

    String importMapDataButtonToolTipText =
        RIFJobSubmissionToolMessages.getMessage(
            "mapAreaListSelectionPanel.buttons.importMapData.toolTip");
    basketControlButtonPanel.includeImportButton(importMapDataButtonToolTipText);

    String exportMapDataButtonToolTipText =
        RIFJobSubmissionToolMessages.getMessage(
            "mapAreaListSelectionPanel.button.exportMapData.toolTip");
    basketControlButtonPanel.includeExportButton(exportMapDataButtonToolTipText);

    basketControlButtonPanel.rightJustifyButtons();
    basketControlButtonPanel.addActionListener(this);

    return basketControlButtonPanel.getPanel();
  }
  /**
   * Instantiates a new map area list selection panel.
   *
   * @param parentDialog the parent dialog
   * @param rifSession the rif session
   */
  public MapAreaListSelectionPanel(JDialog parentDialog, RIFStudySubmissionToolSession rifSession) {

    this.parentDialog = parentDialog;
    this.rifSession = rifSession;
    this.userInterfaceFactory = rifSession.getUIFactory();

    String noDataDisplayedText =
        RIFJobSubmissionToolMessages.getMessage("mapAreaListSelectionPanel.noDataDisplayed");
    noDataDisplayedPanel =
        new NoDataAvailablePanel(userInterfaceFactory, noDataDisplayedText, true);

    mapAreaSelectionTableView = new MapAreaSelectionTableView(parentDialog, rifSession, true);
    // mapAreaSelectionTableView.updateDisplay();
    viewPanel = userInterfaceFactory.createBorderLayoutPanel();
    selectedItemsStatusTextArea = userInterfaceFactory.createNonEditableTextArea(4, 30);

    // mapImagePanel = new MapImagePanel();
    panel = userInterfaceFactory.createPanel();

    basketControlButtonPanel = new ListEditingButtonPanel(userInterfaceFactory);
  }
  /**
   * Is the appearance of the GUI if the geo level controls had not yet been selected. In this case,
   * the parts of the map area list panel would behave as follows:
   *
   * <ul>
   *   <li>the panel that would normally show the table of map areas would instead show "Select an
   *       area"
   *   <li>The panel showing the map would indicate "No map area established"
   *   <li>
   *   <li>The basket would appear cleared
   *
   * @param currentGeographicalArea the current geographical area
   * @param currentMapAreaSelectionBasket the current map area selection basket
   */
  public void initialiseResetSettings(
      AbstractGeographicalArea currentGeographicalArea,
      MapAreaSelectionBasket currentMapAreaSelectionBasket) {

    this.currentGeographicalArea = currentGeographicalArea;
    this.currentMapAreaSelectionBasket = currentMapAreaSelectionBasket;

    viewPanel.removeAll();
    viewPanel.add(noDataDisplayedPanel.getPanel(), BorderLayout.CENTER);
    viewPanel.updateUI();

    // no image can be established yet for the map image panel.
    // therefore it will display a "No map area established"
    // mapImagePanel.setData(null);

    // an update should cause the basket to appear cleared.
    String basketIsEmptyMessage =
        RIFJobSubmissionToolMessages.getMessage("mapAreaListSelectionPanel.basketIsEmpty");
    selectedItemsStatusTextArea.setText(basketIsEmptyMessage);

    updateBasketStatusPanel();
    buildUI();
  }