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
00027
00028
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
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
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
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
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
00115
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
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
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
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
00160 writer.write(" +- " + e.getPlus() );
00161 }
00162 else{
00163
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
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 }