ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / designSurface / DragSelectionProcessor.java
blob2699d454ba77002ccf96c0cc82074600ecc602f8
1 /*
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;
32 /**
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");
39 /**
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) {
54 myEditor = editor;
55 myDragGestureRecognizer = new MyDragGestureRecognizer(DragSource.getDefaultDragSource(),
56 myEditor.getActiveDecorationLayer(),
57 DnDConstants.ACTION_COPY_OR_MOVE);
60 public boolean isDragActive() {
61 return myDragStarted;
64 protected boolean cancelOperation() {
65 if (!myDragStarted) {
66 return true;
68 // Try to drop selection at the point of mouse event.
69 //cancelDrag();
70 myEditor.setDesignTimeInsets(2);
71 myEditor.getActiveDecorationLayer().removeFeedback();
72 myEditor.repaintLayeredPane();
73 return true;
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) {
84 if (!myDragStarted) {
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) {
94 if (!myDragStarted) {
95 if ((Math.abs(e.getX() - myPressPoint.getX()) > TREMOR || Math.abs(e.getY() - myPressPoint.getY()) > TREMOR)) {
96 ArrayList<InputEvent> eventList = new ArrayList<InputEvent>();
97 eventList.add(e);
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);
105 dge.startDrag(null,
106 DraggedComponentList.pickupSelection(myEditor, e.getPoint()),
107 myDragSourceListener);
113 private static class MyDragGestureRecognizer extends DragGestureRecognizer {
114 public MyDragGestureRecognizer(DragSource ds, Component c, int sa) {
115 super(ds, c, sa);
118 protected void registerListeners() {
121 protected void unregisterListeners() {
124 public void setTriggerEvent(final MouseEvent e) {
125 resetRecognizer();
126 appendEvent(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);
136 else {
137 myEditor.setDesignTimeInsets(2);
141 public void dragDropEnd(DragSourceDropEvent dsde) {
142 myDragStarted = false;
143 myEditor.getDropTargetListener().setUseDragDelta(false);
144 myEditor.setDesignTimeInsets(2);