Back to index

im-sdk  12.3.91
LookupWindow.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.event.*;
00049 import java.awt.font.TextHitInfo;
00050 import com.sun.iiim.*;
00051 
00052 class LookupWindow extends IIIMWindow implements IIIMLookupListener {
00053 
00054     // Window's default position in root window style
00055     private static final int LocateX;
00056     private static final int LocateY;
00057     // Create separated or composite window flexibility
00058     private LookupPane pane;
00059     // Input style
00060     private String style;
00061     // offset value
00062     private int offset = 24;
00063    
00064     int columHeight, columWidth;
00065     FontMetrics fm;
00066 
00067     static {
00068        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
00069         LocateX = (d.width*8/9);
00070         LocateY = (d.height*8/9); 
00071     }
00072     
00073     // No argument constructor, it is necessary for downloading
00074     public LookupWindow() {
00075         style = Manager.getProperty("iiimf.preedit.style");
00076         if(style == null) style = "ROOTWINDOW";      
00077         
00078         init();
00079     }
00080     
00081     void init() {
00082         window = new EventWindow(this);
00083                 
00084        Font font = new Font("Monospaced", Font.PLAIN, 12);
00085        
00086         fm = window.getFontMetrics(font);
00087         int fontHeight = fm.getHeight();
00088         int fontWidth = fm.getMaxAdvance();
00089         int fontAscent = fm.getAscent();
00090         columWidth = fontWidth ;
00091         columHeight = fontHeight + 2;
00092               
00093         window.setFont(font);
00094        
00095         pane = new LookupPane();
00096         pane.setFont(font);
00097         window.add(pane);
00098 
00099        window.pack();
00100     }
00101     
00102     protected void dispatchActionEvent(IIIMActionEvent e) {
00103        super.dispatchActionEvent(e);
00104     }
00105 
00106     // Listener method
00107     public void lookupStart(IIIMLookupEvent e) {
00108        if (pane == null) {
00109            init();
00110        }
00111        pane.setTarget(getClientComponent());
00112        pane.setLookupWindow(this);
00113         pane.handleEvent(e);
00114     }
00115 
00116     public void lookupDone(IIIMLookupEvent e) {
00117         pane.handleEvent(e);
00118        window.setVisible(false);
00119     }
00120 
00121     public void lookupDraw(IIIMLookupEvent e) {
00122         pane.handleEvent(e);
00123        setTitle(e.getTitle());
00124        window.setSize(window.getPreferredSize());
00125        calculatePosition();  
00126        window.setVisible(true);
00127     }
00128 
00129     public void lookupProcess(IIIMLookupEvent e) {
00130         pane.handleEvent(e);
00131     }
00132     
00133     public void setStyle(String style) {
00134         this.style = style;
00135     }
00136     
00137     public String getStyle() {
00138         return style;
00139     }
00140     
00141     void setLocation(int x, int y) {
00142         window.setLocation(x,y);
00143     }
00144     
00145     void calculatePosition() {
00146         Component component = getClientComponent();
00147         
00148         Component frame = component;
00149         Insets inset = window.getInsets();
00150         
00151         while(!(frame instanceof Frame) && !(frame instanceof Dialog)) {
00152            frame = frame.getParent();
00153        }
00154         
00155         if(style.equals("BELOWSPOT") 
00156             && component instanceof InputMethodRequests) {
00157                 Rectangle rect = 
00158                     ((InputMethodRequests)component).getTextLocation(TextHitInfo.leading(0));
00159                 Point point = new Point();
00160                 point.x = rect.x;
00161                 point.y = rect.y + (int)rect.getHeight() + offset;
00162                 // Leave space for Preedit window
00163                 point.y += (inset.top + inset.bottom + fm.getHeight());
00164                 window.setLocation(point);            
00165         /*
00166          * ONSPOT style can used as root window
00167         else if(style.equals("ONSPOT") 
00168             && component instanceof InputMethodRequests) {
00169         */
00170         } else  {
00171             // For "ROOTWINDOW" style and component isn't InputMethodRequests
00172             // Place the window on the left-corner of client component
00173             
00174             Point point = frame.getLocation();
00175             Dimension size = frame.getSize();     
00176 
00177             double x = point.getX();
00178             // It must leave the position for PreeditWindow
00179             double y = point.getY() + size.getHeight() 
00180                 + inset.top + inset.bottom + fm.getHeight();
00181             
00182             window.setLocation((int)x, (int)y);
00183             
00184   
00185         }
00186         
00187             // If window is out of screen, adjust it
00188             Point location = window.getLocation();
00189             double xl = location.getX();
00190             double yl = location.getY();
00191             Dimension dm = window.getSize();
00192             if((xl + dm.getWidth()) > screenWidth) 
00193                 xl = screenWidth - dm.getWidth();
00194             if((yl + dm.getHeight()) > screenHeight) 
00195                 yl = screenHeight - dm.getHeight();
00196             window.setLocation((int)xl, (int)yl);                  
00197     }
00198     
00199     synchronized void show() {
00200         if(window == null) init();
00201         window.setVisible(true);
00202     }
00203     
00204     private void setTitle(String str) {
00205         ((Dialog)window).setTitle(str);
00206     }
00207 }