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