home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 9.5 KB | 297 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.*;
-
- /**
- * Base class for cells that provide a drop-down widget or a popup interface.
- */
- public abstract class ComboCell extends EditCell {
- /**
- * Combo-box style flag. If true then the user is allowed to type in the cell.
- */
- protected boolean combo = true;
-
- /**
- * The width of the arrow box for this cell.
- */
- protected int ARROW_WIDTH = 18;
-
- /**
- * Creates a ComboCell that does not allow editing.
- */
- public ComboCell(Grid tv, DataSource ds) {
- this(tv, ds, true);
- }
-
- /**
- * Creates a combo cell.
- * @param comboBox True if cell should allow editing, false to not allow
- * editing.
- */
- public ComboCell(Grid tv, DataSource ds, boolean comboBox) {
- super(tv, ds);
- combo = comboBox;
-
- PAD_RIGHT = ARROW_WIDTH;
- }
-
- /**
- * Called when the user performs some action to indicate that the drop-down
- * (or popup) should be cancelled. Calls Grid.hideAuxControl(). If the
- * cell type does not use an aux control, then the Grid will ignore the call.
- */
- protected void dropDownCancelled() {
- view.hideAuxControl();
- view.redrawCell(this);
- }
-
- /**
- * Called when the button comes up signaling that the aux control should be
- * hidden. Calls Grid.hideAuxControl(). If the cell type does not use
- * an aux control, then the Grid will ignore the call.
- */
- protected void handleArrowRelease() {
- view.hideAuxControl();
- view.redrawCell(this);
- }
-
- /**
- * 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 abstract void showAuxControl();
-
- //by default, displays aux control
- protected void handleArrowPress() {
- if (view.isAuxVisible()) {
- view.hideAuxControl();
- view.redrawCell(this);
- } else {
- showAuxControl();
- }
-
- view.redrawCell(this);
- }
-
- /**
- * Called when the user presses the mouse on the cell to determine
- * if the event occured over the arrow box.
- */
- protected boolean clickOnArrow(CellEvent e) {
- Rectangle b = view.getCellBounds(this);
-
- if (b.width - e.getX() < ARROW_WIDTH
- && e.getY()>0 && e.getY()<b.height && b.width-e.getX() > 0) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Called when the user triggers a mouse event on the cell. If the left mouse
- * button pushed, a check is made to see if occured on the arrow box. If so,
- * handleArrowPress() is called, otherwise handleArrowRelease() is called.
- */
- public void mouseEvent(CellEvent e) {
- if (e.getID() == CellEvent.MOUSE_PRESSED) {
- if (clickOnArrow(e) && e.isLeftMouseButtonDown()) {
- handleArrowPress();
- return;
- } else if (down()) {
- handleArrowRelease();
- }
- }
-
- super.mouseEvent(e);
- }
-
- /**
- * Callback method for adapter class to use when a drop-down widget fires
- * an event indicating the user made a choice. By default, sets the value
- * for the cell, highlights text and hides the aux control.
- */
- public void selectionMade(String item) {
- Data data;
-
- try {
- data = getData();
- } catch (DataNotAvailable ex) {
- view.handleException(row(), col(), ex);
- return;
- }
-
- data.setText(item);
- keyPressedYet = false;
- selectionMade = false;
- cursorPos = 0;
- view.hideAuxControl();
- view.redrawCell(this);
- }
-
- /**
- * If this is not a combo drop-down, all key events are ignored, o.w. if the
- * down arrow is pressed, handleArrowPress() is called. In all other cases, the
- * events are routed to the EditCell class for handling.
- */
- public void keyEvent(CellEvent e) {
- if (!combo) {
- //no cell editing allowed when not combo
- return;
- } else if (keyPressedYet && e.getKeyCode() == KeyEvent.VK_DOWN) {
- handleArrowPress();
- view.redrawCell(this);
- return;
- }
-
- super.keyEvent(e);
- }
-
- /**
- * Draws the text for the cell and calls drawArrow() to allow subclasses to
- * draw the arrow box.
- */
- public void drawCell(Graphics g, CellHints hints) {
- String text;
-
- try {
- text = readDataString();
- } catch (DataNotAvailable ex) {
- view.handleException(row(), col(), ex);
- text = "ERROR";
- }
-
- Rectangle r = hints.bounds();
- FontMetrics fm = view.getCellFontMetrics(this);
- int asc = fm.getAscent() + 1;
-
- int sw = fm.stringWidth(text);
- Color oldfg = hints.fg;
- int origWidth = r.width;
- int origX = r.x;
-
- hints.setBackground(g);
- g.fillRect(r.x, r.y, r.width-1, r.height-1);
-
- int rightPad = (isCurrentCell()) ?ARROW_WIDTH+5 :PAD_LEFT;
- switch(hints.alignment()) {
- case Grid.LEFT:
- r.x = origX + PAD_LEFT;
- break;
- case Grid.CENTER:
- if (sw > r.width-(PAD_LEFT+rightPad)) {
- //won't fit so flush left
- r.x += PAD_LEFT;
- } else {
- //extra room so slide to the right
- r.x = r.x + (r.width-sw-rightPad)/2;
- }
- break;
- case Grid.RIGHT:
- if (sw > r.width-(PAD_LEFT+rightPad)) {
- //won't fit so flush left
- r.x += PAD_LEFT;
- } else {
- //extra room so slide to the right
- r.x = origX+r.width-(sw+rightPad);
- }
- break;
- }
-
- hints.setForeground(g);
- r.width = r.width + origX - (r.x+rightPad-5);
-
- //draw the string
- drawText(text, g, r, fm, hints);
-
- //draw cursor
- if (isCurrentCell() && keyPressedYet) {
- int cpos = 0;
- if (text.length() > 0) {
- cpos = fm.stringWidth(text.substring(toLeftOfCell, cursorPos));
- }
- g.drawLine(r.x + cpos,
- r.y + 2,
- r.x + cpos,
- r.y + fm.getHeight());
- }
-
-
- r.x = origX;
- r.width = origWidth;
-
- if (isCurrentCell()) {
- drawArrow(g, r, hints);
- r.width = r.width - ARROW_WIDTH;
- }
-
- hints.drawBoundary(g);
- }
-
- /**
- * Draws the arrow button and calls drawSymbol() to draw graphic symbol.
- */
- protected void drawArrow(Graphics g, Rectangle r, CellHints hints) {
- int left = r.x+r.width-ARROW_WIDTH;
- int top = r.y;
- int w = ARROW_WIDTH;
- int ht = r.height;
-
- g.setColor(Color.lightGray);
- g.fillRect(left, top, w, ht);
- g.setColor(Color.black);
- g.drawRect(left, top, w, ht);
-
- //add highlight
- g.setColor(!down() ?Color.white :Color.gray);
- g.drawLine(left+1, top+2, left+1, top+ht-3); //left
- g.drawLine(left+1, top+2, left+w-3, top+2); //top
- g.setColor(down() ?Color.white :Color.gray);
- g.drawLine(left+w-3, top+2, left+w-3, top+ht-3); //right
- g.drawLine(left+1, top+ht-3, left+w-3, top+ht-3); //bottom
-
- drawSymbol(g, top, left, w, ht);
- }
-
- /**
- * Returns whether the arrow box is currently depressed.
- */
- protected abstract boolean down();
- /**
- * Method to draw the arrow box symbol.
- */
- protected abstract void drawSymbol(Graphics g, int top, int left, int w, int ht);
- }