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