TableAsHtml.java

Go to the documentation of this file.
00001 package cedar.hepdata.webapp.components;
00002 
00003 import org.apache.tapestry.MarkupWriter;
00004 import org.apache.tapestry.annotations.*;
00005 import cedar.hepdata.model.*;
00006 import cedar.hepdata.webapp.pages.*;
00007 
00008 import java.text.*;
00009 
00010 public class TableAsHtml {
00011 
00012     @BeginRender
00013     void renderMessage(MarkupWriter writer) {
00014 
00015         if (_dataset == null && _yaxis != null) {
00016            _dataset = _yaxis.getDataset(); 
00017         }
00018 
00019         if (_dataset == null && _reaction != null) {
00020            _yaxis = _reaction.getYAxis();
00021            _dataset = _yaxis.getDataset(); 
00022         }
00023 
00024         if (_dataset != null) {
00025            
00026 //          int ya = _yaxis;
00027 //          writer.write("This is yaxis " + ya);
00028 //            YAxis y = _dataset.getYAxis(ya);
00029             int nx = _dataset.getXAxes().size();
00030             int npoints = 0;
00031             int npointsy = 0;
00032             for (XAxis x : _dataset.getXAxes()){npoints = x.getBins().size();}
00033             for (YAxis y : _dataset.getYAxes()){npointsy = y.getPoints().size();}
00034             if(npointsy > npoints) { npoints = npointsy; }
00035 
00036             writer.element("table border=1");
00037 
00038                 int nspan = nx+1;
00039 //              writer.element("tr");
00040 //                  writer.element("th colspan=" + nspan); 
00041 //                     writer.write("Dataset " + _dataset.getId() + " yaxis " + _yaxis.getId());
00042 //                  writer.end();
00043 //              writer.end();
00044 
00045 // Put in the dataset comments
00046 //                for ( String comment : _dataset.getComments() ){
00047 //                  writer.element("tr");
00048 //                     writer.element("th colspan=" + nspan);
00049 //                        writer.write(comment);
00050 //                     writer.end();
00051 //                  writer.end();
00052 //              }
00053 
00054 // first the reactions for this yaxis
00055 //                for ( Reaction reac : _yaxis.getReactions() ){
00056 //                    writer.element("tr");
00057 //                      writer.element("th colspan=" + nspan);
00058 //                            for ( Particle part : reac.getInitialState() ){
00059 //                              writer.write(part.getName() + " ");
00060 //                            }
00061 //                          writer.write("-->");
00062 //                            for ( Particle part : reac.getFinalState() ){
00063 //                              if (part.getMultRelation() != Relation.EQUALS) {writer.write( part.getMultRelation().toString() );}
00064 //                              if (part.getMultiplicity() > 1) {writer.write( part.getMultiplicity().toString() );}
00065 //                              writer.write(part.getName() + " ");
00066 //                          }
00067 //                              writer.end();
00068 //                  writer.end();
00069 //                }
00070 // next the observable
00071 //                writer.element("tr");
00072 //                  writer.element("th colspan=" + nspan); 
00073 //                      writer.write(_yaxis.getObservable());
00074 //                          writer.end();
00075 //              writer.end();
00076 
00077 // next the yaxis comments (non-numerical properties!)
00078                 for (String comment : _yaxis.getComments()){
00079                     writer.element("tr");
00080                         writer.element("th colspan=" + nspan); 
00081                             writer.write(comment);
00082                         writer.end();
00083                     writer.end();
00084                 }
00085 
00086 // next the properties
00087                 for ( Property prop: _yaxis.getProperties() ){
00088                     writer.element("tr");
00089                         writer.element("th colspan=" + nspan);
00090                             writer.write( prop.getName() + " : ");
00091                             if (prop.getLowValue() != null && prop.getHighValue() != null &&
00092                             prop.getLowValue().equals(prop.getHighValue())) {
00093                                 writer.write(prop.getLowValue().toString());
00094                             }
00095                             else{writer.write(prop.getLowValue().toString() + " TO " + prop.getHighValue().toString());}
00096                             if (!prop.getUnit().toString().equals("num")){
00097                                 writer.write(" " + prop.getUnit().toString());
00098                             }
00099                         writer.end();
00100                     writer.end();
00101                 }             
00102 // here for the headers
00103                 writer.element("tr");
00104                     for (XAxis x : _dataset.getXAxes()){
00105                         writer.element("th");
00106                             writer.write(x.getHeader());
00107                         writer.end();
00108                     }
00109                     writer.element("th");
00110                         writer.write(_yaxis.getHeader());
00111                     writer.end();
00112                 writer.end();
00113 
00114 // finally the numbers themselves
00115 // first the xaxes
00116                 Double f = null;
00117                 Double high = null;
00118                 Double low = null;
00119                 writer.element("tr");
00120                     for (int ix=1 ; ix<=nx; ix++){
00121                         XAxis xaxis = _dataset.getXAxis(ix);
00122                         writer.element("th");
00123                         writer.element("pre");
00124                         for (int ip=1 ; ip<=npoints; ip++){
00125                             for (Bin b : xaxis.getBins()){
00126                                 if(b.getId() == ip) {
00127                                     low = b.getLowValue();
00128                                     high = b.getHighValue();
00129                                     f = b.getFocus();
00130                                     if(low != null){
00131 //                                       writer.write(forms(low,8) + " TO " + forms(high,8)); 
00132                                          writer.write(low + " TO " + high); 
00133                                     }
00134                                     else{
00135                                        writer.write(forms(f,8)); 
00136                                     }
00137                                 }              
00138                             }          
00139                             writer.element("br");
00140                             writer.end();
00141                         }              
00142                         writer.end();
00143                         writer.end();
00144                     }    
00145 // next the yaxis
00146         
00147                     writer.element("th align='left'");
00148                     writer.element("pre");
00149                     for (int ip=1 ; ip<=npoints; ip++){
00150                         try {
00151                             Point pt2 = _yaxis.getPoint(ip);
00152 //                          writer.write(forms(pt2.getValue(),10)); 
00153                             writer.write( Double.toString(pt2.getValue()));
00154                             int nume = 0; 
00155                             for ( Uncertainty e : pt2.getErrors() ){
00156                                 nume += 1;
00157                                 if(nume > 1) writer.write(" (");
00158                                 if( e.getPlus().equals(e.getMinus()) ) {
00159 //                                  writer.write(" +- " + forms(e.getPlus(),10) );
00160                                     writer.write(" +- " + e.getPlus() );
00161                                 }
00162                                 else{
00163 //                                  writer.write(" +" + forms(e.getPlus(),10) + ",-" + forms(e.getMinus(),10) );
00164                                     writer.write(" +" + e.getPlus() + ",-" + e.getMinus() );
00165                                 }
00166                                 if ( e.getNormType() == ErrorNorm.PCT ) {
00167                                     writer.write(" (" + e.getNormType().toString() + ")");
00168                                 }
00169                                 if(nume > 1) writer.write(" )");
00170                             }
00171                             writer.element("br");
00172                             writer.end();
00173                         } 
00174                         catch (Exception e) {
00175                             writer.write("  -   ");
00176                             writer.element("br");
00177                             writer.end();
00178                         }
00179                     }
00180                     writer.end();
00181                     writer.end();
00182                 writer.end();
00183 //  end of the table
00184             writer.end();
00185         }
00186 
00187     }
00188     @Parameter(name="ds")
00189     private Dataset _dataset;
00190 
00191     @Parameter(name="yax")
00192     private YAxis _yaxis;
00193 
00194     @Parameter(name="re")
00195     private AxisReaction _reaction;
00196 
00197     @Parameter
00198     private String _foo = "wibble";
00199 
00200     @Parameter
00201     private int _bar = -1;
00202 
00203 
00205     public String forms(Double value){
00206         int width = 20;
00207         return this.forms(value,width);
00208         
00209     }
00210 
00211     public  String forms(Double value,int width){
00212         int position = width/2;
00213         return this.forms(value,width,position);
00214     }
00215 
00216     public String forms(Double value,int width, int position){
00217         String form = "##############.###############################";
00218         DecimalFormat myFormatter = new DecimalFormat(form);
00219         String sval = "cannot format this!!";
00220         if( value instanceof Double ){
00221            sval = myFormatter.format(value);
00222         }
00223         String sout = " ";
00224         for (int i=1 ; i<width; i++){
00225             sout = sout.concat(" ");
00226         }
00227         int lenval = sval.length(); 
00228         int dotpos = sval.indexOf(".");
00229         String before = sval;
00230         String after  = "";
00231         if(dotpos >= 0){
00232             before = sval.substring(0,dotpos);
00233             after  = sval.substring(dotpos+1,lenval);
00234         }
00235         int lenbefore = before.length();
00236         int lenafter = after.length();
00237         int lenout = sout.length();
00238         StringBuffer bout = new StringBuffer(sout);
00239         int lstart = 1;
00240         int l1 = lstart+lenbefore;
00241         int l2 = l1;
00242         int l3 = l2+1;
00243         int l4 = l3+lenafter;
00244         
00245         bout.replace(lstart,l1,before);
00246         bout.replace(l2,l2,".");
00247         bout.replace(l3,l4,after);
00248         sout = bout.toString();
00249         
00250         return sout;
00251     } 
00252 
00253 
00254 }

Generated on Tue Apr 21 15:55:00 2009 for HepData Web servlets by  doxygen 1.5.5