KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > toolbars > ToolbarDnDListener


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui.toolbars;
21
22 import org.openide.awt.Toolbar;
23
24 /** Listener for toolbar motion.
25  *
26  * When somebody works (drag and drop) with any toolbar and this work comes up to this listener.
27  * Listener is adherented to ToolbarConfiguration so every toolbar's motion is reflected in
28  * this ToolbarConfiguration and it's appropriate ToolbarConstraints.
29  * So this is only place, where the ToolbarConfiguration is changed by toolbar's motion.
30  *
31  * @author Libor Kramolis
32  */

33 public class ToolbarDnDListener extends Object JavaDoc implements Toolbar.DnDListener {
34     /** now dragged toolbar */
35     private ToolbarConstraints draggedToolbar;
36     private ToolbarConfiguration configuration;
37
38     /** Create new Toolbar listener.
39      * @param conf specified toolbat configuration.
40      */

41     public ToolbarDnDListener (ToolbarConfiguration conf) {
42         configuration = conf;
43     }
44
45     /** Move toolbar and followers horizontaly.
46      * @param tc first moved toolbar
47      * @param dx horizontaly distance
48      */

49     protected void moveToolbar2EndHorizontally (ToolbarConstraints tc, int dx) {
50         if (dx == 0) // no move
51
return;
52
53         if (dx < 0)
54             tc.moveLeft2End (-dx);
55         if (dx > 0)
56             tc.moveRight2End (dx);
57     }
58
59     /** Move toolbar horizontaly.
60      * @param tc moved toolbar
61      * @param dx horizontal distance
62      */

63     protected void moveToolbarHorizontally (ToolbarConstraints tc, int dx) {
64         if (dx == 0) // no move
65
return;
66
67         if (dx < 0)
68             tc.moveLeft (-dx);
69         if (dx > 0)
70             tc.moveRight (dx);
71     }
72
73     /** Move toolbar verticaly.
74      * @param tc moved toolbar
75      * @param dy vertical distance
76      */

77     protected void moveToolbarVertically (ToolbarConstraints tc, int dy) {
78         if (dy == 0) // no move
79
return;
80
81         if (dy < 0)
82             moveUp (tc, -dy);
83         if (dy > 0)
84             moveDown (tc, dy);
85     }
86
87     /** Try move toolbar up.
88      * @param tc moved toolbar
89      * @param dy vertical distance
90      */

91     protected void moveUp (ToolbarConstraints tc, int dy) {
92         if (dy < ((Toolbar.getBasicHeight() / 2) + 2))
93             return;
94
95         int rI = tc.rowIndex();
96         if (draggedToolbar.isAlone()) { // is alone on row(s) -> no new rows
97
if (rI == 0) // in first row
98
return;
99         }
100
101         int pos = rI - 1;
102         tc.destroy();
103
104         int plus = 0;
105         int rowCount = configuration.getRowCount();
106         for (int i = pos; i < pos + tc.getRowCount(); i++) {
107             configuration.getRow (i + plus).addToolbar (tc, tc.getPosition());
108             if (rowCount != configuration.getRowCount()) {
109                 rowCount = configuration.getRowCount();
110                 plus++;
111             }
112         }
113         configuration.checkToolbarRows();
114         configuration.updateBounds( configuration.getRow(0) );
115     }
116
117     /** Try move toolbar down.
118      * @param tc moved toolbar
119      * @param dy vertical distance
120      */

121     public void moveDown (ToolbarConstraints tc, int dy) {
122         int rI = tc.rowIndex();
123
124         int step = ((Toolbar.getBasicHeight() / 2) + 2);
125
126         if (draggedToolbar.isAlone()) { // is alone on row(s) -> no new rows
127
if (rI == (configuration.getRowCount() - tc.getRowCount())) // in last rows
128
return;
129             step = ((Toolbar.getBasicHeight() / 4) + 2);
130         }
131
132         if (dy < step)
133             return;
134
135         int pos = rI + 1;
136         tc.destroy();
137         
138         for (int i = pos; i < pos + tc.getRowCount(); i++)
139             configuration.getRow (i).addToolbar (tc, tc.getPosition());
140
141         configuration.checkToolbarRows();
142     }
143     
144     ///////////////////////////
145
// from Toolbar.DnDListener
146

147     /** Invoced when toolbar is dragged. */
148     public void dragToolbar (Toolbar.DnDEvent e) {
149         if (draggedToolbar == null) {
150             draggedToolbar = configuration.getToolbarConstraints (e.getName());
151         }
152
153         switch (e.getType()) {
154         case Toolbar.DnDEvent.DND_LINE:
155             // not implemented yet - it's bug [1]
156
// not implemented in this version
157
return; // only Toolbar.DnDEvent.DND_LINE
158
case Toolbar.DnDEvent.DND_END:
159             moveToolbar2EndHorizontally (draggedToolbar, e.getDX());
160             break;
161         case Toolbar.DnDEvent.DND_ONE:
162             moveToolbarVertically (draggedToolbar, e.getDY());
163             break;
164         }
165         if (e.getType() == Toolbar.DnDEvent.DND_ONE)
166             moveToolbarHorizontally (draggedToolbar, e.getDX());
167
168         draggedToolbar.updatePosition();
169
170         configuration.revalidateWindow();
171     }
172
173     /** Invoced when toolbar is dropped. */
174     public void dropToolbar (Toolbar.DnDEvent e) {
175         dragToolbar (e);
176
177         configuration.reflectChanges();
178         draggedToolbar = null;
179     }
180 } // end of class ToolbarDnDListener
181

182
Popular Tags