Search in sources :

Example 1 with XPropertySet

use of com.sun.star.beans.XPropertySet in project languagetool by languagetool-org.

the class Main method getLanguage.

/**
   * Checks the language under the cursor. Used for opening the configuration dialog.
   * @return the language under the visible cursor
   */
@Nullable
private Language getLanguage() {
    XComponent xComponent = getXComponent();
    Locale charLocale;
    XPropertySet xCursorProps;
    try {
        XModel model = UnoRuntime.queryInterface(XModel.class, xComponent);
        XTextViewCursorSupplier xViewCursorSupplier = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, model.getCurrentController());
        XTextViewCursor xCursor = xViewCursorSupplier.getViewCursor();
        if (xCursor.isCollapsed()) {
            // no text selection
            xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xCursor);
        } else {
            // text is selected, need to create another cursor
            // as multiple languages can occur here - we care only
            // about character under the cursor, which might be wrong
            // but it applies only to the checking dialog to be removed
            xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xCursor.getText().createTextCursorByRange(xCursor.getStart()));
        }
        // Thus we check the text itself:
        if (new KhmerDetector().isThisLanguage(xCursor.getText().getString())) {
            return Languages.getLanguageForShortCode("km");
        }
        if (new TamilDetector().isThisLanguage(xCursor.getText().getString())) {
            return Languages.getLanguageForShortCode("ta");
        }
        Object obj = xCursorProps.getPropertyValue("CharLocale");
        if (obj == null) {
            return Languages.getLanguageForShortCode("en-US");
        }
        charLocale = (Locale) obj;
        boolean langIsSupported = false;
        for (Language element : Languages.get()) {
            if (charLocale.Language.equalsIgnoreCase(LIBREOFFICE_SPECIAL_LANGUAGE_TAG) && element.getShortCodeWithCountryAndVariant().equalsIgnoreCase(charLocale.Variant)) {
                langIsSupported = true;
                break;
            }
            if (element.getShortCode().equals(charLocale.Language)) {
                langIsSupported = true;
                break;
            }
        }
        if (!langIsSupported) {
            String message = Tools.i18n(MESSAGES, "language_not_supported", charLocale.Language);
            JOptionPane.showMessageDialog(null, message);
            return null;
        }
    } catch (Throwable t) {
        showError(t);
        return null;
    }
    return getLanguage(charLocale);
}
Also used : XModel(com.sun.star.frame.XModel) XPropertySet(com.sun.star.beans.XPropertySet) XTextViewCursorSupplier(com.sun.star.text.XTextViewCursorSupplier) Language(org.languagetool.Language) XTextViewCursor(com.sun.star.text.XTextViewCursor) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XPropertySet

use of com.sun.star.beans.XPropertySet in project translationstudio8 by heartsome.

the class TerminationOpenoffice method closeOpenoffice.

// public static void main(String[] args) {
/**
	 * Close openoffice.
	 * @param port
	 *            the port
	 */
