Пример #1
0
 private void setPreviewScale(float scale) {
   preview.setFitVertical(false);
   preview.setFitHorizontal(false);
   preview.setScale(scale);
   rememberScrollingPosition();
   updatePreviewSize();
   restoreScrollingPosition();
 }
  /**
   * Executes the GridPrint example.
   *
   * @param args the command line arguments.
   */
  public static void main(String[] args) {
    final Display display = new Display();

    Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setText("GridPrintVerticalAlignmentExample.java");
    shell.setLayout(new GridLayout());
    shell.setSize(600, 800);

    final PrintJob job = new PrintJob("GridPrintVerticalAlignmentExample.java", createPrint());

    Composite buttonPanel = new Composite(shell, SWT.NONE);
    buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    buttonPanel.setLayout(new RowLayout(SWT.HORIZONTAL));

    final PrintPreview preview = new PrintPreview(shell, SWT.BORDER);

    Button prev = new Button(buttonPanel, SWT.PUSH);
    prev.setText("<< Prev");
    prev.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            preview.setPageIndex(Math.max(preview.getPageIndex() - 1, 0));
          }
        });

    Button next = new Button(buttonPanel, SWT.PUSH);
    next.setText("Next >>");
    next.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            preview.setPageIndex(Math.min(preview.getPageIndex() + 1, preview.getPageCount() - 1));
          }
        });

    Button print = new Button(buttonPanel, SWT.PUSH);
    print.setText("Print");
    print.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            PaperClips.print(job, new PrinterData());
          }
        });

    preview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    preview.setFitHorizontal(true);
    preview.setFitVertical(true);
    preview.setPrintJob(job);

    shell.open();

    while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep();

    display.dispose();
  }
Пример #3
0
  public Control createControl(Composite parent) {
    Composite shell = new Composite(parent, SWT.None);

    final PrintJob job = new PrintJob("GridPrintVerticalAlignmentExample.java", createPrint());
    shell.setLayout(new org.eclipse.swt.layout.GridLayout());

    Composite buttonPanel = new Composite(shell, SWT.NONE);
    buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    buttonPanel.setLayout(new RowLayout(SWT.HORIZONTAL));

    final PrintPreview preview = new PrintPreview(shell, SWT.BORDER);

    Button prev = new Button(buttonPanel, SWT.PUSH);
    prev.setText("<< Prev");
    prev.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            preview.setPageIndex(Math.max(preview.getPageIndex() - 1, 0));
          }
        });

    Button next = new Button(buttonPanel, SWT.PUSH);
    next.setText("Next >>");
    next.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            preview.setPageIndex(Math.min(preview.getPageIndex() + 1, preview.getPageCount() - 1));
          }
        });

    Button print = new Button(buttonPanel, SWT.PUSH);
    print.setText("Print");
    print.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            PaperClips.print(job, new PrinterData());
          }
        });

    preview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    preview.setFitHorizontal(true);
    preview.setFitVertical(true);
    preview.setPrintJob(job);
    return shell;
  }
Пример #4
0
 protected void updateJob() {
   printJob = new PrintJob("Default Print job", print.createPrinter());
   preview.setPrintJob(printJob);
   forgetScrollingPosition();
   updatePreviewSize();
   updatePageNumber();
 }
Пример #5
0
 private void restoreScrollingPosition() {
   if (scrollingPosition != null) {
     Point size = preview.getSize();
     scroll.setOrigin(
         (int) Math.round(scrollingPosition[0] * size.x),
         (int) Math.round(scrollingPosition[1] * size.y));
   }
 }
Пример #6
0
 private void updatePageNumber() {
   int pageIndex = preview.getPageIndex();
   int pageCount = preview.getPageCount();
   int visiblePageCount = preview.getHorizontalPageCount() * preview.getVerticalPageCount();
   String text =
       (visiblePageCount > 1
               ? "Pages "
                   + (pageIndex + 1)
                   + "-"
                   + Math.min(pageCount, pageIndex + visiblePageCount)
               : "Page " + (pageIndex + 1))
           + " of "
           + pageCount;
   pageNumber.setText(text);
   previousPage.setEnabled(pageIndex > 0);
   nextPage.setEnabled(pageIndex < pageCount - visiblePageCount);
   shell.layout(new Control[] {pageNumber});
 }
Пример #7
0
 private void rememberScrollingPosition() {
   Point size = preview.getSize();
   if (size.x == 0 || size.y == 0) {
     forgetScrollingPosition();
   } else if (scrollingPosition == null) {
     Point origin = scroll.getOrigin();
     scrollingPosition =
         new double[] {(double) origin.x / (double) size.x, (double) origin.y / (double) size.y};
   }
 }
