Search in sources :

Example 1 with KeywordList

use of org.jabref.model.entry.KeywordList in project jabref by JabRef.

the class ManageKeywordsAction method createDialog.

private void createDialog() {
    if (diag != null) {
        return;
    }
    // keyword to add
    JTextField keyword = new JTextField();
    keywordListModel = new DefaultListModel<>();
    JList<Keyword> keywordList = new JList<>(keywordListModel);
    keywordList.setVisibleRowCount(8);
    JScrollPane kPane = new JScrollPane(keywordList);
    diag = new JDialog(frame, Localization.lang("Manage keywords"), true);
    JButton ok = new JButton(Localization.lang("OK"));
    JButton cancel = new JButton(Localization.lang("Cancel"));
    JButton add = new JButton(Localization.lang("Add"));
    JButton remove = new JButton(Localization.lang("Remove"));
    keywordList.setVisibleRowCount(10);
    intersectKeywords = new JRadioButton(Localization.lang("Display keywords appearing in ALL entries"));
    mergeKeywords = new JRadioButton(Localization.lang("Display keywords appearing in ANY entry"));
    ButtonGroup group = new ButtonGroup();
    group.add(intersectKeywords);
    group.add(mergeKeywords);
    ActionListener stateChanged = e -> fillKeyWordList();
    intersectKeywords.addActionListener(stateChanged);
    mergeKeywords.addActionListener(stateChanged);
    intersectKeywords.setSelected(true);
    FormBuilder builder = FormBuilder.create().layout(new FormLayout("fill:200dlu:grow, 4dlu, fill:pref", "pref, 2dlu, pref, 1dlu, pref, 2dlu, fill:100dlu:grow, 4dlu, pref, 4dlu, pref, "));
    builder.addSeparator(Localization.lang("Keywords of selected entries")).xyw(1, 1, 3);
    builder.add(intersectKeywords).xyw(1, 3, 3);
    builder.add(mergeKeywords).xyw(1, 5, 3);
    builder.add(kPane).xywh(1, 7, 1, 3);
    builder.add(remove).xy(3, 9);
    builder.add(keyword).xy(1, 11);
    builder.add(add).xy(3, 11);
    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    bb.addButton(ok);
    bb.addButton(cancel);
    bb.addGlue();
    builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    ok.addActionListener(e -> {
        canceled = false;
        diag.dispose();
    });
    Action cancelAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            canceled = true;
            diag.dispose();
        }
    };
    cancel.addActionListener(cancelAction);
    final ActionListener addActionListener = arg0 -> addButtonActionListener(keyword);
    add.addActionListener(addActionListener);
    final ActionListener removeActionListenter = arg0 -> {
        List<Keyword> values = keywordList.getSelectedValuesList();
        for (Keyword val : values) {
            keywordListModel.removeElement(val);
        }
    };
    remove.addActionListener(removeActionListenter);
    keywordList.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent arg0) {
        // Do nothing
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        // Do nothing
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_DELETE) {
                removeActionListenter.actionPerformed(null);
            }
        }
    });
    AutoCompleter<String> autoComp = JabRefGUI.getMainFrame().getCurrentBasePanel().getAutoCompleters().get(FieldName.KEYWORDS);
    AutoCompleteListener acl = new AutoCompleteListener(autoComp);
    keyword.addKeyListener(acl);
    keyword.addFocusListener(acl);
    keyword.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        // Do nothing
        }

        @Override
        public void keyReleased(KeyEvent e) {
        // Do nothing
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                addActionListener.actionPerformed(null);
            }
        }
    });
    // Key bindings:
    ActionMap am = builder.getPanel().getActionMap();
    InputMap im = builder.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", cancelAction);
    diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
    diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
}
Also used : KeyListener(java.awt.event.KeyListener) ActionListener(java.awt.event.ActionListener) NamedCompound(org.jabref.gui.undo.NamedCompound) AutoCompleter(org.jabref.logic.autocompleter.AutoCompleter) Enumeration(java.util.Enumeration) JDialog(javax.swing.JDialog) JTextField(javax.swing.JTextField) FieldName(org.jabref.model.entry.FieldName) StringUtil(org.jabref.model.strings.StringUtil) ButtonBarBuilder(com.jgoodies.forms.builder.ButtonBarBuilder) KeywordList(org.jabref.model.entry.KeywordList) Action(javax.swing.Action) BasePanel(org.jabref.gui.BasePanel) JabRefFrame(org.jabref.gui.JabRefFrame) Localization(org.jabref.logic.l10n.Localization) Keyword(org.jabref.model.entry.Keyword) BorderLayout(java.awt.BorderLayout) SpecialFieldsUtils(org.jabref.logic.specialfields.SpecialFieldsUtils) ActionMap(javax.swing.ActionMap) JComponent(javax.swing.JComponent) FormBuilder(com.jgoodies.forms.builder.FormBuilder) JButton(javax.swing.JButton) ButtonGroup(javax.swing.ButtonGroup) BibEntry(org.jabref.model.entry.BibEntry) JList(javax.swing.JList) BorderFactory(javax.swing.BorderFactory) JabRefGUI(org.jabref.JabRefGUI) KeyEvent(java.awt.event.KeyEvent) ActionEvent(java.awt.event.ActionEvent) JRadioButton(javax.swing.JRadioButton) Globals(org.jabref.Globals) JScrollPane(javax.swing.JScrollPane) FieldChange(org.jabref.model.FieldChange) List(java.util.List) DefaultListModel(javax.swing.DefaultListModel) AbstractAction(javax.swing.AbstractAction) FormLayout(com.jgoodies.forms.layout.FormLayout) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) Optional(java.util.Optional) AutoCompleteListener(org.jabref.gui.autocompleter.AutoCompleteListener) InputMap(javax.swing.InputMap) KeyBinding(org.jabref.gui.keyboard.KeyBinding) Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) KeyEvent(java.awt.event.KeyEvent) ButtonBarBuilder(com.jgoodies.forms.builder.ButtonBarBuilder) KeywordList(org.jabref.model.entry.KeywordList) JList(javax.swing.JList) List(java.util.List) AbstractAction(javax.swing.AbstractAction) JScrollPane(javax.swing.JScrollPane) FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) Keyword(org.jabref.model.entry.Keyword) ActionMap(javax.swing.ActionMap) AutoCompleteListener(org.jabref.gui.autocompleter.AutoCompleteListener) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) KeyListener(java.awt.event.KeyListener) InputMap(javax.swing.InputMap) JList(javax.swing.JList) JDialog(javax.swing.JDialog)

