Hi together,

in May we had a discussion on this topic. Meanwhile I solved my problem
due to an other thread on this mailing list. Here is the code I use for
getting the printers and its tray names from OOo:
-----%<-----
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sun.star.awt.XInfoPrinter;
import com.sun.star.awt.XPrinterServer;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.XComponentContext;

import de.twc.oocom.oo.OOWorkbench;
import de.twc.utils.IPrinter;

public class OOPrinter implements IPrinter {

        /**
         * Instance of this class.
         */
        private static volatile IPrinter INSTANCE;
        
        /**
         * The OpenOffice.org [EMAIL PROTECTED] XPrinterServer}.
         */
        private XPrinterServer xPrinterServer = null;
        
        /**
         * Getting the instance of this class. This class is a  
         * singleton.
         * @return [EMAIL PROTECTED] IPrinter} Instance of this class.
         */
        public static IPrinter getInstance() {
                if(INSTANCE == null)
                        synchronized (OOPrinter.class) {
                                if (INSTANCE == null) {
                                        INSTANCE = new OOPrinter();
                                }
                        }
                return INSTANCE;
        }

        /**
         * Getting the printer trays of a printer via OpenOffice.org.           
         * OpenOffice.org does not offer a method to get the printer
         * tray names directly. But is
         * offers a method, to get printer information and returns a
         * String like this: "*;*;Tray Name; ;*;". This method derives
         * the tray name from the
         * string by detecting the sourrounding semicolons.
         * @param printerName String with the name of the printer.
         * @return String[] with the names of the printer trays.
         */
        @Override
        public String[] getPrinterTrays(String printerName) {
                if(xPrinterServer == null)
                        initXPrinterServer();
                
                XInfoPrinter xInfoPrinter =
                        xPrinterServer.createInfoPrinter(printerName);

                // Print the trays received the OOo way
                String ooTrays[] = xInfoPrinter.getFormDescriptions();
                for(int i = 0; i < ooTrays.length; i++) {
                        String rangeRegex = "(.*;){2}[^;]{5,}";
                        Pattern pattern = Pattern.compile(rangeRegex);
                        Matcher matcher = pattern.matcher(ooTrays[i]);
                        while(matcher.find()){
                                String ooTray = matcher.group();
                                ooTrays[i] = ooTray.substring(
                                        ooTray.lastIndexOf(";") + 1);
                        }
                }
                return ooTrays;
        }

        /**
         * Getting the printer names from OpenOffice.org.
         * @return String[] containing the printer names.
         */
        @Override
        public String[] getPrinters() {
                if(xPrinterServer == null)
                        initXPrinterServer();
                return  xPrinterServer.getPrinterNames();
        }

        /**
         * Initialising the [EMAIL PROTECTED] XPrinterServer}.
         */
        private void initXPrinterServer() {
                XComponentContext xCC =
                        OOWorkbench.getXComponentContext();
                XMultiComponentFactory xMCF = xCC.getServiceManager();
                Object oPrintServer = null;
                try {
                        oPrintServer = xMCF.createInstanceWithContext(
                                "com.sun.star.awt.PrinterServer", xCC);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                try {
                        xPrinterServer = (XPrinterServer)
                                AnyConverter.toObject(  
                                XPrinterServer.class, oPrintServer);
                } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                }
        }
}
-----%<-----

I hope this code serves you.

Greetings, Tobias

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to