Package org.eclipse.ui.dnd

Examples of org.eclipse.ui.dnd.IDragAndDropService


   * Initialize the local services.
   */
  private void initializeDefaultServices() {
    // Register an implementation of the service appropriate for the
    // EditorSite.
    final IDragAndDropService editorDTService = new EditorSiteDragAndDropServiceImpl();
    serviceLocator.registerService(IDragAndDropService.class, editorDTService);
  }
View Full Code Here


        // formatter.setSlaveStrategy(
        // new CommentFormattingStrategy(...),
        // IJavaPartitions.JAVA_DOC);
        //

        final IDragAndDropService dndService = (IDragAndDropService) this.getSite().getService(IDragAndDropService.class);
        StyledText st = viewer.getTextWidget();
        textTransfer = TextTransfer.getInstance();
        fileTransfer = FileTransfer.getInstance();
        urlTransfer = URLTransfer.getInstance();
        udigTransfer = UDigByteAndLocalTransfer.getInstance();
        Transfer[] types = new Transfer[]{fileTransfer, textTransfer, urlTransfer, udigTransfer};

        dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT, //
                types, dropTargetListener);
    }
View Full Code Here

   *
   * @param viewer the viewer
   * @since 3.0
   */
  protected void initializeDragAndDrop(ISourceViewer viewer) {
    IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class);
    if (dndService == null)
      return;
   
    ITextEditorDropTargetListener listener= (ITextEditorDropTargetListener) getAdapter(ITextEditorDropTargetListener.class);

    if (listener == null) {
      Object object= Platform.getAdapterManager().loadAdapter(this, "org.eclipse.ui.texteditor.ITextEditorDropTargetListener"); //$NON-NLS-1$
      if (object instanceof ITextEditorDropTargetListener)
        listener= (ITextEditorDropTargetListener)object;
    }

    if (listener != null)
      dndService.addMergedDropTarget(viewer.getTextWidget(), DND.DROP_MOVE | DND.DROP_COPY, listener.getTransfers(), listener);

    IPreferenceStore store= getPreferenceStore();
    if (store != null && store.getBoolean(PREFERENCE_TEXT_DRAG_AND_DROP_ENABLED))
      installTextDragAndDrop(viewer);

