Example usage for org.eclipse.swt.widgets ScrollBar setMaximum

List of usage examples for org.eclipse.swt.widgets ScrollBar setMaximum

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets ScrollBar setMaximum.

Prototype

public void setMaximum(int maximum) 

Source Link

Document

Sets the maximum.

Usage

From source file:org.eclipse.swt.snippets.Snippet9.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL);
    shell.setText("Snippet 9");
    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setSize(700, 600);/*from  w  w  w. j a  va2s  .  c om*/
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    composite.addPaintListener(e -> {
        e.gc.setBackground(red);
        e.gc.fillOval(5, 5, 690, 590);
    });
    final ScrollBar hBar = shell.getHorizontalBar();
    hBar.addListener(SWT.Selection, e -> {
        Point location = composite.getLocation();
        location.x = -hBar.getSelection();
        composite.setLocation(location);
    });
    final ScrollBar vBar = shell.getVerticalBar();
    vBar.addListener(SWT.Selection, e -> {
        Point location = composite.getLocation();
        location.y = -vBar.getSelection();
        composite.setLocation(location);
    });
    shell.addListener(SWT.Resize, e -> {
        Point size = composite.getSize();
        Rectangle rect = shell.getClientArea();
        hBar.setMaximum(size.x);
        vBar.setMaximum(size.y);
        hBar.setThumb(Math.min(size.x, rect.width));
        vBar.setThumb(Math.min(size.y, rect.height));
        int hPage = size.x - rect.width;
        int vPage = size.y - rect.height;
        int hSelection = hBar.getSelection();
        int vSelection = vBar.getSelection();
        Point location = composite.getLocation();
        if (hSelection >= hPage) {
            if (hPage <= 0)
                hSelection = 0;
            location.x = -hSelection;
        }
        if (vSelection >= vPage) {
            if (vPage <= 0)
                vSelection = 0;
            location.y = -vSelection;
        }
        composite.setLocation(location);
    });
    shell.setSize(600, 500);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet9.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL);
    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setSize(200, 400);//from w  w  w.j a va 2s .  com
    final ScrollBar hBar = shell.getHorizontalBar();
    hBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            Point location = composite.getLocation();
            location.x = -hBar.getSelection();
            composite.setLocation(location);
        }
    });
    final ScrollBar vBar = shell.getVerticalBar();
    vBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            Point location = composite.getLocation();
            location.y = -vBar.getSelection();
            composite.setLocation(location);
        }
    });
    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
            Point size = composite.getSize();
            Rectangle rect = shell.getClientArea();
            hBar.setMaximum(size.x);
            vBar.setMaximum(size.y);
            hBar.setThumb(Math.min(size.x, rect.width));
            vBar.setThumb(Math.min(size.y, rect.height));
            int hPage = size.x - rect.width;
            int vPage = size.y - rect.height;
            int hSelection = hBar.getSelection();
            int vSelection = vBar.getSelection();
            Point location = composite.getLocation();
            if (hSelection >= hPage) {
                if (hPage <= 0)
                    hSelection = 0;
                location.x = -hSelection;
            }
            if (vSelection >= vPage) {
                if (vPage <= 0)
                    vSelection = 0;
                location.y = -vSelection;
            }
            composite.setLocation(location);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet48.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 48");
    shell.setLayout(new FillLayout());
    Image originalImage = null;/*from  ww w.j  ava 2 s  .  c  o  m*/
    FileDialog dialog = new FileDialog(shell, SWT.OPEN);
    dialog.setText("Open an image file or cancel");
    String string = dialog.open();
    if (string != null) {
        originalImage = new Image(display, string);
    }
    if (originalImage == null) {
        int width = 150, height = 200;
        originalImage = new Image(display, width, height);
        GC gc = new GC(originalImage);
        gc.fillRectangle(0, 0, width, height);
        gc.drawLine(0, 0, width, height);
        gc.drawLine(0, height, width, 0);
        gc.drawText("Default Image", 10, 10);
        gc.dispose();
    }
    final Image image = originalImage;
    final Point origin = new Point(0, 0);
    final Canvas canvas = new Canvas(shell,
            SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
    final ScrollBar hBar = canvas.getHorizontalBar();
    hBar.addListener(SWT.Selection, e -> {
        int hSelection = hBar.getSelection();
        int destX = -hSelection - origin.x;
        Rectangle rect = image.getBounds();
        canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
        origin.x = -hSelection;
    });
    final ScrollBar vBar = canvas.getVerticalBar();
    vBar.addListener(SWT.Selection, e -> {
        int vSelection = vBar.getSelection();
        int destY = -vSelection - origin.y;
        Rectangle rect = image.getBounds();
        canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
        origin.y = -vSelection;
    });
    canvas.addListener(SWT.Resize, e -> {
        Rectangle rect = image.getBounds();
        Rectangle client = canvas.getClientArea();
        hBar.setMaximum(rect.width);
        vBar.setMaximum(rect.height);
        hBar.setThumb(Math.min(rect.width, client.width));
        vBar.setThumb(Math.min(rect.height, client.height));
        int hPage = rect.width - client.width;
        int vPage = rect.height - client.height;
        int hSelection = hBar.getSelection();
        int vSelection = vBar.getSelection();
        if (hSelection >= hPage) {
            if (hPage <= 0)
                hSelection = 0;
            origin.x = -hSelection;
        }
        if (vSelection >= vPage) {
            if (vPage <= 0)
                vSelection = 0;
            origin.y = -vSelection;
        }
        canvas.redraw();
    });
    canvas.addListener(SWT.Paint, e -> {
        GC gc = e.gc;
        gc.drawImage(image, origin.x, origin.y);
        Rectangle rect = image.getBounds();
        Rectangle client = canvas.getClientArea();
        int marginWidth = client.width - rect.width;
        if (marginWidth > 0) {
            gc.fillRectangle(rect.width, 0, marginWidth, client.height);
        }
        int marginHeight = client.height - rect.height;
        if (marginHeight > 0) {
            gc.fillRectangle(0, rect.height, client.width, marginHeight);
        }
    });
    Rectangle rect = image.getBounds();
    shell.setSize(Math.max(200, rect.width - 100), Math.max(150, rect.height - 100));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    originalImage.dispose();
    display.dispose();
}

