Package DisplayProject.dnd

Source Code of DisplayProject.dnd.ForteTransferHandler$DnDFilesInfo

/*
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.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.TransferHandler;

import DisplayProject.Array_Of_DisplayNode;
import DisplayProject.Constants;
import DisplayProject.DisplayNode;
import DisplayProject.PictureField;
import DisplayProject.UIutils;
import DisplayProject.actions.TreeFieldCurrentNode;
import DisplayProject.controls.ListView;
import DisplayProject.controls.OutlineField;
import DisplayProject.controls.OutlineFieldJTree;
import DisplayProject.controls.PictureGraphic;
import DisplayProject.events.ClientEventManager;
import DisplayProject.events.InstallableListener;
import DisplayProject.events.ListViewListenerHelper;
import Framework.ErrorMgr;
import Framework.ParameterHolder;

/**
* An implementation of TransferHandler that adds support for dropping
* forte objects.
*/
@SuppressWarnings("serial")
public class ForteTransferHandler extends TransferHandler  implements InstallableListener{
  private static String defaultMimeType = DataFlavor.javaJVMLocalObjectMimeType +
                      ";class=DisplayProject.dnd.ForteTransferHandler$DnDInfo";

  DataFlavor[] handledFlavors = new DataFlavor[2];
  Point startDrag;
  private int dropX, dropY;

