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.awt.FontSlant;
26 import com.sun.star.awt.TextAlign;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.container.XNameAccess;
29 import com.sun.star.form.runtime.XFormController;
30 import com.sun.star.frame.XController;
31 import com.sun.star.sdb.application.DatabaseObject;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.util.XCloseable;
34 import connectivity.tools.CRMDatabase;
35 
36 // ---------- junit imports -----------------
37 import org.junit.Test;
38 import static org.junit.Assert.*;
39 // ------------------------------------------
40 
41 public class UISettings extends TestCase
42 {
43     // --------------------------------------------------------------------------------------------------------
44     /** verifies that aliases for inner queries work as expected
45      */
46     @Test
checkTableFormattingPersistence()47     public void checkTableFormattingPersistence() throws java.lang.Exception
48     {
49         // create, load, and connect a DB doc
50         CRMDatabase database = new CRMDatabase( getMSF(), true );
51 
52         // display a table
53         XFormController tableViewController = UnoRuntime.queryInterface( XFormController.class,
54             database.loadSubComponent( DatabaseObject.TABLE, "customers" ) );
55         XPropertySet tableControlModel = UnoRuntime.queryInterface( XPropertySet.class,
56             tableViewController.getCurrentControl().getModel() );
57 
58         // change the table's formatting
59         tableControlModel.setPropertyValue( "FontName", "Andale Sans UI" );
60         tableControlModel.setPropertyValue( "FontHeight", Float.valueOf( 20 ) );
61         tableControlModel.setPropertyValue( "FontSlant", FontSlant.ITALIC );
62 
63         String docURL = database.getDatabase().getModel().getURL();
64 
65         // save close the database document
66         database.saveAndClose();
67 
68         // load a copy of the document
69         // normally, it should be sufficient to load the same doc. However, there might be objects in the Java VM
70         // which are not yet freed, and which effectively hold the document alive. More precise: The document (|doc|)
71         // is certainly disposed, but other objects might hold a reference to one of the many other components
72         // around the database document, the data source, the connection, etc. As long as those objects are
73         // not cleaned up, the "database model impl" - the structure holding all document data - will
74         // stay alive, and subsequent requests to load the doc will just reuse it, without really loading it.
75         docURL = copyToTempFile( docURL );
76         loadDocument( docURL );
77         database = new CRMDatabase( getMSF(), docURL );
78 
79         // display the table, again
80         tableViewController = UnoRuntime.queryInterface( XFormController.class,
81             database.loadSubComponent( DatabaseObject.TABLE, "customers" ) );
82         tableControlModel = UnoRuntime.queryInterface( XPropertySet.class,
83             tableViewController.getCurrentControl().getModel() );
84 
85         // verify the properties
86         assertEquals( "wrong font name", "Andale Sans UI", (String)tableControlModel.getPropertyValue( "FontName" ) );
87         assertEquals( "wrong font height", (float)20, ((Float)tableControlModel.getPropertyValue( "FontHeight" )).floatValue(), 0 );
88         assertEquals( "wrong font slant", FontSlant.ITALIC, (FontSlant)tableControlModel.getPropertyValue( "FontSlant" ) );
89 
90         // close the doc
91         database.saveAndClose();
92     }
93 
94     /**
95      * checks whether query columns use the settings of the underlying table column, if they do not (yet) have own
96      * settings
97      * @throws java.lang.Exception
98      */
99     @Test
checkTransparentQueryColumnSettings()100     public void checkTransparentQueryColumnSettings() throws java.lang.Exception
101     {
102         // create, load, and connect a DB doc
103         CRMDatabase database = new CRMDatabase( getMSF(), true );
104 
105         // display a table
106         XController tableView = database.loadSubComponent( DatabaseObject.TABLE, "customers" );
107         XFormController tableViewController = UnoRuntime.queryInterface( XFormController.class,
108              tableView );
109         XNameAccess tableControlModel = UnoRuntime.queryInterface( XNameAccess.class,
110             tableViewController.getCurrentControl().getModel() );
111 
112         // change the formatting of a table column
113         XPropertySet idColumn = UnoRuntime.queryInterface( XPropertySet.class, tableControlModel.getByName( "ID" ) );
114         assertTrue( "precondition not met: column already centered",
115             ((Short)idColumn.getPropertyValue( "Align" )).shortValue() != TextAlign.CENTER );
116         idColumn.setPropertyValue( "Align", TextAlign.CENTER );
117 
118         // close the table data view
119         XCloseable closeSubComponent = UnoRuntime.queryInterface( XCloseable.class, tableView.getFrame() );
120         closeSubComponent.close( true );
121 
122         // create a query based on that column
123         database.getDatabase().getDataSource().createQuery( "q_customers", "SELECT * FROM \"customers\"" );
124 
125         // load this query, and verify the table column settings was propagated to the query column
126         XFormController queryViewController = UnoRuntime.queryInterface( XFormController.class,
127             database.loadSubComponent( DatabaseObject.QUERY, "q_customers" ) );
128         tableControlModel = UnoRuntime.queryInterface( XNameAccess.class,
129             queryViewController.getCurrentControl().getModel() );
130         idColumn = UnoRuntime.queryInterface( XPropertySet.class, tableControlModel.getByName( "ID" ) );
131 
132         assertTrue( "table column alignment was not propagated to the query column",
133             ((Short)idColumn.getPropertyValue( "Align" )).shortValue() == TextAlign.CENTER );
134 
135         // save close the database document
136         database.saveAndClose();
137     }
138 }
139