Search in sources :

Example 6 with PropertyVetoException

use of com.sun.star.beans.PropertyVetoException in project jabref by JabRef.

the class OpenOfficePanel method initPanel.

private void initPanel() {
    connect.addActionListener(e -> connect(true));
    manualConnect.addActionListener(e -> connect(false));
    selectDocument.setToolTipText(Localization.lang("Select which open Writer document to work on"));
    selectDocument.addActionListener(e -> {
        try {
            ooBase.selectDocument();
            frame.output(Localization.lang("Connected to document") + ": " + ooBase.getCurrentDocumentTitle().orElse(""));
        } catch (UnknownPropertyException | WrappedTargetException | IndexOutOfBoundsException | NoSuchElementException | NoDocumentException ex) {
            JOptionPane.showMessageDialog(frame, ex.getMessage(), Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
            LOGGER.warn("Problem connecting", ex);
        }
    });
    setStyleFile.addActionListener(event -> {
        if (styleDialog == null) {
            styleDialog = new StyleSelectDialog(frame, preferences, loader);
        }
        styleDialog.setVisible(true);
        styleDialog.getStyle().ifPresent(selectedStyle -> {
            style = selectedStyle;
            try {
                style.ensureUpToDate();
            } catch (IOException e) {
                LOGGER.warn("Unable to reload style file '" + style.getPath() + "'", e);
            }
            frame.setStatus(Localization.lang("Current style is '%0'", style.getName()));
        });
    });
    pushEntries.setToolTipText(Localization.lang("Cite selected entries between parenthesis"));
    pushEntries.addActionListener(e -> pushEntries(true, true, false));
    pushEntriesInt.setToolTipText(Localization.lang("Cite selected entries with in-text citation"));
    pushEntriesInt.addActionListener(e -> pushEntries(false, true, false));
    pushEntriesEmpty.setToolTipText(Localization.lang("Insert a citation without text (the entry will appear in the reference list)"));
    pushEntriesEmpty.addActionListener(e -> pushEntries(false, false, false));
    pushEntriesAdvanced.setToolTipText(Localization.lang("Cite selected entries with extra information"));
    pushEntriesAdvanced.addActionListener(e -> pushEntries(false, true, true));
    update.setToolTipText(Localization.lang("Ensure that the bibliography is up-to-date"));
    Action updateAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if (style == null) {
                    style = loader.getUsedStyle();
                } else {
                    style.ensureUpToDate();
                }
                ooBase.updateSortedReferenceMarks();
                List<BibDatabase> databases = getBaseList();
                List<String> unresolvedKeys = ooBase.refreshCiteMarkers(databases, style);
                ooBase.rebuildBibTextSection(databases, style);
                if (!unresolvedKeys.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, Localization.lang("Your OpenOffice/LibreOffice document references the BibTeX key '%0', which could not be found in your current library.", unresolvedKeys.get(0)), Localization.lang("Unable to synchronize bibliography"), JOptionPane.ERROR_MESSAGE);
                }
            } catch (UndefinedCharacterFormatException ex) {
                reportUndefinedCharacterFormat(ex);
            } catch (UndefinedParagraphFormatException ex) {
                reportUndefinedParagraphFormat(ex);
            } catch (ConnectionLostException ex) {
                showConnectionLostErrorMessage();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(frame, Localization.lang("You must select either a valid style file, or use one of the default styles."), Localization.lang("No valid style file defined"), JOptionPane.ERROR_MESSAGE);
                LOGGER.warn("Problem with style file", ex);
            } catch (BibEntryNotFoundException ex) {
                JOptionPane.showMessageDialog(frame, Localization.lang("Your OpenOffice/LibreOffice document references the BibTeX key '%0', which could not be found in your current library.", ex.getBibtexKey()), Localization.lang("Unable to synchronize bibliography"), JOptionPane.ERROR_MESSAGE);
                LOGGER.debug("BibEntry not found", ex);
            } catch (com.sun.star.lang.IllegalArgumentException | PropertyVetoException | UnknownPropertyException | WrappedTargetException | NoSuchElementException | CreationException ex) {
                LOGGER.warn("Could not update bibliography", ex);
            }
        }
    };
    update.addActionListener(updateAction);
    merge.setToolTipText(Localization.lang("Combine pairs of citations that are separated by spaces only"));
    merge.addActionListener(e -> {
        try {
            ooBase.combineCiteMarkers(getBaseList(), style);
        } catch (UndefinedCharacterFormatException ex) {
            reportUndefinedCharacterFormat(ex);
        } catch (com.sun.star.lang.IllegalArgumentException | UnknownPropertyException | PropertyVetoException | CreationException | NoSuchElementException | WrappedTargetException | IOException | BibEntryNotFoundException ex) {
            LOGGER.warn("Problem combining cite markers", ex);
        }
    });
    settingsB.addActionListener(e -> showSettingsPopup());
    manageCitations.addActionListener(e -> {
        try {
            CitationManager cm = new CitationManager(frame, ooBase);
            cm.showDialog();
        } catch (NoSuchElementException | WrappedTargetException | UnknownPropertyException ex) {
            LOGGER.warn("Problem showing citation manager", ex);
        }
    });
    exportCitations.addActionListener(event -> exportEntries());
    selectDocument.setEnabled(false);
    pushEntries.setEnabled(false);
    pushEntriesInt.setEnabled(false);
    pushEntriesEmpty.setEnabled(false);
    pushEntriesAdvanced.setEnabled(false);
    update.setEnabled(false);
    merge.setEnabled(false);
    manageCitations.setEnabled(false);
    exportCitations.setEnabled(false);
    diag = new JDialog((JFrame) null, "OpenOffice/LibreOffice panel", false);
    FormBuilder mainBuilder = FormBuilder.create().layout(new FormLayout("fill:pref:grow", "p,p,p,p,p,p,p,p,p,p,p"));
    FormBuilder topRowBuilder = FormBuilder.create().layout(new FormLayout("fill:pref:grow, 1dlu, fill:pref:grow, 1dlu, fill:pref:grow, 1dlu, fill:pref:grow, 1dlu, fill:pref", "pref"));
    topRowBuilder.add(connect).xy(1, 1);
    topRowBuilder.add(manualConnect).xy(3, 1);
    topRowBuilder.add(selectDocument).xy(5, 1);
    topRowBuilder.add(update).xy(7, 1);
    topRowBuilder.add(help).xy(9, 1);
    mainBuilder.add(topRowBuilder.getPanel()).xy(1, 1);
    mainBuilder.add(setStyleFile).xy(1, 2);
    mainBuilder.add(pushEntries).xy(1, 3);
    mainBuilder.add(pushEntriesInt).xy(1, 4);
    mainBuilder.add(pushEntriesAdvanced).xy(1, 5);
    mainBuilder.add(pushEntriesEmpty).xy(1, 6);
    mainBuilder.add(merge).xy(1, 7);
    mainBuilder.add(manageCitations).xy(1, 8);
    mainBuilder.add(exportCitations).xy(1, 9);
    mainBuilder.add(settingsB).xy(1, 10);
    JPanel content = new JPanel();
    sidePane.setContentContainer(content);
    content.setLayout(new BorderLayout());
    content.add(mainBuilder.getPanel(), BorderLayout.CENTER);
    frame.getTabbedPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(Globals.getKeyPrefs().getKey(KeyBinding.REFRESH_OO), "Refresh OO");
    frame.getTabbedPane().getActionMap().put("Refresh OO", updateAction);
}
Also used : JPanel(javax.swing.JPanel) AbstractAction(javax.swing.AbstractAction) HelpAction(org.jabref.gui.help.HelpAction) Action(javax.swing.Action) WrappedTargetException(com.sun.star.lang.WrappedTargetException) ActionEvent(java.awt.event.ActionEvent) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) AbstractAction(javax.swing.AbstractAction) FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) IOException(java.io.IOException) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) PropertyVetoException(com.sun.star.beans.PropertyVetoException) BibDatabase(org.jabref.model.database.BibDatabase) NoSuchElementException(com.sun.star.container.NoSuchElementException) UndefinedParagraphFormatException(org.jabref.logic.openoffice.UndefinedParagraphFormatException) JDialog(javax.swing.JDialog)

