KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > FastViewDnDHandler


1 /*******************************************************************************
2  * Copyright (c) 2007 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
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.action.ToolBarManager;
19 import org.eclipse.jface.util.Geometry;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.graphics.Cursor;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Event;
28 import org.eclipse.swt.widgets.Listener;
29 import org.eclipse.swt.widgets.ToolBar;
30 import org.eclipse.swt.widgets.ToolItem;
31 import org.eclipse.ui.IViewReference;
32 import org.eclipse.ui.internal.dnd.AbstractDropTarget;
33 import org.eclipse.ui.internal.dnd.DragUtil;
34 import org.eclipse.ui.internal.dnd.IDragOverListener;
35 import org.eclipse.ui.internal.dnd.IDropTarget;
36 import org.eclipse.ui.presentations.PresentationUtil;
37
38 /**
39  * @since 3.3
40  *
41  */

42 public class FastViewDnDHandler implements IDragOverListener {
43     private String JavaDoc id;
44     private ToolBarManager tbm;
45     private WorkbenchWindow wbw;
46     private ViewDropTarget dropTarget = null;
47
48     private Listener dragListener = new Listener() {
49         public void handleEvent(Event event) {
50             Point position = DragUtil.getEventLoc(event);
51             
52             ToolBar toolbar = tbm.getControl();
53             Point local = toolbar.toControl(position);
54             ToolItem item = toolbar.getItem(local);
55             IViewReference ref = (IViewReference) item
56             .getData(ShowFastViewContribution.FAST_VIEW);
57
58             if (ref != null) {
59                 startDraggingFastView(ref, position, false);
60             }
61         }
62     };
63
64     class ViewDropTarget extends AbstractDropTarget {
65         List JavaDoc panes;
66         ToolItem curItem;
67
68         /**
69          * @param panesToDrop the list of ViewPanes to drop at the given position
70          */

71         public ViewDropTarget(List JavaDoc panesToDrop, ToolItem position) {
72             setTarget(panesToDrop, position);
73         }
74         
75         public void setTarget(List JavaDoc panesToDrop, ToolItem position) {
76             panes = panesToDrop;
77             this.curItem = position;
78         }
79
80         /* (non-Javadoc)
81          * @see org.eclipse.ui.internal.dnd.IDropTarget#drop()
82          */

83         public void drop() {
84             Perspective persp = wbw.getActiveWorkbenchPage().getActivePerspective();
85             FastViewManager fvm = persp.getFastViewManager();
86
87             int insertIndex = tbm.getControl().indexOf(curItem);
88             Iterator JavaDoc iter = panes.iterator();
89             while (iter.hasNext()) {
90                 ViewPane pane = (ViewPane) iter.next();
91                 IViewReference ref = pane.getViewReference();
92                 adoptRef(ref);
93                 fvm.addViewReference(id, insertIndex++, ref, !iter.hasNext());
94             }
95         }
96
97         private void adoptRef(IViewReference ref) {
98             Perspective persp = wbw.getActiveWorkbenchPage()
99                     .getActivePerspective();
100             PerspectiveHelper helper = persp.getPresentation();
101             ContainerPlaceholder ourContainerPlaceholder = (ContainerPlaceholder) helper
102                     .findPart(id, null);
103             LayoutPart refPart = helper.findPart(ref.getId(), ref
104                     .getSecondaryId());
105             ILayoutContainer refContainer = refPart.container;
106             if (refContainer != ourContainerPlaceholder) {
107                 // remove the old part... if it's represented by a
108
// placeholder then just remove it...
109
if (refPart instanceof PartPlaceholder) {
110                     if (refContainer instanceof ContainerPlaceholder) {
111                         // just remove the placeholder
112
ViewStack realContainer = (ViewStack) ((ContainerPlaceholder) refContainer)
113                                 .getRealContainer();
114                         realContainer.remove(refPart);
115                     }
116                     else if (refContainer instanceof ViewStack) {
117                         refContainer.remove(refPart);
118                     }
119                 }
120                 else {
121                     // If its a real view ref then defref it...
122
helper.derefPart(refPart);
123                 }
124                 PartPlaceholder newPlaceholder = new PartPlaceholder(ref
125                         .getId());
126                 ourContainerPlaceholder.add(newPlaceholder);
127             }
128         }
129         
130         /*
131          * (non-Javadoc)
132          *
133          * @see org.eclipse.ui.internal.dnd.IDropTarget#getCursor()
134          */

135         public Cursor getCursor() {
136             return DragCursors.getCursor(DragCursors.FASTVIEW);
137         }
138
139         public Rectangle getSnapRectangle() {
140             if (curItem == null) {
141                 // As long as the toolbar is not empty, highlight the place
142
// where this view will appear (we
143
// may have compressed it to save space when empty, so the actual
144
// icon location may not be over the toolbar when it is empty)
145
if (tbm.getControl().getItemCount() > 0) {
146                     return getLocationOfNextIcon();
147                 }
148                 // If the toolbar is empty, highlight the entire toolbar
149
return DragUtil.getDisplayBounds(tbm.getControl());
150             }
151
152             return Geometry.toDisplay(tbm.getControl(), curItem.getBounds());
153         }
154     }
155     
156     /**
157      *
158      */

159     public FastViewDnDHandler(String JavaDoc id, final ToolBarManager tbm, WorkbenchWindow wbw) {
160         this.id = id;
161         this.tbm = tbm;
162         this.wbw = wbw;
163         
164         // Hook the 'drop' listener to the control
165
DragUtil.addDragTarget(tbm.getControl(), this);
166         PresentationUtil.addDragListener(tbm.getControl(), dragListener);
167         
168         // Clean up on dispose
169
tbm.getControl().addDisposeListener(new DisposeListener() {
170             public void widgetDisposed(DisposeEvent e) {
171                 DragUtil.removeDragTarget((Control)(e.widget), FastViewDnDHandler.this);
172                 PresentationUtil.removeDragListener(tbm.getControl(), dragListener);
173             }
174         });
175     }
176
177     /**
178      * Returns the toolbar item at the given position, in display coordinates
179      * @param position
180      */

181     private ToolItem getToolItem(Point position) {
182         ToolBar toolbar = tbm.getControl();
183         Point local = toolbar.toControl(position);
184         return toolbar.getItem(local);
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.ui.internal.dnd.IDragOverListener#drag(org.eclipse.swt.widgets.Control, java.lang.Object, org.eclipse.swt.graphics.Point, org.eclipse.swt.graphics.Rectangle)
189      */

190     public IDropTarget drag(Control currentControl, Object JavaDoc draggedObject,
191             Point position, Rectangle dragRectangle) {
192         // If we're trying to drop onto a 'standalone' stack, don't...
193
if (isStandaloneStack())
194             return null;
195         
196         ToolItem targetItem = getToolItem(position);
197         if (draggedObject instanceof ViewPane) {
198             ViewPane pane = (ViewPane) draggedObject;
199
200             // Can't drag views between windows
201
if (pane.getWorkbenchWindow() != wbw) {
202                 return null;
203             }
204
205             List JavaDoc newList = new ArrayList JavaDoc(1);
206             newList.add(draggedObject);
207
208             return createDropTarget(newList, targetItem);
209         }
210         if (draggedObject instanceof ViewStack) {
211             ViewStack folder = (ViewStack) draggedObject;
212
213             if (folder.getWorkbenchWindow() != wbw) {
214                 return null;
215             }
216
217             List JavaDoc viewList = new ArrayList JavaDoc(folder.getItemCount());
218             LayoutPart[] children = folder.getChildren();
219
220             for (int idx = 0; idx < children.length; idx++) {
221                 if (!(children[idx] instanceof PartPlaceholder)) {
222                     viewList.add(children[idx]);
223                 }
224             }
225
226             return createDropTarget(viewList, targetItem);
227         }
228
229         return null;
230     }
231     /**
232      * Tests the view references associated with the stack and
233      * returns <code>true</code> if any view is a stand-alone view
234      *
235      * @return <code>true</code> is any view is stand-alone
236      */

237     private boolean isStandaloneStack() {
238         Perspective persp = wbw.getActiveWorkbenchPage().getActivePerspective();
239         List JavaDoc fvs = persp.getFastViewManager().getFastViews(id);
240         for (Iterator JavaDoc iterator = fvs.iterator(); iterator.hasNext();) {
241             IViewReference ref = (IViewReference) iterator.next();
242             if (persp.isStandaloneView(ref))
243                 return true;
244         }
245         
246         return false;
247     }
248
249     private IDropTarget createDropTarget(List JavaDoc viewList, ToolItem targetItem) {
250         if (dropTarget == null) {
251             dropTarget = new ViewDropTarget(viewList, targetItem);
252         } else {
253             dropTarget.setTarget(viewList, targetItem);
254         }
255         return dropTarget;
256     }
257
258     /**
259      * Returns the approximate location where the next fastview icon
260      * will be drawn (display coordinates)
261      */

262     public Rectangle getLocationOfNextIcon() {
263         ToolBar control = tbm.getControl();
264
265         Rectangle result = control.getBounds();
266         Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
267         result.height = size.y;
268         result.width = size.x;
269         
270         boolean horizontal = (control.getStyle() & SWT.VERTICAL) == 0;
271         if (control.getItemCount() == 0) {
272             Geometry.setDimension(result, horizontal, 0);
273         }
274         
275         int hoverSide = horizontal ? SWT.RIGHT : SWT.BOTTOM;
276
277         result = Geometry.getExtrudedEdge(result, -Geometry.getDimension(
278                 result, !horizontal), hoverSide);
279
280         return Geometry.toDisplay(control.getParent(), result);
281     }
282
283     /**
284      * Returns the index of the ToolItem fronting the view ref
285      * @param toFind the view reference to find the index of
286      * @return the index or -1 if not found
287      */

288     private int getIndex(IViewReference toFind) {
289         ToolItem[] items = tbm.getControl().getItems();
290         for (int i = 0; i < items.length; i++) {
291             if (items[i].getData(ShowFastViewContribution.FAST_VIEW) == toFind) {
292                 return i;
293             }
294         }
295
296         return -1;
297     }
298     
299     /**
300      * Begins dragging a particular fast view
301      *
302      * @param ref
303      * @param position
304      */

305     protected void startDraggingFastView(IViewReference ref, Point position,
306             boolean usingKeyboard) {
307         int index = getIndex(ref);
308         if (index == -1)
309             return;
310         
311         ToolItem item = tbm.getControl().getItem(index);
312         Rectangle dragRect = Geometry.toDisplay(tbm.getControl(), item.getBounds());
313         startDrag(((WorkbenchPartReference) ref).getPane(), dragRect, position,
314                 usingKeyboard);
315     }
316
317     private void startDrag(Object JavaDoc toDrag, Rectangle dragRect, Point position,
318             boolean usingKeyboard) {
319         WorkbenchPage page = wbw.getActiveWorkbenchPage();
320         Perspective persp = page.getActivePerspective();
321
322         // Prevent dragging non-movable refs out of a minimized stack
323
if (toDrag instanceof ViewPane) {
324             ViewPane pane = (ViewPane) toDrag;
325             if (!persp.isMoveable(pane.getViewReference()))
326                 return;
327         }
328         
329         IViewReference oldFastView = null;
330         if (persp != null) {
331             oldFastView = persp.getActiveFastView();
332
333             if (page != null) {
334                 page.hideFastView();
335             }
336         }
337
338         if (page.isPageZoomed()) {
339             page.zoomOut();
340         }
341
342         boolean success = DragUtil.performDrag(toDrag, dragRect, position,
343                 !usingKeyboard);
344
345         // If the drag was cancelled, reopen the old fast view
346
if (!success && oldFastView != null && page != null) {
347             page.toggleFastView(oldFastView);
348         }
349     }
350 }
351
Popular Tags