2013-03-21 44 views
1

我開發了兩個插件plugin1plugin2。在plugin1中有一個叫做pluginOneView的視圖,而在plugin2中有另一個叫做pluginTwoView的視圖。所以我的要求是在pluginTwoView上會有很少的可拖動組件,我應該可以將它拖到pluginOneView。目前,我正在開發針對同一拖放,我的代碼是(在pluginView2)爲DragSourceEclipse插件開發 - 在多個插件組件之間拖放

DragSource ds = new DragSource(btn, DND.DROP_MOVE); //btn is a draggable component 
    ds.setTransfer(new Transfer[] { TextTransfer.getInstance() }); 

    ds.addDragListener(new DragSourceAdapter() { 
      // There are dragStart and other methods here 
     } 
} 

但我的問題在於DropTarget方法:

DropTarget target = new DropTarget(component, dtl); 
component我的地方

這裏需要添加目標爲pluginOneView(這是另一種觀點)。我的問題是如何在工作空間中獲取該視圖的組件對象,以便我可以將它作爲方法傳遞給DropTarget方法?

我試圖讓

PlatformUI.getWorkbench().getViewRegistry().find("targetId"); 

,但它返回我IViewDescriptor類型,其中,因爲我需要component型的。任何人都可以幫助我嗎?由於我是Eclipse插件開發的新手。

回答

1

嗯,我想你誤解了如何使用DropTarget。你不需要知道你要拖動的插件。您還使用了TextTransfer,但我假設您要拖動Java對象而不是字符串。因此,我做了一個小示例,演示如何在多個視圖(可能位於不同的插件)之間拖動對象。在我的例子中,一個類型爲ISomeClass的對象被傳送。爲了傳輸它,ISomeClass必須是可序列化的。我已經提供了我自己的TransferType處理傳輸的SomeClassTransfer類。請注意,這兩個視圖插件都需要訪問ISomeClass和SomeClassTransfer定義。最簡單的方法是製作第三個包含這些類的插件。這兩個視圖插件都可以包含對第三個插件的引用。

SomeClassTransfer:

public class SomeClassTransfer extends ByteArrayTransfer { 
    private final static String[] typeNames; 
    private final static int[] typeIds; 
    private final static SomeClassTransfer instance; 

    static { 
     String typeName = "SomeClassTransfer"; 
     int id = registerType(typeName); 
     typeNames = new String[] { typeName }; 
     typeIds = new int[] { id }; 
     instance = new SomeClassTransfer(); 
    } 

    public static SomeClassTransfer getInstance() { 
     return instance; 
    } 

    private SomeClassTransfer() { 
    } 

    @Override 
    protected int[] getTypeIds() { 
     return typeIds; 
    } 

    @Override 
    protected String[] getTypeNames() { 
     return typeNames; 
    } 

    @Override 
    protected void javaToNative(Object object, TransferData transferData) { 
     if (object instanceof ISomeClass) { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      ObjectOutput out = null; 
      try { 
       out = new ObjectOutputStream(bos); 
       out.writeObject(object); 
       byte[] objectBytes = bos.toByteArray(); 
       object = objectBytes; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        out.close(); 
        bos.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     super.javaToNative(object, transferData); 
    } 

    @Override 
    protected ISomeClass nativeToJava(TransferData transferData) { 
     ISomeClass someClass = null; 

     byte[] objectBytes = (byte[]) super.nativeToJava(transferData); 
     ByteArrayInputStream bis = new ByteArrayInputStream(objectBytes); 
     ObjectInput in = null; 
     try { 
      in = new ObjectInputStream(bis); 
      Object o = in.readObject(); 
      if (o instanceof ISomeClass) { 
       someClass = (ISomeClass) o; 
      } 
     } catch (IOException | ClassNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       bis.close(); 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return someClass; 
    } 
} 

源視圖:

int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; 
    Transfer[] types = new Transfer[] { SomeClassTransfer.getInstance() }; 
    DragSource source = new DragSource(tableViewer.getControl(), operations); 
    source.setTransfer(types); 

    source.addDragListener(new DragSourceListener() { 
     @Override 
     public void dragStart(DragSourceEvent event) { 
      if (tableViewer.getSelection().isEmpty()) { 
       // do not start drag 
       event.doit = false; 
      } 
     } 

     @Override 
     public void dragSetData(DragSourceEvent event) { 
      if (SomeClassTransfer.getInstance().isSupportedType(
        event.dataType)) { 
       event.data = ((IStructuredSelection) tableViewer 
         .getSelection()).getFirstElement(); 
      } 
     } 

     @Override 
     public void dragFinished(DragSourceEvent event) { 
      // A Move operation has been performed so remove the data 
      // from the source 
      if (event.detail == DND.DROP_MOVE) { 
       tableViewer.remove(((IStructuredSelection) tableViewer 
         .getSelection()).getFirstElement()); 
      } 
     } 
    }); 

的目的地視圖:

int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; 
    Transfer[] types = new Transfer[] { SomeClassTransfer.getInstance() }; 
    DropTarget target = new DropTarget(tableViewer.getControl(), operations); 
    target.setTransfer(types); 

    target.addDropListener(new DropTargetListener() { 
     @Override 
     public void dragEnter(DropTargetEvent event) { 
     } 

     @Override 
     public void dragOver(DropTargetEvent event) { 
     } 

     @Override 
     public void dragLeave(DropTargetEvent event) { 
     } 

     @Override 
     public void dragOperationChanged(DropTargetEvent event) { 
     } 

     @Override 
     public void dropAccept(DropTargetEvent event) { 
     } 

     @Override 
     public void drop(DropTargetEvent event) { 
      if (event.data == null) { 
       // no data to copy, indicate failure in event.detail 
       event.detail = DND.DROP_NONE; 
       return; 
      } 
      // data copied to viewer 
      tableViewer.add((ISomeClass) event.data); 
     } 
    }); 

此代碼使您能夠在兩個完全獨立的視圖之間拖動數據。

+0

+1,Super ... :) – 2013-03-24 06:03:15