From source file:Snippet48.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display,
            SWT.SHELL_TRIM | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
    Image originalImage = null;/*from  w ww . ja va 2s.  c  om*/
    FileDialog dialog = new FileDialog(shell, SWT.OPEN);
    dialog.setText("Open an image file or cancel");
    String string = dialog.open();
    if (string != null) {
        originalImage = new Image(display, string);
    }
    if (originalImage == null) {
        int width = 150, height = 200;
        originalImage = new Image(display, width, height);
        GC gc = new GC(originalImage);
        gc.fillRectangle(0, 0, width, height);
        gc.drawLine(0, 0, width, height);
        gc.drawLine(0, height, width, 0);
        gc.drawText("Default Image", 10, 10);
        gc.dispose();
    }
    final Image image = originalImage;
    final Point origin = new Point(0, 0);
    final ScrollBar hBar = shell.getHorizontalBar();
    hBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            int hSelection = hBar.getSelection();
            int destX = -hSelection - origin.x;
            Rectangle rect = image.getBounds();
            shell.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            origin.x = -hSelection;
        }
    });
    final ScrollBar vBar = shell.getVerticalBar();
    vBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            int vSelection = vBar.getSelection();
            int destY = -vSelection - origin.y;
            Rectangle rect = image.getBounds();
            shell.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            origin.y = -vSelection;
        }
    });
    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
            Rectangle rect = image.getBounds();
            Rectangle client = shell.getClientArea();
            hBar.setMaximum(rect.width);
            vBar.setMaximum(rect.height);
            hBar.setThumb(Math.min(rect.width, client.width));
            vBar.setThumb(Math.min(rect.height, client.height));
            int hPage = rect.width - client.width;
            int vPage = rect.height - client.height;
            int hSelection = hBar.getSelection();
            int vSelection = vBar.getSelection();
            if (hSelection >= hPage) {
                if (hPage <= 0)
                    hSelection = 0;
                origin.x = -hSelection;
            }
            if (vSelection >= vPage) {
                if (vPage <= 0)
                    vSelection = 0;
                origin.y = -vSelection;
            }
            shell.redraw();
        }
    });
    shell.addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event e) {
            GC gc = e.gc;
            gc.drawImage(image, origin.x, origin.y);
            Rectangle rect = image.getBounds();
            Rectangle client = shell.getClientArea();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
                gc.fillRectangle(rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
                gc.fillRectangle(0, rect.height, client.width, marginHeight);
            }
        }
    });
    shell.setSize(200, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ImageScrollFlickerFree.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Image originalImage = null;/*from   w w w  .ja  v  a  2s.  c  o  m*/
    FileDialog dialog = new FileDialog(shell, SWT.OPEN);
    dialog.setText("Open an image file or cancel");
    String string = dialog.open();
    if (string != null) {
        originalImage = new Image(display, string);
    }
    if (originalImage == null) {
        int width = 150, height = 200;
        originalImage = new Image(display, width, height);
        GC gc = new GC(originalImage);
        gc.fillRectangle(0, 0, width, height);
        gc.drawLine(0, 0, width, height);
        gc.drawLine(0, height, width, 0);
        gc.drawText("Default Image", 10, 10);
        gc.dispose();
    }
    final Image image = originalImage;
    final Point origin = new Point(0, 0);
    final Canvas canvas = new Canvas(shell,
            SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
    final ScrollBar hBar = canvas.getHorizontalBar();
    hBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            int hSelection = hBar.getSelection();
            int destX = -hSelection - origin.x;
            Rectangle rect = image.getBounds();
            canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            origin.x = -hSelection;
        }
    });
    final ScrollBar vBar = canvas.getVerticalBar();
    vBar.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            int vSelection = vBar.getSelection();
            int destY = -vSelection - origin.y;
            Rectangle rect = image.getBounds();
            canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            origin.y = -vSelection;
        }
    });
    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
            Rectangle rect = image.getBounds();
            Rectangle client = canvas.getClientArea();
            hBar.setMaximum(rect.width);
            vBar.setMaximum(rect.height);
            hBar.setThumb(Math.min(rect.width, client.width));
            vBar.setThumb(Math.min(rect.height, client.height));
            int hPage = rect.width - client.width;
            int vPage = rect.height - client.height;
            int hSelection = hBar.getSelection();
            int vSelection = vBar.getSelection();
            if (hSelection >= hPage) {
                if (hPage <= 0)
                    hSelection = 0;
                origin.x = -hSelection;
            }
            if (vSelection >= vPage) {
                if (vPage <= 0)
                    vSelection = 0;
                origin.y = -vSelection;
            }
            canvas.redraw();
        }
    });
    canvas.addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event e) {
            GC gc = e.gc;
            gc.drawImage(image, origin.x, origin.y);
            Rectangle rect = image.getBounds();
            Rectangle client = canvas.getClientArea();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
                gc.fillRectangle(rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
                gc.fillRectangle(0, rect.height, client.width, marginHeight);
            }
        }
    });
    shell.setSize(200, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    originalImage.dispose();
    display.dispose();
}

