private static void setValue(
     JTextField tf, String fieldName, AbstractBibtexKeyPattern keyPattern) {
   if (keyPattern.isDefaultValue(fieldName)) {
     tf.setText("");
   } else {
     tf.setText(keyPattern.getValue(fieldName).get(0));
   }
 }
  /**
   * Fills the current values to the text fields
   *
   * @param keyPattern the BibtexKeyPattern to use as initial value
   */
  public void setValues(AbstractBibtexKeyPattern keyPattern) {
    for (Map.Entry<String, JTextField> entry : textFields.entrySet()) {
      setValue(entry.getValue(), entry.getKey(), keyPattern);
    }

    if (keyPattern.getDefaultValue() == null) {
      defaultPat.setText("");
    } else {
      defaultPat.setText(keyPattern.getDefaultValue().get(0));
    }
  }
  /** fill the given LabelPattern by values generated from the text fields */
  private void fillPatternUsingPanelData(AbstractBibtexKeyPattern keypatterns) {
    // each entry type
    for (Map.Entry<String, JTextField> entry : textFields.entrySet()) {
      String text = entry.getValue().getText();
      if (!text.trim().isEmpty()) {
        keypatterns.addBibtexKeyPattern(entry.getKey(), text);
      }
    }

    // default value
    String text = defaultPat.getText();
    if (!text.trim()
        .isEmpty()) { // we do not trim the value at the assignment to enable users to have spaces
      // at the beginning and at the end of the pattern
      keypatterns.setDefaultValue(text);
    }
  }
 protected GlobalBibtexKeyPattern getKeyPatternAsGlobalBibtexKeyPattern() {
   GlobalBibtexKeyPattern res =
       new GlobalBibtexKeyPattern(
           AbstractBibtexKeyPattern.split(
               JabRefPreferences.getInstance().get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN)));
   fillPatternUsingPanelData(res);
   return res;
 }
  @Test
  public void writeCustomKeyPattern() throws Exception {
    AbstractBibtexKeyPattern pattern = new DatabaseBibtexKeyPattern(prefs.getKeyPattern());
    pattern.setDefaultValue("test");
    pattern.addBibtexKeyPattern("article", "articleTest");
    metaData.setCiteKeyPattern(pattern);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE
            + "@Comment{jabref-meta: keypattern_article:articleTest;}"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Comment{jabref-meta: keypatterndefault:test;}"
            + OS.NEWLINE,
        session.getStringValue());
  }