Back to index

im-sdk  12.3.91
LookupPane.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.event.*;
00048 import java.awt.geom.*;
00049 import java.awt.font.*;
00050 import java.text.*;
00051 import com.sun.iiim.*;
00052 
00053 class LookupPane extends Panel {
00054 
00055     // START information
00056     private int master;
00057     private int choicePerWindow;
00058     private Dimension rowColumn;
00059     private int direction;
00060     private int labelOwner;
00061 
00062     // DRAW information
00063     private int firstCandidate;
00064     private int lastCandidate;
00065     private int currentCandidate;
00066     private String[] candidates;
00067     private String[] labelList;
00068     private int processType;
00069     private int indexPage;
00070     private String title;
00071 
00072     // Label cache
00073     private FocusLabel[] awtLabels;
00074     private int cacheSize = 32;
00075     private static final int CANDIDATE_GAP = 5;
00076     private int oldCandidate;
00077     private String[] oldCandidates;
00078 
00079     public LookupPane() {
00080        awtLabels = new FocusLabel[cacheSize];
00081        for (int i = 0; i < awtLabels.length; i++) {
00082            awtLabels[i] = new FocusLabel();
00083        }
00084 
00085         enableInputMethods(false);
00086     }
00087 
00088     private void checkCache(int size) {
00089        if (size > cacheSize) {
00090            FocusLabel[] tmp = new FocusLabel[size];
00091            System.arraycopy(awtLabels, 0, tmp, 0, cacheSize);
00092            for (int i = cacheSize; i < size; i++) {
00093               tmp[i] = new FocusLabel();
00094            }
00095            awtLabels = tmp;
00096            cacheSize = size;
00097        }
00098     }
00099 
00100     private boolean same(String[] sa1, String[] sa2) {
00101        if (sa1 == null || sa2 == null || sa1.length != sa2.length) {
00102            return false;
00103        }
00104 
00105        for (int i = 0; i < sa1.length; i++) {
00106            if (!sa1[i].equals(sa2[i])) {
00107               return false;
00108            }
00109        }
00110        return true;
00111     }
00112 
00113     private boolean same(CharacterIterator[] it1, CharacterIterator[] it2) {
00114        if (it1 == null || it2 == null || it1.length != it2.length) {
00115            return false;
00116        }
00117 
00118        for (int i = 0; i < it1.length; i++) {
00119            for (char c1 = it1[i].first(), c2 = it2[i].first();
00120                c1 != CharacterIterator.DONE;
00121                c1 = it1[i].next(), c2 = it2[i].next()) {
00122               if (c1 != c2) {
00123                   return false;
00124               }
00125            }
00126        }
00127        return true;
00128     }
00129 
00130     void handleEvent(IIIMLookupEvent e) {
00131        switch(e.getType()) {
00132          case IIIMLookupEvent.START:
00133            start(e);
00134            break;
00135          case IIIMLookupEvent.DRAW:
00136            if (Manager.COLOR_SUPPORT) {
00137               draw2(e);
00138            } else {
00139               draw(e);
00140            }
00141            break;
00142          case IIIMLookupEvent.PROCESS:
00143            process(e);
00144            break;
00145          default:
00146            break;
00147         }
00148     }
00149 
00150     private void start(IIIMLookupEvent e) {
00151        // For now, only SERVER_MASTER is supported
00152        // master = e.getMaster();
00153        master = IIIMLookupEvent.SERVER_MASTER;
00154        choicePerWindow = e.getChoicePerWindow();
00155        rowColumn = e.getRowColum();
00156        direction = e.getDirection();
00157        labelOwner = e.getLabelOwner();
00158 
00159        oldCandidates = null;
00160        oldCandidate = -1;
00161        GridLayout gl = new GridLayout(rowColumn.width, rowColumn.height);
00162        gl.setHgap(CANDIDATE_GAP);
00163        setLayout(gl);
00164     }
00165 
00166     private void draw(IIIMLookupEvent e) {
00167        currentCandidate = e.getCurrentCandidate();
00168        if (currentCandidate < 0) currentCandidate = 0;
00169 
00170        candidates = e.getCandidates();
00171        labelList = e.getLabelList();
00172 
00173        if (candidates.length != labelList.length) {
00174            throw new IllegalArgumentException(" Lookup Error.");
00175        }
00176        
00177        if (master == IIIMLookupEvent.SERVER_MASTER) {
00178            if (!same(candidates, oldCandidates)) {
00179               removeAll();
00180               checkCache(candidates.length);
00181               for (int i = 0; i < candidates.length; i++) {
00182                   FocusLabel fl = awtLabels[i];
00183                   fl.setText(labelList[i] + " : " + candidates[i]);
00184                   add(fl);
00185                   fl.setTarget(this, i);
00186                   if (i == currentCandidate) {
00187                      fl.setFocus(true);
00188                   }
00189               }
00190               repaint();
00191               awtLabels[currentCandidate].requestFocus();
00192            } else if (currentCandidate != oldCandidate) {
00193               awtLabels[currentCandidate].setFocus(true);
00194               awtLabels[currentCandidate].repaint();
00195               if (oldCandidate >= 0) { 
00196                   awtLabels[oldCandidate].setFocus(false);
00197                   awtLabels[oldCandidate].repaint();
00198               }
00199            }
00200               
00201        } else {
00202            // CLIENT_MASTER
00203            firstCandidate = e.getFirstCandidate();
00204            lastCandidate = e.getLastCandidate();
00205            checkCache(lastCandidate - firstCandidate);
00206            for (int i = firstCandidate; i < lastCandidate; i++) {
00207               awtLabels[i - firstCandidate].setText(labelList[i] +
00208                                                 " : " + candidates[i]);
00209               add(awtLabels[i - firstCandidate]);
00210               awtLabels[i - firstCandidate].setTarget(this, i);
00211            }
00212        }
00213 
00214        oldCandidate = currentCandidate;
00215        oldCandidates = candidates;
00216     }
00217 
00218     private AttributedCharacterIterator[] candidateIterator;
00219     private AttributedCharacterIterator[] labelIterator;
00220     private AttributedCharacterIterator[] oldCandidateIterator;
00221 
00222     private void draw2(IIIMLookupEvent e) {
00223        candidateIterator = e.getCandidateIterator();
00224        labelIterator = e.getLabelIterator();
00225        if (candidateIterator.length != labelIterator.length ||
00226            master != IIIMLookupEvent.SERVER_MASTER) {
00227            throw new IllegalArgumentException(" Lookup Error.");
00228        }
00229 
00230        if (!same(candidateIterator, oldCandidateIterator)) {
00231            checkCache(candidateIterator.length);
00232            for (int i = 0; i < candidateIterator.length; i++) {
00233               FocusLabel fl = awtLabels[i];
00234               fl.setIterator(labelIterator[i], candidateIterator[i]);
00235               add(fl);
00236               fl.setTarget(this, i);
00237               if (i == currentCandidate) {
00238                   fl.setFocus(true);
00239               }
00240               repaint();
00241               awtLabels[currentCandidate].requestFocus();
00242            }
00243        } else {
00244            awtLabels[currentCandidate].setFocus(true);
00245            awtLabels[currentCandidate].repaint();
00246            if (oldCandidate >= 0) {
00247               awtLabels[oldCandidate].setFocus(false);
00248               awtLabels[oldCandidate].repaint();
00249            }
00250        }
00251        oldCandidate = currentCandidate;
00252        oldCandidateIterator = candidateIterator;
00253     }
00254 
00255     private void process(IIIMLookupEvent e) {
00256        processType = e.getProcessType();
00257        indexPage = e.getProcessIndexPage();
00258 
00259        // currently this protocol never used....
00260        repaint();
00261     }
00262         
00263     void forwardEvent(AWTEvent e) {
00264        target.dispatchEvent(e);
00265     }
00266 
00267     void changeFocus(int index) {
00268        if (currentCandidate != index) {
00269            awtLabels[currentCandidate].setFocus(false);
00270            awtLabels[currentCandidate].repaint();
00271            currentCandidate = index;
00272        }
00273     }
00274 
00275     void clicked(int index) {
00276        IIIMActionEvent e =
00277            new IIIMActionEvent(IIIMActionEvent.LOOKUP_PROCESSED,
00278                             awtLabels[index].getText());
00279        window.dispatchActionEvent(e);
00280     }
00281 
00282     private java.awt.Component target;
00283     void setTarget(java.awt.Component target) {
00284        this.target = target;
00285     }
00286 
00287     private LookupWindow window;
00288     void setLookupWindow(LookupWindow window) {
00289        this.window = window;
00290     }
00291 }
00292 
00293 class FocusLabel extends Label implements KeyListener, MouseListener {
00294 
00295     private boolean focused = false;
00296     private boolean pressed = false;
00297     private LookupPane target;
00298     private int index;
00299     private AttributedCharacterIterator labelIterator;
00300     private AttributedCharacterIterator candidateIterator;
00301 
00302     FocusLabel(String s) {
00303        super(s);
00304        setAlignment(LEFT);
00305        enableInputMethods(false);
00306        addMouseListener(this);
00307        addKeyListener(this);
00308     }
00309 
00310     FocusLabel() {
00311        this(null);
00312     }
00313 
00314     void setFocus(boolean b) {
00315         focused = b;
00316     }
00317     
00318     void setTarget(LookupPane target, int index) {
00319        this.target = target;
00320        this.index = index;
00321     }
00322     
00323     public void setText(String text) {
00324        super.setText(text);
00325        setFocus(false);
00326     }
00327 
00328     public void setIterator(AttributedCharacterIterator labelIterator,
00329                          AttributedCharacterIterator candidateIterator) {
00330        super.setText(iteratorToString(labelIterator) + " : " +
00331                     iteratorToString(candidateIterator));
00332        this.labelIterator = labelIterator;
00333        this.candidateIterator = candidateIterator;
00334     }
00335 
00336     public static String iteratorToString(CharacterIterator iterator) {
00337         StringBuffer sb = new StringBuffer();
00338         for (char c = iterator.first(); c != CharacterIterator.DONE;
00339              c = iterator.next()) {
00340             sb.append(c);
00341         }
00342         return sb.toString();
00343     }
00344 
00345     public void paint(Graphics g) {
00346        Dimension size = getSize();
00347        Color bg = getBackground();
00348        Color fg = getForeground();
00349 
00350        if (candidateIterator == null) {
00351            super.paint(g);
00352        } else {
00353            Graphics2D g2 = (Graphics2D)g;
00354            g2.setColor(bg);
00355            g2.fillRect(0, 0, size.width, size.height);
00356            g2.setColor(fg);
00357 
00358            FontRenderContext frc = g2.getFontRenderContext();
00359            Font font = g2.getFont();
00360            Rectangle2D b1 =
00361               font.getStringBounds(iteratorToString(labelIterator), frc);
00362            Rectangle2D b2 = font.getStringBounds(" : ", frc);
00363            Rectangle2D b3 =
00364               font.getStringBounds(iteratorToString(candidateIterator), frc);
00365 
00366            int startX = (int)
00367               (size.width - (b1.getWidth() + b2.getWidth() + b3.getWidth()))
00368               / 2;
00369            int startY = (int)
00370               (size.height +
00371                (b1.getHeight() + b2.getHeight() + b3.getHeight())) / 2;
00372 
00373            g.drawString(labelIterator, startX, startY);
00374            g.drawString(" : ", startX + (int)b1.getWidth(), startY);
00375            g.drawString(candidateIterator,
00376                       startX + (int)(b1.getWidth() + b2.getWidth()),
00377                       startY);
00378        }
00379 
00380        if (focused) {
00381            if (pressed) {
00382               g.setColor(bg.darker());
00383            } else {
00384               g.setColor(bg.brighter());
00385            }
00386            g.drawLine(1, 1, size.width - 1, 1);
00387            g.drawLine(1, 1, 1, size.height - 1);
00388            if (pressed) {
00389               g.setColor(bg.brighter());
00390            } else {
00391               g.setColor(bg.darker());
00392            }
00393            g.drawLine(1, size.height - 1, size.width - 1, size.height - 1);
00394            g.drawLine(size.width - 1, 1, size.width - 1, size.height - 1);
00395            requestFocus();
00396        } else {
00397            g.setColor(bg);
00398            g.drawRect(1, 1, size.width - 2, size.height - 2);
00399        }
00400     }
00401 
00402     public void mouseClicked(MouseEvent e) {
00403        if (target != null) {
00404            target.clicked(index);
00405        }
00406     }
00407 
00408     public void mouseEntered(MouseEvent e) {
00409         if (target != null) {
00410            setFocus(true);
00411            repaint();
00412            target.changeFocus(index);
00413        }
00414     }
00415 
00416     public void mouseExited(MouseEvent e) {
00417        if (target != null) {
00418            setFocus(false);
00419            pressed = false;
00420            repaint();
00421            target.forwardEvent(e);
00422        }
00423     }
00424 
00425     public void mousePressed(MouseEvent e) {
00426        pressed = true;
00427        repaint();
00428     }
00429 
00430     public void mouseReleased(MouseEvent e) {
00431        pressed = false;
00432        repaint();
00433     }
00434     
00435     public void keyPressed(KeyEvent e) {
00436        if (target != null) {
00437            target.forwardEvent(e);
00438        }
00439     }
00440     
00441     public void keyReleased(KeyEvent e) {
00442        if (target != null) {
00443            target.forwardEvent(e);
00444        }
00445     }
00446     
00447     public void keyTyped(KeyEvent e) {
00448        if (target != null) {
00449            target.forwardEvent(e);
00450        }
00451     }
00452 }