예제 #1
0
 /**
  * Gets a temporary formatter for a zoneless date time. The same formatter is returned on
  * subsequent calls.
  *
  * @param format a {@link java.text.SimpleDateFormat} format string
  */
 protected DateFormat getFormatter(String format) {
   if ((tempFormatter != null) && lastFormat.equals(format)) {
     return tempFormatter;
   }
   tempFormatter = DateTimeUtil.newDateFormat(format);
   tempFormatter.setTimeZone(DateTimeUtil.gmtZone);
   lastFormat = format;
   return tempFormatter;
 }
예제 #2
0
 static {
   String pattern = "EEE, dd MMM yyyy HH:mm:ss zzz";
   tz = TimeZone.getTimeZone("GMT");
   df = new SimpleDateFormat(pattern, Locale.US);
   df.setTimeZone(tz);
 }
예제 #3
0
파일: MessageTag.java 프로젝트: koem/Zimbra
  public int doEndTag() throws JspException {
    String pattern = this.pattern;
    ResourceBundle bundle = null;

    // get pattern from bundle
    if (pattern == null) {
      String prefix = null;

      bundle = this.bundle;
      if (bundle == null) {
        BundleTag bundleTag = (BundleTag) findAncestorWithClass(this, BundleTag.class);
        if (bundleTag != null) {
          bundle = bundleTag.getBundle();
          prefix = bundleTag.getPrefix();
        }
      }
      if (bundle == null) {
        bundle = I18nUtil.findBundle(pageContext);
      }

      // get message
      try {
        pattern = bundle.getString(prefix != null ? prefix + this.key : this.key);
      } catch (Exception e) {
        pattern = "???" + this.key + "???";
      }
    }

    // format message
    String message = pattern;
    if ((this.pattern != null || bundle != null) && this.params.size() != 0) {
      // set timezone on formatters
      TimeZone timeZone = null;
      TimeZoneTag timeZoneTag = (TimeZoneTag) findAncestorWithClass(this, TimeZoneTag.class);
      if (timeZoneTag != null) timeZone = timeZoneTag.getTimeZone();
      if (timeZone == null) timeZone = I18nUtil.findTimeZone(pageContext);

      MessageFormat formatter = new MessageFormat(pattern, I18nUtil.findLocale(pageContext));
      for (Format format : formatter.getFormatsByArgumentIndex()) {
        if (format != null && format instanceof DateFormat) {
          ((DateFormat) format).setTimeZone(timeZone);
        }
      }

      // now format!
      message = formatter.format(getParams());
    }

    // output text
    if (this.var == null) {
      try {
        pageContext.getOut().print(message);
      } catch (IOException e) {
        throw new JspException(e);
      }
    }

    // store result
    else {
      pageContext.setAttribute(this.var, message, this.scope);
    }

    // clear old state
    this.pattern = null;
    this.bundle = null;
    this.key = null;
    this.var = null;
    this.scope = PageContext.PAGE_SCOPE;

    // continue with page
    return EVAL_PAGE;
  }
예제 #4
0
  /**
   * The constructor for this class has a bunch of arguments: The frame argument is required for all
   * printing in Java. The jobname appears left justified at the top of each printed page. The font
   * size is specified in points, as on-screen font sizes are. The margins are specified in inches
   * (or fractions of inches).
   */
  public HardcopyWriter(
      Frame frame,
      String jobname,
      int fontsize,
      double leftmargin,
      double rightmargin,
      double topmargin,
      double bottommargin)
      throws HardcopyWriter.PrintCanceledException {
    // Get the PrintJob object with which we'll do all the printing.
    // The call is synchronized on the static printprops object, which
    // means that only one print dialog can be popped up at a time.
    // If the user clicks Cancel in the print dialog, throw an exception.
    Toolkit toolkit = frame.getToolkit(); // get Toolkit from Frame
    synchronized (printprops) {
      job = toolkit.getPrintJob(frame, jobname, printprops);
    }
    if (job == null) throw new PrintCanceledException("User cancelled print request");

    pagesize = job.getPageDimension(); // query the page size
    pagedpi = job.getPageResolution(); // query the page resolution

    // Bug Workaround:
    // On windows, getPageDimension() and getPageResolution don't work, so
    // we've got to fake them.
    if (System.getProperty("os.name").regionMatches(true, 0, "windows", 0, 7)) {
      // Use screen dpi, which is what the PrintJob tries to emulate, anyway
      pagedpi = toolkit.getScreenResolution();
      System.out.println(pagedpi);
      // Assume a 8.5" x 11" page size.  A4 paper users have to change this.
      pagesize = new Dimension((int) (8.5 * pagedpi), 11 * pagedpi);
      System.out.println(pagesize);
      // We also have to adjust the fontsize.  It is specified in points,
      // (1 point = 1/72 of an inch) but Windows measures it in pixels.
      fontsize = fontsize * pagedpi / 72;
      System.out.println(fontsize);
      System.out.flush();
    }

    // Compute coordinates of the upper-left corner of the page.
    // I.e. the coordinates of (leftmargin, topmargin).  Also compute
    // the width and height inside of the margins.
    x0 = (int) (leftmargin * pagedpi);
    y0 = (int) (topmargin * pagedpi);
    width = pagesize.width - (int) ((leftmargin + rightmargin) * pagedpi);
    height = pagesize.height - (int) ((topmargin + bottommargin) * pagedpi);

    // Get body font and font size
    font = new Font("Monospaced", Font.PLAIN, fontsize);
    metrics = toolkit.getFontMetrics(font);
    lineheight = metrics.getHeight();
    lineascent = metrics.getAscent();
    charwidth = metrics.charWidth('0'); // Assumes a monospaced font!

    // Now compute columns and lines will fit inside the margins
    chars_per_line = width / charwidth;
    lines_per_page = height / lineheight;

    // Get header font information
    // And compute baseline of page header: 1/8" above the top margin
    headerfont = new Font("SansSerif", Font.ITALIC, fontsize);
    headermetrics = toolkit.getFontMetrics(headerfont);
    headery = y0 - (int) (0.125 * pagedpi) - headermetrics.getHeight() + headermetrics.getAscent();

    // Compute the date/time string to display in the page header
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
    df.setTimeZone(TimeZone.getDefault());
    time = df.format(new Date());

    this.jobname = jobname; // save name
    this.fontsize = fontsize; // save font size
  }