/**
   * Get edited Value (MLocation) for GoogleMaps
   *
   * @param MLocation location
   * @return String address
   */
  private String getGoogleMapsLocation(MLocation location) {

    MRegion region = new MRegion(Env.getCtx(), location.getC_Region_ID(), null);
    String address = "";
    address = address + (location.getAddress1() != null ? location.getAddress1() + ", " : "");
    address = address + (location.getAddress2() != null ? location.getAddress2() + ", " : "");
    address = address + (location.getCity() != null ? location.getCity() + ", " : "");
    address = address + (region.getName() != null ? region.getName() + ", " : "");
    address = address + (location.getCountryName() != null ? location.getCountryName() : "");

    return address.replace(" ", "+");
  }
  /** OK - check for changes (save them) & Exit */
  private void action_OK() {
    m_location.setAddress1(fAddress1.getText());
    m_location.setAddress2(fAddress2.getText());
    m_location.setAddress3(fAddress3.getText());
    m_location.setAddress4(fAddress4.getText());
    // m_location.setCity(fCity.getText()); Kenos (linha comentada)
    m_location.setPostal(fPostal.getText());
    m_location.setPostal_Add(fPostalAdd.getText());
    //  Country/Region
    MCountry c = (MCountry) fCountry.getSelectedItem();
    m_location.setCountry(c);
    if (m_location.getCountry().isHasRegion()) {
      MRegion r = (MRegion) fRegion.getSelectedItem();
      m_location.setRegion(r);
      m_location.setRegionName(r.getName());
    } else {
      m_location.setC_Region_ID(0);
      m_location.setRegionName(null);
    }

    // BEGIN fernandom @ faire.com.br
    // SET C_City_ID
    if (fCity.getSelectedItem() != null) {
      Object oo = fCity.getSelectedItem();
      if (oo instanceof MCity) {
        MCity city = (MCity) fCity.getSelectedItem();
        m_location.setC_City_ID(city.getC_City_ID());
        m_location.setCity(city.getName());
      } else {
        m_location.setCity((String) fCity.getSelectedItem()); // Kenos
        m_location.setC_City_ID(0);
      }
    }
    // END

    //	Save changes
    m_location.set_TrxName(null);
    m_location.save();
  } //	actionOK
  /**
   * ActionListener
   *
   * @param e ActionEvent
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals(ConfirmPanel.A_OK)) {
      if (fAddress1.isMandatory() && ((((String) fAddress1.getValue()).trim()).isEmpty()))
        JOptionPane.showMessageDialog(null, "Preencha todos os campos corretamente");
      else if (fAddress2.isMandatory() && ((((String) fAddress2.getValue()).trim()).isEmpty()))
        JOptionPane.showMessageDialog(null, "Preencha todos os campos corretamente");
      else if (fAddress3.isMandatory() && ((((String) fAddress3.getValue()).trim()).isEmpty()))
        JOptionPane.showMessageDialog(null, "Preencha todos os campos corretamente");
      else if (fPostal.isMandatory() && ((((String) fPostal.getValue()).trim()).isEmpty()))
        JOptionPane.showMessageDialog(null, "Preencha todos os campos corretamente");
      else {
        action_OK();
        m_change = true;
        dispose();
      }
    } else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL)) {
      m_change = false;
      dispose();
    } else if (e.getSource() == fCountry) {
      //	Country Changed - display in new Format
      //	Modifier for Mouse selection is 16  - for any key selection 0
      MCountry c = (MCountry) fCountry.getSelectedItem();
      m_location.setCountry(c);
      //	refrseh
      mainPanel.removeAll();
      initLocation();
      fCountry.requestFocus(); // 	allows to use Keybord selection
    }
    // Kenos
    else if (e.getSource() == fRegion) {
      //	Modifier for Mouse selection is 16  - for any key selection 0
      MRegion r = (MRegion) fRegion.getSelectedItem();
      m_location.setRegion(r);
      //	refrseh
      mainPanel.removeAll();
      initLocation();
      fRegion.requestFocus(); // 	allows to use Keybord selection
    }
    // Kenos
    else if (e.getSource() == toLink) {
      String urlString = GOOGLE_MAPS_URL_PREFIX + getGoogleMapsLocation(m_location);
      String message = null;

      try {
        new URL(urlString);
        Env.startBrowser(urlString);
      } catch (Exception ex) {
        message = ex.getMessage();
        ADialog.warn(0, this, "URLnotValid", message);
      }
    } else if (e.getSource() == toRoute) {
      int AD_Org_ID = Env.getAD_Org_ID(Env.getCtx());
      if (AD_Org_ID != 0) {
        MOrgInfo orgInfo = MOrgInfo.get(Env.getCtx(), AD_Org_ID, null);
        MLocation orgLocation = new MLocation(Env.getCtx(), orgInfo.getC_Location_ID(), null);

        String urlString =
            GOOGLE_MAPS_ROUTE_PREFIX
                + GOOGLE_SOURCE_ADDRESS
                + getGoogleMapsLocation(orgLocation)
                + // org
                GOOGLE_DESTINATION_ADDRESS
                + getGoogleMapsLocation(m_location); // partner
        String message = null;
        try {
          new URL(urlString);
          Env.startBrowser(urlString);
        } catch (Exception ex) {
          message = ex.getMessage();
          ADialog.warn(0, this, "URLnotValid", message);
        }
      }
    } else if (e.getSource() == getAddress) {
      if (fPostal != null && !fPostal.getText().equals("")) {
        if (!fAddress1.getText().equals("")
            || !fAddress2.getText().equals("")
            || !fAddress3.getText().equals("")
            || !fAddress4.getText().equals("")
            || fCity.getSelectedIndex() > 0) {
          String warningMsg = "O endereço atual será substituido. Deseja continuar?";
          String warningTitle = "Aviso";
          int response =
              JOptionPane.showConfirmDialog(
                  null, warningMsg, warningTitle, JOptionPane.YES_NO_OPTION);
          if (response == JOptionPane.NO_OPTION) return;
        }

        WebServiceCep cep = WebServiceCep.searchCep(fPostal.getText());
        if (cep.wasSuccessful()) {
          MRegion[] regions = MRegion.getRegions(Env.getCtx(), 139);
          for (MRegion r : regions)
            if (r.getName() != null && r.getName().equals(cep.getUf())) {
              fRegion.setSelectedItem(r);
              break;
            }
          fCity.setSelectedItem(cep.getCidade());
          fAddress1.setText(cep.getLogradouroType() + " " + cep.getLogradouro());
          fAddress3.setText(cep.getBairro());
          if (cep.getCep().length() == 8)
            fPostal.setText(cep.getCep().substring(0, 5) + "-" + cep.getCep().substring(5));
          else fPostal.setText(cep.getCep());
        } else if (cep.getResulCode() == 0)
          JOptionPane.showMessageDialog(null, "CEP não encontrado na base de dados.");
        else if (cep.getResulCode() == 14)
          JOptionPane.showMessageDialog(
              null, "Não foi possível fazer a busca. (Possível problema com a Internet).");
        else JOptionPane.showMessageDialog(null, "Erro ao fazer a busca.");
      } else JOptionPane.showMessageDialog(null, "Preencha o CEP.");
    }
  } //	actionPerformed