View Full Code Here

    if (fIsTextDragAndDropInstalled) {
      fIsTextDragAndDropEnabled= true;
      return;
    }
   
    final IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class);
    if (dndService == null)
      return;

    fIsTextDragAndDropEnabled= true;
   
    final StyledText st= viewer.getTextWidget();
   
    // Install drag source
    final ISelectionProvider selectionProvider= viewer.getSelectionProvider();
    final DragSource source= new DragSource(st, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] {TextTransfer.getInstance()});
    source.addDragListener(new DragSourceAdapter() {
      String fSelectedText;
      Point fSelection;
      public void dragStart(DragSourceEvent event) {
        fTextDragAndDropToken= null;
       
        // XXX: This is only a workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=162192
        if (!fIsTextDragAndDropEnabled) {
          event.doit= false;
          event.image= null;
          return;
        }
       
        try {
          fSelection= st.getSelection();
          int offset= st.getOffsetAtLocation(new Point(event.x, event.y));
          Point p= st.getLocationAtOffset(offset);
          if (p.x > event.x)
            offset--;
          event.doit= offset > fSelection.x && offset < fSelection.y;

          ISelection selection= selectionProvider.getSelection();
          if (selection instanceof ITextSelection)
            fSelectedText= ((ITextSelection)selection).getText();
          else // fallback to widget
            fSelectedText= st.getSelectionText();
        } catch (IllegalArgumentException ex) {
          event.doit= false;
        }
      }
     
      public void dragSetData(DragSourceEvent event) {
        event.data= fSelectedText;
        fTextDragAndDropToken= this; // Can be any non-null object
      }
     
      public void dragFinished(DragSourceEvent event) {
        try {
          if (event.detail == DND.DROP_MOVE && validateEditorInputState()) {
            Point newSelection= st.getSelection();
            int length= fSelection.y - fSelection.x;
            int delta= 0;
            if (newSelection.x < fSelection.x)
              delta= length;
            st.replaceTextRange(fSelection.x + delta, length, ""); //$NON-NLS-1$
           
            if (fTextDragAndDropToken == null) {
              // Move in same editor - end compound change
              IRewriteTarget target= (IRewriteTarget)getAdapter(IRewriteTarget.class);
              if (target != null)
                target.endCompoundChange();
            }
           
          }
        } finally {
          fTextDragAndDropToken= null;
        }
      }
    });
   
    // Install drag target
    DropTargetListener dropTargetListener= new DropTargetAdapter() {
     
      private Point fSelection;
     
      public void dragEnter(DropTargetEvent event) {
        fTextDragAndDropToken= null;
        fSelection= st.getSelection();
       
        // XXX: This is only a workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=162192
        if (!fIsTextDragAndDropEnabled) {
          event.detail= DND.DROP_NONE;
          event.feedback= DND.FEEDBACK_NONE;
          return;
        }

        if (event.detail == DND.DROP_DEFAULT)
          event.detail= DND.DROP_MOVE;
      }
     
      public void dragOperationChanged(DropTargetEvent event) {
        if (!fIsTextDragAndDropEnabled) {
          event.detail= DND.DROP_NONE;
          event.feedback= DND.FEEDBACK_NONE;
          return;
        }
       
        if (event.detail == DND.DROP_DEFAULT)
          event.detail= DND.DROP_MOVE;
      }
     
      public void dragOver(DropTargetEvent event) {
       
        // XXX: This is only a workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=162192
        if (!fIsTextDragAndDropEnabled) {
          event.feedback= DND.FEEDBACK_NONE;
          return;
        }
       
        event.feedback |= DND.FEEDBACK_SCROLL;
      }
     
      public void drop(DropTargetEvent event) {
        try {
          if (!fIsTextDragAndDropEnabled)
            return;
 
          if (fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
            // Move in same editor
            int caretOffset= st.getCaretOffset();
            if (fSelection.x <= caretOffset && caretOffset <= fSelection.y) {
              event.detail= DND.DROP_NONE;
              return;
            }
           
            // Start compound change
            IRewriteTarget target= (IRewriteTarget)getAdapter(IRewriteTarget.class);
            if (target != null)
              target.beginCompoundChange();
          }
         
          if (!validateEditorInputState()) {
            event.detail= DND.DROP_NONE;
            return;
          }
         
          String text= (String)event.data;
          Point newSelection= st.getSelection();
          try {
            int modelOffset= widgetOffset2ModelOffset(viewer, newSelection.x);
            viewer.getDocument().replace(modelOffset, 0, text);
          } catch (BadLocationException e) {
            return;
          }
          st.setSelectionRange(newSelection.x, text.length());
        } finally {
          fTextDragAndDropToken= null;
        }
      }
    };
    dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] {TextTransfer.getInstance()}, dropTargetListener);

    fIsTextDragAndDropInstalled= true;
    fIsTextDragAndDropEnabled= true;
  }
View Full Code Here

      ctf.setTabHeight(0);
    }
  }

  protected void initializeDragAndDrop(final ISourceViewer viewer) {
    IDragAndDropService dndService = (IDragAndDropService) getSite().getService(IDragAndDropService.class);
    if (dndService != null) {
      final StyledText st = viewer.getTextWidget();
      ComponentDropTargetAdaptor dropTarget = new ComponentDropTargetAdaptor(this);
      dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] { LocalSelectionTransfer.getInstance() }, dropTarget);
    }
  }
View Full Code Here

   *
   * @param viewer the viewer
   * @since 3.0
   */
  protected void initializeDragAndDrop(ISourceViewer viewer) {
    IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class);
    if (dndService == null)
      return;

    ITextEditorDropTargetListener listener= (ITextEditorDropTargetListener) getAdapter(ITextEditorDropTargetListener.class);

    if (listener == null) {
      Object object= Platform.getAdapterManager().loadAdapter(this, "org.eclipse.ui.texteditor.ITextEditorDropTargetListener"); //$NON-NLS-1$
      if (object instanceof ITextEditorDropTargetListener)
        listener= (ITextEditorDropTargetListener)object;
    }

    if (listener != null)
      dndService.addMergedDropTarget(viewer.getTextWidget(), DND.DROP_MOVE | DND.DROP_COPY, listener.getTransfers(), listener);

    IPreferenceStore store= getPreferenceStore();
    if (store != null && store.getBoolean(PREFERENCE_TEXT_DRAG_AND_DROP_ENABLED))
      installTextDragAndDrop(viewer);

