Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Need help syncing scrollbar and figurecanvas
Need help syncing scrollbar and figurecanvas [message #641693] Fri, 26 November 2010 19:06 Go to next message
Kazan  is currently offline Kazan
Messages: 5
Registered: July 2009
Junior Member
Hi. I'm trying to get the scrollbars to sync with the view whenever the user clicks on the screen.

If the user clicks on the FigureCanvas, the view should zoom in and be centerd on the point where the user clicked. This example is just setting the view location to the top left corner. I have played around with this for so long, I don't know what I am doing wrong. Need some help:

Here is my code:
package toursheet.tests;

import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.CoordinateListener;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ScalableLayeredPane;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test8 {
private ScalableLayeredPane _layer;
private FigureCanvas _canvas;

public Test8(Shell shell) {
shell.setBounds(0, 0, 400, 200);
shell.setLayout(new FillLayout(SWT.VERTICAL));

_canvas = new FigureCanvas(shell, SWT.DOUBLE_BUFFERED);
_canvas.setBackground(ColorConstants.lightGray);
_layer = new ScalableLayeredPane();
_canvas.setContents(_layer);

GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 4;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;

_layer.setLayoutManager(gridLayout);

{
RectangleFigure r = new RectangleFigure();
r.setBackgroundColor(ColorConstants.red);
r.setSize(new Dimension(100, 200));
_layer.add(r);
}

{
RectangleFigure r = new RectangleFigure();
r.setBackgroundColor(ColorConstants.green);
r.setSize(new Dimension(100, 200));
_layer.add(r);
}

{
RectangleFigure r = new RectangleFigure();
r.setBackgroundColor(ColorConstants.blue);
r.setSize(new Dimension(100, 200));
_layer.add(r);
}

{
RectangleFigure r = new RectangleFigure();
r.setBackgroundColor(ColorConstants.yellow);
r.setSize(new Dimension(100, 200));
_layer.add(r);
}

_canvas.addMouseListener(new MouseRightLeft());
_canvas.getHorizontalBar().addSelectionListener(new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent event){
}
public void widgetSelected(SelectionEvent event){
System.out.println("x=" + _canvas.getViewport().getViewLocation().x);
}
});
}

public class MouseRightLeft implements MouseListener {
Double _scale;
Integer _x;
Integer _y;
Integer _w;
Integer _left;
Integer _w1;
Integer _w2;

public void mouseDoubleClick(MouseEvent arg0) {
}

public void mouseDown(MouseEvent e) {
_scale = _layer.getScale();
_x = e.x;
_y = e.y;
_w = _layer.getSize().width;
_left = _x;
_w1 = _canvas.getViewport().getClientArea().width;
_w2 = _layer.getSize().width;

if(e.button == 1){
final Double scale = _layer.getScale() * 2d;
_layer.setScale(scale);

}
else if(e.button == 3){
_layer.setScale(_layer.getScale() / 2);
}
_layer.revalidate();
}

public void mouseUp(MouseEvent e){
Double scale = _layer.getScale();
Point point = new Point(e.x*scale, e.y*scale);

Double test2 = new Double(point.x)-375d/2d;

System.out.println("test2=" + test2);
_canvas.scrollToX(point.x);
}

public void mouseDoubleClicked(MouseEvent me) {
}
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Test8 app = new Test8(shell);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
Re: Need help syncing scrollbar and figurecanvas [message #642864 is a reply to message #641693] Fri, 03 December 2010 04:34 Go to previous message
Kazan  is currently offline Kazan
Messages: 5
Registered: July 2009
Junior Member
I'm still trying to figure this out, any ideas from anyone?
Previous Topic:Animation on Connection.
Next Topic:Keep creation tool from palette active when modifier key is pressed
Goto Forum:
  


Current Time: Sun Oct 26 09:46:48 GMT 2014

Powered by FUDForum. Page generated in 0.01575 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software