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