View Full Code Here

   */
  protected void installTextDragAndDrop(final ISourceViewer viewer) {
    if (viewer == null || fIsTextDragAndDropInstalled)
      return;

    final IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class);
    if (dndService == null)
      return;

    final StyledText st= viewer.getTextWidget();

    // Install drag source
    final ISelectionProvider selectionProvider= viewer.getSelectionProvider();
    final DragSource source= new DragSource(st, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] {TextTransfer.getInstance()});
    source.addDragListener(new DragSourceAdapter() {
      String fSelectedText;
      Point fSelection;
      public void dragStart(DragSourceEvent event) {
        fTextDragAndDropToken= null;
        try {
          fSelection= st.getSelection();
          event.doit= isLocationSelected(new Point(event.x, event.y));

          ISelection selection= selectionProvider.getSelection();
          if (selection instanceof ITextSelection)
            fSelectedText= ((ITextSelection)selection).getText();
          else // fallback to widget
            fSelectedText= st.getSelectionText();
        } catch (IllegalArgumentException ex) {
          event.doit= false;
        }
      }

      private boolean isLocationSelected(Point point) {
        // FIXME: https://bugs.eclipse.org/bugs/show_bug.cgi?id=260922
        if (isBlockSelectionModeEnabled())
          return false;

        int offset= st.getOffsetAtLocation(point);
        Point p= st.getLocationAtOffset(offset);
        if (p.x > point.x)
          offset--;
        return offset >= fSelection.x && offset < fSelection.y;
      }

      public void dragSetData(DragSourceEvent event) {
        event.data= fSelectedText;
        fTextDragAndDropToken= this; // Can be any non-null object
      }

      public void dragFinished(DragSourceEvent event) {
        try {
          if (event.detail == DND.DROP_MOVE && validateEditorInputState()) {
            Point newSelection= st.getSelection();
            int length= fSelection.y - fSelection.x;
            int delta= 0;
            if (newSelection.x < fSelection.x)
              delta= length;
            st.replaceTextRange(fSelection.x + delta, length, ""); //$NON-NLS-1$

            if (fTextDragAndDropToken == null) {
              // Move in same editor - end compound change
              IRewriteTarget target= (IRewriteTarget)getAdapter(IRewriteTarget.class);
              if (target != null)
                target.endCompoundChange();
            }

          }
        } finally {
          fTextDragAndDropToken= null;
        }
      }
    });

    // Install drag target
    DropTargetListener dropTargetListener= new DropTargetAdapter() {

      private Point fSelection;

      public void dragEnter(DropTargetEvent event) {
        fTextDragAndDropToken= null;
        fSelection= st.getSelection();
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_MOVE) != 0) {
            event.detail= DND.DROP_MOVE;
          } else if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail= DND.DROP_COPY;
          } else {
            event.detail= DND.DROP_NONE;
          }
        }
      }

      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_MOVE) != 0) {
            event.detail= DND.DROP_MOVE;
          } else if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail= DND.DROP_COPY;
          } else {
            event.detail= DND.DROP_NONE;
          }
        }
      }

      public void dragOver(DropTargetEvent event) {
        event.feedback |= DND.FEEDBACK_SCROLL;
      }

      public void drop(DropTargetEvent event) {
        try {
          if (fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
            // Move in same editor
            int caretOffset= st.getCaretOffset();
            if (fSelection.x <= caretOffset && caretOffset <= fSelection.y) {
              event.detail= DND.DROP_NONE;
              return;
            }

            // Start compound change
            IRewriteTarget target= (IRewriteTarget)getAdapter(IRewriteTarget.class);
            if (target != null)
              target.beginCompoundChange();
          }

          if (!validateEditorInputState()) {
            event.detail= DND.DROP_NONE;
            return;
          }

          String text= (String)event.data;
          if (isBlockSelectionModeEnabled()) {
            // FIXME fix block selection and DND
//            if (fTextDNDColumnSelection != null && fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
//              // DND_MOVE within same editor - remove origin before inserting
//              Rectangle newSelection= st.getColumnSelection();
//              st.replaceColumnSelection(fTextDNDColumnSelection, ""); //$NON-NLS-1$
//              st.replaceColumnSelection(newSelection, text);
//              st.setColumnSelection(newSelection.x, newSelection.y, newSelection.x + fTextDNDColumnSelection.width - fTextDNDColumnSelection.x, newSelection.y + fTextDNDColumnSelection.height - fTextDNDColumnSelection.y);
//            } else {
//              Point newSelection= st.getSelection();
//              st.insert(text);
//              IDocument document= getDocumentProvider().getDocument(getEditorInput());
//              int startLine= st.getLineAtOffset(newSelection.x);
//              int startColumn= newSelection.x - st.getOffsetAtLine(startLine);
//              int endLine= startLine + document.computeNumberOfLines(text);
//              int endColumn= startColumn + TextUtilities.indexOf(document.getLegalLineDelimiters(), text, 0)[0];
//              st.setColumnSelection(startColumn, startLine, endColumn, endLine);
//            }
          } else {
            Point newSelection= st.getSelection();
            try {
              int modelOffset= widgetOffset2ModelOffset(viewer, newSelection.x);
              viewer.getDocument().replace(modelOffset, 0, text);
            } catch (BadLocationException e) {
              return;
            }
            st.setSelectionRange(newSelection.x, text.length());
          }
        } finally {
          fTextDragAndDropToken= null;
        }
      }
    };
    dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] {TextTransfer.getInstance()}, dropTargetListener);

    fIsTextDragAndDropInstalled= true;
  }
