Main Page | Compound List | File List | Compound Members

SSPanel.java

Go to the documentation of this file.
00001 
00009 package dndtester;
00010 import javax.swing.*;
00011 import javax.swing.event.*;
00012 import java.awt.*;
00013 import java.awt.event.*;
00014 import java.util.*;
00015 
00024 public class SSPanel extends JPanel{
00026     private int topIndex = 0;
00027     
00029     private int fontHeight;
00030     
00032     public static boolean showDebug = false;
00033 
00035     private JScrollBar scroller;
00036 
00038     private Vector data = new Vector();
00039 
00041     private Vector debug = new Vector();
00042     
00044     private Vector noDebug = data;
00045     
00047     public static final int MAXLINE = 65;
00048 
00050     private Hashtable pairs = new Hashtable();
00051 
00057     public void setScroller(JScrollBar scroll){
00058         scroller = scroll;
00059     }
00060     
00066     public void paintComponent(Graphics graphics){
00067         Color color = graphics.getColor();
00068         String str;
00069         super.paintComponent(graphics);
00070         graphics.setColor(color);
00071         
00072         Dimension size = getSize();
00073         Insets insets = getInsets();
00074         int yaxis = insets.top + getUnitHeight();
00075         // for loop that draws strings till no more fit in the panel or till
00076         // there is no more data
00077         for(int i = topIndex; i < data.size(); ++i, yaxis += fontHeight) {
00078             str = (String)data.elementAt(i);
00079             // if there is no PASS or FAIL just draw the string
00080             if(str.length() < 7){
00081                 graphics.drawString((String)data.elementAt(i), 0, yaxis);
00082                 continue;
00083             }
00084             // If the string PASS is encounter at the end of an entry
00085             // in the data its painted green
00086             if(str.substring(str.length() - 4, str.length()).equals("PASS")){
00087                 graphics.drawString(str.substring(0,str.length() - 4), 0, yaxis);
00088                 graphics.setColor(Color.green);
00089                 graphics.drawString("PASS", (size.width * 3) / 4, yaxis);
00090                 graphics.setColor(color);
00091             }
00092             // If the string FAIL is encounter at the end is painted red 
00093             else if(str.substring(str.length() - 4, str.length()).equals("FAIL")){
00094                 graphics.drawString(str.substring(0,str.length() - 4), 0, yaxis);
00095                 graphics.setColor(Color.red);
00096                 graphics.drawString("FAIL", (size.width * 3) / 4, yaxis);
00097                 graphics.setColor(color);
00098             }
00099             else{
00100                 graphics.drawString((String)data.elementAt(i), 0, yaxis);
00101             }
00102             if(yaxis + fontHeight> size.height - insets.bottom){
00103                 break;
00104             }
00105         }
00106         
00107     }
00108     
00114     public void setTopIndexByPixelValue(int pixelValue){
00115         topIndex = pixelValue/ fontHeight;
00116     }
00117 
00123     public int incrementTopIndex(){
00124         Dimension size = getSize();
00125         if(topIndex >= data.size() - size.height / fontHeight){
00126             return (topIndex + 1) * fontHeight;
00127         }
00128         topIndex++;
00129         return topIndex * fontHeight;
00130     }
00131     
00138     public int decrementTopIndex(){
00139         if(topIndex <= 0){
00140             return 0;
00141         }
00142         topIndex--;
00143         return topIndex * fontHeight;
00144     }
00145 
00150     public Dimension getPreferredSize(){
00151         Dimension dim = new Dimension();
00152         Graphics graphics = getGraphics();
00153         try {
00154             FontMetrics fontmetrics = graphics.getFontMetrics();
00155             fontHeight = fontmetrics.getHeight();
00156             if(data.size() > 0){
00157                 dim.width = fontmetrics.stringWidth((String)data.elementAt(data.size()-1));
00158             }
00159             else{
00160                 dim.width = 0;
00161             }
00162             dim.height = fontmetrics.getHeight() * (data.size() + 1);
00163         }
00164         finally {
00165             graphics.dispose();
00166         }
00167         return dim;
00168     }
00169 
00174     public int getUnitHeight(){
00175         return fontHeight;
00176     }
00177 
00181     public void reset(){
00182         data = new Vector();
00183         debug = new Vector();
00184         noDebug = data;
00185         if(showDebug){
00186             data = debug;
00187         }
00188         pairs = new Hashtable();
00189     }
00190     
00197     public void logCommand(String[] data){
00198         for(int i = 0; i < data.length; i++){
00199             this.noDebug.add(data[i]);
00200             if(DaqDriverTest.debug){
00201                 debug.add(data[i]);
00202             }
00203         }
00204         // Everytime something is added to the data Vectors the size of the
00205         // scroller needs to be recalculated.
00206         Dimension pref = getPreferredSize();
00207         Dimension size = getSize();        
00208         topIndex = this.noDebug.size() - (getSize().height / fontHeight);
00209         if(topIndex < 0){
00210             topIndex = 0;
00211         }
00212 
00213         int extent = size.height, max = pref.height;
00214         int value = Math.max(0, Math.min(scroller.getValue(), max - extent));
00215         scroller.setVisible(extent < max);
00216         scroller.setUnitIncrement(getUnitHeight());
00217         scroller.setBlockIncrement(extent - scroller.getUnitIncrement());
00218         scroller.setValues(value, extent, 0, max);
00219         scroller.setValue(max);   
00220     }
00221     
00229     public int logString(String stri){
00230         String str = stri.replaceAll("\t", "     ");
00231         if(str.length() <= MAXLINE){
00232             noDebug.add(str);
00233             if(DaqDriverTest.debug){
00234                 debug.add(str);
00235             }
00236         }
00237         else{
00238             for(int i = 0; i < str.length() / MAXLINE + 1; i++){
00239                 if(i == 0){
00240                     noDebug.add(str.substring(i * SSPanel.MAXLINE, 
00241                                               Math.min(SSPanel.MAXLINE*(i + 1),str.length())));
00242                     if(DaqDriverTest.debug){
00243                         debug.add(str.substring(i * SSPanel.MAXLINE, 
00244                                                 Math.min(SSPanel.MAXLINE*(i + 1),str.length())));
00245                     }       
00246                 }
00247                 else{
00248                     noDebug.add("     " + 
00249                                 str.substring(i * SSPanel.MAXLINE, 
00250                                               Math.min(SSPanel.MAXLINE*(i + 1),str.length())));
00251                     if(DaqDriverTest.debug){
00252                         debug.add("     " + 
00253                                   str.substring(i * SSPanel.MAXLINE, 
00254                                                 Math.min(SSPanel.MAXLINE*(i + 1),str.length())));
00255                     }       
00256                 }
00257             }            
00258         }
00259        
00260         // Everytime some data is log the scroller needs to be resized
00261         Dimension pref = getPreferredSize();
00262         Dimension size = getSize();        
00263         topIndex = this.data.size() - (getSize().height / fontHeight);
00264         if(topIndex < 0){
00265             topIndex = 0;
00266         }
00267         int extent = size.height, max = pref.height;
00268         int value = Math.max(0, Math.min(scroller.getValue(), max - extent));
00269         scroller.setVisible(extent < max);
00270         scroller.setUnitIncrement(getUnitHeight());
00271         scroller.setBlockIncrement(extent - scroller.getUnitIncrement());
00272         scroller.setValues(value, extent, 0, max);
00273         scroller.setValue(max);   
00274         if(DaqDriverTest.debug){
00275             pairs.put(new Integer(data.size() - 1), new Integer(debug.size() - 1));
00276         }
00277         return noDebug.size() - 1;
00278     }
00285     public int logDebugString(String str){
00286         if(str.length() <= MAXLINE){
00287             debug.add(str);
00288         }
00289         else{
00290             for(int i = 0; i < str.length() / MAXLINE + 1; i++){
00291                 if(i == 0){
00292                     debug.add(str.substring(i * SSPanel.MAXLINE, 
00293                                             Math.min(SSPanel.MAXLINE*(i + 1),str.length())));
00294                     
00295                 }
00296                 else{
00297                     debug.add("     " + str.substring(i * SSPanel.MAXLINE, 
00298                                                       Math.min(SSPanel.MAXLINE*(i + 1),str.length())));
00299                 }
00300             }            
00301         }
00302         // if the debugging information is being shown scroller needs
00303         // to be resize otherwise it does not.
00304         if(showDebug){
00305             Dimension pref = getPreferredSize();
00306             Dimension size = getSize();        
00307             topIndex = this.data.size() - (getSize().height / fontHeight);
00308             if(topIndex < 0){
00309                 topIndex = 0;
00310             }
00311             int extent = size.height, max = pref.height;
00312             int value = Math.max(0, Math.min(scroller.getValue(), max - extent));
00313             scroller.setVisible(extent < max);
00314             scroller.setUnitIncrement(getUnitHeight());
00315             scroller.setBlockIncrement(extent - scroller.getUnitIncrement());
00316             scroller.setValues(value, extent, 0, max);
00317 
00318             scroller.setValue(max);   
00319         } 
00320         return debug.size() - 1;
00321     }
00322     
00331     public void replaceLog(String str, int index){
00332         noDebug.setElementAt(str, index);
00333         if(DaqDriverTest.debug){
00334             if(pairs.containsKey(new Integer(index))){
00335                 debug.setElementAt(str, ((Integer)pairs.get(new Integer(index))).intValue());
00336             }
00337         }
00338     }
00339     
00346     public void showDebugging(){
00347         data = debug;
00348         showDebug = true;
00349         Dimension pref = getPreferredSize();
00350         Dimension size = getSize();        
00351         topIndex = this.data.size() - (getSize().height / fontHeight);
00352         if(topIndex < 0){
00353             topIndex = 0;
00354         }
00355         int extent = size.height, max = pref.height;
00356         int value = Math.max(0, Math.min(scroller.getValue(), max - extent));
00357         scroller.setVisible(extent < max);
00358         scroller.setUnitIncrement(getUnitHeight());
00359         scroller.setBlockIncrement(extent - scroller.getUnitIncrement());
00360         scroller.setValues(value, extent, 0, max);
00361         scroller.setValue(max);   
00362     }
00363     
00370     public void hideDebugging(){
00371         data = noDebug;
00372         showDebug = false;
00373         Dimension pref = getPreferredSize();
00374         Dimension size = getSize();        
00375         topIndex = this.data.size() - (getSize().height / fontHeight);
00376         if(topIndex < 0){
00377             topIndex = 0;
00378         }
00379         int extent = size.height, max = pref.height;
00380         int value = Math.max(0, Math.min(scroller.getValue(), max - extent));
00381         scroller.setVisible(extent < max);
00382         scroller.setUnitIncrement(getUnitHeight());
00383         scroller.setBlockIncrement(extent - scroller.getUnitIncrement());
00384         scroller.setValues(value, extent, 0, max);
00385         scroller.setValue(max);   
00386     }
00387 
00388 }

Generated on Mon Jul 28 13:59:24 2003 by doxygen 1.3.2