|
[HepData-svn] r1388 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/componentsblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed Sep 1 10:45:40 BST 2010
Author: whalley Date: Wed Sep 1 10:45:40 2010 New Revision: 1388 Log: display of table without plot Added: trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/components/DatasetAsHtmlWithoutPlot.java Added: trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/components/DatasetAsHtmlWithoutPlot.java ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/components/DatasetAsHtmlWithoutPlot.java Wed Sep 1 10:45:40 2010 (r1388) @@ -0,0 +1,420 @@ +package cedar.hepdata.webapp.components; + +import org.apache.tapestry5.MarkupWriter; +import org.apache.tapestry5.annotations.BeginRender; +import org.apache.tapestry5.annotations.Parameter; +import org.apache.tapestry5.ioc.annotations.*; +import org.apache.tapestry5.services.*; + +import cedar.hepdata.model.*; +import cedar.hepdata.util.*; +import cedar.hepdata.webapp.pages.*; + +import com.Ostermiller.util.SignificantFigures; + +import java.text.*; +import java.util.*; + + + +public class DatasetAsHtmlWithoutPlot { + + @Inject + private RequestGlobals _reqGlobals; + + @Parameter + private String _format; + public String getFormat() { return _format; } + public void getFormat(String f) { _format = f; } + + + @BeginRender + void renderMessage(MarkupWriter writer) { + + // Output the table comment(CT) and location(TFP) + writer.element("p"); + writer.attributes("class", "datasetcomments"); + for (String comment : _dataset.getComments()) { + writer.element("br"); writer.end(); + writer.write(comment); + } + writer.end(); //p + + // Determine the number of axes and points in the dataset + final int nx = _dataset.getXAxes().size(); // number of xaxes in the dataset + final int ny = _dataset.getYAxes().size(); // number of yaxes in the dataset + int npoints = _dataset.getNumPoints(); + int binIdMax = _dataset.getMaxPointId(); + + // Determine the number of axis comments and properties for this dataset + int ncomm = 0; + int nprop = 0; + if (_dataset.getYAxes().size() > 0) { + ncomm = _dataset.getYAxes().first().getComments().size(); + nprop = _dataset.getYAxes().first().getProperties().size(); + } + // Begin the actual data table... + writer.element("table"); + writer.attributes("class", "dataset"); + + // y-axis comments (i.e. non-numerical properties + // new version has these added in with the properties - below + int nmatch = 0; + + // Next the numerical properties new version + + String[][] store = new String[ny][nprop+ncomm]; + + int numy = -1; + for(YAxis y : _dataset.getYAxes()) { + numy++; + int j=-1; + List <String> storelist = new ArrayList(); + // first add the comment elements (strings) + for (String comment : y.getComments()){storelist.add(comment);} + for (Property property : y.getProperties()) { + j++; + String tempstr = property.getName().toString(); + if(!tempstr.startsWith("sqrts")){ + tempstr = tempstr.concat(" : "); + if (property.getFocus() != null){ + tempstr = tempstr.concat(property.getFocus().toString()); + } + if (property.getLowValue() != null && property.getHighValue() != null && + property.getLowValue().equals(property.getHighValue())) { + tempstr = tempstr.concat(property.getLowValue().toString()); + } else { + if(property.getFocus() != null) { tempstr = tempstr.concat(" ("); } + tempstr = tempstr.concat(property.getLowValue().toString()); + tempstr = tempstr.concat("-"); + tempstr = tempstr.concat(property.getHighValue().toString()); + if(property.getFocus() != null) { tempstr = tempstr.concat(")"); } + } + if (!property.getUnit().isDimensionless()) { + tempstr = tempstr.concat(" "); + tempstr = tempstr.concat(property.getUnit().toString()); + } + storelist.add(tempstr); + } + + } + Collections.sort(storelist); + for (int i=0; i<storelist.size(); i++){ + store[numy][i] = storelist.get(i); + } + } + + for (int i=0; i<nprop+ncomm; i++){ + if(store[0][i] != null){ + writer.element("tr"); + writer.element("td"); + writer.attributes("colspan", nx); + writer.writeRaw(" "); + writer.end(); + +// this is the actual output +// first check is same across row + Boolean same = true; + for (int nmy=0; nmy<ny-1; nmy++){ if(!store[nmy][i].equals(store[nmy+1][i])){ same = false; } } + if(same) { + writer.element("th"); + writer.attributes("colspan",ny); + writer.attributes("class","multiple"); + writer.write(store[0][i]); + writer.end(); + } else { + for (int nmy=0; nmy<ny; nmy++){ + writer.element("th"); + writer.attributes("class","multiple"); + writer.write( store[nmy][i]); + writer.end(); + } + } + writer.end(); //tr + } + } + + // Headers + writer.element("tr"); + writer.attributes("class", "xyheaders"); + + // X headers + for (XAxis x : _dataset.getXAxes()) { + writer.element("th"); + writer.attributes("class", "xheader"); + writer.write(x.getHeader()); + writer.end(); //th + } + + // Y headers (trying to merge if possible) + String yhead = ""; + if (_dataset.getYAxes().size() > 0) { + yhead = _dataset.getYAxis(1).getHeader(); + } + nmatch = 0; + for (YAxis y : _dataset.getYAxes()){ + if ( y.getHeader().equals(yhead) ) nmatch += 1; + } + if (nmatch == ny) { + writer.element("th"); + writer.attributes("class", "yheader"); + writer.attributes("colspan", ny); + writer.write(yhead); + writer.end(); //th + } else { + for (YAxis y : _dataset.getYAxes()) { + writer.element("th"); + writer.attributes("class", "yheader"); + writer.write(y.getHeader()); + writer.end(); //th + } + } + writer.end(); //tr + + + if(_dataset.getNumPoints() == 0){ + writer.element("th"); + writer.attributes("colspan",ny+nx); + writer.attributes("align", "center"); + writer.write("No data is encoded for this table"); + writer.end(); + } + if (getFormat().equals("full") && _dataset.getNumPoints() != 0) { + + // Finally the numbers themselves + for (int ip = 1 ; ip <= npoints; ip++) { + writer.element("tr"); + + // First the xaxes + for (int ix = 1 ; ix <= nx; ix++) { + XAxis x = _dataset.getXAxis(ix); + + // Work out x-axis formatting + /// @todo Do this formatting stuff properly with SignificantFigures as for y-values + int lwidth = 0; + int hwidth = 0; + int fwidth = 0; + // Format as "<focus> (bin: <low>--<high>)" if showmean is true == + // at least one bin has a focus which significantly differs from the mean + boolean asymmfocus = false; + for (Bin b : x.getBins()) { + if (b.getLowValue() != null && b.getHighValue() != null && b.getFocus() != null) { + double diff = b.getFocus() - (b.getLowValue() + b.getHighValue()) / 2.0; + if (Math.abs(diff/b.getFocus()) > 1E-6) { + asymmfocus = true; + } + } + if (b.getLowValue() != null && b.getLowValue().toString().length() > lwidth) { + lwidth = b.getLowValue().toString().length(); + } + if (b.getHighValue() != null && b.getHighValue().toString().length() > hwidth) { + hwidth = b.getHighValue().toString().length(); + } + if (b.getFocus() != null && b.getFocus().toString().length() > fwidth) { + fwidth = b.getFocus().toString().length(); + } + } + + Double f = null; + Double high = null; + Double low = null; + Bin b = x.getBin(ip); + writer.element("td align='center'"); + writer.attributes("class", "xval"); + if (b != null) { + if (b.getDescription() == null){ + if (b.getRelation() == Relation.EQUALS) { + low = b.getLowValue(); + high = b.getHighValue(); + f = b.getFocus(); + String f_val = Formats.forms(f, fwidth); + if (low != null && high != null) { + String f_low = Formats.forms(low, lwidth); + String f_high = Formats.forms(high,hwidth); + Double width = high - low; + if (f != null && asymmfocus) { + writer.writeRaw(f_val); + writer.writeRaw(" (bin: "); + writer.writeRaw(f_low + "–" + f_high); + writer.writeRaw(")"); + } else if (f != null && Math.abs(width/f) < 1E-6) { + writer.write(f_val); + } else { + writer.writeRaw(f_low + "–" + f_high); + } + } else { + writer.write(f_val); + } + } else { + writer.write(b.getRelation().toString()); + if (b.getLowValue() != null) { + writer.write(b.getLowValue().toString()); + } else if (b.getHighValue() != null) { + writer.write(b.getHighValue().toString()); + } + } + } else { + writer.write(b.getDescription()); + } + } + writer.end(); //td + } + + + // Now the y-axes + for (int iy = 1; iy <= ny; ++iy) { + YAxis y = _dataset.getYAxis(iy); + Point pt2 = y.getPoint(ip); + writer.element("td align='center'"); + writer.attributes("class", "yval"); + try { + /// DON'T MODIFY ANY OF THIS AT THE MOMENT: I'M MOVING THE FORMATTING TO AN EXTERNAL CLASS! + + if (pt2.getRelation() != Relation.EQUALS) { + writer.write(pt2.getRelation().toString()); + } + SignificantFigures val = new SignificantFigures(pt2.getValue()); + int lsd = val.getLSD(); + if (pt2.getValue().toString().endsWith("0") || pt2.getValue().toString().indexOf("0E") > 0) { + lsd += 1; + } + + if (pt2.getRelation() == Relation.EQUALS) { + for (PointError e : pt2.getErrors()) { + if (e.getNormType() != ErrorNorm.PCT) { + SignificantFigures vale1 = new SignificantFigures(e.getPlus()); + lsd = Math.min(lsd, vale1.getLSD()); + if (e.getPlus().toString().endsWith("0") || e.getPlus().toString().indexOf("0E") > 0) { +// lsd += 1; + } + SignificantFigures vale2 = new SignificantFigures(e.getMinus()); + lsd = Math.min(lsd, vale2.getLSD()); + if( e.getMinus().toString().endsWith("0") || e.getMinus().toString().indexOf("0E") > 0 ) { +// lsd += 1; + } + } + } + } + int msd = val.getMSD(); + if (msd <= 0) msd = 1; + writer.write(Formats.forms(pt2.getValue(), msd, -lsd)); + if (pt2.getRelation() == Relation.EQUALS) { + for (PointError e : pt2.getErrors()) { + String stype = e.getSourceType().toShortString(); + SignificantFigures vale = null; + if (e.isSymmetric()) { + vale = new SignificantFigures(e.getPlus()); + writer.writeRaw(" ± "); // chosen to allow breaks *before* the +- sign only + if (e.getNormType() == ErrorNorm.PCT) { + lsd = vale.getLSD(); + } + msd = vale.getMSD(); + if (msd <= 0) msd = 1; + writer.write(Formats.forms(e.getPlus(), msd, -lsd)); + writer.write(e.getNormType().toSymbol()); + } else { + vale = new SignificantFigures(e.getPlus()); + writer.write("+"); + if (e.getNormType() == ErrorNorm.PCT) { + lsd = vale.getLSD(); + } + msd = vale.getMSD(); + if (msd <= 0) msd = 1; + writer.write(Formats.forms(e.getPlus(), msd, -lsd)); + writer.write(e.getNormType().toSymbol()); + vale = new SignificantFigures(e.getMinus()); + writer.writeRaw(",-"); + if (e.getNormType() == ErrorNorm.PCT) { + lsd = vale.getLSD(); + } + msd = vale.getMSD(); + if (msd <= 0) msd = 1; + writer.write(Formats.forms(e.getMinus(), msd, -lsd)); + writer.write(e.getNormType().toSymbol()); + } + if(!stype.equals("?")) writer.writeRaw(" (" + stype + ") "); + } + } + } + catch (Exception e) { + writer.writeRaw("–"); + } + writer.end(); //td + } + + writer.end(); //tr + } + + + } // end "full format only" + + writer.end(); //table + + writer.element("input"); + writer.attributes("type", "hidden"); + writer.attributes("name", "string"); + writer.attributes("value", getSearchString()); + writer.end(); + } + + + @Parameter(name="dataset") + private Dataset _dataset; + + + public String forms(Double value) { + int width = 20; + return this.forms(value,width); + } + + + public String forms(Double value, int width) { + int position = width/2; + return this.forms(value,width,position); + } + + + public String forms(Double value, int width, int position) { + String form = "##############.###############################"; + DecimalFormat myFormatter = new DecimalFormat(form); + String sval = "Cannot format this!!"; + if( value instanceof Double ){ + sval = myFormatter.format(value); + } + String sout = " "; + for (int i = 1 ; i < width; ++i) { + sout = sout.concat(" "); + } + int lenval = sval.length(); + int dotpos = sval.indexOf("."); + String before = sval; + String after = ""; + if (dotpos >= 0) { + before = sval.substring(0, dotpos); + after = sval.substring(dotpos+1, lenval); + } + int lenbefore = before.length(); + int lenafter = after.length(); + int lenout = sout.length(); + StringBuffer bout = new StringBuffer(sout); + int lstart = 1; + int l1 = lstart + lenbefore; + int l2 = l1; + int l3 = l2 + 1; + int l4 = l3 + lenafter; + + bout.replace(lstart, l1,before); + bout.replace(l2, l2, "."); + bout.replace(l3, l4, after); + sout = bout.toString(); + + return sout; + } + + + private String getSearchString() { + return _reqGlobals.getRequest().getParameter("string"); + } + + +}
More information about the HepData-svn mailing list |