View Full Code Here

   */
  protected void uninstallTextDragAndDrop(ISourceViewer viewer) {
    if (viewer == null || !fIsTextDragAndDropInstalled)
      return;

    final IDragAndDropService dndService= (IDragAndDropService)getSite().getService(IDragAndDropService.class);
    if (dndService == null)
      return;

    StyledText st= viewer.getTextWidget();
    dndService.removeMergedDropTarget(st);

    DragSource dragSource= (DragSource)st.getData(DND.DRAG_SOURCE_KEY);
    if (dragSource != null) {
      dragSource.dispose();
      st.setData(DND.DRAG_SOURCE_KEY, null);
View Full Code Here

   * @param viewer
   *            the viewer
   * @since 3.0
   */
  protected void initializeDragAndDrop(ISourceViewer viewer) {
    IDragAndDropService dndService = (IDragAndDropService) getSite()
        .getService(IDragAndDropService.class);
    if (dndService == null)
      return;

    ITextEditorDropTargetListener listener = (ITextEditorDropTargetListener) getAdapter(ITextEditorDropTargetListener.class);

    if (listener == null) {
      Object object = Platform.getAdapterManager().loadAdapter(this,
          "org.eclipse.ui.texteditor.ITextEditorDropTargetListener"); //$NON-NLS-1$
      if (object instanceof ITextEditorDropTargetListener)
        listener = (ITextEditorDropTargetListener) object;
    }

    if (listener != null)
      dndService.addMergedDropTarget(viewer.getTextWidget(),
          DND.DROP_MOVE | DND.DROP_COPY, listener.getTransfers(),
          listener);

    IPreferenceStore store = getPreferenceStore();
    if (store != null
View Full Code Here

   */
  protected void installTextDragAndDrop(final ISourceViewer viewer) {
    if (viewer == null || fIsTextDragAndDropInstalled)
      return;

    final IDragAndDropService dndService = (IDragAndDropService) getSite()
        .getService(IDragAndDropService.class);
    if (dndService == null)
      return;

    final StyledText st = viewer.getTextWidget();

    // Install drag source
    final ISelectionProvider selectionProvider = viewer
        .getSelectionProvider();
    final DragSource source = new DragSource(st, DND.DROP_COPY
        | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
      String fSelectedText;
      Point fSelection;

      public void dragStart(DragSourceEvent event) {
        fTextDragAndDropToken = null;
        try {
          fSelection = st.getSelection();
          event.doit = isLocationSelected(new Point(event.x, event.y));

          ISelection selection = selectionProvider.getSelection();
          if (selection instanceof ITextSelection)
            fSelectedText = ((ITextSelection) selection).getText();
          else
            // fallback to widget
            fSelectedText = st.getSelectionText();
        } catch (IllegalArgumentException ex) {
          event.doit = false;
        }
      }

      private boolean isLocationSelected(Point point) {
        // FIXME: https://bugs.eclipse.org/bugs/show_bug.cgi?id=260922
        if (isBlockSelectionModeEnabled())
          return false;

        int offset = st.getOffsetAtLocation(point);
        Point p = st.getLocationAtOffset(offset);
        if (p.x > point.x)
          offset--;
        return offset >= fSelection.x && offset < fSelection.y;
      }

      public void dragSetData(DragSourceEvent event) {
        event.data = fSelectedText;
        fTextDragAndDropToken = this; // Can be any non-null object
      }

      public void dragFinished(DragSourceEvent event) {
        try {
          if (event.detail == DND.DROP_MOVE
              && validateEditorInputState()) {
            Point newSelection = st.getSelection();
            int length = fSelection.y - fSelection.x;
            int delta = 0;
            if (newSelection.x < fSelection.x)
              delta = length;
            st.replaceTextRange(fSelection.x + delta, length, ""); //$NON-NLS-1$

            if (fTextDragAndDropToken == null) {
              // Move in same editor - end compound change
              IRewriteTarget target = (IRewriteTarget) getAdapter(IRewriteTarget.class);
              if (target != null)
                target.endCompoundChange();
            }

          }
        } finally {
          fTextDragAndDropToken = null;
        }
      }
    });

    // Install drag target
    DropTargetListener dropTargetListener = new DropTargetAdapter() {

      private Point fSelection;

      public void dragEnter(DropTargetEvent event) {
        fTextDragAndDropToken = null;
        fSelection = st.getSelection();
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_MOVE) != 0) {
            event.detail = DND.DROP_MOVE;
          } else if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail = DND.DROP_COPY;
          } else {
            event.detail = DND.DROP_NONE;
          }
        }
      }

      public void dragOperationChanged(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_MOVE) != 0) {
            event.detail = DND.DROP_MOVE;
          } else if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail = DND.DROP_COPY;
          } else {
            event.detail = DND.DROP_NONE;
          }
        }
      }

      public void dragOver(DropTargetEvent event) {
        event.feedback |= DND.FEEDBACK_SCROLL;
      }

      public void drop(DropTargetEvent event) {
        try {
          if (fTextDragAndDropToken != null
              && event.detail == DND.DROP_MOVE) {
            // Move in same editor
            int caretOffset = st.getCaretOffset();
            if (fSelection.x <= caretOffset
                && caretOffset <= fSelection.y) {
              event.detail = DND.DROP_NONE;
              return;
            }

            // Start compound change
            IRewriteTarget target = (IRewriteTarget) getAdapter(IRewriteTarget.class);
            if (target != null)
              target.beginCompoundChange();
          }

          if (!validateEditorInputState()) {
            event.detail = DND.DROP_NONE;
            return;
          }

          String text = (String) event.data;
          if (isBlockSelectionModeEnabled()) {
            // FIXME fix block selection and DND
            // if (fTextDNDColumnSelection != null &&
            // fTextDragAndDropToken != null && event.detail ==
            // DND.DROP_MOVE) {
            // // DND_MOVE within same editor - remove origin before
            // inserting
            // Rectangle newSelection= st.getColumnSelection();
            //              st.replaceColumnSelection(fTextDNDColumnSelection, ""); //$NON-NLS-1$
            // st.replaceColumnSelection(newSelection, text);
            // st.setColumnSelection(newSelection.x, newSelection.y,
            // newSelection.x + fTextDNDColumnSelection.width -
            // fTextDNDColumnSelection.x, newSelection.y +
            // fTextDNDColumnSelection.height -
            // fTextDNDColumnSelection.y);
            // } else {
            // Point newSelection= st.getSelection();
            // st.insert(text);
            // IDocument document=
            // getDocumentProvider().getDocument(getEditorInput());
            // int startLine= st.getLineAtOffset(newSelection.x);
            // int startColumn= newSelection.x -
            // st.getOffsetAtLine(startLine);
            // int endLine= startLine +
            // document.computeNumberOfLines(text);
            // int endColumn= startColumn +
            // TextUtilities.indexOf(document.getLegalLineDelimiters(),
            // text, 0)[0];
            // st.setColumnSelection(startColumn, startLine,
            // endColumn, endLine);
            // }
          } else {
            Point newSelection = st.getSelection();
            try {
              int modelOffset = widgetOffset2ModelOffset(viewer,
                  newSelection.x);
              viewer.getDocument().replace(modelOffset, 0, text);
            } catch (BadLocationException e) {
              return;
            }
            st.setSelectionRange(newSelection.x, text.length());
          }
        } finally {
          fTextDragAndDropToken = null;
        }
      }
    };
    dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY,
        new Transfer[] { TextTransfer.getInstance() },
        dropTargetListener);

    fIsTextDragAndDropInstalled = true;
  }
View Full Code Here

TOP

Related Classes of org.eclipse.ui.dnd.IDragAndDropService

Copyright © 2018 www.massapicom. 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.