I try to print a swt TreeViewer to png file. With:
Tree tree = treeViewer.getTree();
Image image = new Image(display, tree.getSize().x, tree.getParent().getSize().y);
GC gc = new GC(image);
System.out.println(new File(pathToSave).getAbsolutePath());
tree.print(gc);
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(pathToSave, SWT.IMAGE_PNG);
gc.dispose();
image.dispose();
the png contains only the visible part of the tree. The tree has a scrollbar because it contains more elements than fit on the form.
I would like to print the tree with all elements visible and without scrollbar. Any ideas?
On swing components i could use .paintall().. swt components don't seem to know that.
First, the size of the image should have the size the tree would have without scrolls, and not the current size. For that you should use computeSize(SWT.DEFAULT, SWT.DEFAULT, true). Then you should resize the tree that size, print, and then resize it back. Since you don't want users to notice that, during this operation you should disabled draws with setRedraw(false).
Here is a full snippet that does all this:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FillLayout());
final Tree tree = new Tree(composite, SWT.NONE);
for (int i = 0; i < 100; i++) {
final TreeItem treeItem = new TreeItem(tree, SWT.NONE);
treeItem.setText(String.format("item %d long name", i));
}
tree.addListener(SWT.DefaultSelection, new Listener() {
#Override
public void handleEvent(Event event) {
tree.getParent().setRedraw(false);
final Point originalSize = tree.getSize();
final Point size = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
final Image image = new Image(display, size.x, size.y);
final GC gc = new GC(image);
tree.setSize(size);
tree.print(gc);
tree.setSize(originalSize);
final ImageLoader loader = new ImageLoader();
loader.data = new ImageData[]{image.getImageData()};
final String pathToSave = "out.png";
System.out.println(new File(pathToSave).getAbsolutePath());
loader.save(pathToSave, SWT.IMAGE_PNG);
gc.dispose();
image.dispose();
tree.getParent().setRedraw(true);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Press enter to save the file.
Related
I want to get the control the mouse hovers over which normally is done by Display#getCursorControl. However when one control in the hierarchy is disabled, this method doesn't work any longer:
Example:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(400, 300);
shell.setLayout(new GridLayout(2, false));
final Label mouseControl = new Label(shell, SWT.BORDER);
mouseControl.setLayoutData(GridDataFactory.fillDefaults().span(2, 1).grab(true, true).create());
display.addFilter(SWT.MouseMove,
e -> mouseControl.setText("" + e.display.getCursorControl()));
final Group enabledGroup = new Group(shell, SWT.NONE);
enabledGroup.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
enabledGroup.setText("Enabled Group");
createControls(enabledGroup);
final Group disabledGroup = new Group(shell, SWT.NONE);
disabledGroup.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
disabledGroup.setText("Disabled Group");
disabledGroup.setEnabled(false);
createControls(disabledGroup);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static void createControls(Composite parent) {
parent.setLayout(new GridLayout());
final Label label = new Label(parent, SWT.NONE);
label.setText("Label");
final Text text = new Text(parent, SWT.BORDER);
text.setText("Text");
}
Hold the mouse over the left label and then over the right one. The control is only displayed for an enabled parent, else the shell is displayed.
How do I get the control below the mouse pointer? Do I have to implement this functionality myself? Are there any methods that can help me or do I have to calculate the bounds of each control inside the tree and check if it is on the mouse position?
I can't see anything in Display that would help.
The following will search the children of a Shell for a control containing the cursor and works with disabled controls:
static Control findCursorinShellChildren(final Shell shell)
{
return findLocationInCompositeChildren(shell, shell.getDisplay().getCursorLocation());
}
static Control findLocationInCompositeChildren(final Composite composite, final Point displayLoc)
{
final var compositeRelativeLoc = composite.toControl(displayLoc);
for (final var child : composite.getChildren())
{
if (child.getBounds().contains(compositeRelativeLoc))
{
if (child instanceof Composite)
{
final var containedControl = findLocationInCompositeChildren((Composite)child, displayLoc);
return containedControl != null ? containedControl : child;
}
return child;
}
}
return null;
}
I imagine this is going to be significantly slower than Display.getCursorControl
I'm trying to display a progress bar (using SWT's ProgressBar class). However, the bar isn't very visible due to my window's background. Therefore, I'm trying to put a white rectangle (using GC.fillRoundedRectangle()) behind the progress bar. I can't find a way to display the progress bar on top of the rectangle. How can I achieve "layering" in SWT?
Thanks!
EDIT: I tried #greg-449's suggestion, but all I got was a blank window-am I implementing this wrong?
public static void main(String [] args){
Display d = new Display();
Shell parent = new Shell(d);
parent.setSize(500, 500);
parent.open();
makeBar(parent);
while (!parent.isDisposed()) {
if (!d.readAndDispatch()) {
d.sleep();
}
}
}
private static void makeBar(Shell parent) {
Composite body = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight = 20;
layout.marginWidth = 20;
body.setLayout(layout);
body.addListener(SWT.Paint, event ->
{
Rectangle rect = body.getClientArea();
event.gc.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WHITE));
event.gc.fillRoundRectangle(rect.x, rect.y, rect.width, rect.height, 20, 20);
});
ProgressBar bar = new ProgressBar(body, SWT.HORIZONTAL);
bar.setMaximum(100);
bar.setSelection(40);
}
This is what I see when I run this code:
Output of code
You could just use a Composite which you paint with the ProgressBar as a child.
Something like:
Composite body = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginHeight = 20;
layout.marginWidth = 20;
body.setLayout(layout);
body.addListener(SWT.Paint, event ->
{
Rectangle rect = body.getClientArea();
event.gc.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WHITE));
event.gc.fillRoundRectangle(rect.x, rect.y, rect.width, rect.height, 20, 20);
});
ProgressBar bar = new ProgressBar(body, SWT.HORIZONTAL);
bar.setMaximum(100);
bar.setSelection(40);
I have a BMP in a bytearray. I would like to display the BMP in an Eclipse Plugin using SWT.
If I want to display the BMP using swing - it can be done as follows:
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new ByteArrayInputStream(getLocalByteArray()));
} catch (IOException ex) {
}
JLabel jLabel = new JLabel(new ImageIcon(bufferedImage));
JPanel jPanel = new JPanel();
jPanel.add(jLabel);
this.add(jPanel);
Update:
The BMP will be represented as a byte array. This is pre requisite of this.
How do I do this in an Eclipse Plugin using SWT? Note I am using a Perspective.
An SWT Image can directly be created from an input stream. Several data formats are supported, including Windows format BMPs.
For example:
Image image = new Image( display, new ByteArrayInputStream( ... ) );
The resulting image can then be set on a Label or used elsewhere.
You can simply specify the file in the Image constructor and then set it to a Label.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
Image image = new Image(display, "image.bmp");
label.setImage(image);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
image.dispose();
}
Remember that you have to dispose() of the image yourself to not create a memory leak.
OK - I figured it out. As the code is short I've included the context:
public void createPartControl(Composite parent) {
try {
BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(getLocalByteArray()));
ImageData imageData = new ImageData(inputStream);
Image image = ImageDescriptor.createFromImageData(imageData).createImage();
// Create the canvas for drawing
Canvas canvas = new Canvas( parent, SWT.NONE);
canvas.addPaintListener( new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc = e.gc;
gc.drawImage( image,10,10);
}
});
I am looking for a method to add a kind of watermark to a SWT CTabFolder.
My goal is that the tab folder does not look as "boring" if there are no tabs present.
I am aware of the setBackgroundImage method of CTabFolder. Unfortunately, this seems to be non adjustable and can only display an image in "tiled" format.
Do you know of any way to add a centered image to an empty tab folder?
You'll have to add your own Listeners for SWT.Paint, and SWT.Resize. Then draw your Image on the GC. Here is an example:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Image image = new Image(null, "info.png");
final CTabFolder folder = new CTabFolder(shell, SWT.TOP);
folder.addListener(SWT.Paint, new Listener()
{
#Override
public void handleEvent(Event event)
{
if (image.isDisposed())
return;
Rectangle parentSize = folder.getBounds();
int tabHeight = folder.getTabHeight();
Rectangle imageSize = image.getBounds();
event.gc.drawImage(image, (parentSize.width - imageSize.width) / 2, (parentSize.height - imageSize.height + tabHeight) / 2);
}
});
folder.addListener(SWT.Resize, new Listener()
{
#Override
public void handleEvent(Event event)
{
folder.redraw();
}
});
CTabItem item = new CTabItem(folder, SWT.CLOSE);
item.setText("TEST");
Composite content = new Composite(folder, SWT.NONE);
content.setLayout(new FillLayout());
new Label(content, SWT.NONE).setText("bla");
item.setControl(content);
shell.pack();
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
image.dispose();
}
Looks like this when you have a CTabItem:
And like this when you close the item:
Here is my code which actually does all the GUI work for Domain(right side contained in the picture attached).In this function What i am doing is I am creating a composite "test" on the "newTabFolder".Then i am creating ScrolledComposite "sc" on it and then Creating a composite "compositeInTab" on it and after placing all the widgets on "compositeInTab" I am creating a TabItem for placing the composite "test" on it.
public DomainUI(Composite composite, TabFolder newTabFolder, boolean comingFromSelf)
{
Composite test = new Composite(newTabFolder,SWT.NONE);
test.setLayout(new FillLayout());
ScrolledComposite sc = new ScrolledComposite(test, SWT.V_SCROLL|SWT.H_SCROLL);
final Composite compositeInTab = new Composite(sc, SWT.NONE);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinHeight(compositeInTab.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
sc.setMinWidth(compositeInTab.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
sc.setContent(compositeInTab);
compositeInTab.setLayout(null);
sc.setAlwaysShowScrollBars(true);
/*HERE I AM CREATING LABELS AND TEXT FIELDS AND SETTING THEIR BOUNDS*/
systemCodeLabel = new Label(compositeInTab, 0);
systemCodeText = new Text(compositeInTab, SWT.BORDER);
systemCodeLabel.setText("System Code");
systemCodeLabel.setBounds(350, 60, 100, 15);
systemCodeText.setBounds(480, 60, 150, 17);
// CREATION OF LABELS AND TEXTFIELDS ENDED
// CREATION OF TABLE STARTS
myTable = new CreateTable(compositeInTab, 1);
myTable.setBounds(50, 230, 0, 0);
myTable.table.setSize(myTable.table.computeSize(570, 250));
//here i filled data in table
for(int i=0; i<myTable.table.getColumnCount(); i++)
{
myTable.table.getColumn(i).pack();
myTable.table.getColumn(i).setWidth(myTable.table.getColumn(i).getWidth()+10);
}
TabItem tabItem1 = new TabItem(newTabFolder, SWT.NONE);
tabItem1.setText("Domain");
tabItem1.setControl(test);
newTabFolder.setBounds(0, 0, 480, 300);
}
It seems to be a matter of layouts. I fiddled around a little bit and managed to get this working:
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
ScrolledComposite scrollComp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
Composite innerComp = new Composite(scrollComp, SWT.NONE);
innerComp.setLayout(new GridLayout(4, true));
for(int i = 0; i < 32; i++)
new Button(innerComp, SWT.PUSH).setText("Button");
scrollComp.setMinHeight(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
scrollComp.setMinWidth(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
scrollComp.setContent(innerComp);
scrollComp.setExpandHorizontal(true);
scrollComp.setExpandVertical(true);
scrollComp.setAlwaysShowScrollBars(true);
shell.pack();
shell.open();
shell.setSize(200, 200);
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
It will show the scrollbars. However, if I change the layout of the shell to a GridLayout, it will not work. Maybe your "combination" of layouts seems to be the problem...