home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / ChoiceCell.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  8.8 KB  |  281 lines

  1. /*
  2.  * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
  3.  *
  4.  * www.krumel.com - controls@krumel.com
  5.  *
  6.  * Permission is given to the buyer of this package for one software
  7.  * developer to use this software on one CPU (one workstation) and to make
  8.  * one backup copy.  You may uitilize and/or midfy this class for use in your
  9.  * projects.  You may distribute or sell any executable which results from
  10.  * using this code in your application, except a utility or class of similar
  11.  * nature to this product.  You may distribute this product in compiled
  12.  * form only, but soley to be used with your cmpiled executable product
  13.  * for the puposes of dynamic loading. You may NOT redistribute the source
  14.  * code in any form or make it accessible through a network or other
  15.  * distribution media to others. Please refer to the file "copyright.html"
  16.  * for further important copyright and licensing information.
  17.  *
  18.  * The source code is the confidential and proprietary information
  19.  * of Krumel & Associates, Inc. ("Confidential Information").  You shall
  20.  * not disclose such Confidential Information and shall use it only in
  21.  * accordance with the terms of the license agreement you entered into
  22.  * with Krumel & Associates, Inc..
  23.  
  24.  * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  25.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
  26.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
  28.  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  29.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  30.  */
  31.  
  32. package symantec.itools.db.awt;
  33.  
  34. import java.awt.*;
  35. import java.awt.event.*;
  36. import symantec.itools.db.awt.event.*;
  37.  
  38. /**
  39.  * Adapter class to route drop down selection to the ChoiceCell.
  40.  */
  41. class ChoiceAdapter implements KeyListener, ActionListener, MouseListener
  42. {
  43.     ChoiceCell cell;
  44.     List list;
  45.  
  46.     ChoiceAdapter(ChoiceCell c, List l) {
  47.         cell = c;
  48.         list = l;
  49.         l.addKeyListener(this);
  50.         l.addActionListener(this);
  51.         l.addMouseListener(this);
  52.     }
  53.  
  54.     void removeListeners() {
  55.         list.removeKeyListener(this);
  56.         list.removeActionListener(this);
  57.         list.removeMouseListener(this);
  58.     }
  59.  
  60.     public void actionPerformed(ActionEvent event) {
  61.         //get the selected item and set value in ChoiceCell
  62.         String item = "";
  63.         if (list.getSelectedIndex() != -1) {
  64.             item = list.getSelectedItem();
  65.         }
  66.  
  67.         cell.selectionMade(item);
  68.     }
  69.  
  70.     public void keyPressed(KeyEvent event) {
  71.         //if escape key pressed we hide the list and make no change in data
  72.         if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
  73.             cell.dropDownCancelled();
  74.         }
  75.  
  76.         //if enter key is pressed then set value in ChoiceCell
  77.         if (event.getKeyCode() == KeyEvent.VK_ENTER) {
  78.             String item = "";
  79.             if (list.getSelectedIndex() != -1) {
  80.                 item = list.getSelectedItem();
  81.             }
  82.  
  83.  
  84.             cell.selectionMade(item);
  85.         }
  86.     }
  87.  
  88.     public void keyReleased(KeyEvent event) { }
  89.  
  90.     public void keyTyped(KeyEvent event) { }
  91.  
  92.     public void mouseClicked(MouseEvent event) { }
  93.  
  94.     public void mouseEntered(MouseEvent event) { }
  95.  
  96.     public void mouseExited(MouseEvent event) { }
  97.  
  98.     public void mousePressed(MouseEvent event) { }
  99.  
  100.     public void mouseReleased(MouseEvent event) {
  101.         //item selection made so set value in ChoiceCell
  102.         String item = "";
  103.         if (list.getSelectedIndex() != -1) {
  104.             item = list.getSelectedItem();
  105.         }
  106.  
  107.         cell.selectionMade(item);
  108.     }
  109. }
  110.  
  111.  
  112. /**
  113.  * Cell type that provides the user with a drop-down arrow box and when
  114.  * pressed, displays a list for them to make choices. If the escape key
  115.  * is pressed while the list is displayed, the list is removed from view.
  116.  * The cell type may be a combo-box (allows editing) or can be a choice
  117.  * type cell.
  118.  */
  119. public class ChoiceCell extends ComboCell {
  120.     List            aux;
  121.     ChoiceAdapter   adapter;
  122.  
  123.     /**
  124.      * Constructs a multiple column drop-down list cell that pulls
  125.      * the value from a specified list. This constuctor always
  126.      * makes the cell a combo-box style cell.
  127.      * @param tv The table view for the cell.
  128.      * @param ds The DataSource that holds the data for the cell.
  129.      * @param auxControl The drop-down list.
  130.      * @param comboBox True if typing should be allowed in the cell.
  131.      */
  132.     public ChoiceCell(Grid tv, DataSource ds, List aux) {
  133.         this(tv, ds, aux, true);
  134.     }
  135.  
  136.     /**
  137.      * Constructs a multiple column drop-down list cell that pulls
  138.      * the value from a specified list.
  139.      * @param tv The table view for the cell.
  140.      * @param ds The DataSource that holds the data for the cell.
  141.      * @param auxControl The drop-down list.
  142.      * @param comboBox True if typing should be allowed in the cell.
  143.      */
  144.     public ChoiceCell(Grid tv, DataSource ds,
  145.                       List auxControl, boolean comboBox)
  146.     {
  147.         super(tv, ds, comboBox);
  148.         if (auxControl != null) {
  149.             aux = auxControl;
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Makes a clone of this cell. Implements the abstract method defined
  155.      * in EditCell.
  156.      */
  157.     public EditCell makeClone() {
  158.         ChoiceCell cs = new ChoiceCell(view, dataSource, aux, combo);
  159.         if (adapter != null) {
  160.             cs.adapter = new ChoiceAdapter(cs, aux);
  161.         }
  162.  
  163.         return cs;
  164.     }
  165.  
  166.     /**
  167.      * Gets the mutli-column list that will act as the drop-down set of
  168.      * choices. Used by the CellHints class.
  169.      */
  170.     public Component auxControl() {
  171.         return aux;
  172.     }
  173.  
  174.     /**
  175.      * Gets the index in the aux list for the current text in the cell.
  176.      */
  177.     int getSelectedIndex() {
  178.         String text = "";
  179.         String items[] = aux.getItems();
  180.         int len = items.length;
  181.  
  182.         try {
  183.             text = readDataString();
  184.         } catch (DataNotAvailable ex) { }
  185.  
  186.         for(int i=0; i<len; i++) {
  187.             if (text.equals(items[i])) {
  188.                 return i;
  189.             }
  190.         }
  191.  
  192.         return 0;
  193.  
  194.     }
  195.  
  196.     /**
  197.      * Makes the aux control visible and selects the item that mathces the
  198.      * current contents of the list, or the first item if there is not a match.
  199.      */
  200.     protected void showAuxControl() {
  201.         keyPressedYet = false;
  202.         int index = getSelectedIndex();
  203.         aux.makeVisible(index);
  204.         aux.select(index);
  205.         view.showAuxControl();
  206.     }
  207.  
  208.     /**
  209.      * Is this cell the current cell.
  210.      */
  211.     public boolean isCurrentCell() {
  212.         return view.isAuxOwner(this);
  213.     }
  214.  
  215.     /**
  216.      * If this cell is getting the focus, the aux control is registered,
  217.      * an event adapter is created and the cell is redrawn. Otherwise,
  218.      * the aux cotrol is unregistered and this cell is removed as
  219.      * a registered event listener with the drop-down list.
  220.      */
  221.     public void focusEvent(CellEvent e) {
  222.         if (e.getID() == CellEvent.GOT_FOCUS) {
  223.             view.routeEvent(e);
  224.             if (aux != null) {
  225.                 view.registerAuxControl(this);
  226.                 adapter = new ChoiceAdapter(this, aux);
  227.             }
  228.             view.redrawCell(this);
  229.         } else {
  230.             keyPressedYet = false;
  231.             selectionMade = false;
  232.             offset = 0;
  233.             cursorPos = 0;
  234.             view.routeEvent(e);
  235.             if (view.isAuxOwner(this)) {
  236.                 view.unregisterAuxControl(this);
  237.             }
  238.             try {
  239.                 dataSource.commitData();
  240.             } catch(Exception ex) {
  241.                 view.handleException(row(), col(), ex);
  242.             }
  243.             if (aux != null) {
  244.                 adapter.removeListeners();
  245.                 adapter = null;
  246.             }
  247.  
  248.             view.redrawAroundCell(this);
  249.         }
  250.     }
  251.  
  252.     /**
  253.      * Returns whether the arrow box is currently depressed.
  254.      */
  255.     protected boolean down() { return view.isAuxOwner(this) && view.isAuxVisible(); }
  256.  
  257.     /**
  258.      * Method to draw the arrow box symbol.
  259.      */
  260.     protected void drawSymbol(Graphics g, int top, int left, int w, int ht) {
  261.         //draw upside down triangle
  262.         if (ht < 5) { return; }
  263.  
  264.         int polyBottom = top + 8/*triangle ht*/ + 5/*top spacing*/;
  265.  
  266.         if (top + ht < polyBottom) {
  267.             polyBottom = Math.max(top + 5, top + ht - 3);
  268.         }
  269.  
  270.         g.setColor(Color.black);
  271.         Polygon poly = new Polygon();
  272.         poly.addPoint(left + 4, top + 5);
  273.         poly.addPoint(left + w/2-1, polyBottom);
  274.         poly.addPoint(left + w - 5, top + 5);
  275.         poly.addPoint(left + 4, top + 5);
  276.  
  277.         g.fillPolygon(poly);
  278.     }
  279. }
  280.  
  281.