public static void closeOpenoffice(String port) {
    XComponentContext xRemoteContext = null;
    XMultiComponentFactory xRemoteServiceManager = null;
    XDesktop xDesktop = null;
    try {
        XComponentContext xLocalContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
        XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
        Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", //$NON-NLS-1$
        xLocalContext);
        XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver);
        Object initialObject = xUnoUrlResolver.resolve(//$NON-NLS-1$ //$NON-NLS-2$
        "uno:socket,host=localhost,port=" + port + ";urp;StarOffice.ServiceManager");
        // Object initialObject =
        // xUnoUrlResolver.resolve("uno:pipe,name=my_app;urp;StarOffice.ServiceManager"
        // );
        XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, initialObject);
        //$NON-NLS-1$
        Object context = xPropertySet.getPropertyValue("DefaultContext");
        xRemoteContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, context);
        xRemoteServiceManager = xRemoteContext.getServiceManager();
        // get Desktop instance
        Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", //$NON-NLS-1$
        xRemoteContext);
        xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
        TerminateListener terminateListener = new TerminateListener();
        xDesktop.addTerminateListener(terminateListener);
        // try to terminate while we are at work
        atWork = true;
        boolean terminated = xDesktop.terminate();
        System.out.println(//$NON-NLS-1$
        "The Office " + //$NON-NLS-1$ //$NON-NLS-2$
        (terminated ? "has been terminated" : "is still running, we are at work"));
        // no longer at work
        atWork = false;
        // once more: try to terminate
        terminated = xDesktop.terminate();
        System.out.println(//$NON-NLS-1$
        "The Office " + (//$NON-NLS-1$
        terminated ? //$NON-NLS-1$
        "has been terminated" : //$NON-NLS-1$
        "is still running. Someone else prevents termination, e.g. the quickstarter"));
    } catch (java.lang.Exception e) {
        if (Converter.DEBUG_MODE) {
            e.printStackTrace();
        }
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XComponentContext(com.sun.star.uno.XComponentContext) XDesktop(com.sun.star.frame.XDesktop) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory) XUnoUrlResolver(com.sun.star.bridge.XUnoUrlResolver)

Example 3 with XPropertySet

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

the class OOBibBase method insertReferenceMark.

private void insertReferenceMark(String name, String citationText, XTextCursor position, boolean withText, OOBibStyle style) throws UnknownPropertyException, WrappedTargetException, PropertyVetoException, IllegalArgumentException, UndefinedCharacterFormatException, CreationException {
    // Check if there is "page info" stored for this citation. If so, insert it into
    // the citation text before inserting the citation:
    Optional<String> pageInfo = getCustomProperty(name);
    String citText;
    if ((pageInfo.isPresent()) && !pageInfo.get().isEmpty()) {
        citText = style.insertPageInfo(citationText, pageInfo.get());
    } else {
        citText = citationText;
    }
    Object bookmark;
    try {
        bookmark = mxDocFactory.createInstance("com.sun.star.text.ReferenceMark");
    } catch (Exception e) {
        throw new CreationException(e.getMessage());
    }
    // Name the reference
    XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, bookmark);
    xNamed.setName(name);
    if (withText) {
        position.setString(citText);
        XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, position);
        // Set language to [None]:
        xCursorProps.setPropertyValue("CharLocale", new Locale("zxx", "", ""));
        if (style.isFormatCitations()) {
            String charStyle = style.getCitationCharacterFormat();
            try {
                xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
            } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                throw new UndefinedCharacterFormatException(charStyle);
            }
        }
    } else {
        position.setString("");
    }
    // get XTextContent interface
    XTextContent xTextContent = UnoRuntime.queryInterface(XTextContent.class, bookmark);
    position.getText().insertTextContent(position, xTextContent, true);
    // Check if we should italicize the "et al." string in citations:
    boolean italicize = style.getBooleanCitProperty(OOBibStyle.ITALIC_ET_AL);
    if (italicize) {
        String etAlString = style.getStringCitProperty(OOBibStyle.ET_AL_STRING);
        int index = citText.indexOf(etAlString);
        if (index >= 0) {
            italicizeOrBold(position, true, index, index + etAlString.length());
        }
    }
    position.collapseToEnd();
}
Also used : Locale(com.sun.star.lang.Locale) XNamed(com.sun.star.container.XNamed) WrappedTargetException(com.sun.star.lang.WrappedTargetException) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) PropertyVetoException(com.sun.star.beans.PropertyVetoException) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) IllegalTypeException(com.sun.star.beans.IllegalTypeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WrappedTargetException(com.sun.star.lang.WrappedTargetException) BootstrapException(com.sun.star.comp.helper.BootstrapException) DisposedException(com.sun.star.lang.DisposedException) NoSuchElementException(com.sun.star.container.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) UndefinedParagraphFormatException(org.jabref.logic.openoffice.UndefinedParagraphFormatException) IOException(java.io.IOException) NotRemoveableException(com.sun.star.beans.NotRemoveableException) PropertyExistException(com.sun.star.beans.PropertyExistException) Point(com.sun.star.awt.Point) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) XTextContent(com.sun.star.text.XTextContent) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Example 4 with XPropertySet

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

