1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| public class MouseMoveButton {
/**
*
*/
public MouseMoveButton() {
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display ();
Shell shell = new Shell (display);
MouseMoveLayout moveLayout = new MouseMoveLayout();
shell.setLayout(moveLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Bouge moi !");
moveLayout.register(button1, 0, 0, true);
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Moi aussi !");
moveLayout.register(button2, 0, 50, true);
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("Je reste là !");
moveLayout.register(button3, 0, 100, false);
try {
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
} catch (Throwable e) {
e.printStackTrace();
}
display.dispose ();
}
static class MouseMoveLayout extends Layout {
private Point size;
private List<Control> controls;
public MouseMoveLayout() {
controls=new ArrayList<Control>();
}
public void register(Control control, int x, int y, boolean move) {
register(control, new Point(x, y), move);
}
public void register(Control control, Point location, boolean move) {
MouseMoveLayoutData layoutData = new MouseMoveLayoutData();
layoutData.move=move;
layoutData.location=new Point(location.x, location.y);
layoutData.mouseListener = new Listener();
control.setLayoutData(layoutData);
control.addMouseMoveListener(layoutData.mouseListener);
control.addMouseListener(layoutData.mouseListener);
}
public void setMoveable(Control control, boolean moveable) {
MouseMoveLayoutData layoutData = (MouseMoveLayoutData) control.getLayoutData();
if ( moveable==false ) {
layoutData.mouseListener.mouseMove=false;
}
layoutData.move=moveable;
}
@Override
protected Point computeSize(Composite composite, int wHint, int hHint,
boolean flushCache) {
if ( size==null || flushCache ) {
computeSize(composite, wHint, hHint);
}
return size;
}
private void computeSize(Composite composite, int wHint, int hHint) {
controls.clear();
Rectangle area = composite.getClientArea();
size=new Point(wHint==SWT.DEFAULT?area.width:wHint,hHint==SWT.DEFAULT?area.height:hHint);
for(Control control : composite.getChildren() ) {
control.setSize(control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
if ( control.getLayoutData() instanceof MouseMoveLayoutData ) {
controls.add(control);
}
}
}
@Override
protected void layout(Composite composite, boolean flushCache) {
if ( size==null || flushCache ) {
computeSize(composite, SWT.DEFAULT, SWT.DEFAULT);
}
for(Control control : controls ) {
MouseMoveLayoutData layoutData = (MouseMoveLayoutData) control.getLayoutData();
control.setLocation(layoutData.location);
}
}
class MouseMoveLayoutData {
public Listener mouseListener;
public boolean move;
public Point location;
public Rectangle bounds;
}
class Listener implements MouseMoveListener, MouseListener {
private boolean mouseMove;
private Point origin;
public void mouseMove(MouseEvent e) {
if ( mouseMove ) {
Control control=(Control) e.widget;
MouseMoveLayoutData layoutData = (MouseMoveLayoutData) control.getLayoutData();
Point mousemove = new Point(e.x-origin.x, e.y-origin.y); // déplacement par rapport à origine (référence bouton)
Point location = layoutData.location;
if ( location==null ) {
location=control.getLocation();
}
location.x+=mousemove.x; // application du déplacement
location.y+=mousemove.y; // application du déplacement
layoutData.location=location; // application du déplacement
origin = new Point(e.x-mousemove.x, e.y-mousemove.y); // on recalcule le point d'origine (référence bouton)
control.getParent().layout();
}
}
public void mouseDoubleClick(MouseEvent e) {
}
public void mouseDown(MouseEvent e) {
Control control=(Control) e.widget;
MouseMoveLayoutData layoutData = (MouseMoveLayoutData) control.getLayoutData();
if ( layoutData.move ) {
if (e.button==3) {
mouseMove=true;
origin = new Point(e.x, e.y); // on mémorise le point cliqué (référence bouton)
}
}
}
public void mouseUp(MouseEvent e) {
if (e.button==3) {
mouseMove=false;
}
}
}
}
} |
Partager