Using java, i created a shell and then opened the browser inside the shell, i want to move the shell to top right corner of the screen. I am trying to use setBound function, but it is not working.. how do i find the coordinates of the top right screen.
How to set the shell to a specific location
Code:
{
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds( x, y, 200 ,200);
}
You can find the top right of the screen by getting the bounds of the Monitor:
final Monitor monitor = Display.getCurrent().getPrimaryMonitor();
final Rectangle monitorBounds = monitor.getBounds();
final Point topRight = new Point(monitorBounds.x + monitorBounds.width, monitorBounds.y);
If you want to account for multiple monitors, you can use Display#getMonitors(), and implement some logic to pick the Monitor that you care about.
You're on the right track for positioning the Shell. You can use the topRight Point from above, and subtract the width of the Shell to keep it on-screen. Alternatively there is a Shell#setLocation(Point) method that you can use.
For example:
public class ShellLocationTest {
private final Display display;
private final Shell shell;
public ShellLocationTest() {
display = new Display();
shell = new Shell(display, SWT.NONE);
final Point shellSize = new Point(400, 200);
shell.setSize(shellSize);
shell.setLayout(new FillLayout());
final Rectangle monitorBounds = display.getPrimaryMonitor().getBounds();
final Point shellLocation = new Point(monitorBounds.x + monitorBounds.width - shellSize.x, monitorBounds.y);
shell.setLocation(shellLocation);
}
public void run() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String... args) {
new ShellLocationTest().run();
}
}
Related
SWT Table has void setTopIndex(int index) and int getTopIndex().
I really need setTopPixel(int pixel) or setTopIndexFractional(double index).
I tried putting a Table into a ScrolledComposite, as such:
public class PixelTable extends ScrolledComposite {
private final Table table;
private final double itemHeight;
public PixelTable(Composite parent) {
super(parent, SWT.V_SCROLL | SWT.BORDER);
// setup the ScrolledComposite
table = new Table(this, SWT.VIRTUAL);
this.setExpandHorizontal(true);
this.setContent(table);
itemHeight = table.getItemHeight();
// setup the Table
table.setLinesVisible(true);
table.addListener(SWT.SetData, new Listener() {
#Override
public void handleEvent(Event e) {
int row = e.index;
TableItem item = (TableItem) e.item;
item.setText(Integer.toString(row));
}
});
}
/** Returns the fractional top index. */
public double getTopIndex() {
return getOrigin().y / itemHeight;
}
/** Sets the fractional top index. */
public void setTopIndex(double top) {
this.setOrigin(0, (int) Math.round(top * itemHeight));
}
/** Sets the item count. */
public void setItemCount(int count) {
table.setItemCount(count);
Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
table.setSize(size);
}
}
This works for while, but once the inner Table's size gets bigger than ~65,000 pixels (~2,500 rows), the Table shows its scroll bar as well (probably an unsigned short somewhere on the native side). Additionally, it will be pretty tricky to get Table headers to work with this approach, since they'll scroll away.
Does anybody know what the issues would be (or how to get started) to add a per-pixel interface to the SWT API? I'm willing to branch the SWT source if necessary. I only care about the latest APIs for Win / OS X / Linux (e.g. I don't care if I have to break compatibility with old stuff).
If you want per-pixel scrolling, then just do the scrolling yourself. All you need to do is listen for SWT.MouseWheel, set Event#doit to false and then set the selection of the ScrollBar according to the scroll direction (increase/decrease by 1 (<-- that's the one pixel)).
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 Table table = new Table(shell, SWT.V_SCROLL);
for (int i = 0; i < 100; i++)
new TableItem(table, SWT.NONE).setText("Item: " + i);
table.addListener(SWT.MouseWheel, new Listener()
{
#Override
public void handleEvent(Event e)
{
int increment = table.getVerticalBar().getSelection();
if (e.count < 0)
increment++;
else if (e.count > 0)
increment--;
table.getVerticalBar().setSelection(increment);
e.doit = false;
}
});
shell.pack();
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
I've been playing with the Zest GraphViewer for over a week now trying to discover what it can do for my application, but I haven't been able to get it's behaviour in-line with my requirements thus far.
I'm hoping that someone can point me to the resources I need, becasue I just can't find all that much of use with Google, or can tell me if what I want is possible.
Version
I've got Zest core 1.3.0 and Zest layout 1.1.0 in my dependencies for the RCP project. This came from the download site I took from the Zest site.
Requirements
Single Node/Edge selection.
De-selection of node/edge when white-space is selected (which may be a bug?)
Right click functionality to change when over a node. (detect when mouse is over a node)
The right click functionality could come from the single selection since I could have the popup anywhere but base it on the current selected node, but I'd rather not do that.
Without being able to do this, due to the nature or our application and users, I may also have a requirement to find another RCP/SWT based Graph drawing package that does have this functionality.
Any help on any of these issues would be greatly appreciated.
Glen x
Based on the Zest tutorial by Vogella, I came up with this:
public static void main(String[] args) throws FontFormatException, IOException
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Graph graph = new Graph(shell, SWT.NONE);
GraphNode node1 = new GraphNode(graph, SWT.NONE, "Jim");
GraphNode node2 = new GraphNode(graph, SWT.NONE, "Jack");
GraphNode node3 = new GraphNode(graph, SWT.NONE, "Joe");
GraphNode node4 = new GraphNode(graph, SWT.NONE, "Bill");
/* Context menu */
graph.addMenuDetectListener(new MenuDetectListener()
{
#Override
public void menuDetected(MenuDetectEvent e)
{
Point point = graph.toControl(e.x, e.y);
IFigure fig = graph.getFigureAt(point.x, point.y);
if (fig != null)
{
Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem exit = new MenuItem(menu, SWT.NONE);
exit.setText("Hello! This is " + ((GraphLabel) fig).getText());
menu.setVisible(true);
}
else
{
Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem exit = new MenuItem(menu, SWT.NONE);
exit.setText("Nothing here...");
menu.setVisible(true);
}
}
});
/* Lets have a directed connection */
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1, node2);
/* Lets have a dotted graph connection */
new GraphConnection(graph, ZestStyles.CONNECTIONS_DOT, node2, node3);
/* Standard connection */
new GraphConnection(graph, SWT.NONE, node3, node1);
/* Change line color and line width */
GraphConnection graphConnection = new GraphConnection(graph, SWT.NONE, node1, node4);
graphConnection.changeLineColor(shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
/* Also set a text */
graphConnection.setText("This is a text");
graphConnection.setHighlightColor(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
graphConnection.setLineWidth(3);
graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
graph.addSelectionListener(new SelectionAdapter()
{
#Override
public void widgetSelected(SelectionEvent e)
{
System.out.println(e.item);
/* Make sure that only the newest item is selected */
graph.setSelection(new GraphItem[]{(GraphItem)e.item});
}
});
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It supports single node/edge selection, de-selection and right-click functionality as requested.
Looks like this:
If you use the GraphViewer example of the tutorial and add this to the View code, it still works fine:
final Graph graph = viewer.getGraphControl();
graph.addSelectionListener(new SelectionAdapter()
{
#Override
public void widgetSelected(SelectionEvent e)
{
System.out.println(e.item);
graph.setSelection(new GraphItem[]{(GraphItem)e.item});
}
});
I created an eclipse wizard which i want to test now with the SWTBot. I already used the SWTWorkbenchBot which finally works but i want to test the wizard now without the eclipse workbench. Thats why i created a shell in my testclass where i want to put on my wizardpage, but all i could see, was a empty shell without my wizardPage.
So i created a new shell class which included this code:
public static void main(String args[]) {
try {
Display display = Display.getDefault();
HorrorShell shell = new HorrorShell(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
*
* #param display
*/
public HorrorShell(Display display) {
super(display, SWT.SHELL_TRIM);
setLayout(new FillLayout());
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
ManualSettingsWizardPage page = new ManualSettingsWizardPage();
page.createControl(this);
}
With the shell class it works, my wizardpage was shown but if i try to run my testclass as SWTBotTest or as JUnitTest it wont show me anything but a empty shell.
Here's the code in my testclass:
private ManualSettingsWizardPage wizard;
private SWTBotShell botShell;
private Shell shell;
private Display display;
private SWTBot bot;
#Before
public void setUp() {
botShell = new SWTBotShell(shell);
bot = new SWTBot();
wizard = new ManualSettingsWizardPage();
display = Display.getDefault();
shell = new Shell(display);
shell.open();
shell.layout();
}
#Test
public void bot() throws Exception {
bot = botShell.bot();
shell.setBounds(200, 200, 400, 400);
shell.setLayout(new FillLayout());
wizard.createControl(shell);
}
I think your problem stems from the fact that you are creating GUI components from the SWTBot-Thread. They should be created by the UIThread, though.
Normally, you'd test some plugin which would open a wizard as the result of choosing an action, e.g. "new xyz". First step would be to put your wizard code into a plugin and register a new action that would fire up the wizard. then you could try finding the shell with SWTBot and executing the desired actions.
I am working with SWT and I would like to be able to resize a composite by dragging the corner of it, the same way that you can resize a shell. I'm sure someone out there has implemented a good solution. Thanks.
I think what you are looking for can be implemented with org.eclipse.swt.widgets.Tracker
here is sample working code:
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.open();
final Composite b = new Composite(shell, SWT.NONE);
b.setBounds(20, 20, 80, 80);
b.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
b.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE);
tracker.setStippled(true);
Rectangle rect = b.getBounds();
tracker.setRectangles(new Rectangle[] { rect });
if (tracker.open()) {
Rectangle after = tracker.getRectangles()[0];
b.setBounds(after);
}
tracker.dispose();
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
My try as follows,which doesn't come up with anything:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Image image = new Image(display,
"D:/topic.png");
GC gc = new GC(image);
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
gc.drawText("I've been drawn on",0,0,true);
gc.dispose();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
// TODO Auto-generated method stub
}
See the SWT-Snippets for examples. This one uses an image label
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.BORDER);
label.setImage (image);
You are missing one thing in your code. Event Handler for paint. Normally when you create a component it generates a paint event. All the drawing related stuff should go in it.
Also you need not to create the GC explicitly.. It comes with the event object :)
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ImageX
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
shell.setLayout(new FillLayout ());
final Image image = new Image(display, "C:\\temp\\flyimage1.png");
shell.addListener (SWT.Paint, new Listener ()
{
public void handleEvent (Event e) {
GC gc = e.gc;
int x = 10, y = 10;
gc.drawImage (image, x, y);
gc.dispose();
}
});
shell.setSize (600, 400);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
if(image != null && !image.isDisposed())
image.dispose();
display.dispose ();
}
}