Search in sources :

Example 6 with DropTargetDragEvent

use of java.awt.dnd.DropTargetDragEvent in project intellij-community by JetBrains.

the class EditorImpl method initComponent.

private void initComponent() {
    myPanel.setLayout(new BorderLayout());
    myPanel.add(myHeaderPanel, BorderLayout.NORTH);
    myGutterComponent.setOpaque(true);
    myScrollPane.setViewportView(myEditorComponent);
    //myScrollPane.setBorder(null);
    myScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    myScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    myScrollPane.setRowHeaderView(myGutterComponent);
    myEditorComponent.setTransferHandler(new MyTransferHandler());
    myEditorComponent.setAutoscrolls(true);
    if (mayShowToolbar()) {
        JLayeredPane layeredPane = new JBLayeredPane() {

            @Override
            public void doLayout() {
                final Component[] components = getComponents();
                final Rectangle r = getBounds();
                for (Component c : components) {
                    if (c instanceof JScrollPane) {
                        c.setBounds(0, 0, r.width, r.height);
                    } else {
                        final Dimension d = c.getPreferredSize();
                        int rightInsets = getVerticalScrollBar().getWidth() + (isMirrored() ? myGutterComponent.getWidth() : 0);
                        c.setBounds(r.width - d.width - rightInsets - 20, 20, d.width, d.height);
                    }
                }
            }

            @Override
            public Dimension getPreferredSize() {
                return myScrollPane.getPreferredSize();
            }
        };
        layeredPane.add(myScrollPane, JLayeredPane.DEFAULT_LAYER);
        myPanel.add(layeredPane);
        new ContextMenuImpl(layeredPane, myScrollPane, this);
    } else {
        myPanel.add(myScrollPane);
    }
    myEditorComponent.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(@NotNull KeyEvent e) {
            if (e.getKeyCode() >= KeyEvent.VK_A && e.getKeyCode() <= KeyEvent.VK_Z) {
                myCharKeyPressed = true;
            }
            KeyboardInternationalizationNotificationManager.showNotification();
        }

        @Override
        public void keyTyped(@NotNull KeyEvent event) {
            myNeedToSelectPreviousChar = false;
            if (event.isConsumed()) {
                return;
            }
            if (processKeyTyped(event)) {
                event.consume();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            myCharKeyPressed = false;
        }
    });
    myEditorComponent.addMouseListener(myMouseListener);
    myGutterComponent.addMouseListener(myMouseListener);
    myEditorComponent.addMouseMotionListener(myMouseMotionListener);
    myGutterComponent.addMouseMotionListener(myMouseMotionListener);
    myEditorComponent.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(@NotNull FocusEvent e) {
            myCaretCursor.activate();
            for (Caret caret : myCaretModel.getAllCarets()) {
                int caretLine = caret.getLogicalPosition().line;
                repaintLines(caretLine, caretLine);
            }
            fireFocusGained();
        }

        @Override
        public void focusLost(@NotNull FocusEvent e) {
            clearCaretThread();
            for (Caret caret : myCaretModel.getAllCarets()) {
                int caretLine = caret.getLogicalPosition().line;
                repaintLines(caretLine, caretLine);
            }
            fireFocusLost();
        }
    });
    UiNotifyConnector connector = new UiNotifyConnector(myEditorComponent, new Activatable.Adapter() {

        @Override
        public void showNotify() {
            myGutterComponent.updateSizeOnShowNotify();
        }
    });
    Disposer.register(getDisposable(), connector);
    try {
        final DropTarget dropTarget = myEditorComponent.getDropTarget();
        if (dropTarget != null) {
            // might be null in headless environment
            dropTarget.addDropTargetListener(new DropTargetAdapter() {

                @Override
                public void drop(@NotNull DropTargetDropEvent e) {
                }

                @Override
                public void dragOver(@NotNull DropTargetDragEvent e) {
                    Point location = e.getLocation();
                    getCaretModel().moveToVisualPosition(getTargetPosition(location.x, location.y, true));
                    getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
                    requestFocus();
                }
            });
        }
    } catch (TooManyListenersException e) {
        LOG.error(e);
    }
    // update area available for soft wrapping on component shown/hidden
    myPanel.addHierarchyListener(e -> mySoftWrapModel.getApplianceManager().updateAvailableArea());
    myPanel.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(@NotNull ComponentEvent e) {
            myMarkupModel.recalcEditorDimensions();
            myMarkupModel.repaint(-1, -1);
        }
    });
}
Also used : UiNotifyConnector(com.intellij.util.ui.update.UiNotifyConnector) EditorFragmentComponent(com.intellij.codeInsight.hint.EditorFragmentComponent) DropTargetDragEvent(java.awt.dnd.DropTargetDragEvent) Activatable(com.intellij.util.ui.update.Activatable) DropTargetAdapter(java.awt.dnd.DropTargetAdapter) JBLayeredPane(com.intellij.ui.components.JBLayeredPane) DropTarget(java.awt.dnd.DropTarget) DropTargetDropEvent(java.awt.dnd.DropTargetDropEvent)

