Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / TDocSupplier.java
blob9c5b8f36d54d12adcf3558609bc497d68587e0a2
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
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.
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.
34 *************************************************************************/
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.UniversalContentBroker;
45 import com.sun.star.ucb.XCommandProcessor;
46 import com.sun.star.ucb.XContent;
47 import com.sun.star.ucb.XContentIdentifier;
48 import com.sun.star.ucb.XSimpleFileAccess;
49 import com.sun.star.ucb.XUniversalContentBroker;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XComponentContext;
52 import javax.swing.JOptionPane;
55 public class TDocSupplier {
56 private XMultiComponentFactory m_xMultiComponentFactory;
57 private XComponentContext m_xComponentContext;
60 /** Creates a new instance of TDocSupplier */
61 public TDocSupplier(XComponentContext _xComponentContext) {
62 m_xComponentContext = _xComponentContext;
63 m_xMultiComponentFactory = m_xComponentContext.getServiceManager();
67 private XComponentContext getXComponentContext(){
68 return m_xComponentContext;
72 private XMultiComponentFactory getXMultiComponentFactory(){
73 return m_xMultiComponentFactory;
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 = UnoRuntime.queryInterface(XModel.class, oModel);
82 return xModel;
84 }catch(Exception exception){
85 exception.printStackTrace(System.err);
87 JOptionPane.showMessageDialog(new javax.swing.JFrame(), "The selected Document could not be opened!", "Object Inspector", JOptionPane.ERROR_MESSAGE);
88 return null;
92 public String getTitleByTDocUrl(String _sTDocUrl){
93 try{
94 XRow xRow = this.getXRowOfTDocUrl(_sTDocUrl, "Title");
95 if (xRow != null){
96 return xRow.getString(1);
98 }catch(Exception exception){
99 exception.printStackTrace(System.err);
101 JOptionPane.showMessageDialog(new javax.swing.JFrame(), "The selected Document could not be opened!", "Object Inspector", JOptionPane.ERROR_MESSAGE);
102 return "";
106 private XRow getXRowOfTDocUrl(String _sTDocUrl, String _sPropertyName){
107 try{
108 XUniversalContentBroker xUCB = UniversalContentBroker.create( getXComponentContext() );
109 XContentIdentifier xId = xUCB.createContentIdentifier(_sTDocUrl);
110 XContent xContent = xUCB.queryContent(xId);
111 XCommandProcessor xCmdProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
112 Property aProperty = new Property();
113 aProperty.Name = _sPropertyName; // "DocumentModel"; //DocumentModel
114 Command aCommand = new Command();
115 aCommand.Name = "getPropertyValues";
116 aCommand.Handle = -1; // not available
117 aCommand.Argument = new Property[]{aProperty};
118 Object oAny = xCmdProcessor.execute(aCommand, 0, null);
119 XRow xRow = UnoRuntime.queryInterface(XRow.class, oAny);
120 return xRow;
121 }catch(Exception exception){
122 exception.printStackTrace(System.err);
123 return null;
127 protected String[] getTDocTitles(String[] _sTDocUrls){
128 String[] sTitles = new String[_sTDocUrls.length];
129 for (int i = 0; i < _sTDocUrls.length; i++){
130 sTitles[i] = getTitleByTDocUrl(_sTDocUrls[i]);
132 return sTitles;
136 protected String[] getTDocUrls(){
137 try{
138 Object oSimpleFileAccess = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess", getXComponentContext());
139 XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class, oSimpleFileAccess);
140 String[] sContent = xSimpleFileAccess.getFolderContents("vnd.sun.star.tdoc:/", false);
141 return sContent;
142 } catch( Exception e ) {
143 System.err.println( e );
144 return new String[]{};
148 public XComponent openEmptyDocument(String _sUrl){
149 try{
150 PropertyValue[] aPropertyValues = new PropertyValue[1];
151 aPropertyValues[0] = new PropertyValue();
152 aPropertyValues[0].Name = "Hidden";
153 aPropertyValues[0].Value = Boolean.TRUE;
154 Object oDesktop = getXMultiComponentFactory().createInstanceWithContext("com.sun.star.frame.Desktop", getXComponentContext());
155 XComponentLoader xCL = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
156 return xCL.loadComponentFromURL(_sUrl, "_default", 0, aPropertyValues);
158 catch( Exception exception ) {
159 System.err.println( exception );
160 return null;