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 package complex.dbaccess; 24 25 import com.sun.star.beans.PropertyValue; 26 import com.sun.star.frame.FrameSearchFlag; 27 import com.sun.star.frame.XComponentLoader; 28 import com.sun.star.frame.XModel; 29 import com.sun.star.frame.XStorable; 30 import com.sun.star.lang.XComponent; 31 import com.sun.star.sdb.XOfficeDatabaseDocument; 32 import com.sun.star.sdb.application.XDatabaseDocumentUI; 33 import com.sun.star.uno.Exception; 34 import com.sun.star.uno.UnoRuntime; 35 import connectivity.tools.DatabaseAccess; 36 37 /** 38 * 39 * @author oj93728 40 */ 41 public class DatabaseApplication 42 { 43 44 private final XOfficeDatabaseDocument databaseDocument; 45 private final XDatabaseDocumentUI documentUI; 46 private final DatabaseAccess db; 47 DatabaseApplication(final DatabaseAccess _db)48 public DatabaseApplication(final DatabaseAccess _db) throws Exception 49 { 50 db = _db; 51 databaseDocument = db.getDatabaseDocument(); 52 53 // load it into a frame 54 final Object object = db.getORB().createInstance("com.sun.star.frame.Desktop"); 55 final XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, object); 56 final XComponent loadedComponent = xComponentLoader.loadComponentFromURL(db.getDocumentURL(), "_blank", FrameSearchFlag.ALL, new PropertyValue[0]); 57 58 // get the controller, which provides access to various UI operations 59 final XModel docModel = UnoRuntime.queryInterface(XModel.class, loadedComponent); 60 documentUI = UnoRuntime.queryInterface(XDatabaseDocumentUI.class, docModel.getCurrentController()); 61 documentUI.connect(); 62 } 63 getDatabaseDocument()64 public XOfficeDatabaseDocument getDatabaseDocument() 65 { 66 return databaseDocument; 67 } 68 getDocumentUI()69 public XDatabaseDocumentUI getDocumentUI() 70 { 71 return documentUI; 72 } 73 getDb()74 public DatabaseAccess getDb() 75 { 76 return db; 77 } 78 store()79 public void store() 80 { 81 // store the doc in a new location 82 try 83 { 84 final XStorable storeDoc = UnoRuntime.queryInterface(XStorable.class, databaseDocument); 85 if (storeDoc != null) 86 { 87 storeDoc.store(); 88 } 89 } 90 catch (com.sun.star.io.IOException iOException) 91 { 92 } 93 } 94 } 95