Example 2 with KeywordList

use of org.jabref.model.entry.KeywordList in project jabref by JabRef.

the class ManageKeywordsAction method fillKeyWordList.

private void fillKeyWordList() {
    BasePanel bp = frame.getCurrentBasePanel();
    List<BibEntry> entries = bp.getSelectedEntries();
    // fill dialog with values
    keywordListModel.clear();
    sortedKeywordsOfAllEntriesBeforeUpdateByUser.clear();
    if (mergeKeywords.isSelected()) {
        for (BibEntry entry : entries) {
            KeywordList separatedKeywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
            sortedKeywordsOfAllEntriesBeforeUpdateByUser.addAll(separatedKeywords);
        }
    } else {
        assert intersectKeywords.isSelected();
        // all keywords from first entry have to be added
        BibEntry firstEntry = entries.get(0);
        KeywordList separatedKeywords = firstEntry.getKeywords(Globals.prefs.getKeywordDelimiter());
        sortedKeywordsOfAllEntriesBeforeUpdateByUser.addAll(separatedKeywords);
        // this approach ensures that one empty keyword list leads to an empty set of common keywords
        for (int i = 1; i < entries.size(); i++) {
            BibEntry entry = entries.get(i);
            separatedKeywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
            sortedKeywordsOfAllEntriesBeforeUpdateByUser.retainAll(separatedKeywords);
        }
    }
    for (Keyword keyword : sortedKeywordsOfAllEntriesBeforeUpdateByUser) {
        keywordListModel.addElement(keyword);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) Keyword(org.jabref.model.entry.Keyword) KeywordList(org.jabref.model.entry.KeywordList)

Example 3 with KeywordList

use of org.jabref.model.entry.KeywordList in project jabref by JabRef.

the class ManageKeywordsAction method updateKeywords.

private NamedCompound updateKeywords(List<BibEntry> entries, KeywordList keywordsToAdd, KeywordList keywordsToRemove) {
    NamedCompound ce = new NamedCompound(Localization.lang("Update keywords"));
    for (BibEntry entry : entries) {
        KeywordList keywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
        // update keywords
        keywords.removeAll(keywordsToRemove);
        keywords.addAll(keywordsToAdd);
        // put keywords back
        Optional<FieldChange> change = entry.putKeywords(keywords, Globals.prefs.getKeywordDelimiter());
        if (change.isPresent()) {
            ce.addEdit(new UndoableFieldChange(change.get()));
        }
        if (Globals.prefs.isKeywordSyncEnabled()) {
            SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter());
        }
    }
    ce.end();
    return ce;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) KeywordList(org.jabref.model.entry.KeywordList) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 4 with KeywordList