Example 7 with DropTargetDragEvent

use of java.awt.dnd.DropTargetDragEvent in project jdk8u_jdk by JetBrains.

the class SunDropTargetContextPeer method processEnterMessage.

/**
     * actual processing on EventQueue Thread
     */
protected void processEnterMessage(SunDropTargetEvent event) {
    Component c = (Component) event.getSource();
    DropTarget dt = c.getDropTarget();
    Point hots = event.getPoint();
    local = getJVMLocalSourceTransferable();
    if (currentDTC != null) {
        // some wreckage from last time
        currentDTC.removeNotify();
        currentDTC = null;
    }
    if (c.isShowing() && dt != null && dt.isActive()) {
        currentDT = dt;
        currentDTC = currentDT.getDropTargetContext();
        currentDTC.addNotify(this);
        currentA = dt.getDefaultActions();
        try {
            ((DropTargetListener) dt).dragEnter(new DropTargetDragEvent(currentDTC, hots, currentDA, currentSA));
        } catch (Exception e) {
            e.printStackTrace();
            currentDA = DnDConstants.ACTION_NONE;
        }
    } else {
        currentDT = null;
        currentDTC = null;
        currentDA = DnDConstants.ACTION_NONE;
        currentSA = DnDConstants.ACTION_NONE;
        currentA = DnDConstants.ACTION_NONE;
    }
}
Also used : DropTargetDragEvent(java.awt.dnd.DropTargetDragEvent) DropTargetListener(java.awt.dnd.DropTargetListener) DropTarget(java.awt.dnd.DropTarget) Point(java.awt.Point) Component(java.awt.Component) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) InvalidDnDOperationException(java.awt.dnd.InvalidDnDOperationException)

Example 8 with DropTargetDragEvent

use of java.awt.dnd.DropTargetDragEvent in project jdk8u_jdk by JetBrains.

the class SunDropTargetContextPeer method processMotionMessage.

/**
     *
     */
protected void processMotionMessage(SunDropTargetEvent event, boolean operationChanged) {
    Component c = (Component) event.getSource();
    Point hots = event.getPoint();
    int id = event.getID();
    DropTarget dt = c.getDropTarget();
    DropTargetContext dtc = null;
    if (c.isShowing() && (dt != null) && dt.isActive()) {
        if (currentDT != dt) {
            if (currentDTC != null) {
                currentDTC.removeNotify();
            }
            currentDT = dt;
            currentDTC = null;
        }
        dtc = currentDT.getDropTargetContext();
        if (dtc != currentDTC) {
            if (currentDTC != null) {
                currentDTC.removeNotify();
            }
            currentDTC = dtc;
            currentDTC.addNotify(this);
        }
        currentA = currentDT.getDefaultActions();
        try {
            DropTargetDragEvent dtde = new DropTargetDragEvent(dtc, hots, currentDA, currentSA);
            DropTargetListener dtl = (DropTargetListener) dt;
            if (operationChanged) {
                dtl.dropActionChanged(dtde);
            } else {
                dtl.dragOver(dtde);
            }
            if (dragRejected) {
                currentDA = DnDConstants.ACTION_NONE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            currentDA = DnDConstants.ACTION_NONE;
        }
    } else {
        currentDA = DnDConstants.ACTION_NONE;
    }
}
Also used : DropTargetDragEvent(java.awt.dnd.DropTargetDragEvent) DropTargetListener(java.awt.dnd.DropTargetListener) DropTargetContext(java.awt.dnd.DropTargetContext) Point(java.awt.Point) DropTarget(java.awt.dnd.DropTarget) Component(java.awt.Component) Point(java.awt.Point) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) InvalidDnDOperationException(java.awt.dnd.InvalidDnDOperationException)

Example 9 with DropTargetDragEvent

use of java.awt.dnd.DropTargetDragEvent in project jna by java-native-access.

the class DropHandler method getDropAction.

