/** * Listor class is vaguely like a table object. But it is simpler, * so it is both easier to understand, and easier to work with. * It allows you to lay out a basic "table" of data, and also allows * the USER to resize the rows as they desire. * * Rather than being 'cell' oriented, Listor is row oriented. * For example, you can highlight a row, but not a specific field of * a row. This simplifies things greatly. * * There are a few hard-codes in here, but not too many. * * General use: Create a new instance of Listor, which is a subclass * of a regular AWT Panel. * You must define the number of columns you want, now and forever. * You also should define a total width currently. * * From this point, you will want to do one or more of three operations; * * Define column names, through Listor.setColName() * Define initial column widths, through Listor.setColName() * Add data rows, through Listor.addValues() * * Note that there is no "changeValues()" call; it is unneccessary, * since the values are only referenced, not copied. */ /* COPYRIGHT: Philip Brown, 2003 * You may use this code however you like, as long as * you leave this copyright notice section unchanged, and * do not hold Philip Brown liable for any issues, bugs, damages, etc. * No warantee, assumed, or implied, is given with this software, etc. etc. * * If you would like different license terms, talk to me * * http://www.bolthole.com/java/ * * END OF COPYRIGHT NOTICE */ /* Listor.java 1.16 */ import java.awt.*; import java.util.Vector; /** * Just a subcomponent class for the Listor class below. * a DataPane can be a read-only dumb display unit, OR a "master" * unit, that allows for column width resizes. */ class DataPane extends Canvas implements java.awt.event.MouseMotionListener, java.awt.event.MouseListener { Dimension mysize; boolean master, havecursor, highlighted; int havecolnum; // joined to "havecursor". user has "grabbed" a col //java.awt.event.ActionListener parent; Listor parent; int widths[]; String values[]; Color bgcolor, fgcolor; /** pass in an array of column widths, and an array of * string values for each column * You also need a totalwidth estimation, although it * is mostly unneccesary these days. */ public DataPane(int w[], String v[], int totalwidth){ mysize=new Dimension(); mysize.height=25; /* nasty hack, sigh */ mysize.width=totalwidth; master=false; havecursor=false; havecolnum=0; widths=w; values=v; setSize(totalwidth, mysize.height); fgcolor=Color.black; bgcolor=Color.white; setBackground(bgcolor); } public void setBackground(Color col){ highlighted=false; super.setBackground(col); bgcolor=col; } /* Modeled on the "standard" addMouseListener. * But note that we can only have ONE registered listener * Note also, that we only send a SUBSET of mouse events back * to the parent * * This says that the 'parent' wants to know when we get clicked on. * We will send the mouseReleased event back to the * parent, so it can know which button was used, etc. * Do not call this for the "master" DataPane */ public void addMouseListener(Listor Parent) { if(parent==null){ addMouseListener(this); } parent=Parent; } public Dimension getPreferredSize(){ return mysize; } public Dimension getMinimumSize(){ System.out.println("datapane: gerMin"); return getPreferredSize(); } void setValues(String v[]){ values=v; } /* return X coordinate of 'start' of a column. * Gives left-most X coordinate of a column. */ int getColStart(int endcol){ int xpos=0; int col=0; if(endcol<0){ return 0; } if(endcol>=widths.length){ endcol=widths.length-1; } while(col=widths.length) Col=widths.length-1; while(curcol <=Col){ xpos+=widths[curcol++]; } return xpos; } public void setHighlighted(boolean isSet){ if(isSet){ super.setBackground(fgcolor); highlighted=true; } else { super.setBackground(bgcolor); highlighted=false; } /* unfortunately, this does a double bg-clear. oh well */ repaint(); } /* We paint the dividers at the 'right' of each column. */ public void paint(Graphics gc){ int xpos=0; int colcount; if(highlighted) gc.setColor(bgcolor); else gc.setColor(fgcolor); /* For speed purposes, circumvent getColStart/End. * However, make sure to stay compatible with them!! */ for(colcount=0; colcount =mysize.width) newx = mysize.width - 1; if(oldx==newx){ //debug("mouseDragged: no size change"); return; } //debug("MasterRow: dragging col "+havecolnum // + " to x coord "+newx); widths[havecolnum]=newx - startcol; parent.showUpdates(); /*repaint();*/ /* This would redraw JUST the master row */ } static public void debug(String msg){ System.out.println(msg); } public void mousePressed(java.awt.event.MouseEvent evt){ int colmatch=-1; if(havecursor){ debug("mousePressed: HAVE cursor already??"); // should never happen, but just in case... return; } /* We want to think of grabbing the separator to the "right" * of a column. */ //debug("mousePressed: looking for match"); for(int col=0;col=rows.size()) return; DataPane dp=(DataPane)rows.elementAt(rownum); dp.setHighlighted(value); } /* This is really written for the future, when and if * we support having multiple rows highlighted */ public void clearHighlights(){ internalclearhighlights(true); } /** simply unsets the "highlight" bit on the given data row. * It is not an error to call this on a row that was not * already highlighted. */ public void clearHighlight(int rownum){ if(rownum>=rows.size()) return; DataPane dp=(DataPane)rows.elementAt(rownum); dp.setHighlighted(false); if(rownum==lastrowclicked) lastrowclicked=-1; } /** This will return the row number of the "first" row * highlighted. returns -1 if none highlighted. * * Use this in conjunction with clearHighlight to walk through * the case of multiple lines highlighted */ public int getHighlightedIndex(){ final int none_set=-1; DataPane dp; int numpanes=rows.size(); if(numpanes==0) return none_set; for(int pcount=0;pcount=numcols){ System.err.println("ERR: setColName called with col=" + colnum); return; } colnames[colnum]=name; /* XXX redraw? */ } /** Set the width of a specific column, different from the default. * This gets stored in an instance-global array. */ public void setColWidth(int colnum, int width){ if(colnum>=numcols){ System.err.println("ERR: setColWidth called with col=" + colnum); return; } colwidths[colnum]=width; } }