home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 14.0 KB | 454 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 modify this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur 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.image.ImageObserver;
- import java.awt.event.*;
- import symantec.itools.db.awt.event.*;
-
- /**
- * A Grid cell containing a button.
- * @see symantec.itools.db.awt.Grid
- */
- public class ButtonCell implements TableCell, ImageObserver {
- TableView view;
- DataSource dataSource;
- Coordinate coords;
- boolean pressed;
- boolean drawUp = true;
- boolean defaultFlag;
- int type;
-
- static final int PADSIDES = 5;
-
- //this one is friendly
- /**
- * Constructs a default ButtonCell object.
- */
- ButtonCell() {}
-
- /**
- * Constructs a ButtonCell.
- * @param v The TableView the cell is contained within.
- * @param ds The datasource the cell obtains its data.
- */
- public ButtonCell(TableView v, DataSource ds) {
- view = v;
- dataSource = ds;
- }
-
- //one relative
- /**
- * Constructs a ButtonCell located at the given coordinates.
- * @param r the one-relative row index
- * @param c the one-relative column index
- */
- public ButtonCell(int r, int c) {
- coords = new Coordinate(r-1, c-1);
- }
-
- /**
- * Gets the type of cell.
- * @return One of CELL, COL_HEADING, ROW_HEADING, CORNER_CELL.
- */
- public int type() {
- return type;
- }
-
- /**
- * Sets the type of cell.
- * @param t One of CELL, COL_HEADING, ROW_HEADING, CORNER_CELL.
- * @return One of CELL, COL_HEADING, ROW_HEADING, CORNER_CELL.
- */
- public int type(int t) {
- if (t > CORNER_CELL || t < 0) {
- throw new IllegalArgumentException("Invalid cell type");
- }
-
- return type = t;
- }
-
- /**
- * Gets a copy of the cell suitable for taking the place of the current cell.
- */
- public TableCell cloneCell() {
- ButtonCell bc = new ButtonCell(view, dataSource);
-
- if (coords != null) {
- bc.coords = new Coordinate(coords);
- }
- bc.type = type;
- bc.defaultFlag = defaultFlag;
-
- return bc;
- }
-
- /**
- * Allows the cell to set instance variables for view and data source.
- * @param v The TableView the cell is contained within.
- * @param ds The datasource the cell obtains its data.
- */
- public void setTableView(TableView v, DataSource ds) {
- view = v;
- dataSource = ds;
- }
-
- /**
- * Sets the cell as a default cell used by the TableView and data
- * sources to conserve resources.
- */
- public void setDefaultFlag() { defaultFlag = true; }
-
- /**
- * Resets the values of the cell to their default values. The TableView uses
- * common cells to perform the drawing and event functions. This allows it to
- * use memory efficiently by not being forced to create a cell for every piece of
- * data stored in a data source. This function is called when a cell is changed
- * to represent another coordinate position. Afterwards, the cell's methods are
- * called to set the internal state properly before being asked to process
- * cell events are perform drawing.
- */
- public void reset() {
- pressed = false;
- drawUp = true;
- }
-
- /**
- * Sets the cell coordinates for the cell. The coordinate parameter is a
- * shared value, so its values should be copied to another variable of type
- * Coordinate.
- */
- public void setCoordinates(Coordinate p) {
- coords = p;
- }
-
- /**
- * Gets the cell coordinates for the cell.
- */
- public Coordinate getCoordinates() {
- return coords;
- }
-
- /**
- * Gets the row number for the cell.
- */
- public int row() { return coords.row; }
-
- /**
- * Sets the row number for the cell.
- */
- public void setRow(int r) { coords.row = r; }
-
- /**
- * Gets the column number for the cell.
- */
- public int col() { return coords.col; }
-
- /**
- * Sets the column number for the cell.
- */
- public void setCol(int c) { coords.col = c; }
-
- boolean inside(int x, int y) {
- Rectangle r = view.getCellBounds(this);
-
- if (x > 0 && y > 0 && x < r.width && y < r.height) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Called when the user triggers a mouse event on the cell.
- */
- public void mouseEvent(CellEvent e) {
- if (e.getID() == CellEvent.MOUSE_PRESSED) {
- view.setCapture(this);
- pressed = true;
- drawUp = false;
- view.routeEvent(e);
- view.redrawCell(this);
- } else if (e.getID() == CellEvent.MOUSE_DRAGGED) {
- if (inside(e.getX(), e.getY()) && drawUp) {
- drawUp = false;
- e.setID(CellEvent.BUTTON_FLICKER_DOWN_EVENT);
- view.routeEvent(e);
- view.redrawCell(this);
- } else if (!inside(e.getX(), e.getY()) && !drawUp) {
- drawUp = true;
- e.setID(CellEvent.BUTTON_FLICKER_UP_EVENT);
- view.routeEvent(e);
- view.redrawCell(this);
- }
- } else if (e.getID() == CellEvent.MOUSE_RELEASED) {
- if (pressed && !drawUp) {
- e.setID(CellEvent.BUTTON_UP_EVENT);
- view.routeEvent(e);
- }
-
- pressed = false;
- drawUp = true;
- view.redrawCell(this);
- view.lostCapture();
- }
- }
-
- /**
- * Takes no action.
- * @return <code>true</code>
- */
- public boolean auxControlEvent(Event e) { return true; }
-
- /**
- * Gets the auxillary control for the cell.
- * @return The widget if the cell supports auxillary controls or null
- * otherwise.
- */
- public Component auxControl() { return null; }
-
- /**
- * Called by the TableView when it hides the auxillary control of the cell.
- */
- public void lostAuxControl() {}
-
- /**
- * Gets the data object for the cell (Normally retrieved via the data source.
- * @exception DataNotAvailable if the data cannot be accessed
- */
- public Data getData() throws DataNotAvailable {
- return dataSource.getData(coords);
- }
-
- String chopString(String text, FontMetrics fm, Rectangle r) {
- int w = r.width;
- int i = 1;
- String t;
-
- if (text.length() == 0) { return text; }
-
-
- do {
- t = text.substring(0, i);
- } while(fm.stringWidth(t) < w - PADSIDES*2 && i++ < text.length());
-
- return text.substring(0, i-1);
- }
-
- /**
- * Called by the TableView when the cell is to be drawn on the screen.
- * @param g The graphics context to perform drawing functions.
- * @param hints Contains the information required for the cell to draw
- * in the proper place and colors.
- */
- public void drawCell(Graphics g, CellHints hints) {
- Data data;
-
- try {
- data = getData();
- } catch (DataNotAvailable ex) {
- data = new ImageStringData(dataSource, "");
- }
-
-
- Rectangle r = hints.bounds();
- FontMetrics fm = g.getFontMetrics();
- int asc = fm.getAscent() + 1;
- String text = chopString(data.toString(), fm, r);
- int sw = fm.stringWidth(text);
- int imageOffset = 0;
- Color fg = g.getColor();
- int origX = r.x;
-
- drawButton(g, hints);
- Image im = data.toImage();
- switch(hints.alignment()) {
- case TableView.LEFT:
- if (im != null) {
- imageOffset = im.getWidth(this) + PADSIDES;
- }
-
- //check to see if it will all fit within cell
- if (imageOffset+sw+2+PADSIDES <= r.width && im != null) {
- r.x += PADSIDES;
- r.y += 3;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 3;
- } else {
- imageOffset = PADSIDES;
- }
-
- r.x = origX + imageOffset + 2;
- break;
- case TableView.CENTER:
- r.x = origX + (r.width-sw)/2;
- break;
- case TableView.RIGHT:
- if (im != null) {
- imageOffset = im.getWidth(this) + PADSIDES;
- }
- //check to see if it will all fit within cell
- if (sw+imageOffset+2+PADSIDES <= r.width && im != null) {
- r.x = r.x + r.width - sw - PADSIDES - imageOffset - 2;
- r.y += 3;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 3;
- }
-
- r.x = origX+r.width-sw-PADSIDES;
- break;
- }
-
- g.setColor(hints.foreground());
-
- g.drawString(text, r.x, r.y+fm.getAscent()+2);
- }
-
- void drawButton(Graphics g, CellHints hints) {
- //all highlighted and ready for delete
- Rectangle r = hints.bounds();
- g.setColor(hints.background());
- g.fillRect(r.x,
- r.y,
- r.width-1,
- r.height);
-
- //draw shade for button
- g.setColor(Color.gray);
- g.drawRect(r.x+1, r.y+1, r.width-3, r.height-3);
- g.setColor(Color.black);
- g.drawRect(r.x, r.y, r.width-1, r.height-1);
-
- //draw the hightlight
- if (drawUp) {
- //draw button raised
- g.setColor(Color.white);
- g.drawLine(r.x+1, r.y+1, r.x + r.width-2, r.y+1);
- g.drawLine(r.x+1, r.y+1, r.x+1, r.y + r.height - 2);
- } else {
- //draw button recessed
- g.setColor(Color.white);
- g.drawLine(r.x+1, r.y + r.height - 2, r.x + r.width-2, r.y + r.height-2);
- g.drawLine(r.x + r.width - 2, r.y+1, r.x + r.width - 2, r.y + r.height - 3);
- }
- }
-
- /**
- * Queries whether the cell type supports user editing (normally from the
- * keyboard. If it cannot, the cell will not receive the keyboard focus.
- */
- public boolean isCellTypeEditable() { return false; }
-
- /**
- * Takes no action.
- * @return <code>true</code>
- */
- public boolean actionEvent(Event e) {
- return true;
- }
-
- /**
- * Called by the TableView when the user triggers a key event while the
- * cell possesses the keyboard focus.
- */
- public void keyEvent(CellEvent e) { }
-
- /**
- * Called by the TableView when the cell possess the keyboard focus to query
- * whether the cell's contents are in a valid state so another control may
- * receive the keyboard focus.
- */
- public boolean canLoseFocus() { return true; }
-
- /**
- * Called by the TableView when the cell either gains or loses the keyboard focus.
- */
- public void focusEvent(CellEvent e) {
- if (e.getID() == CellEvent.GOT_FOCUS) {
- view.setCapture(this);
- view.routeEvent(e);
- } else {
- view.lostCapture();
- view.routeEvent(e);
- }
- }
-
- /**
- * Called by the TableView when the cell possesses the keyboard focus to
- * query whether an arrow key can move the focus.
- */
- public boolean loseFocusOnArrow() {
- return false;
- }
-
- /**
- * Informs the cell to draw a cursor if appropriate. The TableView deactivates
- * the cursor when it loses the keyboard focus.
- */
- public void activateCursor() {
- }
-
- /**
- * Informs the cell not to draw a cursor. The TableView deactivates the cursor
- * when it loses the keyboard focus.
- */
- public void deactivateCursor() {
- }
-
- /**
- * Gets the key statistics of the cell in a String format. Helps with
- * debugging.
- */
- public String stats() {
- return "";
- }
-
- /**
- * Repaints the list when the cell's image has changed.
- * @return true if image has changed; false otherwise.
- */
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
- if ((flags & (ABORT|ERROR)) != 0) {
- return false;
- }
-
- if ((flags & ALLBITS) != 0) {
- view.redrawAsync();
- return false;
- } else {
- return true;
- }
- }
- }
-
-