DragMoveAdapter.java :  » Music » yoyoplayer » com » hadeslee » yoyoplayer » util » Java Open Source

Java Open Source » Music » yoyoplayer 
yoyoplayer » com » hadeslee » yoyoplayer » util » DragMoveAdapter.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hadeslee.yoyoplayer.util;

import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.logging.Logger;

/**
 * 
 * @author hadeslee
 */
public class DragMoveAdapter implements MouseListener, MouseMotionListener {

    private static Logger log = Logger.getLogger(DragMoveAdapter.class.getName());
    //
    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int TOP = 3;
    public static final int BOTTOM = 4;
    private Component me;
    private int startX,  startY;
    private Rectangle myBound,  otherBound;
    private static final int SNAP = 30;//
    private Config config;//
    public DragMoveAdapter(Component com, Config config) {
        this.me = com;
        this.config = config;
        myBound = com.getBounds();
        otherBound = new Rectangle();
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        if (e.getButton() != MouseEvent.BUTTON1) {
            return;
        }
        if (me == config.getTopParent()) {
            config.updateComponentSnap();
        }
        config.updateDistance();
        startX = e.getX();
        startY = e.getY();
    }

    public void mouseReleased(MouseEvent e) {
        if (e.getButton() != MouseEvent.BUTTON1) {
            return;
        }
        //
        if (me == config.getTopParent()) {
            return;
        }
        getSnapObject();
        config.updateComponentSnap();

    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        int endX = e.getX();
        int endY = e.getY();
        int moveX = endX - startX;
        int moveY = endY - startY;
        Point p = me.getLocation();
        me.setLocation(p.x + moveX, p.y + moveY);
    }

    private int getDirection(int dis) {
        int x1 = (int) myBound.getCenterX();
        int y1 = (int) myBound.getCenterY();
        int x2 = (int) otherBound.getCenterX();
        int y2 = (int) otherBound.getCenterY();
        int abs = Math.abs(x1 - x2 - myBound.width / 2 - otherBound.width / 2 - dis);
        if (abs < 3) {
            return RIGHT;
        }
        abs = Math.abs(x2 - x1 - myBound.width / 2 - otherBound.width / 2 - dis);
        if (abs < 3) {
            return LEFT;
        }
        abs = Math.abs(y1 - y2 - myBound.height / 2 - otherBound.height / 2 - dis);
        if (abs < 3) {
            return BOTTOM;
        }
        abs = Math.abs(y2 - y1 - myBound.height / 2 - otherBound.height / 2 - dis);
        if (abs < 3) {
            return TOP;
        }
        return -1;
    }

    private void changeLocation(SnapObject obj) {
        Component com = obj.getCom();
        int location = obj.getLocation();
        int x, y;
        switch (location) {
            case LEFT:
                x = com.getX() - me.getWidth();
                y = me.getY();
                break;
            case RIGHT:
                x = com.getX() + com.getWidth();
                y = me.getY();
                break;
            case TOP:
                x = me.getX();
                y = com.getY() - me.getHeight();
                break;
            case BOTTOM:
                x = me.getX();
                y = com.getY() + com.getHeight();
                break;
            default:
                x = me.getX();
                y = me.getY();
                break;
            }
        me.setLocation(x, y);
    }

    /**
     * 
     * @return
     */
    public void getSnapObject() {
        me.getBounds(myBound);
        //EQ
        Component c1 = config.getEqWindow();
        if (c1 != null && c1 != me && c1.isShowing()) {
            c1.getBounds(otherBound);
            int dis = Util.getDistance(myBound, otherBound);
            if (dis > 0 && dis < SNAP) {
                int dir = getDirection(dis);
                if (dir != -1) {
                    SnapObject obj = new SnapObject(dir, c1);
                    changeLocation(obj);
                }
            }
        }
        me.getBounds(myBound);
        c1 = config.getLrcWindow();
        if (c1 != null && c1 != me && c1.isShowing()) {
            c1.getBounds(otherBound);
            int dis = Util.getDistance(myBound, otherBound);
            if (dis > 0 && dis < SNAP) {
                int dir = getDirection(dis);
                if (dir != -1) {
                    SnapObject obj = new SnapObject(dir, c1);
                    changeLocation(obj);
                }
            }
        }
        me.getBounds(myBound);
        c1 = config.getPlWindow();
        if (c1 != null && c1 != me && c1.isShowing()) {
            c1.getBounds(otherBound);
            int dis = Util.getDistance(myBound, otherBound);
            if (dis > 0 && dis < SNAP) {
                int dir = getDirection(dis);
                if (dir != -1) {
                    SnapObject obj = new SnapObject(dir, c1);
                    changeLocation(obj);
                }
            }
        }
        me.getBounds(myBound);
        c1 = config.getTopParent();
        if (c1 != null && c1 != me && c1.isShowing()) {
            c1.getBounds(otherBound);
            int dis = Util.getDistance(myBound, otherBound);
            if (dis > 0 && dis < SNAP) {
                int dir = getDirection(dis);
                if (dir != -1) {
                    SnapObject obj = new SnapObject(dir, c1);
                    changeLocation(obj);
                }
            }
        }
//        for (Component com : other.keySet()) {
//            //
//            if (me != com&&com.isShowing()) {
//                com.getBounds(otherBound);
//                int dis=Util.getDistance(myBound, otherBound);
//                if(dis>0&&dis<SNAP){
//                    return com;
//                }
//            }
//        }
    }

    public void mouseMoved(MouseEvent e) {
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.