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