use of org.eclipse.draw2d.SWTGraphics in project cogtool by cogtool.
the class GraphicalWidgetBase method regenerateCaches.
protected void regenerateCaches(final int width, final int height) {
// For performance reasons, we defer regeneration until we exit fast mode
if (!this.fast) {
if (View.isDrawingOK()) {
// Recompute the cached data in the background
// new Thread () {
// public void run() {
boolean needsRepaint = false;
// Recompute the midground cache if it is dirty
if (cachedMidgroundDirty) {
needsRepaint = true;
if ((width == 0) || (height == 0)) {
throw new IllegalArgumentException("Width or Height on a widget is 0. This " + "should have been prevented in setShape " + "Size");
}
// Create a temporary canvas on which to draw the midground
Image img = new Image(null, width, height);
GC gc = new GC(img);
SWTGraphics g = new SWTGraphics(gc);
g.setAntialias(SWT.OFF);
// Use the renderer & clipper to paint the midground nicely
g.setBackgroundColor(getSelectionColor());
renderer.paintMidground(g);
// value.
if (OSUtils.WINDOWS) {
ImageData id = img.getImageData();
id.alpha = displayAlpha.determineAlpha(false);
img.dispose();
img = new Image(null, id);
}
// WARNING: this might dispose img, or it might return it
// DO NOT DISPOSE img!
Image newFG = clipper.clip(img);
// Clean the cache
cachedMidgroundDirty = false;
cachedMidground.dispose();
cachedMidground = newFG;
// cachedMidgroundData = newFG.getImageData();
// Dispose of temporary resources
// newFG.dispose();
g.dispose();
gc.dispose();
}
// Recompute the background cache if it is dirty
if (cachedBackgroundDirty) {
needsRepaint = true;
Image newBG = null;
// Check if background is present
byte[] bgImageData = model.getImage();
if (bgImageData != null) {
ImageData bg = new ImageData(new ByteArrayInputStream(bgImageData));
newBG = clipper.clip(new Image(null, bg.scaledTo(width, height)));
}
// Clean the cache
cachedBackgroundDirty = false;
if (cachedBackground != null) {
cachedBackground.dispose();
}
cachedBackground = newBG;
}
// Make sure the results of our update are shown
if (needsRepaint && View.isDrawingOK()) {
// WindowUtil.globalDisplay.syncExec(
// new Runnable() {
// public void run() {
repaint();
// }
// }
// );
}
// }
// }.start();
}
}
}
use of org.eclipse.draw2d.SWTGraphics in project archi by archimatetool.
the class ColoursFontsPreferencePage method createColoursTab.
private void createColoursTab() {
// Reset everything
resetColorsCache(false);
fImageRegistry = new ImageRegistry();
Composite client = new Composite(fTabfolder, SWT.NULL);
client.setLayout(new GridLayout(2, false));
TabItem item = new TabItem(fTabfolder, SWT.NONE);
item.setText(Messages.ColoursFontsPreferencePage_23);
item.setControl(client);
Label label = new Label(client, SWT.NULL);
label.setText(Messages.ColoursFontsPreferencePage_0);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
label.setLayoutData(gd);
// Tree
fTreeViewer = new TreeViewer(client);
gd = new GridData(GridData.FILL_BOTH);
// need this to set a smaller height
gd.heightHint = 80;
fTreeViewer.getTree().setLayoutData(gd);
// Tree Double-click listener
fTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
Object[] selected = ((IStructuredSelection) fTreeViewer.getSelection()).toArray();
if (isValidTreeSelection(selected)) {
RGB newRGB = openColorDialog(selected[0]);
if (newRGB != null) {
for (Object object : selected) {
setColor(object, newRGB);
}
}
}
}
});
// Tree Selection Changed Listener
fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
Object[] selected = ((IStructuredSelection) event.getSelection()).toArray();
fEditFillColorButton.setEnabled(isValidTreeSelection(selected));
fResetFillColorButton.setEnabled(isValidTreeSelection(selected));
}
});
// Tree Content Provider
fTreeViewer.setContentProvider(new ITreeContentProvider() {
@Override
public void dispose() {
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof String) {
return new Object[] { new TreeGrouping(Messages.ColoursFontsPreferencePage_32, ArchimateModelUtils.getStrategyClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_7, ArchimateModelUtils.getBusinessClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_8, ArchimateModelUtils.getApplicationClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_9, ArchimateModelUtils.getTechnologyClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_33, ArchimateModelUtils.getPhysicalClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_10, ArchimateModelUtils.getMotivationClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_11, ArchimateModelUtils.getImplementationMigrationClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_17, ArchimateModelUtils.getOtherClasses()), new TreeGrouping(Messages.ColoursFontsPreferencePage_34, new Object[] { IArchimatePackage.eINSTANCE.getDiagramModelNote(), IArchimatePackage.eINSTANCE.getDiagramModelGroup() }), DEFAULT_ELEMENT_LINE_COLOR, DEFAULT_CONNECTION_LINE_COLOR };
}
return null;
}
@Override
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof TreeGrouping) {
return ((TreeGrouping) parentElement).children;
}
return null;
}
@Override
public Object getParent(Object element) {
return null;
}
@Override
public boolean hasChildren(Object element) {
return element instanceof TreeGrouping;
}
});
// Tree Label Provider
fTreeViewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof EClass) {
return ArchiLabelProvider.INSTANCE.getDefaultName((EClass) element);
}
if (element instanceof TreeGrouping) {
return ((TreeGrouping) element).title;
}
if (element instanceof String) {
String s = (String) element;
if (s.equals(DEFAULT_ELEMENT_LINE_COLOR)) {
return Messages.ColoursFontsPreferencePage_12;
}
if (s.equals(DEFAULT_CONNECTION_LINE_COLOR)) {
return Messages.ColoursFontsPreferencePage_18;
}
}
return null;
}
@Override
public Image getImage(Object element) {
if (element instanceof TreeGrouping) {
return IArchiImages.ImageFactory.getImage(IArchiImages.ECLIPSE_IMAGE_FOLDER);
}
return getColorSwatch(element);
}
// Create a coloured image based on colour and add to the image registry
private Image getColorSwatch(Object object) {
String key = getColorKey(object);
Image image = fImageRegistry.get(key);
if (image == null) {
image = new Image(Display.getCurrent(), 16, 16);
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
graphics.setBackgroundColor(fColorsCache.get(object));
graphics.fillRectangle(0, 0, 15, 15);
graphics.drawRectangle(0, 0, 15, 15);
gc.dispose();
graphics.dispose();
fImageRegistry.put(key, image);
}
return image;
}
});
// fTreeViewer.setAutoExpandLevel(2);
// Set Content in Tree
// $NON-NLS-1$
fTreeViewer.setInput("");
// Buttons
Composite buttonClient = new Composite(client, SWT.NULL);
gd = new GridData(SWT.TOP, SWT.TOP, false, false);
buttonClient.setLayoutData(gd);
buttonClient.setLayout(new GridLayout());
// Edit...
fEditFillColorButton = new Button(buttonClient, SWT.PUSH);
fEditFillColorButton.setText(Messages.ColoursFontsPreferencePage_13);
fEditFillColorButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
fEditFillColorButton.setEnabled(false);
fEditFillColorButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Object[] selected = ((IStructuredSelection) fTreeViewer.getSelection()).toArray();
if (isValidTreeSelection(selected)) {
RGB newRGB = openColorDialog(selected[0]);
if (newRGB != null) {
for (Object object : selected) {
setColor(object, newRGB);
}
}
}
}
});
// Reset
fResetFillColorButton = new Button(buttonClient, SWT.PUSH);
fResetFillColorButton.setText(Messages.ColoursFontsPreferencePage_14);
fResetFillColorButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
fResetFillColorButton.setEnabled(false);
fResetFillColorButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Object[] selected = ((IStructuredSelection) fTreeViewer.getSelection()).toArray();
if (isValidTreeSelection(selected)) {
for (Object object : selected) {
resetColorToInbuiltDefault(object);
}
}
}
});
// Import Scheme
Button importButton = new Button(buttonClient, SWT.PUSH);
importButton.setText(Messages.ColoursFontsPreferencePage_2);
importButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
importButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
importUserColors();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
// Export Scheme
Button exportButton = new Button(buttonClient, SWT.PUSH);
exportButton.setText(Messages.ColoursFontsPreferencePage_3);
exportButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
exportButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
exportUserColors();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
Group elementColorGroup = new Group(client, SWT.NULL);
elementColorGroup.setLayout(new GridLayout(2, false));
elementColorGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
elementColorGroup.setText(Messages.ColoursFontsPreferencePage_20);
// Derive element line colours
fDeriveElementLineColorsButton = new Button(elementColorGroup, SWT.CHECK);
fDeriveElementLineColorsButton.setText(Messages.ColoursFontsPreferencePage_19);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
fDeriveElementLineColorsButton.setLayoutData(gd);
fDeriveElementLineColorsButton.setSelection(getPreferenceStore().getBoolean(DERIVE_ELEMENT_LINE_COLOR));
fDeriveElementLineColorsButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
fElementLineColorContrastSpinner.setEnabled(fDeriveElementLineColorsButton.getSelection());
fContrastFactorLabel.setEnabled(fDeriveElementLineColorsButton.getSelection());
}
});
fContrastFactorLabel = new Label(elementColorGroup, SWT.NULL);
fContrastFactorLabel.setText(Messages.ColoursFontsPreferencePage_21);
fElementLineColorContrastSpinner = new Spinner(elementColorGroup, SWT.BORDER);
fElementLineColorContrastSpinner.setMinimum(1);
fElementLineColorContrastSpinner.setMaximum(10);
fElementLineColorContrastSpinner.setSelection(getPreferenceStore().getInt(DERIVE_ELEMENT_LINE_COLOR_FACTOR));
label = new Label(elementColorGroup, SWT.NULL);
label.setText(Messages.ColoursFontsPreferencePage_22);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
// Persist user default colours
fPersistUserDefaultColors = new Button(client, SWT.CHECK);
fPersistUserDefaultColors.setText(Messages.ColoursFontsPreferencePage_1);
fPersistUserDefaultColors.setLayoutData(gd);
fPersistUserDefaultColors.setSelection(getPreferenceStore().getBoolean(SAVE_USER_DEFAULT_COLOR));
// Use colours in application
fShowUserDefaultFillColorsInApplication = new Button(client, SWT.CHECK);
fShowUserDefaultFillColorsInApplication.setText(Messages.ColoursFontsPreferencePage_6);
fShowUserDefaultFillColorsInApplication.setLayoutData(gd);
fShowUserDefaultFillColorsInApplication.setSelection(getPreferenceStore().getBoolean(SHOW_FILL_COLORS_IN_GUI));
}
use of org.eclipse.draw2d.SWTGraphics in project archi by archimatetool.
the class DiagramUtils method createModelReferencedImage.
private static ModelReferencedImage createModelReferencedImage(IFigure figure, double scale, int margin) {
if (scale <= 0) {
scale = 1;
}
if (scale > 5) {
scale = 5;
}
Rectangle bounds = getMinimumBounds(figure);
if (bounds == null) {
// At least a minimum
bounds = new Rectangle(0, 0, 100, 100);
} else {
bounds.expand(margin / scale, margin / scale);
}
Image image = new Image(Display.getDefault(), (int) (bounds.width * scale), (int) (bounds.height * scale));
GC gc = new GC(image);
SWTGraphics swtGraphics = new SWTGraphics(gc);
Graphics graphics = swtGraphics;
// If scaled, then scale now
if (scale != 1) {
graphics = new ScaledGraphics(swtGraphics);
graphics.scale(scale);
}
// Compensate for negative co-ordinates
graphics.translate(bounds.x * -1, bounds.y * -1);
// Paint onto graphics
figure.paint(graphics);
// Dispose
gc.dispose();
graphics.dispose();
if (swtGraphics != graphics) {
swtGraphics.dispose();
}
return new ModelReferencedImage(image, bounds);
}
use of org.eclipse.draw2d.SWTGraphics in project dbeaver by dbeaver.
the class ERDExportRasterImage method exportDiagram.
@Override
public void exportDiagram(EntityDiagram diagram, IFigure figure, DiagramPart diagramPart, File targetFile) throws DBException {
String filePath = targetFile.getAbsolutePath().toLowerCase();
int imageType = SWT.IMAGE_BMP;
if (filePath.endsWith(".jpg")) {
imageType = SWT.IMAGE_JPEG;
} else if (filePath.endsWith(".png")) {
imageType = SWT.IMAGE_PNG;
} else if (filePath.endsWith(".gif")) {
imageType = SWT.IMAGE_GIF;
}
Rectangle contentBounds = figure instanceof FreeformLayeredPane ? ((FreeformLayeredPane) figure).getFreeformExtent() : figure.getBounds();
try {
if (contentBounds.isEmpty()) {
throw new DBException("Can't save empty diagram");
}
try (FileOutputStream fos = new FileOutputStream(targetFile)) {
Rectangle r = figure.getBounds();
GC gc = null;
Graphics graphics = null;
try {
Image image = new Image(null, contentBounds.width + MARGIN_X * 4, contentBounds.height + MARGIN_Y * 4);
try {
gc = new GC(image);
// gc.setClipping(0, 0, contentBounds.width + MARGIN_X * 2, contentBounds.height + MARGIN_Y * 2);
graphics = new SWTGraphics(gc);
graphics.translate(r.x * -1 + MARGIN_X, r.y * -1 + MARGIN_Y);
figure.paint(graphics);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[1];
if (imageType != SWT.IMAGE_JPEG) {
// Convert to 8bit color
imageLoader.data[0] = ImageUtils.makeWebImageData(image);
} else {
// Use maximum colors for JPEG
imageLoader.data[0] = image.getImageData();
}
imageLoader.save(fos, imageType);
} finally {
UIUtils.dispose(image);
}
} finally {
if (graphics != null) {
graphics.dispose();
}
UIUtils.dispose(gc);
}
fos.flush();
}
UIUtils.launchProgram(filePath);
} catch (Throwable e) {
DBUserInterface.getInstance().showError("Save ERD as image", null, e);
}
}
use of org.eclipse.draw2d.SWTGraphics in project dbeaver by dbeaver.
the class ERDExportRasterImage method exportDiagram.
@Override
public void exportDiagram(EntityDiagram diagram, IFigure figure, DiagramPart diagramPart, File targetFile) throws DBException {
int imageType = SWT.IMAGE_BMP;
{
String filePath = targetFile.getName().toLowerCase();
if (filePath.endsWith(".jpg")) {
imageType = SWT.IMAGE_JPEG;
} else if (filePath.endsWith(".png")) {
imageType = SWT.IMAGE_PNG;
} else if (filePath.endsWith(".gif")) {
imageType = SWT.IMAGE_GIF;
}
}
Rectangle contentBounds = figure instanceof FreeformLayeredPane ? ((FreeformLayeredPane) figure).getFreeformExtent() : figure.getBounds();
try {
if (contentBounds.isEmpty()) {
throw new DBException("Can't serializeDiagram empty diagram");
}
try (FileOutputStream fos = new FileOutputStream(targetFile)) {
Rectangle r = figure.getBounds();
GC gc = null;
Graphics graphics = null;
try {
Image image = new Image(null, contentBounds.width + MARGIN_X * 4, contentBounds.height + MARGIN_Y * 4);
try {
gc = new GC(image);
// gc.setClipping(0, 0, contentBounds.width + MARGIN_X * 2, contentBounds.height + MARGIN_Y * 2);
graphics = new SWTGraphics(gc);
graphics.translate(r.x * -1 + MARGIN_X, r.y * -1 + MARGIN_Y);
figure.paint(graphics);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[1];
if (imageType == SWT.IMAGE_GIF) {
// Convert to 8bit color
imageLoader.data[0] = ImageUtils.makeWebImageData(image);
} else {
// Use maximum colors for JPEG, PNG
imageLoader.data[0] = ImageUtils.getImageDataAtCurrentZoom(image);
}
imageLoader.save(fos, imageType);
} finally {
UIUtils.dispose(image);
}
} finally {
if (graphics != null) {
graphics.dispose();
}
UIUtils.dispose(gc);
}
fos.flush();
}
UIUtils.launchProgram(targetFile.getAbsolutePath());
} catch (Throwable e) {
DBWorkbench.getPlatformUI().showError("Save ERD as image", null, e);
}
}
Aggregations