2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.uiDesigner
.designSurface
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.uiDesigner
.FormEditingUtil
;
20 import com
.intellij
.uiDesigner
.radComponents
.RadComponent
;
21 import com
.intellij
.util
.ui
.UIUtil
;
22 import org
.jetbrains
.annotations
.NotNull
;
24 import java
.awt
.Component
;
25 import java
.awt
.Point
;
26 import java
.awt
.dnd
.*;
27 import java
.awt
.event
.InputEvent
;
28 import java
.awt
.event
.KeyEvent
;
29 import java
.awt
.event
.MouseEvent
;
30 import java
.util
.ArrayList
;
33 * @author Anton Katilin
34 * @author Vladimir Kondratyev
36 public final class DragSelectionProcessor
extends EventProcessor
{
37 private static final Logger LOG
= Logger
.getInstance("#com.intellij.uiDesigner.designSurface.DragSelectionProcessor");
40 * We have not start drag/cancel drop if mouse pointer trembles in small area
42 public static final int TREMOR
= 3;
44 private final GuiEditor myEditor
;
46 private Point myPressPoint
;
48 private boolean myDragStarted
;
50 private final MyDragGestureRecognizer myDragGestureRecognizer
;
51 private final MyDragSourceListener myDragSourceListener
= new MyDragSourceListener();
53 public DragSelectionProcessor(@NotNull final GuiEditor editor
) {
55 myDragGestureRecognizer
= new MyDragGestureRecognizer(DragSource
.getDefaultDragSource(),
56 myEditor
.getActiveDecorationLayer(),
57 DnDConstants
.ACTION_COPY_OR_MOVE
);
60 public boolean isDragActive() {
64 protected boolean cancelOperation() {
68 // Try to drop selection at the point of mouse event.
70 myEditor
.setDesignTimeInsets(2);
71 myEditor
.getActiveDecorationLayer().removeFeedback();
72 myEditor
.repaintLayeredPane();
76 protected void processKeyEvent(final KeyEvent e
) {
79 protected void processMouseEvent(final MouseEvent e
) {
80 if (e
.getID() == MouseEvent
.MOUSE_PRESSED
) {
81 myPressPoint
= e
.getPoint();
83 else if (e
.getID() == MouseEvent
.MOUSE_RELEASED
) {
85 RadComponent component
= FormEditingUtil
.getRadComponentAt(myEditor
.getRootContainer(), e
.getX(), e
.getY());
86 if (component
!= null) {
87 if (UIUtil
.isControlKeyDown(e
)) {
88 component
.setSelected(!component
.isSelected());
93 else if (e
.getID() == MouseEvent
.MOUSE_DRAGGED
) {
95 if ((Math
.abs(e
.getX() - myPressPoint
.getX()) > TREMOR
|| Math
.abs(e
.getY() - myPressPoint
.getY()) > TREMOR
)) {
96 ArrayList
<InputEvent
> eventList
= new ArrayList
<InputEvent
>();
98 myDragGestureRecognizer
.setTriggerEvent(e
);
99 DragGestureEvent dge
= new DragGestureEvent(myDragGestureRecognizer
,
100 UIUtil
.isControlKeyDown(e
) ? DnDConstants
.ACTION_COPY
: DnDConstants
.ACTION_MOVE
,
101 myPressPoint
, eventList
);
103 myDragStarted
= true;
104 myEditor
.getDropTargetListener().setUseDragDelta(true);
106 DraggedComponentList
.pickupSelection(myEditor
, e
.getPoint()),
107 myDragSourceListener
);
113 private static class MyDragGestureRecognizer
extends DragGestureRecognizer
{
114 public MyDragGestureRecognizer(DragSource ds
, Component c
, int sa
) {
118 protected void registerListeners() {
121 protected void unregisterListeners() {
124 public void setTriggerEvent(final MouseEvent e
) {
130 private class MyDragSourceListener
extends DragSourceAdapter
{
131 public void dropActionChanged(DragSourceDragEvent dsde
) {
132 final int shiftDownMask
= (dsde
.getGestureModifiersEx() & KeyEvent
.SHIFT_DOWN_MASK
);
133 if (shiftDownMask
!= 0) {
134 myEditor
.setDesignTimeInsets(12);
137 myEditor
.setDesignTimeInsets(2);
141 public void dragDropEnd(DragSourceDropEvent dsde
) {
142 myDragStarted
= false;
143 myEditor
.getDropTargetListener().setUseDragDelta(false);
144 myEditor
.setDesignTimeInsets(2);