in application/org.openjdk.jmc.flightrecorder.ui/src/main/java/org/openjdk/jmc/flightrecorder/ui/common/DataPageToolkit.java [975:1063]
public void addPages() {
addPage(new WizardPage(Messages.PAGE_CHANGE_ICON_WIZARD_PAGE_TITLE) {
@Override
public String getTitle() {
return Messages.PAGE_CHANGE_ICON_WIZARD_PAGE_TITLE;
}
@Override
public String getDescription() {
return Messages.PAGE_CHANGE_ICON_WIZARD_PAGE_DESC;
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
container.setLayout(layout);
Button button = new Button(container, SWT.NONE);
button.setText(Messages.PAGE_CHANGE_ICON_CHOOSE_IMAGE_FILE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
chooseImageFileDialog();
}
});
if (currentImage != null) {
new Label(container, SWT.NONE).setText(Messages.PAGE_CHANGE_ICON_CURRENT_ICON);
new Label(container, SWT.BORDER).setImage(currentImage);
}
new Label(container, SWT.NONE).setText(Messages.PAGE_CHANGE_ICON_NEW_ICON_PREVIEW);
imageLabel = new Label(container, SWT.BORDER);
GridData gd = new GridData(16, 16);
imageLabel.setLayoutData(gd);
setControl(container);
}
private void chooseImageFileDialog() {
FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
String[] filterNames = new String[] {"Image Files", "All Files (*)"}; //$NON-NLS-1$ //$NON-NLS-2$
String[] filterExtensions = new String[] {"*.gif;*.png;*.xpm;*.jpg;*.jpeg;*.tiff", "*"}; //$NON-NLS-1$ //$NON-NLS-2$
fileDialog.setFilterNames(filterNames);
fileDialog.setFilterExtensions(filterExtensions);
String filename = fileDialog.open();
if (filename == null) {
// Dialog was cancelled. Bail out early to avoid handling that case later. Premature?
return;
}
try (InputStream fis = new FileInputStream(filename)) {
ImageData imageData = new ImageData(fis);
// Validate image data
if (imageData.width != 16 || imageData.height != 16) {
imageData = resizeImage(imageData, 16, 16);
}
DisplayToolkit.dispose(imageLabel.getImage());
imageLabel.setImage(new Image(getShell().getDisplay(), imageData));
imageLabel.getParent().layout();
setPageComplete(isPageComplete());
} catch (Exception e) {
// FIXME: Add proper logging
e.printStackTrace();
}
}
private ImageData resizeImage(ImageData imageData, int width, int height) {
Image original = ImageDescriptor.createFromImageData(imageData).createImage();
Image scaled = new Image(Display.getDefault(), width, height);
GC gc = new GC(scaled);
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.drawImage(original, 0, 0, imageData.width, imageData.height, 0, 0, width, height);
gc.dispose();
original.dispose();
ImageData scaledData = scaled.getImageData();
scaled.dispose();
return scaledData;
}
@Override
public boolean isPageComplete() {
return imageLabel.getImage() != null;
}
});
}