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