home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 31.7 KB | 954 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.event.*;
- import symantec.itools.db.awt.event.CellEvent;
-
- /**
- * Cell type that is used to hold string and image data. Images are not scaled,
- * this cell expects them to be small icons to the left of the text.
- */
- public class BasicCell implements TableCell, java.awt.image.ImageObserver {
- TableView view;
- DataSource dataSource;
- Coordinate coords;
- boolean selected;
- boolean keyPressedYet;
- boolean loseFocusOnArrow;
- int cursorPos;
- int hlFirst, hlLast; //left & right for highlighting
- boolean selectionMade;
- int offset; //will be used for keeping cursor in cell
- int chopIndex;
- int toLeftOfCell;
- int startX;
- boolean defaultCell;
- int type;
-
- static final int PADSIDES = 5;
-
- /**
- * Constructs a BasicCell.
- * @param v The TableView the cell is contained within.
- * @param ds The datasource the cell obtains its data.
- */
- public BasicCell(TableView tv, DataSource ds) {
- view = tv;
- dataSource = ds;
- }
-
- /**
- * Gets a copy of the cell suitable for taking the place of the current cell.
- */
- public TableCell cloneCell() {
- BasicCell bs = new BasicCell(view, dataSource);
-
- if (coords != null) {
- bs.coords = new Coordinate(coords.row, coords.col);
- }
-
- bs.selected = selected;
- bs.keyPressedYet = keyPressedYet;
- bs.loseFocusOnArrow = loseFocusOnArrow;
- bs.cursorPos = cursorPos;
- bs.selectionMade = selectionMade;
- bs.offset = offset;
- bs.type = type;
-
- return bs;
- }
-
- /**
- * Gets the type of cell.
- * @return One of CELL, COL_HEADING, ROW_HEADING, CORNER_CELL.
- */
- public int type() {
- return type;
- }
-
- /**
- * Gets the auxillary control for the cell.
- * @return The widget if the cell supports auxillary controls or null
- * otherwise.
- */
- public Component auxControl() { return null; }
-
- /**
- * 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;
- }
-
-
- /**
- * 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 whether this cell is the defualt maintained by the TableView.
- * This is really an internal method that must be exposed beause of
- * the rules of defining a public interface.
- */
- public void setDefaultFlag() {
- defaultCell = 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() {
- selected = false;
- keyPressedYet = false;
- loseFocusOnArrow = false;
- cursorPos = 0;
- selectionMade = false;
- offset = 0;
- }
-
- /**
- * Gets whether this cell type provides support for editing.
- */
- public boolean isCellTypeEditable() { return 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 c) {
- coords = c;
- }
-
- /**
- * 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; }
-
- /**
- * Called when the user triggers a mouse event on the cell.
- */
- public void mouseEvent(CellEvent e) {
- try {
- switch(e.getID()) {
- case CellEvent.MOUSE_PRESSED:
- if (!e.getAsMouseEvent().isShiftDown()) {
- //if still highlighted position cursor right place and unhighlight
- keyPressedYet = true;
- view.setCapture();
- cursorPos = findCursorPos(e.getX());
- selectionMade = false;
- } else {
- //shift pressed so we make a hightlighted region
- hlFirst = cursorPos;
- hlLast = findCursorPos(e.getX());
- selectionMade = true;
- }
-
- view.redrawCell(this);
- view.routeEvent(e);
- break;
-
- case CellEvent.MOUSE_DOUBLE:
- keyPressedYet = false;
- view.redrawAsync();
- offset = 0;
- view.routeEvent(e);
- break;
-
- case CellEvent.MOUSE_DRAGGED:
- if (!selectionMade && findCursorPos(e.getX())!=cursorPos) {
- //select a region
- hlFirst = cursorPos;
- selectionMade = true;
- }
-
- hlLast = cursorPos = findCursorPos(e.getX());
- view.redrawCell(this);
- view.routeEvent(e);
- break;
-
- case CellEvent.MOUSE_RELEASED:
- view.routeEvent(e);
- break;
- }
- } catch(DataNotAvailable ex) {
- view.handleException(row(), col(), ex);
- }
- }
-
- /**
- * Called by the TableView when it hides the auxillary control of the cell.
- */
- public void lostAuxControl() {}
-
- int findCursorPos(int x) throws DataNotAvailable {
- //mouse past the beginning of string so return initial pos.
- if (x < -offset) {
- return 0;
- }
-
- int total = offset + x - PADSIDES;
- FontMetrics fm = view.getCellFontMetrics(this);
- Data data = readData();
- String text = getDataString(data);
- int len = text.length();
-
- //loop through each letter and determine where cursor goes
- Image im = data.toImage();
- int align = view.getCellAlignment(this);
- if (align == TableView.LEFT) {
- if (offset == 0) {
- total -= PADSIDES + 2;
-
- if (im != null) {
- int imageOffset = im.getWidth(this) + 2;
- int w = view.getColumnWidth(getCoordinates().col+1);
- int sw = fm.stringWidth(text);
-
- if (imageOffset+sw+2+PADSIDES <= w) {
- total -= imageOffset;
- }
- }
- }
- } else if (align == TableView.RIGHT) {
- int sw = fm.stringWidth(text);
- int w = view.getColumnWidth(getCoordinates().col+1);
- if (sw <= w) {
- total = total-w+sw+PADSIDES*2;
- }
- } else if (align == TableView.CENTER) {
- int sw = fm.stringWidth(text);
- int w = view.getColumnWidth(getCoordinates().col+1);
- if (sw<=w) {
- total = total - (w-sw)/2;
- }
- }
-
- //check if all the way to left
- if (total <= 0) { return 0; }
- for(int i=1; i<=len; i++) {
- if (fm.stringWidth(text.substring(0, i-1) + fm.charWidth(text.charAt(i-1))/2) > total)
- return i;
- }
-
- return len;
- }
-
- /**
- * Gets the data object for the cell (Normally retrieved via the data source.
- * @return the cell data
- * @exception DataNotAvailable if the data cannot be accessed
- */
- public Data getData() throws DataNotAvailable {
- return dataSource.getData(coords);
- }
-
- /**
- * Gets the cell data as a String.
- * @return the cell data as a String
- * @exception DataNotAvailable if the data cannot be accessed
- */
- protected String getDataString() throws DataNotAvailable {
- return getDataString(readData());
- }
-
- /**
- * Gets the specified Data as a String.
- * @param the data
- * @param the data as a String
- * @exception DataNotAvailable if the data cannot be accessed
- */
- protected String getDataString(Data data) throws DataNotAvailable {
- if (data instanceof MappedData) {
- return ((MappedData)data).displayValue();
- }
-
- return data.toString();
- }
-
- /**
- * Reads this cell's data from the data source.
- * @return this cell's data
- * @exception DataNotAvailable if the data cannot be accessed
- */
- public Data readData() throws DataNotAvailable {
- return dataSource.readData(coords.row, coords.col);
- }
-
- void deleteChar(Data data) {
- if (cursorPos == 0)
- return;
-
- data.deleteChar(cursorPos);
- cursorPos--;
- }
-
- void deleteSelection(Data data) {
- int hlLeft = Math.min(hlFirst, hlLast);
- int hlRight = Math.max(hlFirst, hlLast);
- for (int i=hlLeft; i<hlRight; i++) {
- data.deleteChar(hlLeft+1);
- }
- cursorPos = hlLeft;
- }
-
- boolean isEditable() {
- try {
- return isEditable(getData());
- } catch (DataNotAvailable ex) {
- view.handleException(row(), col(), ex);
- return true;
- }
- }
-
- boolean isEditable(Data data) {
- return data.isEditable(coords.row, coords.col)
- && view.getCellEditable(this);
- }
-
- /**
- * Called by the TableView when the user triggers a key event while the
- * cell possesses the keyboard focus.
- */
- public void keyEvent(CellEvent e) {
- Data data;
- String text;
-
- try {
- data = getData();
- text = getDataString(data);
- } catch (DataNotAvailable ex) {
- view.handleException(row(), col(), ex);
- return;
- }
-
- KeyEvent keyEvent = e.getAsKeyEvent();
- boolean isActionKey = keyEvent.isActionKey();
-
- char c = keyEvent.getKeyChar();
- boolean changed = false;
- boolean handled = false;
- boolean editable = isEditable(data);
-
- if (!editable) {
- view.routeEvent(e);
- return;
- }
-
- if (isActionKey && e.getID()==CellEvent.KEY_PRESSED) {
- if (keyEvent.isShiftDown() && !selectionMade) {
- selectionMade = true;
- changed = true;
- hlFirst = hlLast = cursorPos;
- } else if (!keyEvent.isShiftDown() && selectionMade) {
- selectionMade = false;
- changed = true;
- }
-
- switch(e.getKeyCode()) {
- case KeyEvent.VK_LEFT:
- if (cursorPos > 0) {
- cursorPos--;
- changed = true;
- }
- break;
- case KeyEvent.VK_RIGHT:
- if (cursorPos < text.length()) {
- cursorPos++;
- changed = true;
- }
- break;
- case KeyEvent.VK_UP:
- case KeyEvent.VK_HOME:
- if (cursorPos != 0) {
- cursorPos = 0;
- changed = true;
- }
- break;
- case KeyEvent.VK_DOWN:
- case KeyEvent.VK_END:
- int len = text.length();
- if (cursorPos != len) {
- cursorPos = len;
- changed = true;
- }
- break;
- }
-
- if (selectionMade) hlLast = cursorPos;
- if (changed) {
- view.redrawCell(this);
- }
-
- view.routeEvent(e);
- return;
- }
-
- int keyCode = e.getKeyCode();
- if (e.getID() == CellEvent.KEY_PRESSED) {
- if (!keyPressedYet) {
- //if hit delete or bs then delete everything and paint cursor
- if (keyCode==KeyEvent.VK_DELETE
- || keyCode==KeyEvent.VK_BACK_SPACE)
- {
- data.clearText();
- keyPressedYet = true;
- view.setCapture();
- cursorPos = 0;
- view.redrawCell(this);
- e.setID(CellEvent.CELL_CONTENT_CHANGE);
- view.routeEvent(e);
- e.setID(CellEvent.KEY_PRESSED);
- view.routeEvent(e);
- return;
- }
-
- //Strip nonprintable characters
- if (!Character.isSpace(c) && !Character.isLetterOrDigit(c)) {
- view.routeEvent(e);
- return;
- }
-
- data.clearText();
- data.appendChar(c);
- keyPressedYet = true;
- selectionMade = false;
- view.setCapture();
- cursorPos = 1; //start at beginning
- } else {
- if (selectionMade) {
- deleteSelection(data);
- selectionMade = false;
- //if b.s. or del then delete highlight and return
- if (keyCode==KeyEvent.VK_BACK_SPACE ||
- keyCode==KeyEvent.VK_DELETE)
- {
- e.setID(CellEvent.CELL_CONTENT_CHANGE);
- view.routeEvent(e);
- e.setID(CellEvent.KEY_PRESSED);
- view.routeEvent(e);
- return;
- }
- }
-
- switch(keyCode) {
- case KeyEvent.VK_ESCAPE:
- cursorPos = 0;
- keyPressedYet = false;
- e.setID(CellEvent.UNDO_CELL_EVENT);
- view.routeEvent(e);
- break;
- case KeyEvent.VK_ENTER:
- try {
- dataSource.commitData();
- keyPressedYet = false;
- cursorPos = 0;
- } catch(TypeNotSupported ex) {
- view.handleException(row(), col(), ex);
- return;
- }
- break;
- case KeyEvent.VK_BACK_SPACE:
- deleteChar(data);
- view.routeEvent(e);
- int align = view.getCellAlignment(this);
- if (align == TableView.RIGHT || align == TableView.CENTER) {
- offset = 0;
- }
- break;
- case KeyEvent.VK_DELETE:
- if (cursorPos != text.length()) {
- cursorPos++;
- deleteChar(data);
- e.setID(CellEvent.CELL_CONTENT_CHANGE);
- view.routeEvent(e);
- }
- break;
- default:
- //Strip nonprintable characters
- if (!isPrintableChar(c)) {
- view.routeEvent(e);
- return;
- }
-
- //determine if append character or insert
- if (cursorPos==text.length()) {
- data.appendChar(c);
- } else {
- data.insertChar(cursorPos, c);
- }
-
- cursorPos++;
- e.setID(CellEvent.CELL_CONTENT_CHANGE);
- view.routeEvent(e);
- }
- }
-
- view.redrawCell(this);
- e.setID(CellEvent.KEY_PRESSED);
- view.routeEvent(e);
- return;
- } else if (e.getID() == CellEvent.KEY_RELEASED) {
- view.routeEvent(e);
- }
- }
-
- boolean isPrintableChar(char c) {
- switch (Character.getType(c)) {
- case Character.CONTROL:
- case Character.FORMAT:
- case Character.LINE_SEPARATOR:
- case Character.NON_SPACING_MARK:
- case Character.PARAGRAPH_SEPARATOR:
- case Character.PRIVATE_USE:
- case Character.UNASSIGNED:
- case Character.SURROGATE:
- return false;
- }
-
- return true;
- }
-
- /**
- * 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 !selected || (selected & !keyPressedYet);
- }
-
- /**
- * Informs the cell to draw a cursor if appropriate. The TableView deactivates
- * the cursor when it loses the keyboard focus.
- */
- public void activateCursor() {
- selected = true;
- view.redrawCell(this, false);
- }
-
- /**
- * Informs the cell not to draw a cursor. The TableView deactivates the cursor
- * when it loses the keyboard focus.
- */
- public void deactivateCursor() {
- selected = false;
- view.redrawCell(this, false);
- }
-
- /**
- * 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() {
- //try to commit data - if successful then return true
- try {
- //commit dataSource's current data and tell the data to commit
- //also
- dataSource.commitData();
- } catch(Exception ex) {
- //Can throw RuntimeExceptions so catch everything
- view.handleException(row(), col(), ex);
- return false;
- }
-
- 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) {
- selected = isEditable();
- view.routeEvent(e);
- view.redrawCell(this);
- } else {
- selected = false;
- keyPressedYet = false;
- selectionMade = false;
- offset = 0;
- cursorPos = 0;
- view.routeEvent(e);
- try {
- dataSource.commitData();
- } catch(Exception ex) {
- view.handleException(row(), col(), ex);
- }
- view.redrawAroundCell(this);
- }
- }
-
- /**
- * 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;
- String text;
-
- try {
- data = readData();
- text = getDataString(data);
- } catch (DataNotAvailable ex) {
- //vj:
- //view.handleException(row(), col(), ex);
- data = new ImageStringData(dataSource, "");
- text = "";
- }
-
- Rectangle r = hints.bounds();
- FontMetrics fm = view.getCellFontMetrics(this);
- int asc = fm.getAscent() + 1;
- int sw = fm.stringWidth(text);
- int imageOffset = 0;
- Color oldfg = hints.fg;
- int origX = r.x,
- origWidth = r.width;
-
- hints.setBackground(g);
- g.fillRect(r.x, r.y, r.width-1, r.height-1);
- Image im = data.toImage();
- switch(hints.alignment()) {
- case TableView.LEFT:
- if (im != null) {
- imageOffset = im.getWidth(this) + 2;
- }
-
- //check to see if it will all fit within cell
- if (imageOffset+sw+2+PADSIDES <= r.width && im != null) {
- r.x += PADSIDES;
- r.y += 1;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 1;
- } else {
- imageOffset = 0;
- }
-
- r.x = origX + imageOffset + PADSIDES;
- break;
- case TableView.CENTER:
- if (sw>(r.width - PADSIDES*2)) {
- r.x += PADSIDES;
- } else {
- r.x = r.x + (r.width-sw)/2;
- }
- break;
- case TableView.RIGHT:
- if (im != null) {
- imageOffset = im.getWidth(this);
- }
- //the 3 is just a fudge factor - there must be a better way!!!!
- //check to see if it will all fit within cell
- if ((imageOffset+2+sw+PADSIDES*2+3) <= r.width && im != null) {
- r.x = r.x + r.width - sw - imageOffset - 2 - PADSIDES - 3;
- r.y += 1;
- g.drawImage(im, r.x, r.y, this);
- r.y -= 1;
- r.x = origX + r.width - sw - PADSIDES - 3;
- } else {
- imageOffset = 0;
- if (sw>(r.width - PADSIDES*2)) {
- r.x += PADSIDES;
- } else {
- r.x = origX + r.width - sw - PADSIDES - 3;
- }
- }
- break;
- }
-
- hints.setForeground(g);
- r.width = r.width + origX - r.x - PADSIDES;
- startX = r.x;
- drawText(data, g, r, fm, hints);
- r.width = origWidth;
-
- //draw cursor
- if (selected && keyPressedYet) {
- int cpos = 0;
- if (text.length() > 0) {
- cpos = fm.stringWidth(data.subString(toLeftOfCell, cursorPos));
- }
- g.drawLine(r.x + cpos,
- r.y + 2,
- r.x + cpos,
- r.y + fm.getHeight());
- }
-
-
- r.x = origX;
-
- hints.drawBoundary(g);
- }
-
- /**
- * Gets a string from the data source that is truncated as needed to
- * fit within the given rectangle.
- * @param data the data source
- * @param r the clipping rectangle
- * @param fm the text font metrics
- * @return the string, truncated as needed
- */
- protected String chopString(Data data, Rectangle r, FontMetrics fm) {
- String text;
-
- try {
- text = getDataString(data);
- } catch(DataNotAvailable ex) { return ""; }
-
- int w = view.getColumnWidth(coords.col+1);
- chopIndex = 1;
- String t;
-
- if (text.length() == 0) {
- return text;
- }
-
- //chop off any part of string not visible
- setOffset(text, r, fm);
- if (offset != 0) {
- int left;
- while(toLeftOfCell < text.length()) {
- left = fm.stringWidth(text.substring(0, toLeftOfCell));
- if (left < offset + PADSIDES) {
- toLeftOfCell++;
- } else {
- toLeftOfCell--;
- break; //found the visible portion of the text
- }
- }
-
- toLeftOfCell -= (toLeftOfCell < text.length()) ?0 :1;
- text = text.substring(toLeftOfCell, text.length());
- } else {
- toLeftOfCell = 0;
- }
-
- //get the portion of string that fits into cell
- do {
- t = text.substring(0, chopIndex);
- } while(fm.stringWidth(t) < w - PADSIDES*2 && chopIndex++ < text.length());
-
- return text.substring(0, --chopIndex);
- }
-
- /**
- * Draws the given cell data on the screen.
- * @param data the cell data
- * @param g the graphics context to perform drawing functions
- * @param r the cell's clipping rectangle
- * @param fm the metrics of the drawing font
- * @param hints information required for the cell to draw
- * in the proper place and colors.
- */
- protected void drawText(Data data, Graphics g, Rectangle r,
- FontMetrics fm, CellHints hints)
- {
- String text = chopString(data, r, fm);
-
- if (view.printMode()) {
- g.drawString(text, r.x, r.y+fm.getAscent()+2);
- return;
- }
-
- if (selected && !keyPressedYet) {
- //all highlighted and ready for delete
- hints.setForeground(g);
- g.fillRect(r.x-1,
- r.y+3,
- fm.stringWidth(text.toString())+4,
- fm.getHeight()-1);
- hints.setBackground(g);
- g.drawString(text, r.x, r.y+fm.getAscent()+2);
- } else if (selected && !selectionMade) {
- //draw string with cursor
- g.drawString(text, r.x, r.y+fm.getAscent()+2);
- } else if(!selected || !selectionMade ) {
- //no highlight
- g.drawString(text, r.x, r.y+fm.getAscent()+2);
- } else {
- //some section must be highlighted
- int hlLeft = Math.min(hlFirst, hlLast);
- int hlRight = Math.max(hlFirst, hlLast);
- hlRight = Math.min(hlRight, toLeftOfCell+chopIndex);
-
- if (toLeftOfCell != 0) {
- hlLeft = Math.max(hlLeft - toLeftOfCell, 0);
- hlRight = hlRight - toLeftOfCell;
- }
-
- String t = text.substring(0, hlLeft);
-
- //draw first unselected portion
- g.drawString(t, r.x, r.y+fm.getAscent()+2);
-
- //draw middle selected portion
- int pixelHLStart = fm.stringWidth(t) + r.x;
- t = text.substring(hlLeft, hlRight);
- int pixelHLStop = fm.stringWidth(t) + pixelHLStart;
- hints.setForeground(g);
- g.fillRect(pixelHLStart,
- r.y+2,
- pixelHLStop - pixelHLStart,
- fm.getHeight()-1);
- hints.setBackground(g);
- g.drawString(t, pixelHLStart, r.y+fm.getAscent()+2);
-
- //draw end unselected portion
- hints.setForeground(g);
- t = text.substring(hlRight);
- g.drawString(t, pixelHLStop, r.y+fm.getAscent()+2);
- }
- }
-
- /**
- * Sets the offset from the start of the string to left side of spcified rectangle.
- */
- void setOffset(String text, Rectangle r, FontMetrics fm) {
- //vj: Reset cursor position to the end of the text. Required because of forced
- // commit of the current cell's data
- if (cursorPos > text.length())
- cursorPos = text.length();
- String sub = text.substring(0, cursorPos);
- int toCursor = fm.stringWidth(sub);
- int fudge = 3;
-
- if (!selected) {
- offset = 0;
- return;
- }
-
- //if cursor pos is not currently visible, put the cursor in sight within
- //1/3 width of rectangle
- if (toCursor > r.width - fudge + offset) {
- //cursor to far to right
- offset = -r.width + toCursor + fudge + r.width/3;
- //don't left right side of string off the right side of cell
- offset = Math.min(offset, fm.stringWidth(text) - r.width + fudge);
- } else if (toCursor < offset) {
- //cursor too far to left
- offset = Math.max(toCursor + fudge - r.width/3, 0);
-
- //amount of string to left of cell just decreased so force recompute
- toLeftOfCell = 0;
- }
- }
-
- /**
- * 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;
- }
- }
-
- /**
- * Inherited from java.lang.Object.
- * @return The string data for the cell.
- */
- public String toString() {
- try {
- return readData().toString();
- } catch(DataNotAvailable ex) {
- return "ERROR getting data";
- }
- }
-
- /**
- * Gets a string with the coordinates and the string data for cell.
- */
- public String stats() {
- try {
- return "BasicCell: row=" + coords.row + " col=" + coords.col +
- " text=" + readData().toString();
- } catch(DataNotAvailable ex) {
- return "BasicCell: row=" + coords.row + " col=" + coords.col +
- " could not retrieve data";
- }
- }
- }
-
-