From source file:ImageAnalyzer.java

void resizeScrollBars() {
    // Set the max and thumb for the image canvas scroll bars.
    ScrollBar horizontal = imageCanvas.getHorizontalBar();
    ScrollBar vertical = imageCanvas.getVerticalBar();
    Rectangle canvasBounds = imageCanvas.getClientArea();
    int width = Math.round(imageData.width * xscale);
    if (width > canvasBounds.width) {
        // The image is wider than the canvas.
        horizontal.setEnabled(true);//from w  w  w  .ja v  a 2  s.c om
        horizontal.setMaximum(width);
        horizontal.setThumb(canvasBounds.width);
        horizontal.setPageIncrement(canvasBounds.width);
    } else {
        // The canvas is wider than the image.
        horizontal.setEnabled(false);
        if (ix != 0) {
            // Make sure the image is completely visible.
            ix = 0;
            imageCanvas.redraw();
        }
    }
    int height = Math.round(imageData.height * yscale);
    if (height > canvasBounds.height) {
        // The image is taller than the canvas.
        vertical.setEnabled(true);
        vertical.setMaximum(height);
        vertical.setThumb(canvasBounds.height);
        vertical.setPageIncrement(canvasBounds.height);
    } else {
        // The canvas is taller than the image.
        vertical.setEnabled(false);
        if (iy != 0) {
            // Make sure the image is completely visible.
            iy = 0;
            imageCanvas.redraw();
        }
    }

    // Set the max and thumb for the palette canvas scroll bar.
    vertical = paletteCanvas.getVerticalBar();
    if (imageData.palette.isDirect) {
        vertical.setEnabled(false);
    } else { // indexed palette
        canvasBounds = paletteCanvas.getClientArea();
        int paletteHeight = imageData.palette.getRGBs().length * 10 + 20; // 10
        // pixels
        // each
        // index
        // + 20
        // for
        // margins.
        vertical.setEnabled(true);
        vertical.setMaximum(paletteHeight);
        vertical.setThumb(canvasBounds.height);
        vertical.setPageIncrement(canvasBounds.height);
    }
}