the class OOBibBase method insertEntry.

/**
     * This method inserts a cite marker in the text for the given BibEntry,
     * and may refresh the bibliography.
     * @param entries The entries to cite.
     * @param database The database the entry belongs to.
     * @param style The bibliography style we are using.
     * @param inParenthesis Indicates whether it is an in-text citation or a citation in parenthesis.
     *   This is not relevant if numbered citations are used.
     * @param withText Indicates whether this should be a normal citation (true) or an empty
     *   (invisible) citation (false).
     * @param sync Indicates whether the reference list should be refreshed.
     * @throws IllegalTypeException
     * @throws PropertyExistException
     * @throws NotRemoveableException
     * @throws UnknownPropertyException
     * @throws UndefinedCharacterFormatException
     * @throws NoSuchElementException
     * @throws WrappedTargetException
     * @throws IOException
     * @throws PropertyVetoException
     * @throws CreationException
     * @throws BibEntryNotFoundException
     * @throws UndefinedParagraphFormatException
     */
public void insertEntry(List<BibEntry> entries, BibDatabase database, List<BibDatabase> allBases, OOBibStyle style, boolean inParenthesis, boolean withText, String pageInfo, boolean sync) throws IllegalArgumentException, UnknownPropertyException, NotRemoveableException, PropertyExistException, IllegalTypeException, UndefinedCharacterFormatException, WrappedTargetException, NoSuchElementException, PropertyVetoException, IOException, CreationException, BibEntryNotFoundException, UndefinedParagraphFormatException {
    try {
        XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
        if (entries.size() > 1) {
            if (style.getBooleanCitProperty(OOBibStyle.MULTI_CITE_CHRONOLOGICAL)) {
                entries.sort(yearAuthorTitleComparator);
            } else {
                entries.sort(entryComparator);
            }
        }
        String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
        // Insert bookmark:
        String bName = getUniqueReferenceMarkName(keyString, withText ? inParenthesis ? OOBibBase.AUTHORYEAR_PAR : OOBibBase.AUTHORYEAR_INTEXT : OOBibBase.INVISIBLE_CIT);
        // If we should store metadata for page info, do that now:
        if (pageInfo != null) {
            LOGGER.info("Storing page info: " + pageInfo);
            setCustomProperty(bName, pageInfo);
        }
        xViewCursor.getText().insertString(xViewCursor, " ", false);
        if (style.isFormatCitations()) {
            XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xViewCursor);
            String charStyle = style.getCitationCharacterFormat();
            try {
                xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
            } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                // Setting the character format failed, so we throw an exception that
                // will result in an error message for the user. Before that,
                // delete the space we inserted:
                xViewCursor.goLeft((short) 1, true);
                xViewCursor.setString("");
                throw new UndefinedCharacterFormatException(charStyle);
            }
        }
        xViewCursor.goLeft((short) 1, false);
        Map<BibEntry, BibDatabase> databaseMap = new HashMap<>();
        for (BibEntry entry : entries) {
            databaseMap.put(entry, database);
        }
        String citeText = style.isNumberEntries() ? "-" : style.getCitationMarker(entries, databaseMap, inParenthesis, null, null);
        insertReferenceMark(bName, citeText, xViewCursor, withText, style);
        xViewCursor.collapseToEnd();
        xViewCursor.goRight((short) 1, false);
        XTextRange position = xViewCursor.getEnd();
        if (sync) {
            // To account for numbering and for uniqiefiers, we must refresh the cite markers:
            updateSortedReferenceMarks();
            refreshCiteMarkers(allBases, style);
            // Insert it at the current position:
            rebuildBibTextSection(allBases, style);
        }
        // Go back to the relevant position:
        xViewCursor.gotoRange(position, false);
    } catch (DisposedException ex) {
        // or catch a DisposedException (which is in a OO JAR file).
        throw new ConnectionLostException(ex.getMessage());
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) XTextRange(com.sun.star.text.XTextRange) WrappedTargetException(com.sun.star.lang.WrappedTargetException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) BibDatabase(org.jabref.model.database.BibDatabase) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) XTextViewCursor(com.sun.star.text.XTextViewCursor) DisposedException(com.sun.star.lang.DisposedException)

