Package DisplayProject.dnd

Source Code of DisplayProject.dnd.DropAdapter

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.dnd;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;

import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.table.JTableHeader;
import javax.swing.tree.TreePath;

import DisplayProject.Constants;
import DisplayProject.UIutils;
import DisplayProject.actions.OutlineFieldHasMultipleSelection;
import DisplayProject.actions.WidgetState;
import DisplayProject.controls.ListView;

/**
* Provides the conditions under which the drop action is successful on a target component
*
*/
@SuppressWarnings("serial")
public class DropAdapter extends DropTarget{
    private boolean canImport;

    private boolean actionSupported(int action) {
        return (action & (TransferHandler.COPY_OR_MOVE )) != TransferHandler.NONE;
    }

    @Override
    public void dragEnter(DropTargetDragEvent e) {
        DataFlavor[] flavors = e.getCurrentDataFlavors();

        JComponent c = (JComponent)e.getDropTargetContext().getComponent();
        TransferHandler importer = c.getTransferHandler();

        if (importer != null && importer.canImport(c, flavors)) {
            canImport = true;
        } else {
            canImport = false;
        }

        int dropAction = e.getDropAction();

        if (canImport && actionSupported(dropAction)) {
            e.acceptDrag(dropAction);
        } else {
            e.rejectDrag();
        }
        this.initializeAutoscrolling(e.getLocation()); // Enable autoscrolling.  CraigM: 10/04/2008
    }
   
    /**
     * @param dropAdapter
     * @return true if the drop component is update or view only
     */
    private boolean isDropable(Object dropAdapter) {
      if (dropAdapter instanceof DropAdapter &&
          ((DropAdapter)dropAdapter).getComponent() != null) {
       
        // TF:04/12/2009:DET-139:Changed this to use the true widget state, ie the worst state out of our state and our parents.
        switch (WidgetState.getWorstState(((DropAdapter)dropAdapter).getComponent())) {
          case Constants.FS_UPDATE:
          case Constants.FS_VIEWONLY:
            return true;
          default:
            return false;
        }
      }
      return true;
    }
   
    @Override
    public void dragOver(DropTargetDragEvent e) {
      int dropAction = e.getDropAction();

      if (canImport && actionSupported(dropAction) && isDropable(e.getSource())) {
          e.acceptDrag(dropAction);

          // If we're dragging over a tree, highlight the node we are currently on.  CraigM: 19/03/2008.
          if (this.getComponent() instanceof JTree) {
            JTree aTree = (JTree)this.getComponent();
            TreePath aPath = aTree.getPathForLocation(e.getLocation().x, e.getLocation().y);
            aTree.setSelectionPath(aPath);
          }

          // If we are dragging over a ListView, highlight the node we are currently on.
          // Note: Currently only supports the table view.  CraigM: 24/03/2008.
          else if (this.getComponent() instanceof ListView) {
            ListView lv = (ListView)this.getComponent();
           
            // CraigM:08/07/2008 -  If we are not dragging over a list marked for multiple selection.
            if (OutlineFieldHasMultipleSelection.get(lv) == false) {

              // Check we are allowed to highlight a node.  CraigM: 04/04/2008
              if (lv.getIsDropHighlightEnabled()) {
                // Get the top left position, taking into account scrolling
                Point p = lv.getTable().getVisibleRect().getLocation();

                // Take into account the header
                JTableHeader header = lv.getTable().getTableHeader();
                if (header != null) {
                  p.y -= header.getHeight();
                }

                // Set the point based on the mouse position
                p.y += e.getLocation().y;

                // Find the row at that point
                int row = lv.getTable().rowAtPoint(p);

                // Set the row if valid
                if (row >= 0 && row < lv.getTable().getRowCount()) {
                  lv.getTable().setRowSelectionInterval(row, row);
                }
              }
            }
          }
        } else {
            e.rejectDrag();
        }
        this.updateAutoscroll(e.getLocation()); // Enable autoscrolling.  CraigM: 10/04/2008
    }

    @Override
    public void dragExit(DropTargetEvent e) {
      this.clearAutoscroll(); // Enable autoscrolling.  CraigM: 10/04/2008
    }

    @Override
    public void drop(DropTargetDropEvent e) {
        int dropAction = e.getDropAction();

        JComponent c = (JComponent)e.getDropTargetContext().getComponent();
        TransferHandler importer = c.getTransferHandler();

        if (canImport && importer != null && actionSupported(dropAction) && isDropable(e.getSource())) {
            e.acceptDrop(dropAction);

            try {
                Transferable t = e.getTransferable();
                ForteTransferHandler ourHandler = (ForteTransferHandler)importer;
               
                // Set the Drop location details (before import)
                ourHandler.setDropCoords(UIutils.pixelsToMils(e.getLocation().x), UIutils.pixelsToMils(e.getLocation().y));
               
                // Import the data
                e.dropComplete(ourHandler.importData(c, t));
                     
            } catch (RuntimeException re) {
                e.dropComplete(false);
            }
        } else {
            e.rejectDrop();
        }
      this.clearAutoscroll(); // Enable autoscrolling.  CraigM: 10/04/2008
    }

    @Override
    public void dropActionChanged(DropTargetDragEvent e) {
        int dropAction = e.getDropAction();

        if (canImport && actionSupported(dropAction)) {
            e.acceptDrag(dropAction);
        } else {
            e.rejectDrag();
        }
        this.updateAutoscroll(e.getLocation()); // Enable autoscrolling.  CraigM: 10/04/2008
    }
}
TOP

Related Classes of DisplayProject.dnd.DropAdapter

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.