Dropped data type depends on target item in table : Table « SWT JFace Eclipse « Java






Dropped data type depends on target item in table

Dropped data type depends on target item in table

/*
 * Dropped data type depends on target item in table
 * 
 * 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.File;

import org.eclipse.swt.SWT;
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.FileTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class Snippet185 {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Label label1 = new Label(shell, SWT.BORDER);
    label1.setText("Drag Source");
    final Table table = new Table(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
      TableItem item = new TableItem(table, SWT.NONE);
      if (i % 2 == 0)
        item.setText("Drop a file");
      if (i % 2 == 1)
        item.setText("Drop text");
    }
    DragSource dragSource = new DragSource(label1, DND.DROP_COPY);
    dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance(),
        FileTransfer.getInstance() });
    dragSource.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
          File file = new File("temp");
          event.data = new String[] { file.getAbsolutePath() };
        }
        if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
          event.data = "once upon a time";
        }
      }
    });
    DropTarget dropTarget = new DropTarget(table, DND.DROP_COPY
        | DND.DROP_DEFAULT);
    dropTarget.setTransfer(new Transfer[] { TextTransfer.getInstance(),
        FileTransfer.getInstance() });
    dropTarget.addDropListener(new DropTargetAdapter() {
      FileTransfer fileTransfer = FileTransfer.getInstance();

      TextTransfer textTransfer = TextTransfer.getInstance();

      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 dragOver(DropTargetEvent event) {
        event.detail = DND.DROP_NONE;
        TableItem item = (TableItem) event.item;
        if (item == null)
          return;
        int itemIndex = table.indexOf(item);
        if (itemIndex % 2 == 0) {
          int index = 0;
          while (index < event.dataTypes.length) {
            if (fileTransfer
                .isSupportedType(event.dataTypes[index]))
              break;
            index++;
          }
          if (index < event.dataTypes.length) {
            event.currentDataType = event.dataTypes[index];
            event.detail = DND.DROP_COPY;
            return;
          }
        } else {
          int index = 0;
          while (index < event.dataTypes.length) {
            if (textTransfer
                .isSupportedType(event.dataTypes[index]))
              break;
            index++;
          }
          if (index < event.dataTypes.length) {
            event.currentDataType = event.dataTypes[index];
            event.detail = DND.DROP_COPY;
            return;
          }
        }
      }

      public void drop(DropTargetEvent event) {
        TableItem item = (TableItem) event.item;
        if (item == null) {
          event.detail = DND.DROP_NONE;
          return;
        }
        if (fileTransfer.isSupportedType(event.currentDataType)) {
          String[] files = (String[]) event.data;
          if (files != null && files.length > 0) {
            item.setText(files[0]);
          }
        }
        if (textTransfer.isSupportedType(event.currentDataType)) {
          String text = (String) event.data;
          if (text != null) {
            item.setText(text);
          }
        }
      }

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


           
       








Related examples in the same category

1.How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener FactoryHow to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory
2.Print KTable (SWT Table)Example Print KTable (SWT Table)Example
3.The source of a custom table class for Java SWT applicationsThe source of a custom table class for Java SWT applications
4.SWT Table Editor
5.Simple File Browser in SWT TableSimple File Browser in SWT Table
6.Bug Tracker JFaceBug Tracker JFace
7.Bug TrackerBug Tracker
8.File Browser DemoFile Browser Demo
9.TableEditor exampleTableEditor example
10.File Browser SampleFile Browser Sample
11.File Browser JFace
12.Bug ReportBug Report
13.Displays ASCII CodesDisplays ASCII Codes
14.Demonstrates the SWT.VIRTUAL styleDemonstrates the SWT.VIRTUAL style
15.Displays a tableDisplays a table
16.A table of baseball players and allows sortingA table of baseball players and allows sorting
17.Demonstrates TableCursorDemonstrates TableCursor
18.Demonstrates TableTreeDemonstrates TableTree
19.Demonstrates TableEditorDemonstrates TableEditor
20.Shows the extensions on the system and their associated programsShows the extensions on the system and their associated programs
21.Table Example 3Table Example 3
22.Table Example 2Table Example 2
23.Table ExampleTable Example
24.Demonstrates TableViewersDemonstrates TableViewers
25.Demonstrates CheckboxTableViewerDemonstrates CheckboxTableViewer
26.Demonstrates CellEditorsDemonstrates CellEditors
27.SWT Table Simple DemoSWT Table Simple Demo
28.Create fake tooltips for items in a SWT tableCreate fake tooltips for items in a SWT table
29.Place a progress bar in a SWT tablePlace a progress bar in a SWT table
30.edit a cell in a SWT table (in place, fancy)edit a cell in a SWT table (in place, fancy)
31.edit the text of a SWT table item (in place)edit the text of a SWT table item (in place)
32.navigate a SWT table cells with arrow keysnavigate a SWT table cells with arrow keys
33.Update SWT table item textUpdate SWT table item text
34.Sort a SWT table by columnSort a SWT table by column
35.Select an index (select and scroll) in a SWT tableSelect an index (select and scroll) in a SWT table
36.Scroll a SWT table (set the top index)Scroll a SWT table (set the top index)
37.Resize columns as SWT table resizesResize columns as SWT table resizes
38.Remove selected items in a SWT tableRemove selected items in a SWT table
39.Print selected items in a SWT tablePrint selected items in a SWT table
40.Place arbitrary controls in a SWT tablePlace arbitrary controls in a SWT table
41.Reorder columns and reorder columns programmaticallyReorder columns and reorder columns programmatically
42.Insert a SWT table column (at an index)Insert a SWT table column (at an index)
43.Insert a SWT table item (at an index)Insert a SWT table item (at an index)
44.Find a SWT table cell from mouse down (works for any table style)Find a SWT table cell from mouse down (works for any table style)
45.Find a table cell from mouse down (SWT.FULL_SELECTION)Find a table cell from mouse down (SWT.FULL_SELECTION)
46.Detect a selection or check event in a table (SWT.CHECK)Detect a selection or check event in a table (SWT.CHECK)
47.Create a SWT table with 1,000,000 itemsCreate a SWT table with 1,000,000 items
48.Create a SWT table (columns, headers, lines)Create a SWT table (columns, headers, lines)
49.Create a SWT table (no columns, no headers)Create a SWT table (no columns, no headers)
50.Color cells and rows in SWT tableColor cells and rows in SWT table
51.Create a virtual SWT table and add 1000 entries to it every 500 msCreate a virtual SWT table and add 1000 entries to it every 500 ms
52.Create a table (lazy)Create a table (lazy)