Esempio n. 1
0
  public void run() {

    Page page = null;
    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    UDIGEditorInput editorInput =
        (UDIGEditorInput) workbenchWindow.getActivePage().getActiveEditor().getEditorInput();
    if (editorInput instanceof PageEditorInput) {
      page = (Page) ((PageEditorInput) editorInput).getProjectElement();
    }
    if (page == null) {
      throw new RuntimeException(Messages.PrintAction_pageError);
    }

    Dimension pSize = page.getSize();

    float factor = (float) pSize.height / (float) pSize.width;
    float xPlus = 10f;
    float yPlus = xPlus * factor;
    int w = pSize.width + (int) xPlus;
    int h = pSize.height + (int) yPlus;
    page.setSize(new Dimension(w, h));

    // IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    // IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
    // PageEditor pageEditor = (PageEditor) activeEditor;
    // ZoomManager zoomManager = (ZoomManager) pageEditor.getAdapter(ZoomManager.class);
    // double next = zoomManager.getNextZoomLevel();
    // zoomManager.setZoom(next);
  }
Esempio n. 2
0
 private float getScaleFactor() {
   if (Float.isNaN(scaleFactor)) {
     // try to get it from the page
     Page page = getBox().getPage();
     if (page != null) {
       scaleFactor = (float) page.getSize().width / (float) page.getPaperSize().height;
     }
   }
   return scaleFactor;
 }
  /**
   * Iterates through the Page's Boxes, drawing to the provided Graphics object
   *
   * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
   */
  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
      throws PrinterException {

    if (pageIndex >= 1) {
      return Printable.NO_SUCH_PAGE;
    }

    Graphics2D graphics2d = (Graphics2D) graphics;

    AffineTransform at = graphics2d.getTransform();
    double dpi = at.getScaleX() * 72;

    if (PrintingPlugin.isDebugging(TRACE_PRINTING)) {
      PrintingPlugin.log("Printing page " + pageIndex, null); // $NON-NLS-1$
      System.out.println("PageFormat: " + pageFormat); // $NON-NLS-1$
      System.out.println("PageFormat height: " + pageFormat.getHeight()); // $NON-NLS-1$
      System.out.println("PageFormat width: " + pageFormat.getWidth()); // $NON-NLS-1$
      System.out.println(
          "PageFormat imageableX,Y "
              + pageFormat.getImageableX()
              + ", "
              + pageFormat.getImageableY()); // $NON-NLS-1$ //$NON-NLS-2$
      System.out.println(
          "PageFormat imageable height: " + pageFormat.getImageableHeight()); // $NON-NLS-1$
      System.out.println(
          "PageFormat imageable width: " + pageFormat.getImageableWidth()); // $NON-NLS-1$
      System.out.println(
          "PageFormat orientation (LANDSCAPE="
              + PageFormat.LANDSCAPE
              + ", PORTRAIT="
              + PageFormat.PORTRAIT
              + "): "
              + pageFormat.getOrientation()); // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

      System.out.println("Graphics: clip bounds: " + graphics2d.getClipBounds()); // $NON-NLS-1$
      System.out.println("Transform: scaleX: " + at.getScaleX()); // $NON-NLS-1$
      System.out.println("Transform: scaleY: " + at.getScaleY()); // $NON-NLS-1$
      System.out.println("DPI?? : " + dpi); // $NON-NLS-1$
    }

    graphics2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

    Iterator<Box> iter = diagram.getBoxes().iterator();
    while (iter.hasNext()) {

      Box box = iter.next();
      graphics2d =
          (Graphics2D)
              graphics.create(
                  box.getLocation().x,
                  box.getLocation().y,
                  box.getSize().width,
                  box.getSize().height);

      box.getBoxPrinter().draw(graphics2d, monitor);
    }

    return Printable.PAGE_EXISTS;
  }
  /**
   * Populates the templates two boxes with a title and map
   *
   * @param page the parent(owner) page
   * @param map the Map to be drawn
   */
  public void init(Page page, Map map) {
    this.page = page;
    com.lowagie.text.Rectangle paperRectangle = getPaperSize();
    Dimension paperSize =
        new Dimension((int) paperRectangle.getWidth(), (int) paperRectangle.getHeight());
    // set the requested papersize
    page.setPaperSize(paperSize);
    // then apply the ratio of the papersize also to the page size.
    setPageSizeFromPaperSize(page, paperSize);

    float scaleFactor = (float) page.getSize().width / (float) page.getPaperSize().height;

    int height = page.getSize().height;
    int width = page.getSize().width;

    int xPos = getPercentagePieceOf(width, LEFT_MARGIN_PERCENT);
    int yPos = getPercentagePieceOf(height, UPPER_MARGIN_PERCENT);
    int w = getPercentagePieceOf(width, TITLE_WIDTH_PERCENT);
    int h = getPercentagePieceOf(height, TITLE_HEIGHT_PERCENT);
    // the base font size is good for the A4 size, scale every other proportional
    float scaledSize = (float) BASEFONT_SIZE * (float) paperSize.height / PageSize.A4.getHeight();
    // float scaledFontSize = scaleValue(page, paperSize, scaledSize);
    addLabelBox(formatName(map.getName()), xPos, yPos, w, h, (int) scaledSize, scaleFactor);

    xPos = getPercentagePieceOf(width, LEFT_MARGIN_PERCENT);
    yPos = getPercentagePieceOf(height, UPPER_MARGIN_PERCENT + TITLE_HEIGHT_PERCENT);
    w = getPercentagePieceOf(width, MAP_WIDTH_PERCENT);
    h = getPercentagePieceOf(height, MAP_HEIGHT_PERCENT);
    addMapBox(map, xPos, yPos, w, h, paperSize);

    // xPos = getPercentagePieceOf(width, 100f - RIGHT_MARGIN_PERCENT - SPACING_PERCENT * 3f
    // - LEGEND_WIDTH_PERCENT);
    // yPos = getPercentagePieceOf(height, 100f - BOTTOM_MARGIN_PERCENT - SPACING_PERCENT * 3f
    // - LEGEND_HEIGHT_PERCENT);
    // w = getPercentagePieceOf(width, LEGEND_WIDTH_PERCENT);
    // h = getPercentagePieceOf(height, LEGEND_HEIGHT_PERCENT);
    // addLegendBox(xPos, yPos, w, h);

    xPos = getPercentagePieceOf(width, LEFT_MARGIN_PERCENT + SPACING_PERCENT * 2f);
    yPos =
        getPercentagePieceOf(
            height, 100f - BOTTOM_MARGIN_PERCENT - SPACING_PERCENT * 3f - SCALE_HEIGHT_PERCENT);
    w = getPercentagePieceOf(width, SCALE_WIDTH_PERCENT);
    h = getPercentagePieceOf(height, SCALE_HEIGHT_PERCENT);
    addScale(xPos, yPos, w, h);
  }
  /**
   * Creates a page based on the template selected in the wizard **Note: this function may swap the
   * width and height if the template prefers different page orientation.
   *
   * @return a page
   */
  protected Page makePage(Rectangle pageSize, Document doc, Template template) {

    Map mapOnlyRasterLayers = null;
    Map mapNoRasterLayers = null;

    // **Note: the iText API doesn't render rasters at a high enough resolution if
    // they are written to the PDF via graphics2d.  To work around this problem, I
    // create two copies of the map: one with only the raster layers, and one with
    // everything else.
    // The "everything else" map gets drawn by a graphics2d.  The other layer must be
    // rasterized and inserted into the PDF via iText's API.

    // make one copy of the map with no raster layers
    mapNoRasterLayers = (Map) ApplicationGIS.copyMap(map);
    List<Layer> layersNoRasters = mapNoRasterLayers.getLayersInternal();
    List<Layer> toRemove = new ArrayList<Layer>();
    for (Layer layer : layersNoRasters) {
      for (IGeoResource resource : layer.getGeoResources()) {
        if (resource.canResolve(GridCoverageReader.class)) {
          toRemove.add(layer);
        }
      }
    }
    layersNoRasters.removeAll(toRemove);

    // adjust scale
    double currentViewportScaleDenom = map.getViewportModel().getScaleDenominator();
    if (currentViewportScaleDenom == -1)
      throw new IllegalStateException(
          "no scale denominator is available from the viewport model"); //$NON-NLS-1$

    if (page1.getScaleOption() == PrintWizardPage1.CUSTOM_MAP_SCALE) {
      float customScale = page1.getCustomScale();
      template.setMapScaleHint(customScale);
    } else if (page1.getScaleOption() == PrintWizardPage1.CURRENT_MAP_SCALE) {
      template.setMapScaleHint(currentViewportScaleDenom);
    } else if (page1.getScaleOption() == PrintWizardPage1.ZOOM_TO_SELECTION) {
      template.setZoomToSelectionHint(true);
      template.setMapScaleHint(currentViewportScaleDenom);
    }

    // 3. make the page itself
    Page page = ModelFactory.eINSTANCE.createPage();
    page.setSize(new Dimension((int) pageSize.getWidth(), (int) pageSize.getHeight()));

    // page name stuff not required, because this page will just get discarded
    MessageFormat formatter =
        new MessageFormat(Messages.CreatePageAction_newPageName, Locale.getDefault());
    if (page.getName() == null || page.getName().length() == 0) {
      page.setName(formatter.format(new Object[] {mapNoRasterLayers.getName()}));
    }

    template.init(page, mapNoRasterLayers);

    if (page1.getRasterEnabled()) {
      // make another copy with only raster layers
      mapOnlyRasterLayers = (Map) ApplicationGIS.copyMap(map);
      List<Layer> layersOnlyRasters = mapOnlyRasterLayers.getLayersInternal();
      List<Layer> toRemove2 = new ArrayList<Layer>();
      for (Layer layer : layersOnlyRasters) {
        for (IGeoResource resource : layer.getGeoResources()) {
          if (!resource.canResolve(GridCoverageReader.class)) {
            toRemove2.add(layer);
          }
        }
      }
      layersOnlyRasters.removeAll(toRemove2);

      // set bounds to match the other map
      SetViewportBBoxCommand cmdBbox =
          new SetViewportBBoxCommand(mapNoRasterLayers.getViewportModel().getBounds());
      mapOnlyRasterLayers.sendCommandSync(cmdBbox);

      if (layersNoRasters.size() > 0) {
        writeRasterLayersOnlyToDocument(
            mapOnlyRasterLayers,
            template.getMapBounds(),
            doc,
            page.getSize(), /*currentViewportScaleDenom*/
            mapNoRasterLayers.getViewportModel().getScaleDenominator());
      }
    }

    // copy the boxes from the template into the page
    Iterator<Box> iter = template.iterator();
    while (iter.hasNext()) {
      page.getBoxes().add(iter.next());
    }
    return page;

    // TODO Throw some sort of exception if the page can't be created

  }