/** Calculate the effective action.  The default implementation 
     * checks whether any {@link DataFlavor}s are supported, and if so,
     * will change the current action from {@link DnDConstants#ACTION_NONE} to 
     * something in common between the source and destination.  Refuse 
     * user-requested actions if they are not supported (rather than silently 
     * accepting a non-user-requested action, which is the Java's DnD default 
     * behavior).  The drop action is forced to {@link DnDConstants#ACTION_NONE} 
     * if there is no supported data flavor.
     * @param e {@link DropTargetEvent}
     * @return effective drop action
     * @see #isSupported(DataFlavor[])
     * @see #getDropActionsForFlavors
     * @see #getDropAction(DropTargetEvent, int, int, int)
     * @see #canDrop(DropTargetEvent, int, Point)
     */
protected int getDropAction(DropTargetEvent e) {
    int currentAction = DragHandler.NONE;
    int sourceActions = DragHandler.NONE;
    Point location = null;
    DataFlavor[] flavors = new DataFlavor[0];
    if (e instanceof DropTargetDragEvent) {
        DropTargetDragEvent ev = (DropTargetDragEvent) e;
        currentAction = ev.getDropAction();
        sourceActions = ev.getSourceActions();
        flavors = ev.getCurrentDataFlavors();
        location = ev.getLocation();
    } else if (e instanceof DropTargetDropEvent) {
        DropTargetDropEvent ev = (DropTargetDropEvent) e;
        currentAction = ev.getDropAction();
        sourceActions = ev.getSourceActions();
        flavors = ev.getCurrentDataFlavors();
        location = ev.getLocation();
    }
    if (isSupported(flavors)) {
        int availableActions = getDropActionsForFlavors(flavors);
        currentAction = getDropAction(e, currentAction, sourceActions, availableActions);
        if (currentAction != DragHandler.NONE) {
            if (canDrop(e, currentAction, location)) {
                return currentAction;
            }
        }
    }
    return DragHandler.NONE;
}
Also used : DropTargetDragEvent(java.awt.dnd.DropTargetDragEvent) Point(java.awt.Point) DropTargetDropEvent(java.awt.dnd.DropTargetDropEvent) Point(java.awt.Point) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 10 with DropTargetDragEvent

use of java.awt.dnd.DropTargetDragEvent in project android by JetBrains.

the class DropTargetDragEventBuilder method build.

public DropTargetDragEvent build() {
    DropTargetDragEvent event = mock(DropTargetDragEvent.class);
    when(event.getSource()).thenReturn(mySource);
    when(event.getLocation()).thenReturn(new Point(myX, myY));
    when(event.getDropTargetContext()).thenReturn(myDropTargetContext);
    when(event.getDropAction()).thenReturn(myDropAction);
    when(event.getSourceActions()).thenReturn(mySourceActions);
    when(event.getTransferable()).thenReturn(myTransferable);
    DataFlavor[] flavors = myTransferable.getTransferDataFlavors();
    when(event.getCurrentDataFlavors()).thenReturn(flavors);
    when(event.getCurrentDataFlavorsAsList()).thenReturn(Arrays.asList(flavors));
    return event;
}
Also used : DropTargetDragEvent(java.awt.dnd.DropTargetDragEvent) DataFlavor(java.awt.datatransfer.DataFlavor)

Aggregations

DropTargetDragEvent (java.awt.dnd.DropTargetDragEvent)12 DropTarget (java.awt.dnd.DropTarget)9 DropTargetDropEvent (java.awt.dnd.DropTargetDropEvent)9 DropTargetAdapter (java.awt.dnd.DropTargetAdapter)7 EditorFragmentComponent (com.intellij.codeInsight.hint.EditorFragmentComponent)3 JBLayeredPane (com.intellij.ui.components.JBLayeredPane)3 Activatable (com.intellij.util.ui.update.Activatable)3 UiNotifyConnector (com.intellij.util.ui.update.UiNotifyConnector)3 Point (java.awt.Point)3 Component (java.awt.Component)2 DataFlavor (java.awt.datatransfer.DataFlavor)2 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)2 DropTargetContext (java.awt.dnd.DropTargetContext)2 DropTargetListener (java.awt.dnd.DropTargetListener)2 InvalidDnDOperationException (java.awt.dnd.InvalidDnDOperationException)2 IOException (java.io.IOException)2 DropTargetEvent (java.awt.dnd.DropTargetEvent)1 Override (java.lang.Override)1 TooManyListenersException (java.util.TooManyListenersException)1 JFrame (javax.swing.JFrame)1