Drag and Drop example snippet: define my own data transfer type : Drag Drop « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » Drag DropScreenshots 
Drag and Drop example snippet: define my own data transfer type
Drag and Drop example snippet: define my own data transfer type


/*
 * Drag and Drop example snippet: define my own data transfer type
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Snippet79 {

  /*
   * The data being transferred is an <bold>array of type MyType</bold> where
   * MyType is define as:
   */
  static class MyType {
    String fileName;

    long fileLength;

    long lastModified;
  }

  static class MyTransfer extends ByteArrayTransfer {

    private static final String MYTYPENAME = "name_for_my_type";

    private static final int MYTYPEID = registerType(MYTYPENAME);

    private static MyTransfer _instance = new MyTransfer();

    public static MyTransfer getInstance() {
      return _instance;
    }

    public void javaToNative(Object object, TransferData transferData) {
      if (!checkMyType(object|| !isSupportedType(transferData)) {
        DND.error(DND.ERROR_INVALID_DATA);
      }
      MyType[] myTypes = (MyType[]) object;
      try {
        // write data to a byte array and then ask super to convert to
        // pMedium
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream writeOut = new DataOutputStream(out);
        for (int i = 0, length = myTypes.length; i < length; i++) {
          byte[] buffer = myTypes[i].fileName.getBytes();
          writeOut.writeInt(buffer.length);
          writeOut.write(buffer);
          writeOut.writeLong(myTypes[i].fileLength);
          writeOut.writeLong(myTypes[i].lastModified);
        }
        byte[] buffer = out.toByteArray();
        writeOut.close();
        super.javaToNative(buffer, transferData);
      catch (IOException e) {
      }
    }

    public Object nativeToJava(TransferData transferData) {
      if (isSupportedType(transferData)) {
        byte[] buffer = (byte[]) super.nativeToJava(transferData);
        if (buffer == null)
          return null;

        MyType[] myData = new MyType[0];
        try {
          ByteArrayInputStream in = new ByteArrayInputStream(buffer);
          DataInputStream readIn = new DataInputStream(in);
          while (readIn.available() 20) {
            MyType datum = new MyType();
            int size = readIn.readInt();
            byte[] name = new byte[size];
            readIn.read(name);
            datum.fileName = new String(name);
            datum.fileLength = readIn.readLong();
            datum.lastModified = readIn.readLong();
            MyType[] newMyData = new MyType[myData.length + 1];
            System
                .arraycopy(myData, 0, newMyData, 0,
                    myData.length);
            newMyData[myData.length= datum;
            myData = newMyData;
          }
          readIn.close();
        catch (IOException ex) {
          return null;
        }
        return myData;
      }

      return null;
    }

    protected String[] getTypeNames() {
      return new String[] { MYTYPENAME };
    }

    protected int[] getTypeIds() {
      return new int[] { MYTYPEID };
    }

    boolean checkMyType(Object object) {
      if (object == null || !(object instanceof MyType[])
          || ((MyType[]) object).length == 0) {
        return false;
      }
      MyType[] myTypes = (MyType[]) object;
      for (int i = 0; i < myTypes.length; i++) {
        if (myTypes[i== null || myTypes[i].fileName == null
            || myTypes[i].fileName.length() == 0) {
          return false;
        }
      }
      return true;
    }

    protected boolean validate(Object object) {
      return checkMyType(object);
    }
  }

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Label label1 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label1.setText("Drag Source for MyData[]");
    final Label label2 = new Label(shell, SWT.BORDER | SWT.WRAP);
    label2.setText("Drop Target for MyData[]");

    DragSource source = new DragSource(label1, DND.DROP_COPY);
    source.setTransfer(new Transfer[] { MyTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        MyType myType1 = new MyType();
        myType1.fileName = "C:\\abc.txt";
        myType1.fileLength = 1000;
        myType1.lastModified = 12312313;
        MyType myType2 = new MyType();
        myType2.fileName = "C:\\xyz.txt";
        myType2.fileLength = 500;
        myType2.lastModified = 12312323;
        event.data = new MyType[] { myType1, myType2 };
      }
    });
    DropTarget target = new DropTarget(label2, DND.DROP_COPY
        | DND.DROP_DEFAULT);
    target.setTransfer(new Transfer[] { MyTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          event.detail = DND.DROP_COPY;
        }
      }

      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          event.detail = DND.DROP_COPY;
        }
      }

      public void drop(DropTargetEvent event) {
        if (event.data != null) {
          MyType[] myTypes = (MyType[]) event.data;
          if (myTypes != null) {
            String string = "";
            for (int i = 0; i < myTypes.length; i++) {
              string += myTypes[i].fileName + " ";
            }
            label2.setText(string);
          }
        }
      }

    });
    shell.setSize(200200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}


           
       
Related examples in the same category
1. SWT DND (Drag and Drop) comprehensive Example SWT DND (Drag and Drop) comprehensive Example
2. Word JumblesWord Jumbles
3. Illustrates draggingIllustrates dragging
4. Simple DND (Drag and Drop) ExampleSimple DND (Drag and Drop) Example
5. SWT DnD (Drag and drop) Composite
6. Drag and Drop: determine native data types available (motif only)
7. Drag and Drop: determine data types available (win32 only)Drag and Drop: determine data types available (win32 only)
8. Drag and Drop example snippet: define a default operation (in this example, Copy)Drag and Drop example snippet: define a default operation (in this example, Copy)
9. Drag and Drop example snippet: drag leaf items in a treeDrag and Drop example snippet: drag leaf items in a tree
10. Drag and Drop example snippet: drag text between two labelsDrag and Drop example snippet: drag text between two labels
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.