Example 7 with PropertyVetoException

use of com.sun.star.beans.PropertyVetoException in project jabref by JabRef.

the class OOBibBase method combineCiteMarkers.

public void combineCiteMarkers(List<BibDatabase> databases, OOBibStyle style) throws IOException, WrappedTargetException, NoSuchElementException, IllegalArgumentException, UndefinedCharacterFormatException, UnknownPropertyException, PropertyVetoException, CreationException, BibEntryNotFoundException {
    XNameAccess nameAccess = getReferenceMarks();
    // TODO: doesn't work for citations in footnotes/tables
    List<String> names = getSortedReferenceMarks(nameAccess);
    final XTextRangeCompare compare = UnoRuntime.queryInterface(XTextRangeCompare.class, text);
    int piv = 0;
    boolean madeModifications = false;
    while (piv < (names.size() - 1)) {
        XTextRange range1 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv))).getAnchor().getEnd();
        XTextRange range2 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv + 1))).getAnchor().getStart();
        if (range1.getText() != range2.getText()) {
            piv++;
            continue;
        }
        XTextCursor mxDocCursor = range1.getText().createTextCursorByRange(range1);
        mxDocCursor.goRight((short) 1, true);
        boolean couldExpand = true;
        while (couldExpand && (compare.compareRegionEnds(mxDocCursor, range2) > 0)) {
            couldExpand = mxDocCursor.goRight((short) 1, true);
        }
        String cursorText = mxDocCursor.getString();
        // Check if the string contains no line breaks and only whitespace:
        if ((cursorText.indexOf('\n') == -1) && cursorText.trim().isEmpty()) {
            // marks are removed, preventing damage to the user's document:
            if (style.isFormatCitations()) {
                XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, mxDocCursor);
                String charStyle = style.getCitationCharacterFormat();
                try {
                    xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
                } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                    // will result in an error message for the user:
                    throw new UndefinedCharacterFormatException(charStyle);
                }
            }
            List<String> keys = parseRefMarkName(names.get(piv));
            keys.addAll(parseRefMarkName(names.get(piv + 1)));
            removeReferenceMark(names.get(piv));
            removeReferenceMark(names.get(piv + 1));
            List<BibEntry> entries = new ArrayList<>();
            for (String key : keys) {
                for (BibDatabase database : databases) {
                    Optional<BibEntry> entry = database.getEntryByKey(key);
                    if (entry.isPresent()) {
                        entries.add(entry.get());
                        break;
                    }
                }
            }
            Collections.sort(entries, new FieldComparator(FieldName.YEAR));
            String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
            // Insert bookmark:
            String bName = getUniqueReferenceMarkName(keyString, OOBibBase.AUTHORYEAR_PAR);
            insertReferenceMark(bName, "tmp", mxDocCursor, true, style);
            names.set(piv + 1, bName);
            madeModifications = true;
        }
        piv++;
    }
    if (madeModifications) {
        updateSortedReferenceMarks();
        refreshCiteMarkers(databases, style);
    }
}
Also used : XTextRange(com.sun.star.text.XTextRange) BibEntry(org.jabref.model.entry.BibEntry) WrappedTargetException(com.sun.star.lang.WrappedTargetException) ArrayList(java.util.ArrayList) XNameAccess(com.sun.star.container.XNameAccess) XTextCursor(com.sun.star.text.XTextCursor) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) XTextRangeCompare(com.sun.star.text.XTextRangeCompare) Point(com.sun.star.awt.Point) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) BibDatabase(org.jabref.model.database.BibDatabase) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Aggregations

PropertyVetoException (com.sun.star.beans.PropertyVetoException)7 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)7 WrappedTargetException (com.sun.star.lang.WrappedTargetException)7 BibDatabase (org.jabref.model.database.BibDatabase)6 XPropertySet (com.sun.star.beans.XPropertySet)4 NoSuchElementException (com.sun.star.container.NoSuchElementException)4 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)4 IOException (java.io.IOException)4 BibEntry (org.jabref.model.entry.BibEntry)4 Point (com.sun.star.awt.Point)3 UndefinedParagraphFormatException (org.jabref.logic.openoffice.UndefinedParagraphFormatException)3 IllegalTypeException (com.sun.star.beans.IllegalTypeException)2 NotRemoveableException (com.sun.star.beans.NotRemoveableException)2 PropertyExistException (com.sun.star.beans.PropertyExistException)2 XNameAccess (com.sun.star.container.XNameAccess)2 DisposedException (com.sun.star.lang.DisposedException)2 XTextContent (com.sun.star.text.XTextContent)2 XTextCursor (com.sun.star.text.XTextCursor)2 XTextRange (com.sun.star.text.XTextRange)2 ArrayList (java.util.ArrayList)2