private void displaySelectedValues() {
    DatabaseSrs refSys = srsComboBox.getSelectedItem();
    if (refSys == null) return;

    sridText.setValue(refSys.getSrid());
    gmlSrsNameText.setText(refSys.getGMLSrsName());
    descriptionText.setText(refSys.toString());
    srsComboBox.setToolTipText(refSys.getDescription());
    dbSrsNameText.setText(wrap(refSys.getDatabaseSrsName(), 40));
    dbSrsTypeText.setText(refSys.getType().toString());

    boolean isDBSrs = !srsComboBox.isDBReferenceSystemSelected();
    sridText.setEditable(isDBSrs);
    gmlSrsNameText.setEditable(isDBSrs);
    descriptionText.setEditable(isDBSrs);
    applyButton.setEnabled(isDBSrs);
    deleteButton.setEnabled(isDBSrs);
    copyButton.setEnabled(!dbPool.isConnected() ? isDBSrs : true);
  }
  @Override
  public boolean isModified() {
    DatabaseSrs refSys = srsComboBox.getSelectedItem();

    try {
      sridText.commitEdit();
    } catch (ParseException e) {
    }
    if (((Number) sridText.getValue()).intValue() != refSys.getSrid()) return true;

    if (!gmlSrsNameText.getText().equals(refSys.getGMLSrsName())) return true;
    if (!descriptionText.getText().equals(refSys.getDescription())) return true;

    return false;
  }
  private boolean requestChange() {
    if (isModified()) {
      String text = Language.I18N.getString("pref.db.srs.apply.msg");
      Object[] args = new Object[] {srsComboBox.getSelectedItem().getDescription()};
      String formattedMsg = MessageFormat.format(text, args);

      int res =
          JOptionPane.showConfirmDialog(
              getTopLevelAncestor(),
              formattedMsg,
              Language.I18N.getString("pref.db.srs.apply.title"),
              JOptionPane.YES_NO_CANCEL_OPTION);
      if (res == JOptionPane.CANCEL_OPTION) return false;
      else if (res == JOptionPane.YES_OPTION) {
        setSettings();
      } else loadSettings();
    }

    return true;
  }
  @Override
  public void setSettings() {
    DatabaseSrs refSys = srsComboBox.getSelectedItem();
    int prev = refSys.getSrid();

    refSys.setSrid(((Number) sridText.getValue()).intValue());
    if (dbPool.isConnected() && prev != refSys.getSrid()) {
      try {
        dbPool.getActiveDatabaseAdapter().getUtil().getSrsInfo(refSys);

        if (refSys.isSupported()) LOG.debug("SRID " + refSys.getSrid() + " is supported.");
        else LOG.warn("SRID " + refSys.getSrid() + " is NOT supported.");
      } catch (SQLException sqlEx) {
        LOG.error("Error while checking user-defined SRSs: " + sqlEx.getMessage().trim());
      }
    }

    refSys.setGMLSrsName(gmlSrsNameText.getText().trim());
    refSys.setDescription(descriptionText.getText().trim());
    if (refSys.getDescription().length() == 0) refSys.setDescription(getNewRefSysDescription());

    updateSrsComboBoxes(true);
    displaySelectedValues();
  }
 private void updateSrsComboBoxes(boolean sort) {
   srsComboBox.removeActionListener(srsComboBoxListener);
   srsComboBoxFactory.updateAll(sort);
   srsComboBox.addActionListener(srsComboBoxListener);
 }
  private void initGui() {
    srsComboBoxLabel = new JLabel();
    sridLabel = new JLabel();

    DecimalFormat tileFormat = new DecimalFormat("##########");
    tileFormat.setMaximumIntegerDigits(10);
    tileFormat.setMinimumIntegerDigits(1);
    sridText = new JFormattedTextField(tileFormat);

    srsNameLabel = new JLabel();
    gmlSrsNameText = new JTextField();
    descriptionLabel = new JLabel();
    descriptionText = new JTextField();
    newButton = new JButton();
    applyButton = new JButton();
    deleteButton = new JButton();
    checkButton = new JButton();
    checkButton.setEnabled(false);
    copyButton = new JButton();

    fileLabel = new JLabel();
    fileText = new JTextField();
    browseFileButton = new JButton();
    addFileButton = new JButton();
    replaceWithFileButton = new JButton();
    saveFileButton = new JButton();

    dbSrsTypeLabel = new JLabel();
    dbSrsTypeText = new JTextPane();
    dbSrsNameLabel = new JLabel();
    dbSrsNameText = new JTextPane();

    srsComboBoxFactory = SrsComboBoxFactory.getInstance(config);
    srsComboBox = srsComboBoxFactory.createSrsComboBox(false);

    PopupMenuDecorator.getInstance()
        .decorate(
            sridText, gmlSrsNameText, descriptionText, fileText, dbSrsTypeText, dbSrsNameText);

    sridText.addPropertyChangeListener(
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            if (sridText.getValue() != null) {
              if (((Number) sridText.getValue()).intValue() < 0) sridText.setValue(0);
              else if (((Number) sridText.getValue()).intValue() > Integer.MAX_VALUE)
                sridText.setValue(Integer.MAX_VALUE);
            }
          }
        });

    setLayout(new GridBagLayout());

    contentsPanel = new JPanel();
    contentsPanel.setBorder(BorderFactory.createTitledBorder(""));
    contentsPanel.setLayout(new GridBagLayout());
    add(contentsPanel, GuiUtil.setConstraints(0, 0, 1.0, 0.0, GridBagConstraints.BOTH, 5, 0, 5, 0));
    {
      JPanel srsPanel = new JPanel();
      srsPanel.setLayout(new GridBagLayout());

      gmlSrsNameText.setPreferredSize(gmlSrsNameText.getPreferredSize());
      descriptionText.setPreferredSize(gmlSrsNameText.getPreferredSize());
      srsComboBox.setPreferredSize(gmlSrsNameText.getPreferredSize());

      srsPanel.add(
          sridLabel,
          GuiUtil.setConstraints(
              0, 0, 0, 0, GridBagConstraints.BOTH, 0, BORDER_THICKNESS, 0, BORDER_THICKNESS));
      srsPanel.add(
          sridText,
          GuiUtil.setConstraints(
              1, 0, 1, 0, GridBagConstraints.HORIZONTAL, 0, BORDER_THICKNESS, 0, BORDER_THICKNESS));
      srsPanel.add(
          checkButton,
          GuiUtil.setConstraints(
              2, 0, 0, 0, GridBagConstraints.HORIZONTAL, 0, BORDER_THICKNESS, 0, BORDER_THICKNESS));

      srsPanel.add(
          srsNameLabel,
          GuiUtil.setConstraints(
              0,
              1,
              0,
              0,
              GridBagConstraints.BOTH,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              0,
              BORDER_THICKNESS));
      srsPanel.add(
          gmlSrsNameText,
          GuiUtil.setConstraints(
              1,
              1,
              2,
              1,
              1,
              0,
              GridBagConstraints.HORIZONTAL,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              0,
              BORDER_THICKNESS));

      srsPanel.add(
          descriptionLabel,
          GuiUtil.setConstraints(
              0,
              2,
              0,
              0,
              GridBagConstraints.BOTH,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              0,
              BORDER_THICKNESS));
      srsPanel.add(
          descriptionText,
          GuiUtil.setConstraints(
              1,
              2,
              2,
              1,
              1,
              0,
              GridBagConstraints.HORIZONTAL,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              0,
              BORDER_THICKNESS));

      dbSrsTypeText.setEditable(false);
      dbSrsTypeText.setBorder(sridText.getBorder());
      dbSrsTypeText.setBackground(srsPanel.getBackground());
      dbSrsTypeText.setMargin(sridText.getMargin());

      dbSrsNameText.setEditable(false);
      dbSrsNameText.setBorder(sridText.getBorder());
      dbSrsNameText.setBackground(srsPanel.getBackground());
      dbSrsNameText.setMargin(sridText.getMargin());

      srsPanel.add(
          dbSrsNameLabel,
          GuiUtil.setConstraints(
              0,
              3,
              0,
              0,
              GridBagConstraints.HORIZONTAL,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              0,
              BORDER_THICKNESS));
      srsPanel.add(
          dbSrsNameText,
          GuiUtil.setConstraints(
              1,
              3,
              2,
              1,
              1,
              0,
              GridBagConstraints.BOTH,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              0,
              BORDER_THICKNESS));
      srsPanel.add(
          dbSrsTypeLabel,
          GuiUtil.setConstraints(
              0,
              4,
              0,
              0,
              GridBagConstraints.HORIZONTAL,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              BORDER_THICKNESS));
      srsPanel.add(
          dbSrsTypeText,
          GuiUtil.setConstraints(
              1,
              4,
              2,
              1,
              1,
              0,
              GridBagConstraints.BOTH,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              BORDER_THICKNESS));

      Box buttonsPanel = Box.createHorizontalBox();
      buttonsPanel.add(applyButton);
      buttonsPanel.add(Box.createRigidArea(new Dimension(2 * BORDER_THICKNESS, 0)));
      buttonsPanel.add(newButton);
      buttonsPanel.add(Box.createRigidArea(new Dimension(2 * BORDER_THICKNESS, 0)));
      buttonsPanel.add(copyButton);
      buttonsPanel.add(Box.createRigidArea(new Dimension(2 * BORDER_THICKNESS, 0)));
      buttonsPanel.add(deleteButton);
      buttonsPanel.add(Box.createRigidArea(new Dimension(2 * BORDER_THICKNESS, 0)));

      GridBagConstraints c =
          GuiUtil.setConstraints(
              0,
              5,
              3,
              1,
              1,
              0,
              GridBagConstraints.NONE,
              BORDER_THICKNESS * 2,
              BORDER_THICKNESS,
              BORDER_THICKNESS,
              BORDER_THICKNESS);
      c.anchor = GridBagConstraints.CENTER;
      srsPanel.add(buttonsPanel, c);

      JPanel currentlySupportedContent = new JPanel();
      currentlySupportedContent.setBorder(BorderFactory.createEmptyBorder());
      currentlySupportedContent.setLayout(new GridBagLayout());
      currentlySupportedContent.add(
          srsComboBoxLabel,
          GuiUtil.setConstraints(0, 0, 0.0, 1.0, GridBagConstraints.BOTH, 0, 5, 0, 5));
      currentlySupportedContent.add(
          srsComboBox, GuiUtil.setConstraints(1, 0, 1.0, 1.0, GridBagConstraints.BOTH, 0, 5, 0, 5));

      contentsPanel.add(
          currentlySupportedContent,
          GuiUtil.setConstraints(0, 0, 1.0, 0.0, GridBagConstraints.BOTH, 0, 0, 5, 0));
      contentsPanel.add(
          srsPanel, GuiUtil.setConstraints(0, 1, 1.0, 0.0, GridBagConstraints.BOTH, 20, 0, 5, 0));
    }

    impExpPanel = new JPanel();
    impExpPanel.setBorder(BorderFactory.createTitledBorder(""));
    impExpPanel.setLayout(new GridBagLayout());
    add(impExpPanel, GuiUtil.setConstraints(0, 1, 1.0, 0.0, GridBagConstraints.BOTH, 5, 0, 5, 0));
    {
      JPanel browse = new JPanel();
      JPanel button = new JPanel();

      impExpPanel.add(
          browse, GuiUtil.setConstraints(0, 0, 1.0, 1.0, GridBagConstraints.BOTH, 0, 0, 0, 0));
      impExpPanel.add(
          button, GuiUtil.setConstraints(0, 1, 0.0, 1.0, GridBagConstraints.BOTH, 5, 0, 5, 0));
      fileText.setPreferredSize(fileText.getPreferredSize());

      browse.setLayout(new GridBagLayout());
      {
        browse.add(
            fileLabel,
            GuiUtil.setConstraints(0, 0, 1.0, 0.0, GridBagConstraints.HORIZONTAL, 5, 5, 0, 5));
        browse.add(
            fileText,
            GuiUtil.setConstraints(0, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL, 0, 5, 5, 5));
        browse.add(
            browseFileButton,
            GuiUtil.setConstraints(1, 1, 0.0, 0.0, GridBagConstraints.BOTH, 0, 5, 5, 5));
      }

      button.setLayout(new GridBagLayout());
      {
        button.add(
            addFileButton, GuiUtil.setConstraints(0, 0, 0, 0, GridBagConstraints.NONE, 5, 5, 5, 5));
        button.add(
            replaceWithFileButton,
            GuiUtil.setConstraints(1, 0, 0, 0, GridBagConstraints.NONE, 5, 5, 5, 5));
        button.add(
            saveFileButton,
            GuiUtil.setConstraints(2, 0, 0, 0, GridBagConstraints.NONE, 5, 20, 5, 5));
      }
    }

    // influence focus policy
    checkButton.setFocusCycleRoot(false);

    srsComboBoxListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            displaySelectedValues();
          }
        };

    DropTarget dropTarget = new DropTarget(fileText, this);
    fileText.setDropTarget(dropTarget);
    impExpPanel.setDropTarget(dropTarget);

    srsComboBox.addActionListener(srsComboBoxListener);

    newButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (requestChange()) {
              DatabaseSrs refSys = DatabaseSrs.createDefaultSrs();
              refSys.setDescription(getNewRefSysDescription());
              refSys.setSupported(!dbPool.isConnected());

              config.getProject().getDatabase().addReferenceSystem(refSys);
              updateSrsComboBoxes(false);
              srsComboBox.setSelectedItem(refSys);

              displaySelectedValues();
            }
          }
        });

    copyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (requestChange()) {
              DatabaseSrs orig = srsComboBox.getSelectedItem();
              DatabaseSrs copy = new DatabaseSrs(orig);
              copy.setDescription(getCopyOfDescription(orig));

              config.getProject().getDatabase().addReferenceSystem(copy);
              updateSrsComboBoxes(false);
              srsComboBox.setSelectedItem(copy);

              displaySelectedValues();
            }
          }
        });

    applyButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            setSettings();
            LOG.info("Settings successfully applied.");
          }
        });

    deleteButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            DatabaseSrs refSys = srsComboBox.getSelectedItem();
            int index = srsComboBox.getSelectedIndex();

            String text = Language.I18N.getString("pref.db.srs.dialog.delete.msg");
            Object[] args = new Object[] {refSys.getDescription()};
            String formattedMsg = MessageFormat.format(text, args);

            if (JOptionPane.showConfirmDialog(
                    getTopLevelAncestor(),
                    formattedMsg,
                    Language.I18N.getString("pref.db.srs.dialog.delete.title"),
                    JOptionPane.YES_NO_OPTION)
                == JOptionPane.YES_OPTION) {
              config.getProject().getDatabase().getReferenceSystems().remove(refSys);
              updateSrsComboBoxes(false);
              srsComboBox.setSelectedIndex(index < srsComboBox.getItemCount() ? index : index - 1);
              displaySelectedValues();
            }
          }
        });

    checkButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int srid = 0;

            try {
              srid = Integer.parseInt(sridText.getText().trim());
            } catch (NumberFormatException nfe) {
              //
            }

            try {
              DatabaseSrs tmp = DatabaseSrs.createDefaultSrs();
              tmp.setSrid(srid);
              dbPool.getActiveDatabaseAdapter().getUtil().getSrsInfo(tmp);
              if (tmp.isSupported()) {
                LOG.all(LogLevel.INFO, "SRID " + srid + " is supported.");
                LOG.all(LogLevel.INFO, "Database name: " + tmp.getDatabaseSrsName());
                LOG.all(LogLevel.INFO, "SRS type: " + tmp.getType());
              } else LOG.all(LogLevel.WARN, "SRID " + srid + " is NOT supported.");
            } catch (SQLException sqlEx) {
              LOG.error("Error while checking user-defined SRSs: " + sqlEx.getMessage().trim());
            }
          }
        });

    browseFileButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            browseReferenceSystemFile(Language.I18N.getString("pref.db.srs.label.file"));
          }
        });

    addFileButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            importReferenceSystems(false);
          }
        });

    replaceWithFileButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            importReferenceSystems(true);
          }
        });

    saveFileButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            exportReferenceSystems();
          }
        });
  }