From source file:org.eclipse.swt.examples.imageanalyzer.ImageAnalyzer.java

void resizeScrollBars() {
    // Set the max and thumb for the image canvas scroll bars.
    ScrollBar horizontal = imageCanvas.getHorizontalBar();
    ScrollBar vertical = imageCanvas.getVerticalBar();
    Rectangle canvasBounds = imageCanvas.getClientArea();
    int width = Math.round(imageData.width * xscale);
    if (width > canvasBounds.width) {
        // The image is wider than the canvas.
        horizontal.setEnabled(true);//from www .j  a v a2  s  .c  o m
        horizontal.setMaximum(width);
        horizontal.setThumb(canvasBounds.width);
        horizontal.setPageIncrement(canvasBounds.width);
    } else {
        // The canvas is wider than the image.
        horizontal.setEnabled(false);
        if (ix != 0) {
            // Make sure the image is completely visible.
            ix = 0;
            imageCanvas.redraw();
        }
    }
    int height = Math.round(imageData.height * yscale);
    if (height > canvasBounds.height) {
        // The image is taller than the canvas.
        vertical.setEnabled(true);
        vertical.setMaximum(height);
        vertical.setThumb(canvasBounds.height);
        vertical.setPageIncrement(canvasBounds.height);
    } else {
        // The canvas is taller than the image.
        vertical.setEnabled(false);
        if (iy != 0) {
            // Make sure the image is completely visible.
            iy = 0;
            imageCanvas.redraw();
        }
    }

    // Set the max and thumb for the palette canvas scroll bar.
    vertical = paletteCanvas.getVerticalBar();
    if (imageData.palette.isDirect) {
        vertical.setEnabled(false);
    } else { // indexed palette
        canvasBounds = paletteCanvas.getClientArea();
        int paletteHeight = imageData.palette.getRGBs().length * 10 + 20; // 10 pixels each index + 20 for margins.
        vertical.setEnabled(true);
        vertical.setMaximum(paletteHeight);
        vertical.setThumb(canvasBounds.height);
        vertical.setPageIncrement(canvasBounds.height);
    }
}

From source file:org.eclipse.swt.examples.clipboard.ClipboardExample.java

