SWT drag and drop custom Control displayed next to cursor - java

How do i display some custom Controls or widgets next to cursor when dragging in SWT?
Like Button or Tree or Table not Image.
If it is not possible - how do i render such a Button into Image?

I've found the way of rendering arbitrary Control (widget) into Image using GC (canvas) class. Here is how:
dragSource.addDragListener(new DragSourceListener() {
#Override
public void dragStart(DragSourceEvent dragSourceEvent) {
// getting control widget - Composite in this case
Composite composite = (Composite) ((DragSource) dragSourceEvent.getSource()).getControl();
// Getting dimensions of this widget
Point compositeSize = composite.getSize();
// creating new GC
GC gc = new GC(composite);
// Creating new Image
Image image = new Image(Display.getCurrent(),compositeSize.x,compositeSize.y);
// Rendering widget to image
gc.copyArea(image,0,0);
// Setting widget to DnD image
dragSourceEvent.image = image;
}
... other overriden methods ...
}

You could use an undecorated Shell that follows the mouse position, and in that Shell have the widget(s) you want to display.

Related

To display an image in RCP - View, what do i do?

I have got a code snippet to display image in SWT Application which is
Image image = new Image(display, "c:\\temp\\swt.png");
To display the same in View of RCP application?
What do i do? How do i display the image from an Absolute Path?
View{
createPartControl(){
Image image = new Image(display, "c:\\temp\\swt.png");
}
}
We cant use "display" in the createPartControl method.
The display is also available in a view. There are several ways to get it, one of which is:
Display display = Display.getDefault();
new Image(display, ....);
Your view is also given a Composite so you can also do:
public void createPartControl(Composite parent)
{
.... other code ...
Display display = parent.getDisplay();
new Image(display, ....);
The one thing you must not do is try to create a new Display object.

adding text box on top of gwt java

I am currently working on a school project where we are creating a GWT web application which uses a GeoChart widget to display information about the servers we have crawled. Simply put, I would wish to create a text box on top of our GeoChart widget which shows an interactive world map that takes up the whole screen right now to input information. I have searched quite extensively but I have been unable to come up with an answer.
Here is the code as follows:
#Override
public void onModuleLoad() {
dataReader = (DataReaderAsync) GWT.create(DataReader.class);
RootLayoutPanel.get().add(getSimpleLayoutPanel());
// Create the API Loader
ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
chartLoader.loadApi(new Runnable() {
#Override
public void run() {
getSimpleLayoutPanel().setWidget(getGeoChart());
drawGeoChart();
}
});
}
As GeoChart is a widget, it is wrapped under(i am not sure if this is the right word) a SimpleLayoutPanel right now which will display it into a full screen. As stated above, I would wish to include text above the geoChart. From my understanding, I would need to create another widget containing my text and add both the GeoChart widget and the text box widget into it. What would be the best way to go about doing this?
I believe DialogBox could solve your problem. People usually program the DialogBox in a way that it only pops up into display when certain event is triggered and disappears after user finishes some operation. In your particular case, you can simply make the DialogBox shows up from the beginning and never disappears. And the best part of it: you don't need to add the DialogBox widget to the geoChart widget. Calling dialogBox.center() or dialogBox.show() will do the magic for you.
Here is the sample code.
#Override
public void onModuleLoad() {
dataReader = (DataReaderAsync) GWT.create(DataReader.class);
RootLayoutPanel.get().add(getSimpleLayoutPanel());
// Create the API Loader
ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
chartLoader.loadApi(new Runnable() {
#Override
public void run() {
getSimpleLayoutPanel().setWidget(getGeoChart());
drawGeoChart();
}
});
// NOTE: the first argument 'false' makes sure that this dialog box
// will not disappear when user clicks outside of it
// NOTE: the second argument 'false' makes sure that mouse and keyboard
// events outside of the dialog box will NOT be ignored
DialogBox dialogBox = new DialogBox(false, false);
DialogBox.setText("Demo");
HorizontalPanel panel = new HorizontalPanel();
panel.setSpacing(5);
InlineLabel labelOfTextBox = new InlineLabel("Label");
TextBox textBox = new TextBox();
panel.add(labelOfTextBox);
panel.add(textBox);
dialogBox.setWidget(panel);
// show up in the center
dialogBox.center();
}
Dear all thanks for answering my question. To rectify this problem, I have made use of the custom widget API within GWT(known as Composite). Here's the code as below:
private static class CombinedWidget extends Composite {
public CombinedWidget() {
// place the check above the text box using a vertical panel.
VerticalPanel panel = new VerticalPanel();
DockLayoutPanel dPanel = new DockLayoutPanel(Unit.EM);
panel.setSpacing(13);
panel.add(nameProject);
nameProject.setStyleName("gwt-Group-Label");
panel.add(className);
panel.add(nameKs);
panel.add(nameEsmond);
panel.add(nameBowen);
panel.add(nameAaron);
dPanel.addWest(panel, 13);
dPanel.add(getGeoChart());
// all composites must call initWidget() in their constructors.
initWidget(dPanel);
setWidth("100%");
}
Actually I sort of changed from the original idea. Instead of putting it on the very top, I attached the labels into a VerticalPanel and then created a CombinedWidget(custom widget) which adds both a VerticalPanel and DockLayoutPanel together. I then added the VerticalPanel(containing all the labels) and the GeoChart into the DockLayoutPanel.
This solved my problem of displaying both the labels and the GeoChart on the same page(as originally i added it into a VerticalPanel but it would not work as the app would not read the GeoChart due to the VerticalPanel being overlayed on top of the GeoChart).
If you guys want a picture of my app to visualise, please say so!

Setting a JFreeChart ChartComposite to a fixed size in SWT

I'm trying to add JFree step charts to a scrollable view in eclipse as a plugin, but the charts keep resizing (kinda randomly) each time I modify the view's dimensions.
I want to set them to a fixed size, however large or small the view might get (that's why I made it scrollable in the first place) so I tried something like this:
public void createPartControl(final Composite parent) {
ScrolledComposite scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
Composite comp = new Composite(scrolled, SWT.NONE);
comp.setLayout(new FillLayout(SWT.VERTICAL));
final JFreeChart chart = createChart();
ChartComposite chartComposite1 = new ChartComposite(comp, SWT.NONE, chart, true);
// set chart size attempt
chartComposite1.setSize(500, 200);
chartComposite1.redraw();
comp.redraw();
scrolled.redraw();
// ScrolledComposite stuff
scrolled.setContent(comp);
scrolled.setExpandVertical(true);
scrolled.setExpandHorizontal(true);
scrolled.setAlwaysShowScrollBars(true);
scrolled.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
org.eclipse.swt.graphics.Rectangle r = scrolled.getClientArea();
scrolled.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
}
});
But it doesn't do anything. The chart keeps auto resizing as I modify the view's size.
Any help?
Make sure that you undestand how layouts work in SWT. Understanding Layouts in SWT is a good start.
chartComposite1.setSize() is useless, it will be overwritten by the layout that was set in comp.setLayout( ... ).
... and forcing controls to redraw() doesn't help either as it doesn't update the location or size of controls.
As I suggested earlier, if you know the size of the charts, use a single columned GridLayout and control the size of the charts through GridData::widthHint and heightHint.

TitleAreaDialog - adjusting the title image

So I am creating a image to place in the title area. Everything works with the exception that only a 1/4th of the image is displayed?
my image is actually text and a image combine in one image EX: JKTeater [ ] <-- icon
so right now only JKT is showing in the title area
Here is the create() method
public void create() {
super.create();
setTitle("JKTeater Application");
setMessage("Hello World");
if (image != null) setTitleImage(image);
}
Is there a specific size that the title area code allows for?
Is there a way to place the end of the image at the end of the title area?
Can you use a layout to move it around?
How can I get a black horizonal line at the bottom of the title area?
EDIT
I am sure that it would be asking to much to see if you can actually change the background color from a basic color to a gradient
Here is an example TitleAreaDialog. As you can see, the Image is completely shown and aligned to the right:
public static void main(String[] args) {
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
TitleAreaDialog dialog = new MyTitleAreaDialog(shell);
dialog.setTitleAreaColor(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB());
dialog.open();
}
private static class MyTitleAreaDialog extends TitleAreaDialog
{
private Image image;
public MyTitleAreaDialog(Shell parentShell) {
super(parentShell);
image = new Image(Display.getDefault(), "/home/baz/Desktop/StackOverflow.png");
}
#Override
public boolean close() {
if (image != null)
image.dispose();
return super.close();
}
#Override
protected Control createContents(Composite parent) {
Control contents = super.createContents(parent);
setTitle("Title");
setMessage("Message");
if (image != null)
setTitleImage(image);
return contents;
}
#Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
// YOUR LINE HERE!
Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
line.setLayoutData(new GridData(SWT.FILL, SWT.END, true, true));
return composite;
}
}
Is there a specific size that the title area code allows for?
AFAIK there are no restrictions to the size. I tried using an Image that was larger than my screen resolution and it was fully displayed. The Dialog was obviously unusable though.
I am sure that it would be asking to much to see if you can actually change the background color from a basic color to a gradient
The background color can be changed using dialog.setTitleAreaColor(RGB) (in this case the widget background color), but you cannot use a gradient. There is a deprecated method getTitleArea() which would return the title area Composite, but I really wouldn't recommend using that.
How can I get a black horizonal line at the bottom of the title area?
The line at the bottom was achieved by using:
Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
line.setLayoutData(new GridData(SWT.FILL, SWT.END, true, true));
Can you use a layout to move it around?
There is a similar question here:
Moving an image of a TitleAreaDialog to the left
The answers there explain how to change details of the TitleAreaDialog. Maybe read up on them.

How to remove the title bar from a JFrame screenshot?

I'm capturing a screenshot image of a JFrame via a "double buffering" approach, per below:
public BufferedImage getScreenshot() {
java.awt.Dimension dim = this.getPreferredSize();
BufferedImage image = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB);
this.paint(image.getGraphics());
return image;
}
where this extends JFrame. The image that I get has a blank strip along the top where the title bar was. What's the most straightforward way to capture an image of the contents of the JFrame without the extra space allocated for the title bar?
You should be able to use the Screen Image class. Just specify the content pane of the frame (or the root pane if you have a menu) as the component you want the image of.
Or your basic code should work, again just specify the content pane (or root pane) as the component you want to paint, not the frame itelf.

Categories