private void storeSettings(ExternalFileType entry) {
    entry.setName(name.getText().trim());
    entry.setMimeType(mimeType.getText().trim());
    // Set extension, but remove initial dot if user has added that:
    String ext = extension.getText().trim();
    if (!ext.isEmpty() && ext.charAt(0) == '.') {
      entry.setExtension(ext.substring(1));
    } else {
      entry.setExtension(ext);
    }

    if (selectedIcon != null) {
      entry.setIconName(selectedIcon);
      entry.setIcon(IconTheme.getImage(entry.getIconName()));
    }
    if (!OS.WINDOWS) {
      entry.setOpenWith(application.getText().trim());
    } else {
      // On Windows, store application as empty if the "Default" option is selected,
      // or if the application name is empty:
      if (useDefault.isSelected() || application.getText().trim().isEmpty()) {
        entry.setOpenWith("");
      } else {
        entry.setOpenWith(application.getText().trim());
      }
    }
  }
  public boolean openLink() {
    frame.output(Localization.lang("External viewer called") + ".");
    try {
      ExternalFileType type = fileType;
      if (this.fileType == null) {
        if (this.fieldName == null) {
          // We don't already know the file type, so we try to deduce it from the extension:
          File file = new File(link);
          // We try to check the extension for the file:
          String name = file.getName();
          int pos = name.indexOf('.');
          String extension =
              (pos >= 0) && (pos < (name.length() - 1))
                  ? name.substring(pos + 1).trim().toLowerCase()
                  : null;
          // Now we know the extension, check if it is one we know about:
          type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension);
          fileType = type;
        } else {
          JabRefDesktop.openExternalViewer(
              frame.getCurrentBasePanel().getBibDatabaseContext().getMetaData(), link, fieldName);
          return true;
        }
      }

      if (type instanceof UnknownExternalFileType) {
        return JabRefDesktop.openExternalFileUnknown(
            frame, entry, metaData, link, (UnknownExternalFileType) type);
      } else {
        return JabRefDesktop.openExternalFileAnyFormat(metaData, link, type);
      }

    } catch (IOException e1) {
      // See if we should show an error message concerning the application to open the
      // link with. We check if the file type is set, and if the file type has a non-empty
      // application link. If that link is referred by the error message, we can assume
      // that the problem is in the open-with-application setting:
      if ((fileType != null)
          && (fileType.getOpenWith() != null)
          && !fileType.getOpenWith().isEmpty()
          && e1.getMessage().contains(fileType.getOpenWith())) {

        JOptionPane.showMessageDialog(
            frame,
            Localization.lang(
                "Unable to open link. "
                    + "The application '%0' associated with the file type '%1' could not be called.",
                fileType.getOpenWith(), fileType.getName()),
            Localization.lang("Could not open link"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }

      LOGGER.warn("Unable to open link", e1);
    }
    return false;
  }
 private void setValues(ExternalFileType entry) {
   name.setText(entry.getName());
   extension.setText(entry.getExtension());
   mimeType.setText(entry.getMimeType());
   application.setText(entry.getOpenWith());
   icon.setIcon(entry.getIcon());
   if (application.getText().isEmpty()) {
     useDefault.setSelected(true);
   } else {
     other.setSelected(true);
   }
   selectedIcon = null;
 }
 public void setEntry(ExternalFileType entry) {
   this.entry = entry;
   if (entry.getName().isEmpty()) {
     diag.setTitle(newFileTitle);
   } else {
     diag.setTitle(editFileTitle);
   }
   setValues(entry);
 }
  private void init(ExternalFileType entry) {
    this.entry = entry;
    icon.setText(null);

    ButtonGroup bg = new ButtonGroup();
    bg.add(useDefault);
    bg.add(other);

    FormBuilder builder = FormBuilder.create();
    builder.layout(
        new FormLayout(
            "left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref",
            "p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p"));
    builder.add(Localization.lang("Icon")).xy(1, 1);
    builder.add(icon).xy(3, 1);
    builder.add(Localization.lang("Name")).xy(1, 3);
    builder.add(name).xy(3, 3);
    builder.add(Localization.lang("Extension")).xy(1, 5);
    builder.add(extension).xy(3, 5);
    builder.add(Localization.lang("MIME type")).xy(1, 7);
    builder.add(mimeType).xy(3, 7);
    builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    builder.add(Localization.lang("Application")).xy(1, 9);
    JButton browseBut = new JButton(Localization.lang("Browse"));
    if (OS.WINDOWS) {
      builder.add(useDefault).xy(3, 9);
      builder.appendRows("2dlu, p");
      JPanel p1 = new JPanel();
      builder.add(p1).xy(1, 11);
      JPanel p2 = new JPanel();
      application.setPreferredSize(new Dimension(300, application.getPreferredSize().height));
      BorderLayout bl = new BorderLayout();
      bl.setHgap(4);
      p2.setLayout(bl);
      p2.add(other, BorderLayout.WEST);
      p2.add(application, BorderLayout.CENTER);
      builder.add(p2).xy(3, 11);
      builder.add(browseBut).xy(5, 11);
    } else {
      builder.add(application).xy(3, 9);
      builder.add(browseBut).xy(5, 9);
    }
    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    bb.addButton(ok);
    bb.addButton(cancel);
    bb.addGlue();

    ok.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            okPressed = true;
            storeSettings(ExternalFileTypeEntryEditor.this.entry);
            diag.dispose();
          }
        });
    cancel.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            diag.dispose();
          }
        });

    icon.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent actionEvent) {
            String initSel = ExternalFileTypeEntryEditor.this.entry.getIconName();
            if (selectedIcon != null) {
              initSel = selectedIcon;
            }
            IconSelection ic = new IconSelection(diag, initSel);
            ic.setVisible(true);
            if (ic.isOkPressed()) {
              selectedIcon = ic.getSelectedIconKey();
              icon.setIcon(IconTheme.getImage(selectedIcon));
            }
            // JOptionPane.showMessageDialog(null, "Sorry, the icon can unfortunately not be changed
            // in this version of JabRef");
          }
        });

    if (OS.WINDOWS) {
      application
          .getDocument()
          .addDocumentListener(
              new DocumentListener() {

                private void handle(DocumentEvent e) {
                  if (application.getText().isEmpty()) {
                    useDefault.setSelected(true);
                  } else {
                    other.setSelected(true);
                  }
                }

                @Override
                public void insertUpdate(DocumentEvent e) {
                  handle(e);
                }

                @Override
                public void removeUpdate(DocumentEvent documentEvent) {
                  handle(documentEvent);
                }

                @Override
                public void changedUpdate(DocumentEvent documentEvent) {
                  handle(documentEvent);
                }
              });
    }

    String title = editFileTitle;

    if (entry.getName().isEmpty()) {
      title = newFileTitle;
    }

    if (dParent != null) {
      diag = new JDialog(dParent, title, true);
    } else {
      diag = new JDialog(fParent, title, true);
    }
    diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
    diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
    diag.pack();

    BrowseListener browse = new BrowseListener(diag, application);
    browseBut.addActionListener(browse);

    if (dParent != null) {
      diag.setLocationRelativeTo(dParent);
    } else {
      diag.setLocationRelativeTo(fParent);
      // Util.placeDialog(diag, parent);
    }

    setValues(entry);
  }