use of org.jabref.model.entry.KeywordList in project jabref by JabRef.

the class FieldContentSelector method selectionMade.

private void selectionMade() {
    // The first element is empty to avoid a preselection
    if (comboBox.getSelectedIndex() == 0) {
        return;
    }
    String chosen = (String) comboBox.getSelectedItem();
    if ((chosen == null) || chosen.isEmpty()) {
        return;
    }
    String currentText = editor.getText();
    KeywordList words = KeywordList.parse(currentText, this.delimiter.charAt(0));
    boolean alreadyInList = words.contains(new Keyword(chosen));
    // not the first word and no duplicate -> we need a comma
    if (!"".equals(currentText) && !alreadyInList) {
        editor.append(FieldContentSelector.this.delimiter);
    }
    // no duplicate -> add it
    if (!alreadyInList) {
        editor.append(chosen);
    }
    comboBox.setSelectedIndex(0);
    // Fire event that we changed the editor
    if (action != null) {
        action.actionPerformed(new ActionEvent(editor, 0, ""));
    }
    // Transfer focus to the editor.
    editor.requestFocus();
}
Also used : Keyword(org.jabref.model.entry.Keyword) ActionEvent(java.awt.event.ActionEvent) KeywordList(org.jabref.model.entry.KeywordList)

Example 5 with KeywordList

use of org.jabref.model.entry.KeywordList in project jabref by JabRef.

the class BibtexKeyPatternUtil method makeLabel.

