Example usage for org.eclipse.swt.widgets Canvas isDisposed

List of usage examples for org.eclipse.swt.widgets Canvas isDisposed

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Canvas isDisposed.

Prototype

public boolean isDisposed() 

Source Link

Document

Returns true if the widget has been disposed, and false otherwise.

Usage

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

public static void main(String[] args) {
    final int INTERVAL = 888;
    final Display display = new Display();
    final Image image = new Image(display, 750, 750);
    GC gc = new GC(image);
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillRectangle(image.getBounds());
    gc.dispose();//from w  w  w  .j av a  2s  . c  o  m

    Shell shell = new Shell(display);
    shell.setText("Snippet 275");
    shell.setBounds(10, 10, 790, 790);
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setBounds(10, 10, 750, 750);
    canvas.addListener(SWT.Paint, event -> {
        value = String.valueOf(System.currentTimeMillis());
        event.gc.drawImage(image, 0, 0);
        event.gc.drawString(value, 10, 10, true);
    });
    display.timerExec(INTERVAL, new Runnable() {
        @Override
        public void run() {
            if (canvas.isDisposed())
                return;
            // canvas.redraw (); // <-- bad, damages more than is needed
            GC gc = new GC(canvas);
            Point extent = gc.stringExtent(value + '0');
            gc.dispose();
            canvas.redraw(10, 10, extent.x, extent.y, false);
            display.timerExec(INTERVAL, this);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("OpenGL in SWT");
    shell.setLayout(new FillLayout());
    final Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    canvas.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            resize(canvas);//from  ww w.  ja  v  a  2  s  . co m
        }
    });
    final GLContext context = init(canvas);
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            context.dispose();
        }
    });
    new Runnable() {
        public void run() {
            if (canvas.isDisposed())
                return;
            render();
            context.swapBuffers();
            canvas.getDisplay().timerExec(50, this);
        }
    }.run();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:BrowserExample.java

/**
 * Creates an instance of a ControlExample embedded inside the supplied
 * parent Composite./* w w w  .j  av  a 2  s.c om*/
 * 
 * @param parent
 *            the container of the example
 */
public BrowserExample(Composite parent) {

    final Display display = parent.getDisplay();
    FormLayout layout = new FormLayout();
    parent.setLayout(layout);
    ToolBar toolbar = new ToolBar(parent, SWT.NONE);
    final ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
    itemBack.setText(getResourceString("Back"));
    final ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
    itemForward.setText(getResourceString("Forward"));
    final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
    itemStop.setText(getResourceString("Stop"));
    final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
    itemRefresh.setText(getResourceString("Refresh"));
    final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
    itemGo.setText(getResourceString("Go"));

    location = new Text(parent, SWT.BORDER);

    images = new Image[] { new Image(display, "java2s.gif") };

    final Canvas canvas = new Canvas(parent, SWT.NO_BACKGROUND);
    final Rectangle rect = images[0].getBounds();
    canvas.addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event e) {
            Point pt = canvas.getSize();
            e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y);
        }
    });
    canvas.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            browser.setUrl(getResourceString("Startup"));
        }
    });

    display.asyncExec(new Runnable() {
        public void run() {
            if (canvas.isDisposed())
                return;
            if (busy) {
                index++;
                if (index == images.length)
                    index = 0;
                canvas.redraw();
            }
            display.timerExec(150, this);
        }
    });

    final Label status = new Label(parent, SWT.NONE);
    final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE);

    FormData data = new FormData();
    data.top = new FormAttachment(0, 5);
    toolbar.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.top = new FormAttachment(canvas, 5, SWT.DEFAULT);
    data.bottom = new FormAttachment(status, -5, SWT.DEFAULT);
    try {
        browser = new Browser(parent, SWT.NONE);
        browser.setLayoutData(data);
    } catch (SWTError e) {
        /* Browser widget could not be instantiated */
        Label label = new Label(parent, SWT.CENTER | SWT.WRAP);
        label.setText(getResourceString("BrowserNotCreated"));
        label.setLayoutData(data);
    }

    data = new FormData();
    data.width = 24;
    data.height = 24;
    data.top = new FormAttachment(0, 5);
    data.right = new FormAttachment(100, -5);
    canvas.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(toolbar, 0, SWT.TOP);
    data.left = new FormAttachment(toolbar, 5, SWT.RIGHT);
    data.right = new FormAttachment(canvas, -5, SWT.DEFAULT);
    location.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 5);
    data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT);
    data.bottom = new FormAttachment(100, -5);
    status.setLayoutData(data);

    data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.bottom = new FormAttachment(100, -5);
    progressBar.setLayoutData(data);

    if (browser != null) {
        itemBack.setEnabled(browser.isBackEnabled());
        itemForward.setEnabled(browser.isForwardEnabled());

        Listener listener = new Listener() {
            public void handleEvent(Event event) {
                ToolItem item = (ToolItem) event.widget;
                if (item == itemBack)
                    browser.back();
                else if (item == itemForward)
                    browser.forward();
                else if (item == itemStop)
                    browser.stop();
                else if (item == itemRefresh)
                    browser.refresh();
                else if (item == itemGo)
                    browser.setUrl(location.getText());
            }
        };
        browser.addLocationListener(new LocationListener() {
            public void changed(LocationEvent event) {
                busy = true;
                if (event.top)
                    location.setText(event.location);
            }

            public void changing(LocationEvent event) {
            }
        });
        browser.addProgressListener(new ProgressListener() {
            public void changed(ProgressEvent event) {
                if (event.total == 0)
                    return;
                int ratio = event.current * 100 / event.total;
                progressBar.setSelection(ratio);
                busy = event.current != event.total;
                if (!busy) {
                    index = 0;
                    canvas.redraw();
                }
            }

            public void completed(ProgressEvent event) {
                itemBack.setEnabled(browser.isBackEnabled());
                itemForward.setEnabled(browser.isForwardEnabled());
                progressBar.setSelection(0);
                busy = false;
                index = 0;
                canvas.redraw();
            }
        });
        browser.addStatusTextListener(new StatusTextListener() {
            public void changed(StatusTextEvent event) {
                status.setText(event.text);
            }
        });
        if (parent instanceof Shell) {
            final Shell shell = (Shell) parent;
            browser.addTitleListener(new TitleListener() {
                public void changed(TitleEvent event) {
                    shell.setText(event.title + " - " + getResourceString("window.title"));
                }
            });
        }
        itemBack.addListener(SWT.Selection, listener);
        itemForward.addListener(SWT.Selection, listener);
        itemStop.addListener(SWT.Selection, listener);
        itemRefresh.addListener(SWT.Selection, listener);
        itemGo.addListener(SWT.Selection, listener);
        location.addListener(SWT.DefaultSelection, new Listener() {
            public void handleEvent(Event e) {
                browser.setUrl(location.getText());
            }
        });

        initialize(display, browser);
        browser.setUrl(getResourceString("Startup"));
    }
}