Package jsynoptic.ui

Source Code of jsynoptic.ui.ShapeListModel$ShapeTransferable

package jsynoptic.ui;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;

import simtools.ui.DefaultPageListModel;


/**
* Extends DefaultPageListModel API to create a page list model with shapes and shape categories
* @author zxpletran007
*
*/
public class ShapeListModel extends DefaultPageListModel{

    /** The page that contains all shapes creators */
    ShapePageModel allShapePage;
   
    public static List excludedShapes = new ArrayList();
   
    public ShapeListModel() {
        super();
        allShapePage = createShapeCategory(Panes.resources.getString("allShapes"), Panes.resources.getIcon("allShapesIcon"));
        setSelectedPage(allShapePage);
    }


    /**
     * Create a shape category. If the category already exist, return it
     * @param categoryName
     * @param categoryIcon
     * @return the existing or created shape category
     */
    public ShapePageModel createShapeCategory(String categoryName, Icon categoryIcon){
        ShapePageModel pm = (ShapePageModel)getPageFromTitle(categoryName);
        if (pm == null){
            pm = new ShapePageModel(categoryName, categoryIcon);
            addPage(pm);
        }
        return pm;
    }

   
    /**
     * Add a shape to the shape pane
     * @param shapeCreator - The shape creator to be added in the shape pane
     * @param category - optional shape category
     */
    public void addShapeCreator(ShapeCreator shapeCreator){   

        if (!excludedShapes.contains(shapeCreator.shape)) {
            if (shapeCreator.shapeCategory != null){
                ShapePageModel pm = createShapeCategory(shapeCreator.shapeCategory, shapeCreator.shapeCategoryIcon);
                if (pm != null){
                    pm.addShapeCreator(shapeCreator);
                }  
            }

            // Add the shapeCreator to the default allshape page
            allShapePage.addShapeCreator(shapeCreator);
        }
    }

    public class ShapePageModel extends DefaultPageModel {

        List shapeCreatorList;
        JList listComponent;
       
        /**
         * Create an empty shape page model
         * @param pageTitle
         * @param pageIcon
         * @param shapeCreatorList
         */
        public ShapePageModel(String pageTitle, Icon pageIcon) {
            super(pageTitle, pageIcon, null);
            
            this.shapeCreatorList = new ArrayList();
           
            listComponent = new JList();
            setPageContent(new JScrollPane(listComponent));
        }
       
        public ShapeCreator getSelectedShapeCreator(){
            return (ShapeCreator)listComponent.getSelectedValue();
        }

        /**
         * Add a shape creator to the shape page
         * @param sp
         */
        public void addShapeCreator(ShapeCreator sp){
            shapeCreatorList.add(sp);

            Object[] array = shapeCreatorList.toArray();
            Arrays.sort(array);
           
            listComponent = new JList(array);
            listComponent.setTransferHandler(new ShapeTransferHandler());
            listComponent.setDragEnabled(true);

            setPageContent(new JScrollPane(listComponent));
        }
    }
   
    protected class ShapeTransferHandler extends TransferHandler {
        protected Transferable createTransferable(JComponent c) {
            if ( !( c instanceof JList )) {
                return null;
            }
            return new ShapeTransferable();
        }

        public int getSourceActions(JComponent c) {
            return COPY;
        }
    }
   
    protected class ShapeTransferable implements Transferable {
       
        /* (non-Javadoc)
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
         */
        public DataFlavor[] getTransferDataFlavors() {
            DataFlavor[] res =  new DataFlavor[0];
           
            ShapePageModel pm = (ShapePageModel)getSelectedPage();
            if (pm!= null){
                ShapeCreator sc = pm.getSelectedShapeCreator();
               
                if (sc != null){
                    res =  new DataFlavor[1];
                    res[0] = new DataFlavor(ShapeCreator.class, sc.toString());
                }
            }
            return res;
        }

        /* (non-Javadoc)
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
         */
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            boolean res = false;
           
            ShapePageModel pm = (ShapePageModel)getSelectedPage();
            if (pm!= null){
                ShapeCreator sc = pm.getSelectedShapeCreator();
                if (sc != null) {
                    res = ShapeCreator.class.equals(flavor.getRepresentationClass());
                }
            }
           
            return res;
        }

        /* (non-Javadoc)
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
         */
        public Object getTransferData(DataFlavor flavor) {
            Object res = null;
           
            ShapePageModel pm = (ShapePageModel)getSelectedPage();
            if (pm!= null){
                ShapeCreator sc = pm.getSelectedShapeCreator();
            
                if (sc != null && ShapeCreator.class.equals(flavor.getRepresentationClass()) ) {
                    res = sc;
                }
            }
           
            return res;
        }
    }
   
}
TOP

Related Classes of jsynoptic.ui.ShapeListModel$ShapeTransferable

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.