Example usage for org.eclipse.jface.resource ImageDescriptor createResource

List of usage examples for org.eclipse.jface.resource ImageDescriptor createResource

Introduction

In this page you can find the example usage for org.eclipse.jface.resource ImageDescriptor createResource.

Prototype

@Override
    public Object createResource(Device device) throws DeviceResourceException 

Source Link

Usage

From source file:com.diffplug.common.swt.jface.ImageDescriptors.java

License:Apache License

/**
 * {@link ImageDescriptor} allows an {@link Image} to be shared in a pool using reference counting. In order to not screw-up the reference
 * counting, you need to be pretty careful with how you use them.
 * <p>/*  w ww .  j  av a2s  .  c o  m*/
 * This creates a {@link com.diffplug.common.base.Box.Nullable Box.Nullable&lt;ImageDescriptor&gt;} which sets and gets images in a way that will keep the reference counting happy.
 * <p>
 * <b>NO ONE MUST SET THE IMAGE EXCEPT THIS SETTER.</b>
 *
 * @param lifecycle   Any outstanding images will be destroyed with the lifecycle of this Widget.
 * @param imageGetter Function which returns the image currently on the Widget (used to ensure that nobody messed with it).
 * @param imageSetter Function which sets the image on the Widget.
 * @return A `Box.Nullable<ImageDescriptor>` for setting the {@link Image} using an {@link ImageDescriptor}.
 */
public static Box.Nullable<ImageDescriptor> createSetter(Widget lifecycle, Supplier<Image> imageGetter,
        Consumer<Image> imageSetter) {
    return new Box.Nullable<ImageDescriptor>() {
        private ImageDescriptor lastDesc;
        private Image lastImg;

        {
            // when the control is disposed, we'll clear the image
            lifecycle.addListener(SWT.Dispose, e -> {
                if (lastDesc != null) {
                    lastDesc.destroyResource(lastImg);
                }
            });
        }

        @Override
        public ImageDescriptor get() {
            return lastDesc;
        }

        @Override
        public void set(ImageDescriptor newDesc) {
            // make sure nobody else messed with the image
            if (imageGetter.get() != lastImg) {
                // if someone else did mess with it, we can probably survive, so best to just
                // log the failure and continue with setting the image
                Errors.log().accept(
                        new IllegalStateException("Setter must have exclusive control over the image field."));
            }

            // set the new image
            Image newImg;
            if (newDesc != null) {
                newImg = (Image) newDesc.createResource(lifecycle.getDisplay());
            } else {
                newImg = null;
            }
            imageSetter.accept(newImg);

            // if an image was already set, destroy it
            if (lastDesc != null) {
                lastDesc.destroyResource(lastImg);
            }

            // save the fields for the next go-round
            lastDesc = newDesc;
            lastImg = newImg;
        }
    };
}

From source file:gov.nasa.arc.spife.ui.timeline.part.ScaleTimelineMarkerEditPart.java

License:Open Source License

private Image getImage() {
    if (image == null || image.isDisposed()) {
        TimelineMarker marker = getModel();
        ImageDescriptor imageDescriptor = marker.getImageDescriptor();
        if (imageDescriptor != null) {
            image = (Image) imageDescriptor.createResource(getViewer().getControl().getDisplay());
        }// w w w.  j  av  a2s .co m
    }
    return image;
}

From source file:gr.scharf.workingsets.internal.RegExWorkingSetPage.java

License:Open Source License

public void createTableViewer(Composite parent) {
    Table table = new Table(parent,
            SWT.VIRTUAL | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.SINGLE | SWT.BORDER);
    TableLayout layout = new TableLayout();
    table.setLayout(layout);//from   www.j a  v a2  s. c o m
    table.setHeaderVisible(true);
    this.fPreviewTableViewer = new TableViewer(table);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.minimumHeight = 300;

    this.fPreviewTableViewer.getTable().setLayoutData(data);
    fComparator = new ResourceComparator();
    createTableViewerColumn("Name", 200, 0);
    createTableViewerColumn("Path", 400, 1);
    this.fPreviewTableViewer.setLabelProvider(new ITableLabelProvider() {
        public Image getColumnImage(Object element, int columnIndex) {
            if (columnIndex == 1)
                return null;
            IWorkbenchAdapter adapter = (IWorkbenchAdapter) ((IAdaptable) element)
                    .getAdapter(IWorkbenchAdapter.class);
            if (adapter == null) {
                return null;
            }
            ImageDescriptor descriptor = adapter.getImageDescriptor(element);
            if (descriptor == null) {
                return null;
            }
            Image image = (Image) descriptor.createResource(Display.getCurrent());
            return image;
        }

        public String getColumnText(Object element, int columnIndex) {
            IResource resource = (IResource) element;
            if (columnIndex == 1)
                return resource.getFullPath().toPortableString();
            return resource.getName();
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void dispose() {
        }

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void removeListener(ILabelProviderListener listener) {
        }
    });

    this.fPreviewTableViewer.setContentProvider(new ILazyContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @Override
        public void dispose() {
        }

        @Override
        public void updateElement(int index) {
            fPreviewTableViewer.replace(fResources[index], index);
        }
    });

    this.fPreviewTableViewer.setInput(this.fResources);
}

