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        candidates = e.getCandidates();
00169        labelList = e.getLabelList();
00170 
00171        if (candidates.length != labelList.length) {
00172            throw new IllegalArgumentException(" Lookup Error.");
00173        }
00174        
00175        if (master == IIIMLookupEvent.SERVER_MASTER) {
00176            if (!same(candidates, oldCandidates)) {
00177               removeAll();
00178               checkCache(candidates.length);
00179               for (int i = 0; i < candidates.length; i++) {
00180                   FocusLabel fl = awtLabels[i];
00181                   fl.setText(labelList[i] + " : " + candidates[i]);
00182                   add(fl);
00183                   fl.setTarget(this, i);
00184                   if (i == currentCandidate) {
00185                      fl.setFocus(true);
00186                   }
00187               }
00188               repaint();
00189               awtLabels[currentCandidate].requestFocus();
00190            } else if (currentCandidate != oldCandidate) {
00191               awtLabels[currentCandidate].setFocus(true);
00192               awtLabels[currentCandidate].repaint();
00193               if (oldCandidate >= 0) { 
00194                   awtLabels[oldCandidate].setFocus(false);
00195                   awtLabels[oldCandidate].repaint();
00196               }
00197            }
00198               
00199        } else {
00200            // CLIENT_MASTER
00201            firstCandidate = e.getFirstCandidate();
00202            lastCandidate = e.getLastCandidate();
00203            checkCache(lastCandidate - firstCandidate);
00204            for (int i = firstCandidate; i < lastCandidate; i++) {
00205               awtLabels[i - firstCandidate].setText(labelList[i] +
00206                                                 " : " + candidates[i]);
00207               add(awtLabels[i - firstCandidate]);
00208               awtLabels[i - firstCandidate].setTarget(this, i);
00209            }
00210        }
00211 
00212        oldCandidate = currentCandidate;
00213        oldCandidates = candidates;
00214     }
00215 
00216     private AttributedCharacterIterator[] candidateIterator;
00217     private AttributedCharacterIterator[] labelIterator;
00218     private AttributedCharacterIterator[] oldCandidateIterator;
00219 
00220     private void draw2(IIIMLookupEvent e) {
00221        candidateIterator = e.getCandidateIterator();
00222        labelIterator = e.getLabelIterator();
00223        if (candidateIterator.length != labelIterator.length ||
00224            master != IIIMLookupEvent.SERVER_MASTER) {
00225            throw new IllegalArgumentException(" Lookup Error.");
00226        }
00227 
00228        if (!same(candidateIterator, oldCandidateIterator)) {
00229            checkCache(candidateIterator.length);
00230            for (int i = 0; i < candidateIterator.length; i++) {
00231               FocusLabel fl = awtLabels[i];
00232               fl.setIterator(labelIterator[i], candidateIterator[i]);
00233               add(fl);
00234               fl.setTarget(this, i);
00235               if (i == currentCandidate) {
00236                   fl.setFocus(true);
00237               }
00238               repaint();
00239               awtLabels[currentCandidate].requestFocus();
00240            }
00241        } else {
00242            awtLabels[currentCandidate].setFocus(true);
00243            awtLabels[currentCandidate].repaint();
00244            if (oldCandidate >= 0) {
00245               awtLabels[oldCandidate].setFocus(false);
00246               awtLabels[oldCandidate].repaint();
00247            }
00248        }
00249        oldCandidate = currentCandidate;
00250        oldCandidateIterator = candidateIterator;
00251     }
00252 
00253     private void process(IIIMLookupEvent e) {
00254        processType = e.getProcessType();
00255        indexPage = e.getProcessIndexPage();
00256 
00257        // currently this protocol never used....
00258        repaint();
00259     }
00260         
00261     void forwardEvent(AWTEvent e) {
00262        target.dispatchEvent(e);
00263     }
00264 
00265     void changeFocus(int index) {
00266        if (currentCandidate != index) {
00267            awtLabels[currentCandidate].setFocus(false);
00268            awtLabels[currentCandidate].repaint();
00269            currentCandidate = index;
00270        }
00271     }
00272 
00273     void clicked(int index) {
00274        IIIMActionEvent e =
00275            new IIIMActionEvent(IIIMActionEvent.LOOKUP_PROCESSED,
00276                             awtLabels[index].getText());
00277        window.dispatchActionEvent(e);
00278     }
00279 
00280     private java.awt.Component target;
00281     void setTarget(java.awt.Component target) {
00282        this.target = target;
00283     }
00284 
00285     private LookupWindow window;
00286     void setLookupWindow(LookupWindow window) {
00287        this.window = window;
00288     }
00289 }
00290 
00291 class FocusLabel extends Label implements KeyListener, MouseListener {
00292 
00293     private boolean focused = false;
00294     private boolean pressed = false;
00295     private LookupPane target;
00296     private int index;
00297     private AttributedCharacterIterator labelIterator;
00298     private AttributedCharacterIterator candidateIterator;
00299 
00300     FocusLabel(String s) {
00301        super(s);
00302        setAlignment(LEFT);
00303        enableInputMethods(false);
00304        addMouseListener(this);
00305        addKeyListener(this);
00306     }
00307 
00308     FocusLabel() {
00309        this(null);
00310     }
00311 
00312     void setFocus(boolean b) {
00313         focused = b;
00314     }
00315     
00316     void setTarget(LookupPane target, int index) {
00317        this.target = target;
00318        this.index = index;
00319     }
00320     
00321     public void setText(String text) {
00322        super.setText(text);
00323        setFocus(false);
00324     }
00325 
00326     public void setIterator(AttributedCharacterIterator labelIterator,
00327                          AttributedCharacterIterator candidateIterator) {
00328        super.setText(iteratorToString(labelIterator) + " : " +
00329                     iteratorToString(candidateIterator));
00330        this.labelIterator = labelIterator;
00331        this.candidateIterator = candidateIterator;
00332     }
00333 
00334     public static String iteratorToString(CharacterIterator iterator) {
00335         StringBuffer sb = new StringBuffer();
00336         for (char c = iterator.first(); c != CharacterIterator.DONE;
00337              c = iterator.next()) {
00338             sb.append(c);
00339         }
00340         return sb.toString();
00341     }
00342 
00343     public void paint(Graphics g) {
00344        Dimension size = getSize();
00345        Color bg = getBackground();
00346        Color fg = getForeground();
00347 
00348        if (candidateIterator == null) {
00349            super.paint(g);
00350        } else {
00351            Graphics2D g2 = (Graphics2D)g;
00352            g2.setColor(bg);
00353            g2.fillRect(0, 0, size.width, size.height);
00354            g2.setColor(fg);
00355 
00356            FontRenderContext frc = g2.getFontRenderContext();
00357            Font font = g2.getFont();
00358            Rectangle2D b1 =
00359               font.getStringBounds(iteratorToString(labelIterator), frc);
00360            Rectangle2D b2 = font.getStringBounds(" : ", frc);
00361            Rectangle2D b3 =
00362               font.getStringBounds(iteratorToString(candidateIterator), frc);
00363 
00364            int startX = (int)
00365               (size.width - (b1.getWidth() + b2.getWidth() + b3.getWidth()))
00366               / 2;
00367            int startY = (int)
00368               (size.height +
00369                (b1.getHeight() + b2.getHeight() + b3.getHeight())) / 2;
00370 
00371            g.drawString(labelIterator, startX, startY);
00372            g.drawString(" : ", startX + (int)b1.getWidth(), startY);
00373            g.drawString(candidateIterator,
00374                       startX + (int)(b1.getWidth() + b2.getWidth()),
00375                       startY);
00376        }
00377 
00378        if (focused) {
00379            if (pressed) {
00380               g.setColor(bg.darker());
00381            } else {
00382               g.setColor(bg.brighter());
00383            }
00384            g.drawLine(1, 1, size.width - 1, 1);
00385            g.drawLine(1, 1, 1, size.height - 1);
00386            if (pressed) {
00387               g.setColor(bg.brighter());
00388            } else {
00389               g.setColor(bg.darker());
00390            }
00391            g.drawLine(1, size.height - 1, size.width - 1, size.height - 1);
00392            g.drawLine(size.width - 1, 1, size.width - 1, size.height - 1);
00393            requestFocus();
00394        } else {
00395            g.setColor(bg);
00396            g.drawRect(1, 1, size.width - 2, size.height - 2);
00397        }
00398     }
00399 
00400     public void mouseClicked(MouseEvent e) {
00401        if (target != null) {
00402            target.clicked(index);
00403        }
00404     }
00405 
00406     public void mouseEntered(MouseEvent e) {
00407         if (target != null) {
00408            setFocus(true);
00409            repaint();
00410            target.changeFocus(index);
00411        }
00412     }
00413 
00414     public void mouseExited(MouseEvent e) {
00415        if (target != null) {
00416            setFocus(false);
00417            pressed = false;
00418            repaint();
00419            target.forwardEvent(e);
00420        }
00421     }
00422 
00423     public void mousePressed(MouseEvent e) {
00424        pressed = true;
00425        repaint();
00426     }
00427 
00428     public void mouseReleased(MouseEvent e) {
00429        pressed = false;
00430        repaint();
00431     }
00432     
00433     public void keyPressed(KeyEvent e) {
00434        if (target != null) {
00435            target.forwardEvent(e);
00436        }
00437     }
00438     
00439     public void keyReleased(KeyEvent e) {
00440        if (target != null) {
00441            target.forwardEvent(e);
00442        }
00443     }
00444     
00445     public void keyTyped(KeyEvent e) {
00446        if (target != null) {
00447            target.forwardEvent(e);
00448        }
00449     }
00450 }