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 com.sun.star.wizards.text;
24 
25 import com.sun.star.awt.Size;
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.style.XStyleFamiliesSupplier;
30 import com.sun.star.style.XStyleLoader;
31 import com.sun.star.text.XTextDocument;
32 import com.sun.star.uno.AnyConverter;
33 import com.sun.star.uno.Exception;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.wizards.common.PropertyNames;
36 
37 public class TextStyleHandler
38 {
39 
40     public XStyleFamiliesSupplier xStyleFamiliesSupplier;
41     private XMultiServiceFactory xMSFDoc;
42     private XTextDocument xTextDocument;
43 
44     /** Creates a new instance of TextStyleHandler */
TextStyleHandler(com.sun.star.lang.XMultiServiceFactory xMSF, XTextDocument xTextDocument)45     public TextStyleHandler(com.sun.star.lang.XMultiServiceFactory xMSF, XTextDocument xTextDocument)
46     {
47         this.xMSFDoc = xMSF;
48         this.xTextDocument = xTextDocument;
49         xStyleFamiliesSupplier = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDocument);
50     }
51 
loadStyleTemplates(String sTemplateUrl, String OptionString)52     public void loadStyleTemplates(String sTemplateUrl, String OptionString)
53     {
54         try
55         {
56             XStyleLoader xStyleLoader = UnoRuntime.queryInterface(XStyleLoader.class, xStyleFamiliesSupplier.getStyleFamilies());
57             com.sun.star.beans.PropertyValue[] StyleOptions = xStyleLoader.getStyleLoaderOptions();
58             String CurOptionName = PropertyNames.EMPTY_STRING;
59             int PropCount = StyleOptions.length;
60             for (int i = 0; i < PropCount; i++)
61             {
62                 CurOptionName = StyleOptions[i].Name;
63                 StyleOptions[i].Value = Boolean.valueOf((CurOptionName.compareTo(OptionString) == 0) || (CurOptionName.compareTo("OverwriteStyles") == 0));
64             }
65             xStyleLoader.loadStylesFromURL(sTemplateUrl, StyleOptions);
66         }
67         catch (Exception exception)
68         {
69             exception.printStackTrace(System.out);
70         }
71     }
72 
getStyleByName(String sStyleFamily, String sStyleName)73     public XPropertySet getStyleByName(String sStyleFamily, String sStyleName)
74     {
75         try
76         {
77             XPropertySet xPropertySet = null;
78             Object oStyleFamily = xStyleFamiliesSupplier.getStyleFamilies().getByName(sStyleFamily);
79             XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, oStyleFamily);
80             if (xNameAccess.hasByName(sStyleName))
81             {
82                 xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xNameAccess.getByName(sStyleName));
83             }
84             return xPropertySet;
85         }
86         catch (Exception e)
87         {
88             e.printStackTrace(System.out);
89         }
90         return null;
91     }
92 
changePageAlignment(XPropertySet _xPropPageStyle, boolean _bIsLandscape)93     public Size changePageAlignment(XPropertySet _xPropPageStyle, boolean _bIsLandscape)
94     {
95         try
96         {
97             _xPropPageStyle.setPropertyValue("IsLandscape", Boolean.valueOf(_bIsLandscape));
98             Size aPageSize = (Size) AnyConverter.toObject(Size.class, _xPropPageStyle.getPropertyValue("Size"));
99             int nPageWidth = aPageSize.Width;
100             int nPageHeight = aPageSize.Height;
101             Size aSize = new Size(nPageHeight, nPageWidth);
102             _xPropPageStyle.setPropertyValue("Size", aSize);
103             return (Size) AnyConverter.toObject(Size.class, _xPropPageStyle.getPropertyValue("Size"));
104         }
105         catch (Exception e)
106         {
107             e.printStackTrace(System.out);
108             return null;
109         }
110     }
111 }
112