|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vector Informatik GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the terms of the Eclipse |
| 5 | + * Public License 2.0 which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Michael Bangas (Vector Informatik GmbH) - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.swt.snippets; |
| 14 | + |
| 15 | +import java.io.*; |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +import org.eclipse.swt.*; |
| 19 | +import org.eclipse.swt.graphics.*; |
| 20 | +import org.eclipse.swt.internal.*; |
| 21 | +import org.eclipse.swt.internal.DPIUtil.*; |
| 22 | +import org.eclipse.swt.widgets.*; |
| 23 | + |
| 24 | +public class Snippet386 { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + drawWithImageFileNameProvider(); |
| 28 | + drawWithImageDataProvider(); |
| 29 | + } |
| 30 | + |
| 31 | + private static void drawWithImageFileNameProvider() { |
| 32 | + ImageFileNameProvider provider = createImageFileNameProvider(); |
| 33 | + Image image = new Image(Display.getDefault(), provider); |
| 34 | + createShellWithImage(image, "Snippet 386 - ImageFileNameProvider"); |
| 35 | + } |
| 36 | + |
| 37 | + private static void drawWithImageDataProvider() { |
| 38 | + ImageDataProvider provider = createImageDataProvider(); |
| 39 | + Image image = new Image(Display.getDefault(), provider); |
| 40 | + createShellWithImage(image, "Snippet 386 - ImageDataProvider"); |
| 41 | + } |
| 42 | + |
| 43 | + private static ImageFileNameProvider createImageFileNameProvider() { |
| 44 | + return new ImageFileNameAtSizeProvider() { |
| 45 | + @Override |
| 46 | + public Optional<String> getImagePath(int targetWidth, int targetHeight) { |
| 47 | + return Optional.of("resources/Snippet386/collapseall.png"); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getImagePath(int zoom) { |
| 52 | + return "resources/Snippet386/collapseall.png"; |
| 53 | + } |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + private static ImageDataProvider createImageDataProvider() { |
| 58 | + return new ImageDataAtSizeProvider() { |
| 59 | + @SuppressWarnings("restriction") |
| 60 | + @Override |
| 61 | + public Optional<ImageData> getImageData(int targetWidth, int targetHeight) { |
| 62 | + try (InputStream stream = new FileInputStream("resources/Snippet386/collapseall.svg")) { |
| 63 | + return Optional.of(NativeImageLoader.load(stream, new ImageLoader(), targetWidth, targetHeight)); |
| 64 | + } catch (IOException e) { |
| 65 | + SWT.error(SWT.ERROR_IO, e); |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("restriction") |
| 71 | + @Override |
| 72 | + public ImageData getImageData(int zoom) { |
| 73 | + try (InputStream stream = new FileInputStream("resources/Snippet386/collapseall.svg")) { |
| 74 | + return NativeImageLoader.load(new ElementAtZoom<>(stream, 100), new ImageLoader(), 100).get(0).element(); |
| 75 | + } catch (IOException e) { |
| 76 | + SWT.error(SWT.ERROR_IO, e); |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + private static void createShellWithImage(Image image, String title) { |
| 84 | + Shell shell = new Shell(Display.getDefault(), SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED); |
| 85 | + shell.setText(title); |
| 86 | + |
| 87 | + shell.addListener(SWT.Paint, e -> { |
| 88 | + Rectangle rect = image.getBounds(); |
| 89 | + e.gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, 100, 200); |
| 90 | + }); |
| 91 | + |
| 92 | + shell.setSize(600, 400); |
| 93 | + shell.open(); |
| 94 | + while (!shell.isDisposed()) { |
| 95 | + if (!Display.getDefault().readAndDispatch()) |
| 96 | + Display.getDefault().sleep(); |
| 97 | + } |
| 98 | + Display.getDefault().dispose(); |
| 99 | + } |
| 100 | +} |
0 commit comments