[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: How would I implement zoom-in-region for a canvas?

Grant,

Thanks for the reference to Tracker. I have been dabbling with it on and off the past few days. I took the snippet that draws a fix-sized box and started to hack at it. I figured out the SWT.RESIZE style is the one I want for my redrawable rectangle.

There's one problem; I can't seem to get MouseUp events to work. I got the initial MouseDown event to work since it creates the tracker, but I want a MouseUp event to know when user is finished drawing the tracker. I've tried a generic listener with SWT.MouseUp, as well as a MouseListener with MouseUp() overridden. Neither seems to work.

This might be a more generic problem that warrants its own thread, but I thought I'd tack it on to this discussion first. Here is my source code.

package practice;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tracker;

public class TrackerMouseDown {

	private static int firstX = -1;
	private static int firstY = -1;
	private static Display display;
	private static Shell shell;

	private static ControlListener trackerListener = new ControlListener() {
		public void controlMoved(ControlEvent e) {
			System.out.println("Moved");
		}
		
		public void controlResized(ControlEvent e) {
			System.out.println("Resized");			
		}
	};

/* Experiment with a MouseListener
private static MouseListener mouseListener = new MouseListener() { public void mouseDown(MouseEvent e) {
System.out.println("Mouse is down");
firstX = e.x;
firstY = e.y;


// Use the RESIZE style to insist on drawing a resizeable rectangle
Tracker tracker = new Tracker(shell, SWT.RESIZE);
tracker.addControlListener(trackerListener);
tracker.setRectangles(new Rectangle[] { new Rectangle(firstX, firstY, e.x - firstX, e.y - firstY), });
tracker.open();
}


		public void mouseUp(MouseEvent e) {						
			System.out.println("Mouse is up");
		}

public void mouseDoubleClick(MouseEvent arg0) {
// TODO Auto-generated method stub

}
};
*/

private static Listener listener = new Listener() { public void handleEvent(Event e) {
switch(e.type) {
case SWT.MouseDown:
System.out.println("Mouse is down");
firstX = e.x;
firstY = e.y;


// Use the RESIZE style to insist on drawing a resizeable rectangle
Tracker tracker = new Tracker(shell, SWT.RESIZE);
tracker.addControlListener(trackerListener);
tracker.setRectangles(new Rectangle[] { new Rectangle(firstX, firstY, e.x - firstX, e.y - firstY), });
tracker.open();
break;
case SWT.MouseUp:
System.out.println("Mouse is up");
break;

case SWT.MouseMove:
// Do nothing for now
break;

}
}
};


	public static void main(String[] args) {

		display = new Display();
		shell = new Shell(display);

		shell.open();
		shell.addListener(SWT.MouseDown, listener);
		shell.addListener(SWT.MouseUp, listener);
		shell.addListener(SWT.MouseMove, listener);

		/*  MouseListener
		shell.addMouseListener(mouseListener);
		*/
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}