void createImageTransfer(Composite copyParent, Composite pasteParent) {
    final Image[] copyImage = new Image[] { null };
    Label l = new Label(copyParent, SWT.NONE);
    l.setText("ImageTransfer:"); //$NON-NLS-1$
    GridData data = new GridData();
    data.verticalSpan = 2;/*ww  w . j  a va 2 s  .  c o  m*/
    l.setLayoutData(data);

    final Canvas copyImageCanvas = new Canvas(copyParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.verticalSpan = 2;
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    copyImageCanvas.setLayoutData(data);

    final Point copyOrigin = new Point(0, 0);
    final ScrollBar copyHBar = copyImageCanvas.getHorizontalBar();
    copyHBar.setEnabled(false);
    copyHBar.addListener(SWT.Selection, e -> {
        if (copyImage[0] != null) {
            int hSelection = copyHBar.getSelection();
            int destX = -hSelection - copyOrigin.x;
            Rectangle rect = copyImage[0].getBounds();
            copyImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            copyOrigin.x = -hSelection;
        }
    });
    final ScrollBar copyVBar = copyImageCanvas.getVerticalBar();
    copyVBar.setEnabled(false);
    copyVBar.addListener(SWT.Selection, e -> {
        if (copyImage[0] != null) {
            int vSelection = copyVBar.getSelection();
            int destY = -vSelection - copyOrigin.y;
            Rectangle rect = copyImage[0].getBounds();
            copyImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            copyOrigin.y = -vSelection;
        }
    });
    copyImageCanvas.addListener(SWT.Paint, e -> {
        if (copyImage[0] != null) {
            GC gc = e.gc;
            gc.drawImage(copyImage[0], copyOrigin.x, copyOrigin.y);
            Rectangle rect = copyImage[0].getBounds();
            Rectangle client = copyImageCanvas.getClientArea();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
                gc.fillRectangle(rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
                gc.fillRectangle(0, rect.height, client.width, marginHeight);
            }
            gc.dispose();
        }
    });
    Button openButton = new Button(copyParent, SWT.PUSH);
    openButton.setText("Open Image");
    openButton.addSelectionListener(widgetSelectedAdapter(e -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText("Open an image file or cancel");
        String string = dialog.open();
        if (string != null) {
            if (copyImage[0] != null) {
                System.out.println("CopyImage");
                copyImage[0].dispose();
            }
            copyImage[0] = new Image(e.display, string);
            copyVBar.setEnabled(true);
            copyHBar.setEnabled(true);
            copyOrigin.x = 0;
            copyOrigin.y = 0;
            Rectangle rect = copyImage[0].getBounds();
            Rectangle client = copyImageCanvas.getClientArea();
            copyHBar.setMaximum(rect.width);
            copyVBar.setMaximum(rect.height);
            copyHBar.setThumb(Math.min(rect.width, client.width));
            copyVBar.setThumb(Math.min(rect.height, client.height));
            copyImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true);
            copyVBar.setSelection(0);
            copyHBar.setSelection(0);
            copyImageCanvas.redraw();
        }
    }));
    Button b = new Button(copyParent, SWT.PUSH);
    b.setText("Copy");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        if (copyImage[0] != null) {
            status.setText("");
            // Fetch ImageData at current zoom and save in the clip-board.
            clipboard.setContents(new Object[] { copyImage[0].getImageDataAtCurrentZoom() },
                    new Transfer[] { ImageTransfer.getInstance() });
        } else {
            status.setText("No image to copy");
        }
    }));

    final Image[] pasteImage = new Image[] { null };
    l = new Label(pasteParent, SWT.NONE);
    l.setText("ImageTransfer:");
    final Canvas pasteImageCanvas = new Canvas(pasteParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    pasteImageCanvas.setLayoutData(data);
    final Point pasteOrigin = new Point(0, 0);
    final ScrollBar pasteHBar = pasteImageCanvas.getHorizontalBar();
    pasteHBar.setEnabled(false);
    pasteHBar.addListener(SWT.Selection, e -> {
        if (pasteImage[0] != null) {
            int hSelection = pasteHBar.getSelection();
            int destX = -hSelection - pasteOrigin.x;
            Rectangle rect = pasteImage[0].getBounds();
            pasteImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            pasteOrigin.x = -hSelection;
        }
    });
    final ScrollBar pasteVBar = pasteImageCanvas.getVerticalBar();
    pasteVBar.setEnabled(false);
    pasteVBar.addListener(SWT.Selection, e -> {
        if (pasteImage[0] != null) {
            int vSelection = pasteVBar.getSelection();
            int destY = -vSelection - pasteOrigin.y;
            Rectangle rect = pasteImage[0].getBounds();
            pasteImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            pasteOrigin.y = -vSelection;
        }
    });
    pasteImageCanvas.addListener(SWT.Paint, e -> {
        if (pasteImage[0] != null) {
            GC gc = e.gc;
            gc.drawImage(pasteImage[0], pasteOrigin.x, pasteOrigin.y);
            Rectangle rect = pasteImage[0].getBounds();
            Rectangle client = pasteImageCanvas.getClientArea();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
                gc.fillRectangle(rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
                gc.fillRectangle(0, rect.height, client.width, marginHeight);
            }
        }
    });
    b = new Button(pasteParent, SWT.PUSH);
    b.setText("Paste");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
        if (imageData != null) {
            if (pasteImage[0] != null) {
                System.out.println("PasteImage");
                pasteImage[0].dispose();
            }
            status.setText("");
            // Consume the ImageData at current zoom as-is.
            pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData));
            pasteVBar.setEnabled(true);
            pasteHBar.setEnabled(true);
            pasteOrigin.x = 0;
            pasteOrigin.y = 0;
            Rectangle rect = pasteImage[0].getBounds();
            Rectangle client = pasteImageCanvas.getClientArea();
            pasteHBar.setMaximum(rect.width);
            pasteVBar.setMaximum(rect.height);
            pasteHBar.setThumb(Math.min(rect.width, client.width));
            pasteVBar.setThumb(Math.min(rect.height, client.height));
            pasteImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true);
            pasteVBar.setSelection(0);
            pasteHBar.setSelection(0);
            pasteImageCanvas.redraw();
        } else {
            status.setText("No image to paste");
        }
    }));
}

