Example #1
0
  public PreferencesDialog(JabRefFrame parent) {
    super(parent, Localization.lang("JabRef preferences"), false);
    JabRefPreferences prefs = JabRefPreferences.getInstance();
    frame = parent;

    main = new JPanel();
    JPanel mainPanel = new JPanel();
    JPanel lower = new JPanel();

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(mainPanel, BorderLayout.CENTER);
    getContentPane().add(lower, BorderLayout.SOUTH);

    final CardLayout cardLayout = new CardLayout();
    main.setLayout(cardLayout);

    List<PrefsTab> tabs = new ArrayList<>();
    tabs.add(new GeneralTab(prefs));
    tabs.add(new NetworkTab(prefs));
    tabs.add(new FileTab(frame, prefs));
    tabs.add(new FileSortTab(prefs));
    tabs.add(new EntryEditorPrefsTab(frame, prefs));
    tabs.add(new GroupsPrefsTab(prefs));
    tabs.add(new AppearancePrefsTab(prefs));
    tabs.add(new ExternalTab(frame, this, prefs));
    tabs.add(new TablePrefsTab(prefs));
    tabs.add(new TableColumnsTab(prefs, parent));
    tabs.add(new BibtexKeyPatternPrefTab(prefs, parent.getCurrentBasePanel()));
    tabs.add(new PreviewPrefsTab(prefs));
    tabs.add(new NameFormatterTab(prefs));
    tabs.add(new ImportSettingsTab(prefs));
    tabs.add(new XmpPrefsTab(prefs));
    tabs.add(new AdvancedTab(prefs));

    // add all tabs
    tabs.forEach(tab -> main.add((Component) tab, tab.getTabName()));

    mainPanel.setBorder(BorderFactory.createEtchedBorder());

    String[] tabNames = tabs.stream().map(PrefsTab::getTabName).toArray(String[]::new);
    JList<String> chooser = new JList<>(tabNames);
    chooser.setBorder(BorderFactory.createEtchedBorder());
    // Set a prototype value to control the width of the list:
    chooser.setPrototypeCellValue("This should be wide enough");
    chooser.setSelectedIndex(0);
    chooser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // Add the selection listener that will show the correct panel when
    // selection changes:
    chooser.addListSelectionListener(
        e -> {
          if (e.getValueIsAdjusting()) {
            return;
          }
          String o = chooser.getSelectedValue();
          cardLayout.show(main, o);
        });

    JPanel buttons = new JPanel();
    buttons.setLayout(new GridLayout(4, 1));
    buttons.add(importPreferences, 0);
    buttons.add(exportPreferences, 1);
    buttons.add(showPreferences, 2);
    buttons.add(resetPreferences, 3);

    JPanel westPanel = new JPanel();
    westPanel.setLayout(new BorderLayout());
    westPanel.add(chooser, BorderLayout.CENTER);
    westPanel.add(buttons, BorderLayout.SOUTH);
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(main, BorderLayout.CENTER);
    mainPanel.add(westPanel, BorderLayout.WEST);

    JButton ok = new JButton(Localization.lang("OK"));
    JButton cancel = new JButton(Localization.lang("Cancel"));
    ok.addActionListener(new OkAction());
    CancelAction cancelAction = new CancelAction();
    cancel.addActionListener(cancelAction);
    lower.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    ButtonBarBuilder buttonBarBuilder = new ButtonBarBuilder(lower);
    buttonBarBuilder.addGlue();
    buttonBarBuilder.addButton(ok);
    buttonBarBuilder.addButton(cancel);
    buttonBarBuilder.addGlue();

    // Key bindings:
    KeyBinder.bindCloseDialogKeyToCancelAction(this.getRootPane(), cancelAction);

    // Import and export actions:
    exportPreferences.setToolTipText(Localization.lang("Export preferences to file"));
    exportPreferences.addActionListener(
        e -> {
          Optional<Path> path =
              new NewFileDialogs(frame, System.getProperty("user.home"))
                  .withExtension(FileExtensions.XML)
                  .saveNewFile();
          path.ifPresent(
              exportFile -> {
                try {
                  prefs.exportPreferences(exportFile.toString());
                } catch (JabRefException ex) {
                  LOGGER.warn(ex.getMessage(), ex);
                  JOptionPane.showMessageDialog(
                      PreferencesDialog.this,
                      ex.getLocalizedMessage(),
                      Localization.lang("Export preferences"),
                      JOptionPane.ERROR_MESSAGE);
                }
              });
        });

    importPreferences.setToolTipText(Localization.lang("Import preferences from file"));
    importPreferences.addActionListener(
        e -> {
          Optional<Path> fileName =
              new NewFileDialogs(frame, System.getProperty("user.home"))
                  .withExtension(FileExtensions.XML)
                  .openDlgAndGetSelectedFile();

          if (fileName.isPresent()) {
            try {
              prefs.importPreferences(fileName.get().toString());
              updateAfterPreferenceChanges();
              JOptionPane.showMessageDialog(
                  PreferencesDialog.this,
                  Localization.lang("You must restart JabRef for this to come into effect."),
                  Localization.lang("Import preferences"),
                  JOptionPane.WARNING_MESSAGE);
            } catch (JabRefException ex) {
              LOGGER.warn(ex.getMessage(), ex);
              JOptionPane.showMessageDialog(
                  PreferencesDialog.this,
                  ex.getLocalizedMessage(),
                  Localization.lang("Import preferences"),
                  JOptionPane.ERROR_MESSAGE);
            }
          }
        });

    showPreferences.addActionListener(
        e ->
            new PreferencesFilterDialog(new JabRefPreferencesFilter(Globals.prefs), frame)
                .setVisible(true));
    resetPreferences.addActionListener(
        e -> {
          if (JOptionPane.showConfirmDialog(
                  PreferencesDialog.this,
                  Localization.lang(
                      "Are you sure you want to reset all settings to default values?"),
                  Localization.lang("Reset preferences"),
                  JOptionPane.OK_CANCEL_OPTION)
              == JOptionPane.OK_OPTION) {
            try {
              prefs.clear();
              JOptionPane.showMessageDialog(
                  PreferencesDialog.this,
                  Localization.lang("You must restart JabRef for this to come into effect."),
                  Localization.lang("Reset preferences"),
                  JOptionPane.WARNING_MESSAGE);
            } catch (BackingStoreException ex) {
              LOGGER.warn(ex.getMessage(), ex);
              JOptionPane.showMessageDialog(
                  PreferencesDialog.this,
                  ex.getLocalizedMessage(),
                  Localization.lang("Reset preferences"),
                  JOptionPane.ERROR_MESSAGE);
            }
            updateAfterPreferenceChanges();
          }
        });

    setValues();

    pack();
  }