List of usage examples for org.eclipse.swt.custom ScrolledComposite getOrigin
public Point getOrigin()
From source file:ScrollWidgetViewFocusIn.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Button b1 = new Button(shell, SWT.PUSH); b1.setText("top"); b1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); Composite c = new Composite(sc, SWT.NONE); c.setLayout(new GridLayout(10, true)); for (int i = 0; i < 300; i++) { Button b = new Button(c, SWT.PUSH); b.setText("Button " + i); }// w ww.j av a 2 s . c o m Button b2 = new Button(shell, SWT.PUSH); b2.setText("bottom"); b2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); sc.setContent(c); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT)); Listener listener = new Listener() { public void handleEvent(Event e) { Control child = (Control) e.widget; Rectangle bounds = child.getBounds(); Rectangle area = sc.getClientArea(); Point origin = sc.getOrigin(); if (origin.x > bounds.x) origin.x = Math.max(0, bounds.x); if (origin.y > bounds.y) origin.y = Math.max(0, bounds.y); if (origin.x + area.width < bounds.x + bounds.width) origin.x = Math.max(0, bounds.x + bounds.width - area.width); if (origin.y + area.height < bounds.y + bounds.height) origin.y = Math.max(0, bounds.y + bounds.height - area.height); sc.setOrigin(origin); } }; Control[] controls = c.getChildren(); for (int i = 0; i < controls.length; i++) { controls[i].addListener(SWT.Activate, listener); } shell.setSize(300, 500); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet296.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 296"); shell.setBounds(10, 10, 300, 300);/*from www. jav a2 s. co m*/ final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL); sc.setBounds(10, 10, 280, 200); final int clientWidth = sc.getClientArea().width; final Tree tree = new Tree(sc, SWT.NONE); for (int i = 0; i < 99; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText("item " + i); new TreeItem(item, SWT.NONE).setText("child"); } sc.setContent(tree); int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; tree.setSize(clientWidth, prefHeight); /* * The following listener ensures that the Tree is always large * enough to not need to show its own vertical scrollbar. */ tree.addTreeListener(new TreeListener() { @Override public void treeExpanded(TreeEvent e) { int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; tree.setSize(clientWidth, prefHeight); } @Override public void treeCollapsed(TreeEvent e) { int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; tree.setSize(clientWidth, prefHeight); } }); /* * The following listener ensures that a newly-selected item * in the Tree is always visible. */ tree.addSelectionListener(widgetSelectedAdapter(e -> { TreeItem[] selectedItems = tree.getSelection(); if (selectedItems.length > 0) { Rectangle itemRect = selectedItems[0].getBounds(); Rectangle area = sc.getClientArea(); Point origin = sc.getOrigin(); if (itemRect.x < origin.x || itemRect.y < origin.y || itemRect.x + itemRect.width > origin.x + area.width || itemRect.y + itemRect.height > origin.y + area.height) { sc.setOrigin(itemRect.x, itemRect.y); } } })); /* * The following listener scrolls the Tree one item at a time * in response to MouseWheel events. */ tree.addListener(SWT.MouseWheel, event -> { Point origin = sc.getOrigin(); if (event.count < 0) { origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y); } else { origin.y = Math.max(origin.y - tree.getItemHeight(), 0); } sc.setOrigin(origin); }); Button downButton = new Button(shell, SWT.PUSH); downButton.setBounds(10, 220, 120, 30); downButton.setText("Down 10px"); downButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y + 10)); Button upButton = new Button(shell, SWT.PUSH); upButton.setBounds(140, 220, 120, 30); upButton.setText("Up 10px"); upButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y - 10)); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet322.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 322"); shell.setBounds(10, 10, 300, 300);//from w w w . j a v a 2 s. com final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL); sc.setBounds(10, 10, 280, 200); final int clientWidth = sc.getClientArea().width; final Tree tree = new Tree(sc, SWT.NONE); for (int i = 0; i < 99; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText("item " + i); new TreeItem(item, SWT.NONE).setText("child"); } sc.setContent(tree); int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; tree.setSize(clientWidth, prefHeight); /* * The following listener ensures that the Tree is always large * enough to not need to show its own vertical scrollbar. */ tree.addTreeListener(new TreeListener() { @Override public void treeExpanded(TreeEvent e) { int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; tree.setSize(clientWidth, prefHeight); } @Override public void treeCollapsed(TreeEvent e) { int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; tree.setSize(clientWidth, prefHeight); } }); /* * The following listener ensures that a newly-selected item * in the Tree is always visible. */ tree.addSelectionListener(widgetSelectedAdapter(e -> { TreeItem[] selectedItems = tree.getSelection(); if (selectedItems.length > 0) { Rectangle itemRect = selectedItems[0].getBounds(); Rectangle area = sc.getClientArea(); Point origin = sc.getOrigin(); if (itemRect.x < origin.x || itemRect.y < origin.y || itemRect.x + itemRect.width > origin.x + area.width || itemRect.y + itemRect.height > origin.y + area.height) { sc.setOrigin(itemRect.x, itemRect.y); } } })); /* * The following listener scrolls the Tree one item at a time * in response to MouseWheel events. */ tree.addListener(SWT.MouseWheel, event -> { Point origin = sc.getOrigin(); if (event.count < 0) { origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y); } else { origin.y = Math.max(origin.y - tree.getItemHeight(), 0); } sc.setOrigin(origin); }); Button disableButton = new Button(shell, SWT.PUSH); disableButton.setBounds(10, 220, 120, 30); disableButton.setText("Disable"); disableButton.addListener(SWT.Selection, event -> tree.setEnabled(false)); Button enableButton = new Button(shell, SWT.PUSH); enableButton.setBounds(140, 220, 120, 30); enableButton.setText("Enable"); enableButton.addListener(SWT.Selection, event -> tree.setEnabled(true)); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }