Back to index

im-sdk  12.3.91
PreeditWindow.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package sun.awt.im.iiimp;
00045 
00046 import java.awt.*;
00047 import java.awt.im.*;
00048 import java.awt.font.*;
00049 //For testing
00050 import java.text.*;
00051 import com.sun.iiim.*;
00052 
00053 class PreeditWindow extends IIIMWindow implements IIIMPreeditListener {
00054     String style;
00055     
00056     PreeditPane pane;
00057     
00058     int columWidth, columHeight;
00059     
00060     // Below-spot offset value
00061     int offset = 20;
00062     
00063     // Default, no arg constructor
00064     public PreeditWindow() {
00065         style = Manager.getProperty("iiimf.preedit.style");
00066         if(style == null) style = "ROOTWINDOW";
00067     }
00068     
00069     void init() {
00070         window = new Dialog(new Frame(""));
00071         ((Dialog)window).setResizable(true);
00072         
00073         ((Dialog)window).setTitle("Preedit Window");
00074         
00075        Font font = new Font("Monospaced", Font.PLAIN, 12);
00076        
00077         FontMetrics fm = window.getFontMetrics(font);
00078         int fontHeight = fm.getHeight();
00079         int fontWidth = fm.getMaxAdvance();
00080         int fontAscent = fm.getAscent();
00081         columWidth = fontWidth ;
00082         columHeight = fontHeight + 2;
00083               
00084         window.setFont(font);
00085 
00086         pane = new PreeditPane();
00087         pane.setFont(font);
00088         window.add(pane);
00089 
00090        window.pack();                
00091     }
00092     
00093     // IIIMPreeditListener method
00094     public void preeditStart(IIIMPreeditEvent e) {
00095     }
00096     public void preeditDone(IIIMPreeditEvent e) {
00097     }
00098     public void preeditDraw(IIIMPreeditEvent e) {
00099     }
00100 
00101     public void dispatchEvent(IIIMPreeditEvent e) {
00102         if(pane == null) init();
00103         
00104         pane.handleEvent(e);
00105         
00106         Insets inset = window.getInsets();
00107         
00108         int width = inset.left + inset.right + pane.width();
00109         int height = inset.top + inset.bottom + pane.height();
00110         
00111         window.setSize(width, height);
00112         calculatePosition();
00113 
00114         window.show();
00115     }
00116     
00117     public void setStyle(String style) {
00118         this.style = style;
00119     }
00120     
00121     public String getStyle() {
00122         return style;
00123     }
00124     
00125     void calculatePosition() {
00126         Component component = getClientComponent();
00127         
00128         Component frame = component;
00129         while(!(frame instanceof Frame)) frame = component.getParent();        
00130 
00131         if(style.equals("BELOWSPOT") && 
00132             component instanceof InputMethodRequests) {
00133                 Rectangle rect = 
00134                     ((InputMethodRequests)component).getTextLocation(TextHitInfo.leading(0));
00135                 Point point = new Point();
00136                 point.x = rect.x;
00137                 point.y = rect.y + (int)rect.getHeight() + offset;
00138                 
00139                 window.setLocation(point);
00140         } else {
00141             // for ROOTWINDOW style and client not InputMethodRequests
00142             // Place the window on the left-corner of client component
00143             Point point = frame.getLocation();
00144             Dimension size = frame.getSize();     
00145         
00146             double x = point.getX();
00147             double y = point.getY() + size.getHeight();
00148             window.setLocation((int)x, (int)y);
00149         }
00150         
00151         // If window is out of screen, adjust it
00152         Point location = window.getLocation();
00153         double xl = location.getX();
00154         double yl = location.getY();
00155         Dimension dm = window.getSize();
00156         if((xl + dm.getWidth()) > screenWidth) 
00157             xl = screenWidth - dm.getWidth();
00158         if((yl + dm.getHeight()) > screenHeight) 
00159             yl = screenHeight - dm.getHeight();
00160         window.setLocation((int)xl, (int)yl);
00161     }
00162 }