home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 8.8 KB | 281 lines |
- /*
- * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or midfy this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in your application, except a utility or class of similar
- * nature to this product. You may distribute this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package symantec.itools.db.awt;
-
- import java.awt.*;
- import java.awt.event.*;
- import symantec.itools.db.awt.event.*;
-
- /**
- * Adapter class to route drop down selection to the ChoiceCell.
- */
- class ChoiceAdapter implements KeyListener, ActionListener, MouseListener
- {
- ChoiceCell cell;
- List list;
-
- ChoiceAdapter(ChoiceCell c, List l) {
- cell = c;
- list = l;
- l.addKeyListener(this);
- l.addActionListener(this);
- l.addMouseListener(this);
- }
-
- void removeListeners() {
- list.removeKeyListener(this);
- list.removeActionListener(this);
- list.removeMouseListener(this);
- }
-
- public void actionPerformed(ActionEvent event) {
- //get the selected item and set value in ChoiceCell
- String item = "";
- if (list.getSelectedIndex() != -1) {
- item = list.getSelectedItem();
- }
-
- cell.selectionMade(item);
- }
-
- public void keyPressed(KeyEvent event) {
- //if escape key pressed we hide the list and make no change in data
- if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
- cell.dropDownCancelled();
- }
-
- //if enter key is pressed then set value in ChoiceCell
- if (event.getKeyCode() == KeyEvent.VK_ENTER) {
- String item = "";
- if (list.getSelectedIndex() != -1) {
- item = list.getSelectedItem();
- }
-
-
- cell.selectionMade(item);
- }
- }
-
- public void keyReleased(KeyEvent event) { }
-
- public void keyTyped(KeyEvent event) { }
-
- public void mouseClicked(MouseEvent event) { }
-
- public void mouseEntered(MouseEvent event) { }
-
- public void mouseExited(MouseEvent event) { }
-
- public void mousePressed(MouseEvent event) { }
-
- public void mouseReleased(MouseEvent event) {
- //item selection made so set value in ChoiceCell
- String item = "";
- if (list.getSelectedIndex() != -1) {
- item = list.getSelectedItem();
- }
-
- cell.selectionMade(item);
- }
- }
-
-
- /**
- * Cell type that provides the user with a drop-down arrow box and when
- * pressed, displays a list for them to make choices. If the escape key
- * is pressed while the list is displayed, the list is removed from view.
- * The cell type may be a combo-box (allows editing) or can be a choice
- * type cell.
- */
- public class ChoiceCell extends ComboCell {
- List aux;
- ChoiceAdapter adapter;
-
- /**
- * Constructs a multiple column drop-down list cell that pulls
- * the value from a specified list. This constuctor always
- * makes the cell a combo-box style cell.
- * @param tv The table view for the cell.
- * @param ds The DataSource that holds the data for the cell.
- * @param auxControl The drop-down list.
- * @param comboBox True if typing should be allowed in the cell.
- */
- public ChoiceCell(Grid tv, DataSource ds, List aux) {
- this(tv, ds, aux, true);
- }
-
- /**
- * Constructs a multiple column drop-down list cell that pulls
- * the value from a specified list.
- * @param tv The table view for the cell.
- * @param ds The DataSource that holds the data for the cell.
- * @param auxControl The drop-down list.
- * @param comboBox True if typing should be allowed in the cell.
- */
- public ChoiceCell(Grid tv, DataSource ds,
- List auxControl, boolean comboBox)
- {
- super(tv, ds, comboBox);
- if (auxControl != null) {
- aux = auxControl;
- }
- }
-
- /**
- * Makes a clone of this cell. Implements the abstract method defined
- * in EditCell.
- */
- public EditCell makeClone() {
- ChoiceCell cs = new ChoiceCell(view, dataSource, aux, combo);
- if (adapter != null) {
- cs.adapter = new ChoiceAdapter(cs, aux);
- }
-
- return cs;
- }
-
- /**
- * Gets the mutli-column list that will act as the drop-down set of
- * choices. Used by the CellHints class.
- */
- public Component auxControl() {
- return aux;
- }
-
- /**
- * Gets the index in the aux list for the current text in the cell.
- */
- int getSelectedIndex() {
- String text = "";
- String items[] = aux.getItems();
- int len = items.length;
-
- try {
- text = readDataString();
- } catch (DataNotAvailable ex) { }
-
- for(int i=0; i<len; i++) {
- if (text.equals(items[i])) {
- return i;
- }
- }
-
- return 0;
-
- }
-
- /**
- * Makes the aux control visible and selects the item that mathces the
- * current contents of the list, or the first item if there is not a match.
- */
- protected void showAuxControl() {
- keyPressedYet = false;
- int index = getSelectedIndex();
- aux.makeVisible(index);
- aux.select(index);
- view.showAuxControl();
- }
-
- /**
- * Is this cell the current cell.
- */
- public boolean isCurrentCell() {
- return view.isAuxOwner(this);
- }
-
- /**
- * If this cell is getting the focus, the aux control is registered,
- * an event adapter is created and the cell is redrawn. Otherwise,
- * the aux cotrol is unregistered and this cell is removed as
- * a registered event listener with the drop-down list.
- */
- public void focusEvent(CellEvent e) {
- if (e.getID() == CellEvent.GOT_FOCUS) {
- view.routeEvent(e);
- if (aux != null) {
- view.registerAuxControl(this);
- adapter = new ChoiceAdapter(this, aux);
- }
- view.redrawCell(this);
- } else {
- keyPressedYet = false;
- selectionMade = false;
- offset = 0;
- cursorPos = 0;
- view.routeEvent(e);
- if (view.isAuxOwner(this)) {
- view.unregisterAuxControl(this);
- }
- try {
- dataSource.commitData();
- } catch(Exception ex) {
- view.handleException(row(), col(), ex);
- }
- if (aux != null) {
- adapter.removeListeners();
- adapter = null;
- }
-
- view.redrawAroundCell(this);
- }
- }
-
- /**
- * Returns whether the arrow box is currently depressed.
- */
- protected boolean down() { return view.isAuxOwner(this) && view.isAuxVisible(); }
-
- /**
- * Method to draw the arrow box symbol.
- */
- protected void drawSymbol(Graphics g, int top, int left, int w, int ht) {
- //draw upside down triangle
- if (ht < 5) { return; }
-
- int polyBottom = top + 8/*triangle ht*/ + 5/*top spacing*/;
-
- if (top + ht < polyBottom) {
- polyBottom = Math.max(top + 5, top + ht - 3);
- }
-
- g.setColor(Color.black);
- Polygon poly = new Polygon();
- poly.addPoint(left + 4, top + 5);
- poly.addPoint(left + w/2-1, polyBottom);
- poly.addPoint(left + w - 5, top + 5);
- poly.addPoint(left + 4, top + 5);
-
- g.fillPolygon(poly);
- }
- }
-
-