From source file:org.eclipse.eavp.viz.service.widgets.TimeSliderComposite.java

License:Open Source License

/**
 * Creates the "next" button that increments the timestep.
 * /*from   w w w  . ja  v  a  2s  . c  o m*/
 * @param parent
 *            The parent Composite for this widget. Assumed not to be
 *            {@code null}.
 * @return The new widget.
 */
private Button createNextButton(Composite parent) {
    Button nextButton = new Button(parent, SWT.PUSH);

    // When the button is clicked, playback should be stopped and the
    // timestep should be incremented.
    nextButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // Disable playback.
            setPlayback(false, e);

            // Increment the timestep.
            if (incrementTimestep()) {
                notifyListeners(e);
            }
        }
    });

    // Load the button image as necessary.
    ImageDescriptor imgd = getImageDescriptor("nav_forward.gif");
    Image img = (Image) imgd.createResource(getDisplay());
    imageDescriptors.add(imgd);
    images.add(img);

    // Set the initial tool tip and image.
    nextButton.setToolTipText("Next");
    nextButton.setImage(img);

    return nextButton;
}

From source file:org.eclipse.eavp.viz.service.widgets.TimeSliderComposite.java

License:Open Source License

/**
 * Creates the "options" button that can be used to configure playback
 * behavior.//from ww w. j  av a  2  s  . c om
 * 
 * @param parent
 *            The parent Composite for this widget. Assumed not to be
 *            {@code null}.
 * @return The new widget.
 */
private Button createOptionsButton(Composite parent) {
    final Button optionsButton = new Button(parent, SWT.PUSH);

    // When the button is clicked, playback should be stopped and the
    // timestep should be incremented.
    optionsButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // Open up the menu below this button.
            Rectangle r = optionsButton.getBounds();
            Point p = new Point(r.x, r.y + r.height);
            p = optionsButton.getParent().toDisplay(p.x, p.y);
            Menu menu = optionsMenuManager.getMenu();
            menu.setLocation(p.x, p.y);
            menu.setVisible(true);
        }
    });

    // Load the button image as necessary.
    ImageDescriptor imgd = getImageDescriptor("thread_obj.gif");
    Image img = (Image) imgd.createResource(getDisplay());
    imageDescriptors.add(imgd);
    images.add(img);

    // Set the initial tool tip and image.
    optionsButton.setToolTipText("Playback settings");
    optionsButton.setImage(img);

    return optionsButton;
}

From source file:org.eclipse.eavp.viz.service.widgets.TimeSliderComposite.java

License:Open Source License

/**
 * Creates the "play" button that toggles the playback operation.
 * //from w ww .  j av a2  s.co m
 * @param parent
 *            The parent Composite for this widget. Assumed not to be
 *            {@code null}.
 * @return The new widget.
 */
private Button createPlayButton(Composite parent) {
    Button playButton = new Button(parent, SWT.PUSH);

    // When the button is clicked, playback should be toggled.
    playButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // Toggle playback.
            setPlayback(!isPlaying, e);
        }
    });

    // Load the play image.
    ImageDescriptor imgd = getImageDescriptor("nav_go.gif");
    playImage = (Image) imgd.createResource(getDisplay());
    imageDescriptors.add(imgd);
    images.add(playImage);
    // Load the pause image.
    imgd = getImageDescriptor("suspend_co.gif");
    pauseImage = (Image) imgd.createResource(getDisplay());
    imageDescriptors.add(imgd);
    images.add(pauseImage);

    // Set the initial tool tip and image.
    playButton.setToolTipText("Play");
    playButton.setImage(playImage);

    return playButton;
}

From source file:org.eclipse.eavp.viz.service.widgets.TimeSliderComposite.java

License:Open Source License

/**
 * Creates the "previous" button that deccrements the timestep.
 * /*from  ww  w.  j a  v a2  s  . c  om*/
 * @param parent
 *            The parent Composite. Assumed not to be {@code null}.
 * @return The new button.
 */
private Button createPrevButton(Composite parent) {
    Button prevButton = new Button(parent, SWT.PUSH);

    // When the button is clicked, playback should be stopped and the
    // timestep should be decremented.
    prevButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // Disable playback.
            setPlayback(false, e);

            // Decrement the timestep.
            if (decrementTimestep()) {
                notifyListeners(e);
            }
        }
    });

    // Load the button image as necessary.
    ImageDescriptor imgd = getImageDescriptor("nav_backward.gif");
    Image img = (Image) imgd.createResource(getDisplay());
    imageDescriptors.add(imgd);
    images.add(img);

    // Set the initial tool tip and image.
    prevButton.setToolTipText("Previous");
    prevButton.setImage(img);

    return prevButton;
}