Example 5 with XPropertySet

use of com.sun.star.beans.XPropertySet in project translationstudio8 by heartsome.

the class OPConnection method connect.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.converter.ooconnect.OPconnect#connect()
	 * @throws ConnectException
	 */
public void connect() throws ConnectException {
    try {
        XComponentContext localContext;
        localContext = Bootstrap.createInitialComponentContext(null);
        XMultiComponentFactory localServiceManager = localContext.getServiceManager();
        XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, localServiceManager.createInstanceWithContext("com.sun.star.connection.Connector", //$NON-NLS-1$
        localContext));
        XConnection connection = connector.connect(strConnection);
        XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, //$NON-NLS-1$
        localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext));
        //$NON-NLS-1$ //$NON-NLS-2$
        bridge = bridgeFactory.createBridge("ms2ooBridge", "urp", connection, null);
        bgComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
        // bgComponent.addEventListener(this);
        serviceMg = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridge.getInstance(//$NON-NLS-1$
        "StarOffice.ServiceManager"));
        XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceMg);
        componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, properties.getPropertyValue(//$NON-NLS-1$
        "DefaultContext"));
        connected = true;
        if (connected) {
            //$NON-NLS-1$
            System.out.println("has already connected");
        } else {
            //$NON-NLS-1$
            System.out.println("connect to Openoffice fail,please check OpenOffice service that have to open");
        }
    } catch (NoConnectException connectException) {
        //$NON-NLS-1$ //$NON-NLS-2$
        throw new ConnectException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection + ": " + connectException.getMessage()));
    } catch (Exception exception) {
        //$NON-NLS-1$
        throw new OPException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection), exception);
    } catch (java.lang.Exception e) {
        if (Converter.DEBUG_MODE) {
            e.printStackTrace();
        }
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XComponentContext(com.sun.star.uno.XComponentContext) NoConnectException(com.sun.star.connection.NoConnectException) XConnection(com.sun.star.connection.XConnection) XBridgeFactory(com.sun.star.bridge.XBridgeFactory) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory) Exception(com.sun.star.uno.Exception) ConnectException(java.net.ConnectException) NoConnectException(com.sun.star.connection.NoConnectException) XConnector(com.sun.star.connection.XConnector) ConnectException(java.net.ConnectException) NoConnectException(com.sun.star.connection.NoConnectException)

Aggregations

XPropertySet (com.sun.star.beans.XPropertySet)11 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)6 PropertyVetoException (com.sun.star.beans.PropertyVetoException)4 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)4 WrappedTargetException (com.sun.star.lang.WrappedTargetException)4 Point (com.sun.star.awt.Point)3 XTextCursor (com.sun.star.text.XTextCursor)3 XTextRange (com.sun.star.text.XTextRange)3 ArrayList (java.util.ArrayList)3 BibDatabase (org.jabref.model.database.BibDatabase)3 BibEntry (org.jabref.model.entry.BibEntry)3 XNameAccess (com.sun.star.container.XNameAccess)2 DisposedException (com.sun.star.lang.DisposedException)2 XMultiComponentFactory (com.sun.star.lang.XMultiComponentFactory)2 XParagraphCursor (com.sun.star.text.XParagraphCursor)2 XTextContent (com.sun.star.text.XTextContent)2 XTextViewCursor (com.sun.star.text.XTextViewCursor)2 XComponentContext (com.sun.star.uno.XComponentContext)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2