Пример #8
0
 private void updatePreviewSize() {
   Point minSize;
   Rectangle bounds = scroll.getClientArea();
   if (preview.isFitHorizontal()) {
     if (preview.isFitVertical()) minSize = new Point(0, 0); // Best fit
     else minSize = new Point(0, preview.computeSize(bounds.width, SWT.DEFAULT).y); // Fit to width
   } else {
     if (preview.isFitVertical())
       minSize = new Point(preview.computeSize(SWT.DEFAULT, bounds.height).x, 0); // Fit to height
     else minSize = preview.computeSize(SWT.DEFAULT, SWT.DEFAULT); // Custom
     // scale
   }
   scroll.setMinSize(minSize);
 }
Пример #9
0
 private void setPreviewPageIndex(int pageIndex) {
   preview.setPageIndex(Math.max(Math.min(pageIndex, preview.getPageCount() - 1), 0));
   updatePageNumber();
 }
Пример #10
0
  private Control createScrollingPreview(Composite parent) {
    scroll = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scroll.setExpandHorizontal(true);
    scroll.setExpandVertical(true);

    preview = new PrintPreview(scroll, SWT.NONE);
    scroll.setContent(preview);

    scroll.addListener(
        SWT.Resize,
        new Listener() {
          public void handleEvent(Event event) {
            Rectangle bounds = scroll.getClientArea();

            scroll.getHorizontalBar().setPageIncrement(bounds.width * 2 / 3);
            scroll.getVerticalBar().setPageIncrement(bounds.height * 2 / 3);

            if (preview.isFitHorizontal() ^ preview.isFitVertical()) {
              rememberScrollingPosition();
              updatePreviewSize();
              restoreScrollingPosition();
            }
          }
        });

    preview.setFitVertical(true);
    preview.setFitHorizontal(true);

    Listener dragListener =
        new Listener() {
          private final Point dpi = display.getDPI();
          private boolean scrollable = false;

          private boolean dragging = false;
          private Point dragStartScrollOrigin = null;
          private Point dragStartMouseAnchor = null;

          public void handleEvent(Event event) {
            switch (event.type) {
              case SWT.Resize:
                forgetScrollingPosition();
                Rectangle bounds = scroll.getClientArea();
                Point size = preview.getSize();
                scrollable = size.x > bounds.width || size.y > bounds.height;
                if (!scrollable && dragging) endDragging();
                break;
              case SWT.MouseDown:
                forgetScrollingPosition();
                if (scrollable && event.button == 1) beginDragging(event);
                break;
              case SWT.MouseMove:
                if (dragging) {
                  forgetScrollingPosition();
                  Point point = preview.toDisplay(event.x, event.y);
                  scroll.setOrigin(
                      dragStartScrollOrigin.x + dragStartMouseAnchor.x - point.x,
                      dragStartScrollOrigin.y + dragStartMouseAnchor.y - point.y);
                }
                break;
              case SWT.MouseUp:
                forgetScrollingPosition();
                if (dragging) endDragging();
                break;
              case SWT.MouseEnter:
                display.addFilter(SWT.MouseWheel, this);
                break;
              case SWT.MouseWheel:
                if (event.count != 0) {
                  if (scrollable
                      && !dragging
                      && (event.stateMask == SWT.NONE || event.stateMask == SWT.SHIFT)) {
                    forgetScrollingPosition();
                    bounds = scroll.getClientArea();
                    size = preview.getSize();
                    Point origin = scroll.getOrigin();
                    int direction = event.count > 0 ? -1 : 1;
                    // Prefer vertical scrolling unless user is
                    // pressing Shift
                    if (size.y > bounds.height && event.stateMask == SWT.NONE)
                      origin.y += direction * Math.min(dpi.y, bounds.height / 4);
                    else if (size.x > bounds.width)
                      origin.x += direction * Math.min(dpi.x, bounds.width / 4);
                    scroll.setOrigin(origin);
                    event.doit = false;
                  } else if (event.stateMask == SWT.CTRL) { // Ctrl+MouseWheel
                    // ->
                    // zoom
                    float scale = preview.getAbsoluteScale();
                    setPreviewScale(event.count > 0 ? scale / 1.1f : scale * 1.1f);
                  }
                }
                break;
              case SWT.MouseExit:
                display.removeFilter(SWT.MouseWheel, this);
                break;
            }
          }

          private void beginDragging(Event event) {
            dragStartScrollOrigin = scroll.getOrigin();
            dragStartMouseAnchor = preview.toDisplay(event.x, event.y);
            dragging = true;
          }

          private void endDragging() {
            dragging = false;
            dragStartMouseAnchor = null;
            dragStartScrollOrigin = null;
          }
        };

    scroll.addListener(SWT.Resize, dragListener);
    preview.addListener(SWT.MouseDown, dragListener);
    preview.addListener(SWT.MouseMove, dragListener);
    preview.addListener(SWT.MouseUp, dragListener);

    // These are for mouse wheel handling
    preview.addListener(SWT.MouseEnter, dragListener);
    preview.addListener(SWT.MouseExit, dragListener);

    return scroll;
  }