KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > TreeDragUnderEffect


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.dnd;
12
13  
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16
17 class TreeDragUnderEffect extends DragUnderEffect {
18
19     private Tree tree;
20     private TreeItem currentItem = null;
21     private int currentEffect = DND.FEEDBACK_NONE;
22     private TreeItem[] selection = new TreeItem[0];
23
24 TreeDragUnderEffect(Tree tree) {
25     this.tree = tree;
26 }
27 void show(int effect, int x, int y) {
28     TreeItem item = null;
29     if (effect != DND.FEEDBACK_NONE) item = findItem(x, y);
30     if (item == null) effect = DND.FEEDBACK_NONE;
31     if (currentEffect != effect && currentEffect == DND.FEEDBACK_NONE) {
32         selection = tree.getSelection();
33         tree.setSelection(new TreeItem[0]);
34     }
35     boolean restoreSelection = currentEffect != effect && effect == DND.FEEDBACK_NONE;
36     setDragUnderEffect(effect, item);
37     if (restoreSelection) {
38         tree.setSelection(selection);
39         selection = new TreeItem[0];
40     }
41 }
42 private TreeItem findItem(int x , int y){
43     Point coordinates = new Point(x, y);
44     coordinates = tree.toControl(coordinates);
45     return tree.getItem(coordinates);
46 }
47 private void setDragUnderEffect(int effect, TreeItem item) {
48     switch (effect) {
49         case DND.FEEDBACK_SELECT:
50             if (currentEffect == DND.FEEDBACK_INSERT_AFTER ||
51                 currentEffect == DND.FEEDBACK_INSERT_BEFORE) {
52                 setInsertMark(null, false);
53                 currentEffect = DND.FEEDBACK_NONE;
54                 currentItem = null;
55             }
56             if (currentEffect != effect || currentItem != item) {
57                 setDropSelection(item);
58                 currentEffect = DND.FEEDBACK_SELECT;
59                 currentItem = item;
60             }
61             break;
62         case DND.FEEDBACK_INSERT_AFTER:
63         case DND.FEEDBACK_INSERT_BEFORE:
64             if (currentEffect == DND.FEEDBACK_SELECT) {
65                 setDropSelection(null);
66                 currentEffect = DND.FEEDBACK_NONE;
67                 currentItem = null;
68             }
69             if (currentEffect != effect || currentItem != item) {
70                 setInsertMark(item, effect == DND.FEEDBACK_INSERT_AFTER);
71                 currentEffect = effect;
72                 currentItem = item;
73             }
74             break;
75         default :
76             if (currentEffect == DND.FEEDBACK_INSERT_AFTER ||
77                 currentEffect == DND.FEEDBACK_INSERT_BEFORE) {
78                 setInsertMark(null, false);
79             }
80             if (currentEffect == DND.FEEDBACK_SELECT) {
81                 setDropSelection(null);
82             }
83             currentEffect = DND.FEEDBACK_NONE;
84             currentItem = null;
85             break;
86     }
87 }
88 private void setDropSelection (TreeItem item) {
89     if (item == null) {
90         tree.setSelection(new TreeItem[0]);
91     } else {
92         tree.setSelection(new TreeItem[]{item});
93     }
94 }
95 private void setInsertMark (TreeItem item, boolean after) {
96     // not currently implemented
97
}
98 }
99
Popular Tags