public static String makeLabel(BibEntry entry, String value, Character keywordDelimiter, BibDatabase database) {
    String val = value;
    try {
        if (val.startsWith("auth") || val.startsWith("pureauth")) {
            /*
                 * For label code "auth...": if there is no author, but there
                 * are editor(s) (e.g. for an Edited Book), use the editor(s)
                 * instead. (saw27@mrao.cam.ac.uk). This is what most people
                 * want, but in case somebody really needs a field which expands
                 * to nothing if there is no author (e.g. someone who uses both
                 * "auth" and "ed" in the same label), we provide an alternative
                 * form "pureauth..." which does not do this fallback
                 * substitution of editor.
                 */
            String authString;
            if (database != null) {
                authString = entry.getField(FieldName.AUTHOR).map(authorString -> normalize(database.resolveForStrings(authorString))).orElse("");
            } else {
                authString = entry.getField(FieldName.AUTHOR).orElse("");
            }
            if (val.startsWith("pure")) {
                // remove the "pure" prefix so the remaining
                // code in this section functions correctly
                val = val.substring(4);
            }
            if (authString.isEmpty()) {
                if (database != null) {
                    authString = entry.getField(FieldName.EDITOR).map(authorString -> normalize(database.resolveForStrings(authorString))).orElse("");
                } else {
                    authString = entry.getField(FieldName.EDITOR).orElse("");
                }
            }
            // have to check all the time.
            if ("auth".equals(val)) {
                return firstAuthor(authString);
            } else if ("authForeIni".equals(val)) {
                return firstAuthorForenameInitials(authString);
            } else if ("authFirstFull".equals(val)) {
                return firstAuthorVonAndLast(authString);
            } else if ("authors".equals(val)) {
                return allAuthors(authString);
            } else if ("authorsAlpha".equals(val)) {
                return authorsAlpha(authString);
            } else // Last author's last name
            if ("authorLast".equals(val)) {
                return lastAuthor(authString);
            } else if ("authorLastForeIni".equals(val)) {
                return lastAuthorForenameInitials(authString);
            } else if ("authorIni".equals(val)) {
                return oneAuthorPlusIni(authString);
            } else if (val.matches("authIni[\\d]+")) {
                int num = Integer.parseInt(val.substring(7));
                return authIniN(authString, num);
            } else if ("auth.auth.ea".equals(val)) {
                return authAuthEa(authString);
            } else if ("auth.etal".equals(val)) {
                return authEtal(authString, ".", ".etal");
            } else if ("authEtAl".equals(val)) {
                return authEtal(authString, "", "EtAl");
            } else if ("authshort".equals(val)) {
                return authshort(authString);
            } else if (val.matches("auth[\\d]+_[\\d]+")) {
                String[] nums = val.substring(4).split("_");
                return authNofMth(authString, Integer.parseInt(nums[0]), Integer.parseInt(nums[1]));
            } else if (val.matches("auth\\d+")) {
                // authN. First N chars of the first author's last
                // name.
                String fa = firstAuthor(authString);
                int num = Integer.parseInt(val.substring(4));
                if (num > fa.length()) {
                    num = fa.length();
                }
                return fa.substring(0, num);
            } else if (val.matches("authors\\d+")) {
                return nAuthors(authString, Integer.parseInt(val.substring(7)));
            } else {
                // use it literally:
                return entry.getFieldOrAlias(val).orElse("");
            }
        } else if (val.startsWith("ed")) {
            // don't have to check all the time.
            if ("edtr".equals(val)) {
                return firstAuthor(entry.getField(FieldName.EDITOR).orElse(""));
            } else if ("edtrForeIni".equals(val)) {
                return firstAuthorForenameInitials(entry.getField(FieldName.EDITOR).orElse(""));
            } else if ("editors".equals(val)) {
                return allAuthors(entry.getField(FieldName.EDITOR).orElse(""));
            // Last author's last name
            } else if ("editorLast".equals(val)) {
                return lastAuthor(entry.getField(FieldName.EDITOR).orElse(""));
            } else if ("editorLastForeIni".equals(val)) {
                return lastAuthorForenameInitials(entry.getField(FieldName.EDITOR).orElse(""));
            } else if ("editorIni".equals(val)) {
                return oneAuthorPlusIni(entry.getField(FieldName.EDITOR).orElse(""));
            } else if (val.matches("edtrIni[\\d]+")) {
                int num = Integer.parseInt(val.substring(7));
                return authIniN(entry.getField(FieldName.EDITOR).orElse(""), num);
            } else if (val.matches("edtr[\\d]+_[\\d]+")) {
                String[] nums = val.substring(4).split("_");
                return authNofMth(entry.getField(FieldName.EDITOR).orElse(""), Integer.parseInt(nums[0]), Integer.parseInt(nums[1]) - 1);
            } else if ("edtr.edtr.ea".equals(val)) {
                return authAuthEa(entry.getField(FieldName.EDITOR).orElse(""));
            } else if ("edtrshort".equals(val)) {
                return authshort(entry.getField(FieldName.EDITOR).orElse(""));
            } else // name.
            if (val.matches("edtr\\d+")) {
                String fa = firstAuthor(entry.getField(FieldName.EDITOR).orElse(""));
                int num = Integer.parseInt(val.substring(4));
                if (num > fa.length()) {
                    num = fa.length();
                }
                return fa.substring(0, num);
            } else {
                // use it literally:
                return entry.getFieldOrAlias(val).orElse("");
            }
        } else if ("firstpage".equals(val)) {
            return firstPage(entry.getField(FieldName.PAGES).orElse(""));
        } else if ("lastpage".equals(val)) {
            return lastPage(entry.getField(FieldName.PAGES).orElse(""));
        } else if ("title".equals(val)) {
            return camelizeSignificantWordsInTitle(entry.getField(FieldName.TITLE).orElse(""));
        } else if ("shorttitle".equals(val)) {
            return getTitleWords(3, entry.getField(FieldName.TITLE).orElse(""));
        } else if ("shorttitleINI".equals(val)) {
            return keepLettersAndDigitsOnly(applyModifiers(getTitleWordsWithSpaces(3, entry.getField(FieldName.TITLE).orElse("")), Collections.singletonList("abbr"), 0));
        } else if ("veryshorttitle".equals(val)) {
            return getTitleWords(1, removeSmallWords(entry.getField(FieldName.TITLE).orElse("")));
        } else if ("camel".equals(val)) {
            return getCamelizedTitle(entry.getField(FieldName.TITLE).orElse(""));
        } else if ("shortyear".equals(val)) {
            String yearString = entry.getFieldOrAlias(FieldName.YEAR).orElse("");
            if (yearString.isEmpty()) {
                return yearString;
            // In press/in preparation/submitted
            } else if (yearString.startsWith("in") || yearString.startsWith("sub")) {
                return "IP";
            } else if (yearString.length() > 2) {
                return yearString.substring(yearString.length() - 2);
            } else {
                return yearString;
            }
        } else if (val.matches("keyword\\d+")) {
            // according to LabelPattern.php, it returns keyword number n
            int num = Integer.parseInt(val.substring(7));
            KeywordList separatedKeywords = entry.getKeywords(keywordDelimiter);
            if (separatedKeywords.size() < num) {
                // not enough keywords
                return "";
            } else {
                // num counts from 1 to n, but index in arrayList count from 0 to n-1
                return separatedKeywords.get(num - 1).toString();
            }
        } else if (val.matches("keywords\\d*")) {
            // return all keywords, not separated
            int num;
            if (val.length() > 8) {
                num = Integer.parseInt(val.substring(8));
            } else {
                num = Integer.MAX_VALUE;
            }
            KeywordList separatedKeywords = entry.getKeywords(keywordDelimiter);
            StringBuilder sb = new StringBuilder();
            int i = 0;
            for (Keyword keyword : separatedKeywords) {
                // remove all spaces
                sb.append(keyword.toString().replaceAll("\\s+", ""));
                i++;
                if (i >= num) {
                    break;
                }
            }
            return sb.toString();
        } else {
            // we haven't seen any special demands
            return entry.getFieldOrAlias(val).orElse("");
        }
    } catch (NullPointerException ex) {
        LOGGER.debug("Problem making label", ex);
        return "";
    }
}
Also used : Keyword(org.jabref.model.entry.Keyword) KeywordList(org.jabref.model.entry.KeywordList)

Aggregations

KeywordList (org.jabref.model.entry.KeywordList)13 Keyword (org.jabref.model.entry.Keyword)8 FieldChange (org.jabref.model.FieldChange)7 ArrayList (java.util.ArrayList)5 BibEntry (org.jabref.model.entry.BibEntry)5 BasePanel (org.jabref.gui.BasePanel)3 NamedCompound (org.jabref.gui.undo.NamedCompound)3 ActionEvent (java.awt.event.ActionEvent)2 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)2 SpecialField (org.jabref.model.entry.specialfields.SpecialField)2 ButtonBarBuilder (com.jgoodies.forms.builder.ButtonBarBuilder)1 FormBuilder (com.jgoodies.forms.builder.FormBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 BorderLayout (java.awt.BorderLayout)1 ActionListener (java.awt.event.ActionListener)1 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 Optional (java.util.Optional)1