1 package org.openoffice.examples.embedding; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.awt.image.*; 6 import javax.swing.JTextArea; 7 import javax.swing.JFrame; 8 import java.io.*; 9 import javax.imageio.ImageIO; 10 11 import org.openoffice.examples.embedding.OwnEmbeddedObject; 12 13 public class EditorFrame extends JFrame 14 { 15 OwnEmbeddedObject m_aEmbObj; 16 JTextArea m_aTextArea; 17 BufferedImage m_aBufImage; 18 19 WindowListener m_aCloser = new WindowAdapter() 20 { 21 public void windowClosing( WindowEvent e ) 22 { 23 // m_aBufImage = m_aTextArea.getGraphicsConfiguration().createCompatibleImage( m_aTextArea.getWidth(), m_aTextArea.getHeight() ); 24 m_aBufImage = new BufferedImage( m_aTextArea.getWidth(), m_aTextArea.getHeight(), BufferedImage.TYPE_INT_RGB ); 25 Graphics2D aGr = m_aBufImage.createGraphics(); 26 m_aTextArea.paintAll( aGr ); 27 aGr.dispose(); 28 29 hide(); 30 m_aEmbObj.CloseFrameRequest(); 31 } 32 }; 33 34 public EditorFrame( String sName, OwnEmbeddedObject aEmbObj, int nWidth, int nHeight ) 35 { 36 super( sName ); 37 m_aEmbObj = aEmbObj; 38 addWindowListener( m_aCloser ); 39 m_aTextArea = new JTextArea( "", nWidth, nHeight ); 40 41 add( "Center", m_aTextArea ); 42 pack(); 43 // setResizable( false ); 44 } 45 46 public String getText() 47 { 48 return m_aTextArea.getText(); 49 } 50 51 public void setText( String aText ) 52 { 53 m_aTextArea.setText( aText ); 54 } 55 56 public Dimension getAppSize() 57 { 58 return m_aTextArea.getSize(); 59 } 60 61 public void setAppSize( Dimension aSize ) 62 { 63 Dimension aOwnSize = getSize(); 64 Dimension aAppSize = m_aTextArea.getSize(); 65 Dimension aToSet = 66 new Dimension( (int)( aSize.getWidth() + aOwnSize.getWidth() - aAppSize.getWidth() ), 67 (int)(aSize.getHeight() + aOwnSize.getHeight() - aAppSize.getHeight() ) ); 68 69 setSize( aToSet ); 70 validate(); 71 72 // pack(); 73 } 74 75 public byte[] getReplacementImage() 76 { 77 Dimension aDim = m_aTextArea.getSize(); 78 BufferedImage aBufImage = null; 79 80 if ( m_aBufImage != null ) 81 aBufImage = m_aBufImage; 82 else 83 { 84 try 85 { 86 int nWidth = (int)aDim.getWidth(); 87 int nHeight = (int)aDim.getHeight(); 88 aBufImage = new BufferedImage( nWidth, nHeight, BufferedImage.TYPE_INT_RGB ); 89 Graphics2D aGr = aBufImage.createGraphics(); 90 aGr.setBackground( Color.WHITE ); 91 aGr.clearRect( 0, 0, nWidth, nHeight ); 92 aGr.dispose(); 93 } 94 catch ( java.lang.Exception e ) 95 {} 96 } 97 98 if ( aBufImage != null ) 99 { 100 try 101 { 102 File aTmpFile = File.createTempFile( "temp", ".png" ); 103 ImageIO.write( aBufImage, "png", aTmpFile ); 104 105 int nLen = (int)aTmpFile.length(); 106 byte[] aResult = new byte[nLen]; 107 FileInputStream aTmpStream = new FileInputStream( aTmpFile ); 108 aTmpStream.read( aResult ); 109 aTmpStream.close(); 110 aTmpFile.delete(); 111 112 return aResult; 113 } 114 catch ( java.lang.Exception e ) 115 {} 116 } 117 118 return new byte[0]; 119 } 120 } 121 122