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.beans.PropertyVetoException;
26 import com.sun.star.beans.UnknownPropertyException;
27 import com.sun.star.container.XIndexAccess;
28 import com.sun.star.container.XNameAccess;
29 import com.sun.star.lang.IllegalArgumentException;
30 import com.sun.star.lang.WrappedTargetException;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.style.XStyleFamiliesSupplier;
33 import com.sun.star.text.XPageCursor;
34 import com.sun.star.text.XTextContent;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.text.XTextRange;
38 import com.sun.star.text.XTextViewCursor;
39 import com.sun.star.text.XTextViewCursorSupplier;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.view.XViewSettingsSupplier;
42 import com.sun.star.wizards.common.Helper;
43 
44 public class ViewHandler
45 {
46 
47     private XTextViewCursorSupplier xTextViewCursorSupplier;
48     private XMultiServiceFactory xMSFDoc;
49     private XTextDocument xTextDocument;
50     private XStyleFamiliesSupplier xStyleFamiliesSupplier;
51     private XViewSettingsSupplier xViewSettingsSupplier;
52 
53     /** Creates a new instance of View */
ViewHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)54     public ViewHandler(XMultiServiceFactory xMSF, XTextDocument xTextDocument)
55     {
56         this.xMSFDoc = xMSF;
57         this.xTextDocument = xTextDocument;
58         xTextViewCursorSupplier = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xTextDocument.getCurrentController());
59         xViewSettingsSupplier = UnoRuntime.queryInterface(XViewSettingsSupplier.class, xTextDocument.getCurrentController());
60         xStyleFamiliesSupplier = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDocument);
61     }
62 
selectFirstPage(TextTableHandler oTextTableHandler)63     public void selectFirstPage(TextTableHandler oTextTableHandler)
64     {
65         try
66         {
67             XPageCursor xPageCursor = UnoRuntime.queryInterface(XPageCursor.class, xTextViewCursorSupplier.getViewCursor());
68             XTextCursor xViewTextCursor = UnoRuntime.queryInterface(XTextCursor.class, xPageCursor);
69             xPageCursor.jumpToFirstPage();
70             xPageCursor.jumpToStartOfPage();
71             Helper.setUnoPropertyValue(xPageCursor, "PageDescName", "First Page");
72             Object oPageStyles = xStyleFamiliesSupplier.getStyleFamilies().getByName("PageStyles");
73             XNameAccess xName = UnoRuntime.queryInterface(XNameAccess.class, oPageStyles);
74             Object oPageStyle = xName.getByName("First Page");
75             XIndexAccess xAllTextTables = UnoRuntime.queryInterface(XIndexAccess.class, oTextTableHandler.xTextTablesSupplier.getTextTables());
76             XTextContent xTextTable = UnoRuntime.queryInterface(XTextContent.class, xAllTextTables.getByIndex(0));
77             XTextRange xRange = UnoRuntime.queryInterface(XTextRange.class, xTextTable.getAnchor().getText());
78             xViewTextCursor.gotoRange(xRange, false);
79 //            if (xPageCursor.getPage() == (short) 1) {
80 //                Helper.setUnoPropertyValue(xTextTable, "PageDescName", "First Page");
81 //                TextTableHandler.resetBreakTypeofTextTable(xTextTable);
82 //            }
83             //      }
84             XTextRange xHeaderRange = (XTextRange) Helper.getUnoPropertyValue(oPageStyle, "HeaderText", XTextRange.class);
85             if (!com.sun.star.uno.AnyConverter.isVoid(xHeaderRange))
86             {
87                 xViewTextCursor.gotoRange(xHeaderRange, false);
88                 xViewTextCursor.collapseToStart();
89             }
90             else
91             {
92                 System.out.println("No Headertext available");
93             }
94         }
95         catch (com.sun.star.uno.Exception exception)
96         {
97             exception.printStackTrace(System.out);
98         }
99     }
100 
setViewSetting(String Setting, Object Value)101     public void setViewSetting(String Setting, Object Value) throws UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException
102     {
103         xViewSettingsSupplier.getViewSettings().setPropertyValue(Setting, Value);
104     }
105 
collapseViewCursorToStart()106     public void collapseViewCursorToStart()
107     {
108         XTextViewCursor xTextViewCursor = xTextViewCursorSupplier.getViewCursor();
109         xTextViewCursor.collapseToStart();
110     }
111 }
112