Search in sources :

Example 1 with IntegrityCheck

use of org.jabref.logic.integrity.IntegrityCheck in project jabref by JabRef.

the class IntegrityCheckAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    IntegrityCheck check = new IntegrityCheck(frame.getCurrentBasePanel().getBibDatabaseContext(), Globals.prefs.getFileDirectoryPreferences(), Globals.prefs.getBibtexKeyPatternPreferences(), Globals.journalAbbreviationLoader.getRepository(Globals.prefs.getJournalAbbreviationPreferences()));
    List<IntegrityMessage> messages = check.checkBibtexDatabase();
    if (messages.isEmpty()) {
        JOptionPane.showMessageDialog(frame.getCurrentBasePanel(), Localization.lang("No problems found."));
    } else {
        Map<String, Boolean> showMessage = new HashMap<>();
        // prepare data model
        Object[][] model = new Object[messages.size()][4];
        int i = 0;
        for (IntegrityMessage message : messages) {
            model[i][0] = message.getEntry().getId();
            model[i][1] = message.getEntry().getCiteKeyOptional().orElse("");
            model[i][2] = message.getFieldName();
            model[i][3] = message.getMessage();
            showMessage.put(message.getMessage(), true);
            i++;
        }
        // construct view
        JTable table = new JTable(model, new Object[] { "ID", Localization.lang("BibTeX key"), Localization.lang("Field"), Localization.lang("Message") });
        // hide IDs
        TableColumnModel columnModel = table.getColumnModel();
        columnModel.removeColumn(columnModel.getColumn(0));
        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {

            @Override
            public boolean include(Entry<?, ?> entry) {
                return showMessage.get(entry.getStringValue(3));
            }
        };
        TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
        sorter.setRowFilter(filter);
        table.setRowSorter(sorter);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setDefaultEditor(Object.class, null);
        ListSelectionModel selectionModel = table.getSelectionModel();
        selectionModel.addListSelectionListener(event -> {
            if (!event.getValueIsAdjusting()) {
                try {
                    String entryId = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][0];
                    String fieldName = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][2];
                    frame.getCurrentBasePanel().editEntryByIdAndFocusField(entryId, fieldName);
                } catch (ArrayIndexOutOfBoundsException exception) {
                }
            }
        });
        // BibTeX key
        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        // field name
        table.getColumnModel().getColumn(1).setPreferredWidth(60);
        // message
        table.getColumnModel().getColumn(2).setPreferredWidth(400);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        JScrollPane scrollPane = new JScrollPane(table);
        String title = Localization.lang("%0 problem(s) found", String.valueOf(messages.size()));
        JDialog dialog = new JDialog(frame, title, false);
        JPopupMenu menu = new JPopupMenu();
        for (String messageString : showMessage.keySet()) {
            JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(messageString, true);
            menuItem.addActionListener(event -> {
                showMessage.put(messageString, menuItem.isSelected());
                ((AbstractTableModel) table.getModel()).fireTableDataChanged();
            });
            menu.add(menuItem);
        }
        JButton menuButton = new JButton(Localization.lang("Filter"));
        menuButton.addActionListener(entry -> menu.show(menuButton, 0, menuButton.getHeight()));
        FormBuilder builder = FormBuilder.create().layout(new FormLayout("fill:pref:grow", "fill:pref:grow, 2dlu, pref"));
        builder.add(scrollPane).xy(1, 1);
        builder.add(menuButton).xy(1, 3, "c, b");
        dialog.add(builder.getPanel());
        dialog.setSize(600, 600);
        // show view
        dialog.setVisible(true);
    }
}
Also used : HashMap(java.util.HashMap) JButton(javax.swing.JButton) TableColumnModel(javax.swing.table.TableColumnModel) IntegrityCheck(org.jabref.logic.integrity.IntegrityCheck) IntegrityMessage(org.jabref.logic.integrity.IntegrityMessage) RowFilter(javax.swing.RowFilter) TableRowSorter(javax.swing.table.TableRowSorter) JScrollPane(javax.swing.JScrollPane) FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) ListSelectionModel(javax.swing.ListSelectionModel) JPopupMenu(javax.swing.JPopupMenu) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) JTable(javax.swing.JTable) AbstractTableModel(javax.swing.table.AbstractTableModel) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel) JDialog(javax.swing.JDialog)

Aggregations

FormBuilder (com.jgoodies.forms.builder.FormBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 HashMap (java.util.HashMap)1 JButton (javax.swing.JButton)1 JCheckBoxMenuItem (javax.swing.JCheckBoxMenuItem)1 JDialog (javax.swing.JDialog)1 JPopupMenu (javax.swing.JPopupMenu)1 JScrollPane (javax.swing.JScrollPane)1 JTable (javax.swing.JTable)1 ListSelectionModel (javax.swing.ListSelectionModel)1 RowFilter (javax.swing.RowFilter)1 AbstractTableModel (javax.swing.table.AbstractTableModel)1 TableColumnModel (javax.swing.table.TableColumnModel)1 TableModel (javax.swing.table.TableModel)1 TableRowSorter (javax.swing.table.TableRowSorter)1 IntegrityCheck (org.jabref.logic.integrity.IntegrityCheck)1 IntegrityMessage (org.jabref.logic.integrity.IntegrityMessage)1