package ui; import javax.swing.event.MouseInputAdapter; import java.awt.event.MouseEvent; import javax.swing.JComponent; /** * MoveInteractor is a generic interactor for dragging components around * inside a container. It can listen to any number of components for * click-and-drag events, but only one component can be actively dragged * at any moment. (This is not a serious limitation, since most user * interfaces only support one mouse.) It can handle any number of containers, * but a component can't be dragged outside of its container. *

* If a container has a layout manager, the behavior of MoveInteractor is * not well defined, since it will fight with the layout manager to position * the dragged component. */ public class MoveInteractor extends MouseInputAdapter { /** * Make a MoveInteractor. */ public MoveInteractor() { } /** * Attach this move interactor to a component that can be * dragged around. * @param comp Draggable component * @modifies comp's listeners * @effects When the user clicks and drags comp, this * MoveInteractor moves comp around with the mouse. */ public void listenTo(JComponent comp) { comp.addMouseListener(this); comp.addMouseMotionListener(this); } /** * Test whether a component may be picked up and dragged. * Called when the user presses the mouse button over a component * that this interactor is listening to. * Subclasses should override this method if components * are sometimes undraggable. (For example, in a game, * the user may move only their own pieces, only during their turn.) * The default implementation of this method simply returns true. * * @param comp component that user clicked on * @param x x coordinate of mouse pointer, relative to comp * @param y y coordinate of mouse pointer, relative to comp * @return true if component may be dragged, false if drag should be * stopped */ public boolean pickUp(JComponent comp, int x, int y) { return true; } /** * Finish a drag operation by dropping the component. * Called when the user releases the mouse button. * Subclasses should override this method if there are some * locations that a component can't be dropped (like * illegal moves in a game), or if the drop should have some * other effect (like changing a backend model). * The default implementation of this method simply returns true. * * @param comp component being dragged * @param x x coordinate of mouse pointer, relative to comp * @param y y coordinate of mouse pointer, relative to comp * * @effects May reposition comp, e.g. snapping it to the correct location. * May also trigger effects in other classes. * * @return true if the drop is permitted, in which * case MoveInteractor leaves the component where it is; * false if the drop should be canceled, in which case MoveInteractor * restores the component to its original location */ public boolean drop(JComponent comp, int x, int y) { return true; } /** * Part of MouseListener interface. Called when user presses * the mouse button over a component that this MoveInteractor is * listening to. */ public void mousePressed(MouseEvent e) { // YOUR CODE HERE } /** * Part of MouseMotionListener interface. Called when user is * dragging the mouse. */ public void mouseDragged(MouseEvent e) { // YOUR CODE HERE } /** * Part of MouseListener interface. Called when user releases * the mouse button. */ public void mouseReleased(MouseEvent e) { // YOUR CODE HERE } }