1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 import com.sun.star.beans.Property;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.frame.XComponentLoader;
28 import com.sun.star.frame.XModel;
29 import com.sun.star.lang.XComponent;
30 import com.sun.star.lang.XMultiComponentFactory;
31 import com.sun.star.sdbc.XRow;
32 import com.sun.star.ucb.Command;
33 import com.sun.star.ucb.XCommandProcessor;
34 import com.sun.star.ucb.XContent;
35 import com.sun.star.ucb.XContentIdentifier;
36 import com.sun.star.ucb.XContentIdentifierFactory;
37 import com.sun.star.ucb.XContentProvider;
38 import com.sun.star.ucb.XSimpleFileAccess;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XComponentContext;
41 import javax.swing.JOptionPane;
42 
43 
44 public class TDocSupplier {
45     private XMultiComponentFactory m_xMultiComponentFactory;
46     private XComponentContext m_xComponentContext;
47 
48 
49     /** Creates a new instance of TDocSupplier */
TDocSupplier(XComponentContext _xComponentContext)50     public TDocSupplier(XComponentContext _xComponentContext) {
51         m_xComponentContext = _xComponentContext;
52         m_xMultiComponentFactory = m_xComponentContext.getServiceManager();
53     }
54 
55 
getXComponentContext()56     protected XComponentContext getXComponentContext(){
57         return m_xComponentContext;
58     }
59 
60 
getXMultiComponentFactory()61     protected XMultiComponentFactory getXMultiComponentFactory(){
62         return m_xMultiComponentFactory;
63     }
64 
getXModelByTDocUrl(String _sTDocUrl)65         public XModel getXModelByTDocUrl(String _sTDocUrl){
66         try{
67             XRow xRow = getXRowOfTDocUrl(_sTDocUrl, "DocumentModel");
68             if (xRow != null){
69                 Object oModel = xRow.getObject(1, null);
70                 XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, oModel);
71                 return xModel;
72             }
73         }catch(Exception exception){
74             exception.printStackTrace(System.out);
75         }
76         JOptionPane.showMessageDialog(new javax.swing.JFrame(), "The selected Document could not be opened!", "Object Inspector", JOptionPane.ERROR_MESSAGE);
77         return null;
78         }
79 
80 
getTitleByTDocUrl(String _sTDocUrl)81         public String getTitleByTDocUrl(String _sTDocUrl){
82         try{
83             XRow xRow = this.getXRowOfTDocUrl(_sTDocUrl, "Title");
84             if (xRow != null){
85                 return xRow.getString(1);
86             }
87         }catch(Exception exception){
88             exception.printStackTrace(System.out);
89         }
90         JOptionPane.showMessageDialog(new javax.swing.JFrame(), "The selected Document could not be opened!", "Object Inspector", JOptionPane.ERROR_MESSAGE);
91         return "";
92         }
93 
94 
getXRowOfTDocUrl(String _sTDocUrl, String _sPropertyName)95         private XRow getXRowOfTDocUrl(String _sTDocUrl, String _sPropertyName){
96         try{
97             String[] keys = new String[2];
98             keys[ 0 ] = "Local";
99             keys[ 1 ] = "Office";
100             Object oUCB = getXMultiComponentFactory().createInstanceWithArgumentsAndContext( "com.sun.star.ucb.UniversalContentBroker", keys, getXComponentContext() );
101             XContentIdentifierFactory xIdFactory = (XContentIdentifierFactory)UnoRuntime.queryInterface(XContentIdentifierFactory.class, oUCB);
102             XContentProvider xProvider = (XContentProvider)UnoRuntime.queryInterface(XContentProvider.class, oUCB);
103             XContentIdentifier xId = xIdFactory.createContentIdentifier(_sTDocUrl);
104             XContent xContent = xProvider.queryContent(xId);
105             XCommandProcessor xCmdProcessor = (XCommandProcessor) UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
106             Property aProperty = new Property();
107             aProperty.Name = _sPropertyName; // "DocumentModel";                //DocumentModel
108             Command aCommand  = new Command();
109             aCommand.Name = "getPropertyValues";
110             aCommand.Handle = -1; // not available
111             aCommand.Argument = new Property[]{aProperty};
112             Object oAny = xCmdProcessor.execute(aCommand, 0, null);
113             XRow xRow = (XRow) UnoRuntime.queryInterface(XRow.class, oAny);
114             return xRow;
115         }catch(Exception exception){
116             exception.printStackTrace(System.out);
117             return null;
118         }}
119 
120 
getTDocTitles(String[] _sTDocUrls)121         protected String[] getTDocTitles(String[] _sTDocUrls){
122             String[] sTitles = new String[_sTDocUrls.length];
123             for (int i = 0; i < _sTDocUrls.length; i++){
124                 sTitles[i] = getTitleByTDocUrl(_sTDocUrls[i]);
125             }
126             return sTitles;
127         }
128 
129 
getTDocUrls()130         protected String[] getTDocUrls(){
131         try{
132             Object oSimpleFileAccess = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", getXComponentContext());
133             XSimpleFileAccess xSimpleFileAccess =  (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class, oSimpleFileAccess);
134             String[] sContent = xSimpleFileAccess.getFolderContents("vnd.sun.star.tdoc:/", false);
135             return sContent;
136         } catch( Exception e ) {
137             System.err.println( e );
138             return new String[]{};
139         }}
140 
141 
openEmptyDocument(String _sUrl)142     public XComponent openEmptyDocument(String _sUrl){
143     try{
144         PropertyValue[] aPropertyValues = new PropertyValue[1];
145         aPropertyValues[0] = new PropertyValue();
146         aPropertyValues[0].Name = "Hidden";
147         aPropertyValues[0].Value = Boolean.TRUE;
148         Object oDesktop = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.frame.Desktop", getXComponentContext());
149         XComponentLoader xCL = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
150         return xCL.loadComponentFromURL(_sUrl, "_default", 0, aPropertyValues);
151     }
152     catch( Exception exception ) {
153         System.err.println( exception );
154         return null;
155     }}
156 
157 }
158