From source file:org.eclipse.swt.examples.accessibility.CTable.java

/**
 * Removes all of the items from the receiver.
 *
 * @exception SWTException <ul>//from w w w. j ava2 s. c o  m
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void removeAll() {
    checkWidget();
    if (itemsCount == 0)
        return;
    setRedraw(false);

    setFocusItem(null, false);
    for (int i = 0; i < itemsCount; i++) {
        items[i].dispose(false);
    }
    items = new CTableItem[0];
    selectedItems = new CTableItem[0];
    int oldCount = itemsCount;
    itemsCount = topIndex = 0;
    anchorItem = lastClickedItem = null;
    lastSelectionEvent = null;

    int[] eventData = new int[5];
    eventData[0] = ACC.DELETE;
    eventData[1] = 0;
    eventData[2] = oldCount;
    eventData[3] = 0;
    eventData[4] = 0;
    getAccessible().sendEvent(ACC.EVENT_TABLE_CHANGED, eventData);

    ScrollBar vBar = getVerticalBar();
    if (vBar != null) {
        vBar.setMaximum(1);
        vBar.setVisible(false);
    }
    if (columns.length == 0) {
        horizontalOffset = 0;
        ScrollBar hBar = getHorizontalBar();
        if (hBar != null) {
            hBar.setMaximum(1);
            hBar.setVisible(false);
        }
    }

    setRedraw(true);
}

From source file:org.eclipse.swt.examples.accessibility.CTable.java

void destroyItem(CTableColumn column) {
    headerHideToolTip();/*from w w w  .  j  a  va2 s  .  co m*/
    int index = column.getIndex();
    int orderedIndex = column.getOrderIndex();

    CTableColumn[] newColumns = new CTableColumn[columns.length - 1];
    System.arraycopy(columns, 0, newColumns, 0, index);
    System.arraycopy(columns, index + 1, newColumns, index, newColumns.length - index);
    columns = newColumns;

    if (orderedColumns != null) {
        if (columns.length < 2) {
            orderedColumns = null;
        } else {
            int removeIndex = column.getOrderIndex();
            CTableColumn[] newOrderedColumns = new CTableColumn[orderedColumns.length - 1];
            System.arraycopy(orderedColumns, 0, newOrderedColumns, 0, removeIndex);
            System.arraycopy(orderedColumns, removeIndex + 1, newOrderedColumns, removeIndex,
                    newOrderedColumns.length - removeIndex);
            orderedColumns = newOrderedColumns;
        }
    }

    /* ensure that column 0 always has left-alignment */
    if (index == 0 && columns.length > 0) {
        int style = columns[0].getStyle();
        style |= SWT.LEFT;
        style &= ~(SWT.CENTER | SWT.RIGHT);
        columns[0].setStyle(style);
    }

    /* allow all items to update their internal structures accordingly */
    for (int i = 0; i < itemsCount; i++) {
        items[i].removeColumn(column, index);
    }

    /* update horizontal scrollbar */
    int lastColumnIndex = columns.length - 1;
    if (lastColumnIndex < 0) { /* no more columns */
        updateHorizontalBar();
    } else {
        int newWidth = 0;
        for (CTableColumn column2 : columns) {
            newWidth += column2.width;
        }
        ScrollBar hBar = getHorizontalBar();
        if (hBar != null) {
            hBar.setMaximum(newWidth);
            hBar.setVisible(clientArea.width < newWidth);
        }
        int selection = hBar.getSelection();
        if (selection != horizontalOffset) {
            horizontalOffset = selection;
            redraw();
            if (header.isVisible() && drawCount <= 0)
                header.redraw();
        }
    }
    CTableColumn[] orderedColumns = getOrderedColumns();
    for (int i = orderedIndex; i < orderedColumns.length; i++) {
        if (!orderedColumns[i].isDisposed()) {
            orderedColumns[i].notifyListeners(SWT.Move, new Event());
        }
    }

    int[] eventData = new int[5];
    eventData[0] = ACC.DELETE;
    eventData[1] = 0;
    eventData[2] = 0;
    eventData[3] = index;
    eventData[4] = 1;
    getAccessible().sendEvent(ACC.EVENT_TABLE_CHANGED, eventData);

    if (sortColumn == column) {
        sortColumn = null;
    }
}