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.ui; 24 25 import com.sun.star.awt.*; 26 import com.sun.star.beans.PropertyValue; 27 import com.sun.star.frame.XComponentLoader; 28 import com.sun.star.frame.XFrame; 29 import com.sun.star.io.IOException; 30 import com.sun.star.lang.XComponent; 31 import com.sun.star.lang.XMultiServiceFactory; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.util.CloseVetoException; 34 import com.sun.star.util.XCloseable; 35 import com.sun.star.wizards.common.Properties; 36 import com.sun.star.wizards.common.PropertyNames; 37 38 /** 39 * @author rpiterman 40 * 41 * To change the template for this generated type comment go to 42 * Window>Preferences>Java>Code Generation>Code and Comments 43 */ 44 public class DocumentPreview 45 { 46 47 /** 48 * The window in which the preview is showed. 49 */ 50 private XWindow xWindow; 51 /** 52 * The frame service which is used to show the preview 53 */ 54 private XFrame xFrame; 55 /** 56 * The content component of the frame. 57 */ 58 private XComponent xComponent; 59 private XControl xControl; 60 private int step; 61 private PropertyValue[] loadArgs; 62 private String url; 63 public static final int PREVIEW_MODE = 1; 64 65 /********************************************************************* 66 main 67 68 - create new frame with window inside 69 - load a component as preview into this frame 70 */ DocumentPreview(XMultiServiceFactory xmsf, Object control)71 public DocumentPreview(XMultiServiceFactory xmsf, Object control) throws Exception 72 { 73 74 //((XWindow)UnoRuntime.queryInterface(XWindow.class,control)).addPaintListener(this); 75 xControl = UnoRuntime.queryInterface(XControl.class, control); 76 //register this object as a listener, to close the frame when disposing. 77 //((XComponent) UnoRuntime.queryInterface(XComponent.class, control)).addEventListener(this); 78 79 createPreviewFrame(xmsf, xControl); 80 } 81 setDocument(String url_, String[] propNames, Object[] propValues)82 protected XComponent setDocument(String url_, String[] propNames, Object[] propValues) throws com.sun.star.lang.IllegalArgumentException, IOException, CloseVetoException 83 { 84 url = url_; 85 86 Properties ps = new Properties(); 87 88 for (int i = 0; i < propNames.length; i++) 89 { 90 ps.put(propNames[i], propValues[i]); 91 } 92 return setDocument(url, ps.getProperties()); 93 } 94 setDocument(String url, PropertyValue[] lArgs)95 protected XComponent setDocument(String url, PropertyValue[] lArgs) throws com.sun.star.lang.IllegalArgumentException, IOException, CloseVetoException 96 { 97 loadArgs = lArgs; 98 XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, xFrame); 99 xFrame.activate(); 100 return xComponent = xCompLoader.loadComponentFromURL(url, "_self", 0, loadArgs); 101 } 102 reload(XMultiServiceFactory xmsf)103 public void reload(XMultiServiceFactory xmsf) throws com.sun.star.lang.IllegalArgumentException, IOException, CloseVetoException, com.sun.star.uno.Exception 104 { 105 closeFrame(); 106 createPreviewFrame(xmsf, xControl); 107 //System.out.println(xControl); 108 setDocument(url, loadArgs); 109 } 110 closeFrame()111 private void closeFrame() throws CloseVetoException 112 { 113 if (xFrame != null) 114 { 115 UnoRuntime.queryInterface(XCloseable.class, xFrame).close(false); 116 } 117 } 118 setDocument(String url, int mode)119 public XComponent setDocument(String url, int mode) throws com.sun.star.lang.IllegalArgumentException, IOException, CloseVetoException 120 { 121 switch (mode) 122 { 123 case PREVIEW_MODE: 124 return setDocument(url, new String[] 125 { 126 "Preview", PropertyNames.READ_ONLY 127 }, new Object[] 128 { 129 Boolean.TRUE, Boolean.TRUE 130 }); 131 } 132 return null; 133 } 134 135 /********************************************************************* 136 create a new frame with a new container window inside, 137 which isnt part of the global frame tree. 138 139 Attention: 140 a) This frame wont be destroyed by the office. It must be closed by you! 141 Do so - please call XCloseable::close(). 142 b) The container window is part of the frame. Dont hold it alive - nor try to kill it. 143 It will be destroyed inside close(). 144 */ createPreviewFrame(XMultiServiceFactory xmsf, XControl xControl)145 public void createPreviewFrame(XMultiServiceFactory xmsf, XControl xControl) throws com.sun.star.uno.Exception, com.sun.star.lang.IllegalArgumentException 146 { 147 XWindowPeer controlPeer = xControl.getPeer(); 148 XWindow controlWindow = UnoRuntime.queryInterface(XWindow.class, xControl); 149 Rectangle r = controlWindow.getPosSize(); 150 151 Object toolkit = xmsf.createInstance("com.sun.star.awt.Toolkit"); 152 XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class, toolkit); 153 154 WindowDescriptor aDescriptor = new WindowDescriptor(); 155 aDescriptor.Type = WindowClass.SIMPLE; 156 aDescriptor.WindowServiceName = "window"; 157 aDescriptor.ParentIndex = -1; 158 aDescriptor.Parent = controlPeer; //xWindowPeer; //argument ! 159 aDescriptor.Bounds = new Rectangle(0, 0, r.Width, r.Height); 160 aDescriptor.WindowAttributes = VclWindowPeerAttribute.CLIPCHILDREN | WindowAttribute.SHOW; 161 162 XWindowPeer xPeer = xToolkit.createWindow(aDescriptor); 163 xWindow = UnoRuntime.queryInterface(XWindow.class, xPeer); 164 Object frame = xmsf.createInstance("com.sun.star.frame.Frame"); 165 xFrame = UnoRuntime.queryInterface(XFrame.class, frame); 166 // XFrame xF = (XFrame) UnoRuntime.queryInterface(XFrame.class, Desktop.getDesktop(xmsf)); 167 // xFrame = xF.findFrame("_blank", 0); 168 xFrame.initialize(xWindow); 169 xWindow.setVisible(true); 170 } 171 dispose()172 public void dispose() 173 { 174 try 175 { 176 closeFrame(); 177 } 178 catch (CloseVetoException ex) 179 { 180 ex.printStackTrace(); 181 } 182 } 183 } 184