  public ForteTransferHandler() {
    try {
      // Add default Object and FileList DataFlavors, should be enough for most applications
      handledFlavors[0] = (new DataFlavor(defaultMimeType));
      handledFlavors[1] = (new DataFlavor(DataFlavor.javaFileListFlavor.getMimeType()));
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  // 22/07/2008 JNL DropAdapter needs to set this before importing
  public void setDropCoords(int x, int y) {
    this.dropX = x;
    this.dropY = y;
  }

  @SuppressWarnings("unchecked")
  @Override
  public boolean importData(JComponent c, Transferable t) {

    DataFlavor[] flavors = t.getTransferDataFlavors();
    DataFlavor fileListFlavor = null;
    DataFlavor objectFlavor = null;

    // See what types of data we can import
    for (DataFlavor flav : flavors) {
      //      System.out.println(flav);
      if (flav.isFlavorJavaFileListType()) {
        fileListFlavor = flav;
      }
      else if (flav.isMimeTypeEqual(defaultMimeType) || flav.isFlavorSerializedObjectType()) {
        objectFlavor = flav;
      }
    } 

    if (objectFlavor != null || fileListFlavor != null) {
      String eventName;

      if (objectFlavor != null) {

        try {
          eventName = "ObjectDrop";

          // Extract the Object info
          DnDInfo info = (DnDInfo)t.getTransferData(objectFlavor);
          info.setTargetField(c);
          info.setDropX(this.dropX);
          info.setDropY(this.dropY);
          info.setTargetX(info.getDropX());
          info.setTargetY(info.getDropY());

          // Put the info in a Hashtable to post back
          Hashtable<String, Object> params = new Hashtable<String, Object>();
          params.put("sourceField", new ParameterHolder(info.getSourceField()));
          params.put("sourceX", new ParameterHolder(info.getSourceX()));
          params.put("sourceY", new ParameterHolder(info.getSourceX()));
          params.put("targetX", new ParameterHolder(info.getTargetX()));
          params.put("targetY", new ParameterHolder(info.getTargetY()));
          params.put("dropX", new ParameterHolder(info.getDropX()));
          params.put("dropY", new ParameterHolder(info.getDropY()));
          params.put("targetField", new ParameterHolder(info.getTargetField()));
          params.put("dropField", new ParameterHolder(info.getTargetField()));
          params.put("sourceData", new ParameterHolder(info.getSourceData()));
          params.put("sourceDataType", new ParameterHolder(info.getSourceDataType()));

          // Post Event to the Target
          Object postTarget;
          if (ListViewListenerHelper.isMemberOfListView(info.getTargetField())) {
            // if the target is a member of the listview then treat it as if it were the ListView
            postTarget = ListViewListenerHelper.findListView(info.getTargetField());
          } else if (info.getTargetField() instanceof OutlineFieldJTree) {
            // if the target is a member of the OutlineField then treat it as if it were the OutlineField
            postTarget = ((OutlineFieldJTree)info.getTargetField()).getOutlineField();
          } else {
            // default to TargetField
            postTarget = info.getTargetField();
          }

          // CraigM:07/07/2008 - Handle multiple selection drag and drop for list views
          if (info.getSourceField() instanceof ListView) {
            ListView lv = (ListView)info.getSourceField();
            Array_Of_DisplayNode<DisplayNode> nodes = lv.getSelectedNodes();
            if (nodes.size() > 1) {
              for (DisplayNode node : nodes) {
                Hashtable<String, Object> newParams = (Hashtable<String, Object>)params.clone();
                newParams.put("sourceData", new ParameterHolder(node));
                ClientEventManager.postEvent(postTarget, eventName, newParams);
              }
            }
            else {
              ClientEventManager.postEvent(postTarget, eventName, params);
            }
          }
          else {
            ClientEventManager.postEvent(postTarget, eventName, params);
          }

          return true;
        } catch (UnsupportedFlavorException ufe) {
          System.out.println("importData: unsupported data flavor");
        } catch (IOException ioe) {
          System.out.println("importData: I/O exception");
        }
      }
      if (fileListFlavor != null) {
        try {
          eventName = "FileDrop";

          // Extract the File info - contained in an Array
          List<File> droppedFiles = (List<File>) t.getTransferData(fileListFlavor);

          Hashtable<String, Object> params = new Hashtable<String, Object>();
          params.put("sourceData", new ParameterHolder(droppedFiles));

          // Determine the Target
          JComponent targetField = c;
          Object postTarget;
          if (ListViewListenerHelper.isMemberOfListView(targetField)) {
            // if the target is a member of the listview then treat it as if it were the ListView
            postTarget = ListViewListenerHelper.findListView(targetField);
          } else if (targetField instanceof OutlineFieldJTree) {
            // if the target is a member of the OutlineField then treat it as if it were the OutlineField
            postTarget = ((OutlineFieldJTree)targetField).getOutlineField();
          } else {
            // default to TargetField
            postTarget = targetField;
          }

          // Post event back to Target
          ClientEventManager.postEvent(postTarget, eventName, params);

          return true;

        } catch (UnsupportedFlavorException ufe) {
          System.out.println("importData: unsupported data flavor");
        } catch (IOException ioe) {
          System.out.println("importData: I/O exception");
        }
      }
    }
    return false;
  }

  protected boolean hasDnDFlavor(DataFlavor[] flavors) {
    if (handledFlavors == null) {
      return false;
    }

    for (int i = 0; i < flavors.length; i++) {
      for (int j = 0; j < handledFlavors.length; j++) {
        if (handledFlavors[j].equals(flavors[i])) {
          return true;
        }
      }
    }
    return false;
  }

  @Override
  public boolean canImport(JComponent c, DataFlavor[] flavors) {
    return hasDnDFlavor(flavors);
  }

  public void install(Object o, String pEvent) {
    if (o instanceof JComponent) {
      ((JComponent)o).setTransferHandler(this);
      ((JComponent)o).setDropTarget(new DropAdapter());
    }
  }

  @Override
  protected Transferable createTransferable(JComponent c) {
    DnDInfo info = new DnDInfo();
    info.setSourceField(c);
    if (this.startDrag != null){
      info.setSourceX(UIutils.pixelsToMils(this.startDrag.x));
      info.setSourceY(UIutils.pixelsToMils(this.startDrag.y));
    }
    if (c instanceof JTable) {
      ListView lv = (ListView)c.getClientProperty("qq_ListView");
      if (lv != null){
        info.setSourceDataType(Constants.SD_NODE);
        info.setSourceData(lv.getCurrentNode());
        info.setSourceField(lv);
      } else {
        info.setSourceDataType(Constants.SD_FIELDWIDGET);
        info.setSourceData(c);
      }
    } else if (c instanceof JTree){
      OutlineField olf = (OutlineField)c.getClientProperty("qq_OutlineField");
      info.setSourceDataType(Constants.SD_NODE);
      if (olf != null){
        info.setSourceData(olf.getCurrentNode());
        info.setSourceField(olf);
      } else {
        info.setSourceData(TreeFieldCurrentNode.get((JTree)c));
      }
    } else if (c instanceof PictureField){
      info.setSourceDataType(Constants.SD_IMAGE);
      info.setSourceData(((PictureField)c).getImageValue());
    } else if (c instanceof PictureGraphic){
      info.setSourceDataType(Constants.SD_IMAGE);
      info.setSourceData(((PictureGraphic)c).getImageValue());
    } else {
      info.setSourceDataType(Constants.SD_FIELDWIDGET);
      info.setSourceData(c);
    }
    return info;
  }

  @Override
  public int getSourceActions(JComponent c) {
    return COPY_OR_MOVE;
  }

  @Override
  public void exportAsDrag(JComponent comp, InputEvent e, int action) {
    this.startDrag = ((MouseEvent)e).getPoint();
    super.exportAsDrag(comp, e, action);
  }

  public class DnDInfo implements Transferable{
    private JComponent sourceField;
    private int sourceX;
    private int sourceY;
    private int targetX;
    private int targetY;
    private int dropX;
    private int dropY;
    private JComponent targetField;
    private JComponent dropField;
    private Object sourceData;
    private int sourceDataType;

    public JComponent getSourceField() {
      return sourceField;
    }
    public void setSourceField(JComponent sourceField) {
      this.sourceField = sourceField;
    }
    /*
     * The sourceX and sourceY parameters show the
     * original coordinates for the mouse pointer
     * hot spot for the dropped widget, in mils, relative to its parent widget.
     */
    public int getSourceX() {
      return sourceX;
    }
    public void setSourceX(int sourceX) {
      this.sourceX = sourceX;
    }
    public int getSourceY() {
      return sourceY;
    }
    public void setSourceY(int sourceY) {
      this.sourceY = sourceY;
    }
    /*
     * The targetX and targetY parameters show the target
     * coordinates for the mouse pointer hot spot for the dropped widget,
     * in mils, relative to the target widget of the drop action.
     * The target widget is the widget that has registered for the ObjectDrop event,
     * as indicated by the targetField parameter.
     */
    public int getTargetX() {
      return targetX;
    }
    public void setTargetX(int targetX) {
      this.targetX = targetX;
    }
    public int getTargetY() {
      return targetY;
    }
    public void setTargetY(int targetY) {
      this.targetY = targetY;
    }
    /*
     * The targetField parameter shows the widget on which the object was dropped
     * if the field has registered for the ObjectDrop event.
     * The targetField parameter is different from the dropField parameter only
     * when the field the end user drops the object on is not registered for the FileDrop event.
     */
    public JComponent getTargetField() {
      return targetField;
    }
    public void setTargetField(JComponent targetField) {
      this.targetField = targetField;
    }
    /*
     * The dropField parameter shows the field on which the mouse was released.
     * The dropField parameter differs from the targetField parameter in that it
     * shows a receiving widget that is not registered for the ObjectDrop event.
     * The dropField parameter is delivered only if the widget immediately beneath
     * the mouse cursor when the drop occurs has not registered for the ObjectDrop event; if registered, a field receiving an ObjectDrop event is instead its target field, as shown by the targetField parameter.
     */
    public JComponent getDropField() {
      return dropField;
    }
    public void setDropField(JComponent dropField) {
      this.dropField = dropField;
    }
    public Object getSourceData() {
      return sourceData;
    }
    public void setSourceData(Object sourceData) {
      this.sourceData = sourceData;
    }
    public int getSourceDataType() {
      return sourceDataType;
    }
    public void setSourceDataType(int sourceDataType) {
      this.sourceDataType = sourceDataType;
    }
    /*
     * The dropX and dropY parameters show the coordinates of the dropped widget,
     * relative to any child widget in which it is dropped within the target widget.
     * These parameters are delivered only if the widget receiving the dropped widget
     * has not registered for the ObjectDrop event. If the target field is the field
     * immediately beneath the mouse when the object drop occurs, the dropField parameter is NIL,
     * and the dropX and dropY parameters are not set.
     */
    public int getDropX() {
      return dropX;
    }
    public void setDropX(int dropX) {
      this.dropX = dropX;
    }
    public int getDropY() {
      return dropY;
    }
    public void setDropY(int dropY) {
      this.dropY = dropY;
    }
    public Object getTransferData(DataFlavor flavor)
    throws UnsupportedFlavorException, IOException {
      if (!isDataFlavorSupported(flavor)) {
        UnsupportedFlavorException errorVar = new UnsupportedFlavorException(flavor);
        ErrorMgr.addError(errorVar);
        throw errorVar;
      }
      return this;
    }
    public DataFlavor[] getTransferDataFlavors() {
      return handledFlavors;
    }
    public boolean isDataFlavorSupported(DataFlavor flavor) {
      if (flavor.isMimeTypeEqual(defaultMimeType) || flavor.isFlavorSerializedObjectType())
        return true;
      else
        return false;
    }
  }  // DnDInfo

  public class DnDFilesInfo implements Transferable {
    private List<File> fileList;

    public JComponent getSourceField() { return null; }
    public void setSourceField(JComponent sourceField) {}
    public int getSourceX() { return 0; }
    public void setSourceX(int sourceX) {}
    public int getSourceY() { return 0; }
    public void setSourceY(int sourceY) {}
    public int getTargetX() { return 0; }
    public void setTargetX(int targetX) {}
    public int getTargetY() { return 0; }
    public void setTargetY(int targetY) {}
    public JComponent getTargetField() { return null; }
    public void setTargetField(JComponent targetField) {}
    public JComponent getDropField() { return null; }
    public void setDropField(JComponent dropField) {}
    public int getSourceDataType() { return 0; }
    public void setSourceDataType(int sourceDataType) {}
    public int getDropX() { return 0; }
    public void setDropX(int dropX) {}
    public int getDropY() { return 0; }
    public void setDropY(int dropY) {}

    public List<File> getSourceData() {
      return fileList;
    }
    public void setSourceData(List<File> fileList) {
      this.fileList = fileList;
    }
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
      if (!isDataFlavorSupported(flavor)) {
        UnsupportedFlavorException errorVar = new UnsupportedFlavorException(flavor);
        ErrorMgr.addError(errorVar);
        throw errorVar;
      }
      return getSourceData();
    }
   
    // 11/07/2008 New methods for DataFlavors
    public DataFlavor[] getTransferDataFlavors() {
      return handledFlavors;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
      return flavor.isFlavorJavaFileListType();
    }
  }  // DndFilesInfo

}
TOP

Related Classes of DisplayProject.dnd.ForteTransferHandler$DnDFilesInfo

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.