Package org.jdesktop.wonderland.modules.userlist.client

Source Code of org.jdesktop.wonderland.modules.userlist.client.CoverScreenListener

/**
* Copyright (c) 2014, WonderBuilders, Inc., All Rights Reserved
*/

package org.jdesktop.wonderland.modules.userlist.client;

import com.jme.math.Vector3f;
import java.awt.Canvas;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import org.jdesktop.wonderland.client.cell.Cell;
import org.jdesktop.wonderland.client.cell.CellStatusChangeListener;
import org.jdesktop.wonderland.client.jme.ClientContextJME;
import org.jdesktop.wonderland.client.jme.JmeClientMain;
import org.jdesktop.wonderland.client.jme.MainFrame;
import org.jdesktop.wonderland.common.cell.CellStatus;

/**
*
* @author Abhishek Upadhyay
*/
public class CoverScreenListener implements CellStatusChangeListener{

    private int flg=1;
    private Collection<Cell> rootCells = new ArrayList<Cell>();
    private CellStatusChangeListener csl=null;
    private ScheduledExecutorService exec=null;
    private CellStatus cs=null;
    private ScheduledExecutorService exec_cellStatus = null;
    private JDialog dialog=null;

    public CoverScreenListener(final Vector3f goalPos,final CoverScreenData csd) {
        Vector3f fromLoc = ClientContextJME.getViewManager()
                .getPrimaryViewCell().getWorldTransform().getTranslation(null);
        Vector3f toLoc = goalPos;
        if(Math.abs((fromLoc.x)-(toLoc.x))<=150 &&
                Math.abs((fromLoc.y)-(toLoc.y))<=150 &&
                Math.abs((fromLoc.z)-(toLoc.z))<=150) {
            //Cover Screen not needed
        } else {
            //attach cover screen
            final MainFrame mainFrame =  JmeClientMain.getFrame();
            final Canvas canvas = mainFrame.getCanvas();
            final CoverScreen coverScreenPanel = new CoverScreen(csd,canvas.getSize());
           
            try {
                //show cover screen in JDialog over the canvas panel
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        dialog = new JDialog(mainFrame.getFrame(), "Child", false);
                        coverScreenPanel.setPreferredSize(canvas.getSize());
                        dialog.setSize(canvas.getSize().width
                                ,canvas.getSize().height);
                        dialog.setLocation(canvas.getLocationOnScreen().x
                                ,canvas.getLocationOnScreen().y);
                        JButton button = new JButton("Button");
                        button.addActionListener(new ActionListener() {
                          @Override
                          public void actionPerformed(ActionEvent e) {
                            dialog.dispose();
                          }
                        });
                        mainFrame.getFrame().addComponentListener(new ComponentAdapter() {

                            private void doChange() {
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        if (dialog != null && dialog.isVisible()) {
                                            Component cs = dialog.getComponent(0);
                                            Canvas canvas = mainFrame.getCanvas();
                                            cs.setPreferredSize(canvas.getSize());
                                            dialog.setSize(canvas.getSize().width, canvas.getSize().height);
                                            dialog.setLocation(canvas.getLocationOnScreen().x, canvas.getLocationOnScreen().y);
                                            dialog.pack();
                                            dialog.setVisible(true);
                                        }
                                    }
                                });
                            }
                           
                            public void componentResized(ComponentEvent e) {
                                doChange();
                            }

                            public void componentMoved(ComponentEvent e) {
                                doChange();
                            }

                        });
                        dialog.getRootPane().setOpaque(false);
                        dialog.add(coverScreenPanel);

                        dialog.setUndecorated(true);
                        dialog.pack();
                        dialog.setVisible(true);
                    }
                });
                //Listener for the close icon
                coverScreenPanel.getCloseComponent().addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        if(exec_cellStatus!=null) {
                            exec_cellStatus.shutdown();
                            exec_cellStatus=null;
                        }
                        if(exec!=null) {
                            exec.shutdown();
                            exec=null;
                        }
                        if(dialog!=null ) {
                            dialog.dispose();
                        }
                    }
                   
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            ClientContextJME.getCellManager().addCellStatusChangeListener(this);
        }
    }
   
    private boolean checkIfAllCellsLoaded(Collection<Cell> rootCells,int level) {
       
        Iterator<Cell> itr = rootCells.iterator();
        while(itr.hasNext()) {
            Cell c = itr.next();
            List<Cell> childs = c.getChildren();
            if(!c.getStatus().equals(CellStatus.VISIBLE)) {
                return false;
            }
            if(childs.size()!=0) {
                if(!checkIfAllCellsLoaded(childs,level+1)) {
                    return false;
                }
            }
        }
        return true;
    }
   
    public void cellStatusChanged(final Cell cell, CellStatus status) {
       
        if(status.equals(CellStatus.VISIBLE)) {
            if(flg==1) {
                if(exec!=null) {
                    exec.shutdown();
                }
                csl = this;
                exec_cellStatus =  Executors.newSingleThreadScheduledExecutor();
                exec_cellStatus.scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                       
                        rootCells = cell.getCellCache().getRootCells();
                        if(rootCells.size()==1) {
                            try {
                                exec_cellStatus.awaitTermination(20, TimeUnit.SECONDS);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(CoverScreenListener.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            rootCells = cell.getCellCache().getRootCells();
                        }
                        if(checkIfAllCellsLoaded(rootCells,0)) {
                            if(dialog!=null) {
                                dialog.dispose();
                            }
                            exec_cellStatus.shutdown();
                            exec_cellStatus=null;
                            ClientContextJME.getCellManager().removeCellStatusChangeListener(csl);
                        }
                    }
                }, 0, 3, TimeUnit.SECONDS);
            }
            flg++;
        }
    }
}
TOP

Related Classes of org.jdesktop.wonderland.modules.userlist.client.CoverScreenListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.