update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / designSurface / EventProcessor.java
blob4ed9db73a8de31db1e776d63b995567234b3622b
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 java.awt.*;
19 import java.awt.event.KeyEvent;
20 import java.awt.event.MouseEvent;
22 /**
23 * @author Anton Katilin
24 * @author Vladimir Kondratyev
26 public abstract class EventProcessor {
27 private Cursor myCursor;
29 public final Cursor getCursor(){
30 return myCursor;
33 public final void setCursor(final Cursor cursor){
34 myCursor = cursor;
37 protected abstract void processKeyEvent(KeyEvent e);
39 protected abstract void processMouseEvent(MouseEvent e);
41 /**
42 * @return true if processor cancelled its operation; false otherwise
43 */
44 protected abstract boolean cancelOperation();
46 public boolean isDragActive() {
47 return false;
50 public boolean needMousePressed() {
51 return false;
54 protected static ComponentDropLocation.Direction directionFromKey(final int keyCode) {
55 switch(keyCode) {
56 case KeyEvent.VK_RIGHT: return ComponentDropLocation.Direction.RIGHT;
57 case KeyEvent.VK_LEFT: return ComponentDropLocation.Direction.LEFT;
58 case KeyEvent.VK_UP: return ComponentDropLocation.Direction.UP;
59 case KeyEvent.VK_DOWN: return ComponentDropLocation.Direction.DOWN;
60 case KeyEvent.VK_END: return ComponentDropLocation.Direction.RIGHT;
61 case KeyEvent.VK_HOME: return ComponentDropLocation.Direction.LEFT;
62 case KeyEvent.VK_PAGE_UP: return ComponentDropLocation.Direction.UP;
63 case KeyEvent.VK_PAGE_DOWN: return ComponentDropLocation.Direction.DOWN;
64 default: return null;
68 private static boolean isMoveToLast(final int keyCode) {
69 return keyCode == KeyEvent.VK_HOME || keyCode == KeyEvent.VK_END ||
70 keyCode == KeyEvent.VK_PAGE_UP || keyCode == KeyEvent.VK_PAGE_DOWN;
73 protected static ComponentDropLocation moveDropLocation(final GuiEditor editor, final ComponentDropLocation location,
74 final ComponentDragObject dragObject, final KeyEvent e) {
75 ComponentDropLocation.Direction dir = directionFromKey(e.getKeyCode());
76 boolean moveToLast = isMoveToLast(e.getKeyCode());
77 if (dir != null && location != null) {
78 e.consume();
79 ComponentDropLocation adjacentLocation;
80 if (moveToLast) {
81 adjacentLocation = location.getAdjacentLocation(dir);
82 ComponentDropLocation lastLocation = location;
83 while(adjacentLocation != null) {
84 if (adjacentLocation.canDrop(dragObject)) {
85 lastLocation = adjacentLocation;
87 adjacentLocation = adjacentLocation.getAdjacentLocation(dir);
89 adjacentLocation = lastLocation;
91 else {
92 adjacentLocation = location.getAdjacentLocation(dir);
93 while(adjacentLocation != null && !adjacentLocation.canDrop(dragObject)) {
94 adjacentLocation = adjacentLocation.getAdjacentLocation(dir);
98 if (adjacentLocation != null && adjacentLocation.canDrop(dragObject)) {
99 adjacentLocation.placeFeedback(editor.getActiveDecorationLayer(), dragObject);
100 return adjacentLocation;
103 return location;