1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /************************************************************************* 4 * 5 * The Contents of this file are made available subject to the terms of 6 * the BSD license. 7 * 8 * Copyright 2000, 2010 Oracle and/or its affiliates. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 30 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 33 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 *************************************************************************/ 36 37 import com.sun.star.beans.Property; 38 import com.sun.star.beans.PropertyValue; 39 import com.sun.star.frame.XComponentLoader; 40 import com.sun.star.frame.XModel; 41 import com.sun.star.lang.XComponent; 42 import com.sun.star.lang.XMultiComponentFactory; 43 import com.sun.star.sdbc.XRow; 44 import com.sun.star.ucb.Command; 45 import com.sun.star.ucb.UniversalContentBroker; 46 import com.sun.star.ucb.XCommandProcessor; 47 import com.sun.star.ucb.XContent; 48 import com.sun.star.ucb.XContentIdentifier; 49 import com.sun.star.ucb.XSimpleFileAccess; 50 import com.sun.star.ucb.XUniversalContentBroker; 51 import com.sun.star.uno.UnoRuntime; 52 import com.sun.star.uno.XComponentContext; 53 import javax.swing.JOptionPane; 54 55 56 public class TDocSupplier { 57 private XMultiComponentFactory m_xMultiComponentFactory; 58 private XComponentContext m_xComponentContext; 59 60 61 /** Creates a new instance of TDocSupplier */ TDocSupplier(XComponentContext _xComponentContext)62 public TDocSupplier(XComponentContext _xComponentContext) { 63 m_xComponentContext = _xComponentContext; 64 m_xMultiComponentFactory = m_xComponentContext.getServiceManager(); 65 } 66 67 getXComponentContext()68 private XComponentContext getXComponentContext(){ 69 return m_xComponentContext; 70 } 71 72 getXMultiComponentFactory()73 private XMultiComponentFactory getXMultiComponentFactory(){ 74 return m_xMultiComponentFactory; 75 } 76 getXModelByTDocUrl(String _sTDocUrl)77 public XModel getXModelByTDocUrl(String _sTDocUrl){ 78 try{ 79 XRow xRow = getXRowOfTDocUrl(_sTDocUrl, "DocumentModel"); 80 if (xRow != null){ 81 Object oModel = xRow.getObject(1, null); 82 XModel xModel = UnoRuntime.queryInterface(XModel.class, oModel); 83 return xModel; 84 } 85 }catch(Exception exception){ 86 exception.printStackTrace(System.err); 87 } 88 JOptionPane.showMessageDialog(new javax.swing.JFrame(), "The selected Document could not be opened!", "Object Inspector", JOptionPane.ERROR_MESSAGE); 89 return null; 90 } 91 92 getTitleByTDocUrl(String _sTDocUrl)93 public String getTitleByTDocUrl(String _sTDocUrl){ 94 try{ 95 XRow xRow = this.getXRowOfTDocUrl(_sTDocUrl, "Title"); 96 if (xRow != null){ 97 return xRow.getString(1); 98 } 99 }catch(Exception exception){ 100 exception.printStackTrace(System.err); 101 } 102 JOptionPane.showMessageDialog(new javax.swing.JFrame(), "The selected Document could not be opened!", "Object Inspector", JOptionPane.ERROR_MESSAGE); 103 return ""; 104 } 105 106 getXRowOfTDocUrl(String _sTDocUrl, String _sPropertyName)107 private XRow getXRowOfTDocUrl(String _sTDocUrl, String _sPropertyName){ 108 try{ 109 XUniversalContentBroker xUCB = UniversalContentBroker.create( getXComponentContext() ); 110 XContentIdentifier xId = xUCB.createContentIdentifier(_sTDocUrl); 111 XContent xContent = xUCB.queryContent(xId); 112 XCommandProcessor xCmdProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent); 113 Property aProperty = new Property(); 114 aProperty.Name = _sPropertyName; // "DocumentModel"; //DocumentModel 115 Command aCommand = new Command(); 116 aCommand.Name = "getPropertyValues"; 117 aCommand.Handle = -1; // not available 118 aCommand.Argument = new Property[]{aProperty}; 119 Object oAny = xCmdProcessor.execute(aCommand, 0, null); 120 XRow xRow = UnoRuntime.queryInterface(XRow.class, oAny); 121 return xRow; 122 }catch(Exception exception){ 123 exception.printStackTrace(System.err); 124 return null; 125 }} 126 127 getTDocTitles(String[] _sTDocUrls)128 protected String[] getTDocTitles(String[] _sTDocUrls){ 129 String[] sTitles = new String[_sTDocUrls.length]; 130 for (int i = 0; i < _sTDocUrls.length; i++){ 131 sTitles[i] = getTitleByTDocUrl(_sTDocUrls[i]); 132 } 133 return sTitles; 134 } 135 136 getTDocUrls()137 protected String[] getTDocUrls(){ 138 try{ 139 Object oSimpleFileAccess = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", getXComponentContext()); 140 XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class, oSimpleFileAccess); 141 String[] sContent = xSimpleFileAccess.getFolderContents("vnd.sun.star.tdoc:/", false); 142 return sContent; 143 } catch( Exception e ) { 144 System.err.println( e ); 145 return new String[]{}; 146 }} 147 148 openEmptyDocument(String _sUrl)149 public XComponent openEmptyDocument(String _sUrl){ 150 try{ 151 PropertyValue[] aPropertyValues = new PropertyValue[1]; 152 aPropertyValues[0] = new PropertyValue(); 153 aPropertyValues[0].Name = "Hidden"; 154 aPropertyValues[0].Value = Boolean.TRUE; 155 Object oDesktop = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.frame.Desktop", getXComponentContext()); 156 XComponentLoader xCL = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); 157 return xCL.loadComponentFromURL(_sUrl, "_default", 0, aPropertyValues); 158 } 159 catch( Exception exception ) { 160 System.err.println( exception ); 161 return null; 162 }} 163 164 } 165 166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 167