/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.event.EventType; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; import javafx.stage.StageStyle; /** * * @author kumarse */ public class WindowManager extends Application { /** * @param args the command line arguments */ int stageNumber; final int numberOfStages=3; boolean handleDrag; Stage[] stage; Group[] root; Scene[] scene; double[] distance; double[] prevDistance; Color[] colors; int[] direction; double initX,initY,startX,startY; double[][] Dimensions; // Used to store the left,right,top and bottom bounds of a stage final static double distanceThreshold=15,overlapThreshold=0.75; public void init() { handleDrag=true; distance=new double[numberOfStages]; prevDistance=new double[numberOfStages]; direction=new int[numberOfStages]; stage=new Stage[numberOfStages]; Dimensions=new double[numberOfStages][4]; for(stageNumber=0;stageNumber() { final int temp=stageNumber; public void handle(MouseEvent me) { for(int i=0;i() { final int temp=stageNumber; public void handle(MouseEvent me) { handleMouseDragEvent(temp,me); } }); scene[stageNumber].setOnMouseReleased(new EventHandler() { final int temp=stageNumber; public void handle(MouseEvent me) { handleDrag=true; if(stage[temp].getX()!=startX || stage[temp].getY()!=startY) //Check for overlap handleOverlap(temp,startX,startY); } }); stage[stageNumber].xProperty().addListener(new ChangeListener() { final int temp=stageNumber; public void changed(ObservableValue val,Object oldVal,Object newVal) { handleMove(temp); } }); stage[stageNumber].yProperty().addListener(new ChangeListener() { final int temp=stageNumber; public void changed(ObservableValue val,Object oldVal,Object newVal) { handleMove(temp); } }); } } private void handleMousePressEvent(int stageNumber,MouseEvent me) { startX=stage[stageNumber].getX(); startY=stage[stageNumber].getY(); initX=me.getScreenX()-stage[stageNumber].getX(); initY=me.getScreenY()-stage[stageNumber].getY(); } private void handleMouseDragEvent(int stageNumber,MouseEvent me) { if(handleDrag) { stage[stageNumber].setX(me.getScreenX()-initX); stage[stageNumber].setY(me.getScreenY()-initY); } } private void handleMove(int stageNumber) { double X=stage[stageNumber].getX(),Y=stage[stageNumber].getY(); double height=stage[stageNumber].getHeight(),width=stage[stageNumber].getWidth(); double mLeft,mRight,mTop,mBottom,fLeft,fRight,fTop,fBottom; Dimensions[stageNumber][0]=mLeft=X; Dimensions[stageNumber][1]=mRight=X+width; Dimensions[stageNumber][2]=mTop=Y; Dimensions[stageNumber][3]=mBottom=Y+height; /* * Find the nearest window and stick the moving window with that if the distance is less than the distance threshold * of 15 pixels. */ for(int i=0;ifTop && mTop=overlapThreshold) { //System.out.println("Stick moving window to the left of window "+i); distance[i]=fLeft-mRight; direction[i]=0; } } else { if((fBottom-mTop)/(fBottom-fTop)>=overlapThreshold) { //System.out.println("Stick moving window to the left of window "+i); distance[i]=fLeft-mRight; direction[i]=0; } } } else if(mLeft>fRight && (mLeft-fRight<=distanceThreshold) && mBottom>fTop &&mTop=overlapThreshold) { //System.out.println("Stick moving window to the right of window "+i); distance[i]=mLeft-fRight; direction[i]=1; } } else { if((fBottom-mTop)/(fBottom-fTop)>=overlapThreshold) { //System.out.println("Stick moving window to the right of window "+i); distance[i]=mLeft-fRight; direction[i]=1; } } } else if(mBottomfLeft && mLeft=overlapThreshold) { //System.out.println("Stick moving window to the top of window "+i); distance[i]=fTop-mBottom; direction[i]=2; } } else { if((fLeft-mRight)/(fRight-fLeft)>=overlapThreshold) { //System.out.println("Stick moving window to the top of window "+i); distance[i]=fTop-mBottom; direction[i]=2; } } } else if(mTop>fBottom && (mTop-fBottom<=distanceThreshold) && mRight>fLeft && mLeft=overlapThreshold) { //System.out.println("Stick moving window to the bottom of window "+i); distance[i]=mTop-fBottom; direction[i]=3; } } else { if((fRight-mLeft)/(fRight-fLeft)>=overlapThreshold) { //System.out.println("Stick moving window to the bottom of window "+i); distance[i]=mTop-fBottom; direction[i]=3; } } } } } int stick=findStickWindow(); if(stick!=-1 && distance[stick]curLeft && topcurTop) { setStage(stageNumber,startX,startY); break; } } } } }