KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > docking > GenericDragTargetListener


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * GenericDragTargetListener.java
28  *
29  * Created on March 16, 2006, 10:59 PM
30  *
31  */

32
33 package it.businesslogic.ireport.gui.docking;
34
35 import it.businesslogic.ireport.gui.MainFrame;
36 import java.awt.BasicStroke JavaDoc;
37 import java.awt.Color JavaDoc;
38 import java.awt.Component JavaDoc;
39 import java.awt.Graphics2D JavaDoc;
40 import java.awt.datatransfer.DataFlavor JavaDoc;
41 import java.awt.datatransfer.Transferable JavaDoc;
42 import java.awt.dnd.DnDConstants JavaDoc;
43 import java.awt.dnd.DropTargetContext JavaDoc;
44 import java.awt.dnd.DropTargetDragEvent JavaDoc;
45 import java.awt.dnd.DropTargetDropEvent JavaDoc;
46 import java.awt.dnd.DropTargetEvent JavaDoc;
47 import java.awt.dnd.DropTargetListener JavaDoc;
48
49
50   /**
51    * GenericDragTargetListener
52    * a listener that tracks the state of the operation
53    * @see java.awt.dnd.DropTargetListener
54    * @see java.awt.dnd.DropTarget
55    */

56  public class GenericDragTargetListener implements DropTargetListener JavaDoc {
57
58     public static JDraggableTabbedPane lastDp = null;
59     
60     public static final int POSITION_UP = 0;
61     public static final int POSITION_CENTER = 1;
62     public static final int POSITION_BOTTOM = 2;
63     public static final Color JavaDoc COLOR = new java.awt.Color JavaDoc(255,0,0,128);
64     
65     private int lastPosition = -1;
66     
67     public static final BasicStroke JavaDoc STROKE = new BasicStroke JavaDoc(3);
68     /**
69      * start "drag under" feedback on component
70      * invoke acceptDrag or rejectDrag based on isDragOk
71      */

72     public void dragEnter(DropTargetDragEvent JavaDoc e) {
73         handleMotion(e);
74       e.acceptDrag(e.getDropAction());
75     }
76
77     /**
78      * continue "drag under" feedback on component
79      * invoke acceptDrag or rejectDrag based on isDragOk
80      */

81     public void dragOver(DropTargetDragEvent JavaDoc e) {
82         
83          handleMotion(e);
84           
85       e.acceptDrag(e.getDropAction());
86     }
87     
88     public void handleMotion(DropTargetDragEvent JavaDoc dtde)
89     {
90         DropTargetContext JavaDoc context = dtde.getDropTargetContext();
91       
92       //Transferable tr = dtde.getTransferable();
93
// JDK 1.4.2 workaround Bug Id 4248542
94
DropTargetDropEvent JavaDoc tempDTDropEvent = new
95         DropTargetDropEvent JavaDoc(dtde.getDropTargetContext(),
96                 dtde.getLocation(), 0, 0);
97         Transferable JavaDoc tr = tempDTDropEvent.getTransferable();
98
99         
100       DataFlavor JavaDoc[] df = tr.getTransferDataFlavors();
101
102       if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.gui.docking.PanelView"))
103       {
104           JDraggableTabbedPane dp = lookForParentDraggablePane( dtde.getDropTargetContext().getComponent() );
105           //System.out.println( "Sono in!!" + dtde.getLocation() + " ->" + dp);
106

107           if (lastDp != dp)
108           {
109               if (lastDp != null) lastDp.repaint();
110               lastDp = dp;
111               lastPosition = -1;
112           }
113           if (dp != null)
114           {
115               Graphics2D JavaDoc g = (Graphics2D JavaDoc)dp.getGraphics();
116               int height = dp.getHeight();
117               if (dtde.getLocation()!= null)
118               {
119                   try {
120                
121                   int position = (int)dtde.getLocation().getY();
122                   
123                   if (position <= height/3)
124                   {
125                       if (lastPosition != POSITION_UP)
126                       {
127                         lastPosition = POSITION_UP;
128                         dp.paint(g);
129                         //g.setClip( dp.getBounds() );
130
g.setStroke(STROKE);
131                         g.setColor( COLOR );
132                         g.drawRect(1,1,dp.getWidth()-4, (height/3)-6);
133                       }
134                   }
135                   else if ( (position <= (height/3) * 2))
136                   {
137                       if (lastPosition != POSITION_CENTER)
138                       {
139                         lastPosition = POSITION_CENTER;
140                         dp.paint(g);
141                         //g.setClip( dp.getBounds() );
142
g.setStroke(STROKE);
143                         g.setColor( COLOR );
144                         g.drawRect(1,1,dp.getWidth()-4, height-6);
145                       }
146                   }
147                   else if ( lastPosition != POSITION_BOTTOM)
148                   {
149                       if (lastPosition != POSITION_BOTTOM)
150                       {
151                         lastPosition = POSITION_BOTTOM;
152                         dp.paint(g);
153                         g.setStroke(STROKE);
154                         g.setColor( COLOR );
155                         g.drawRect(1,height-(height/3),dp.getWidth()-4, (height/3)-4);
156                       }
157                   }
158                   } catch (Exception JavaDoc ex){
159                       
160                   }
161               }
162           }
163       }
164     }
165     
166     public void dropActionChanged(DropTargetDragEvent JavaDoc e) {
167     }
168     
169     public void dragExit(DropTargetEvent JavaDoc e) {
170         //if (lastDp != null) lastDp.repaint();
171
//lastDp = null;
172
}
173
174     public JDraggableTabbedPane lookForParentDraggablePane(Component JavaDoc component)
175     {
176         if (component == null) return null;
177         if (component instanceof JDraggableTabbedPane) return (JDraggableTabbedPane)component;
178         return lookForParentDraggablePane(component.getParent());
179         
180     }
181     /**
182      * perform action from getSourceActions on
183      * the transferrable
184      * invoke acceptDrop or rejectDrop
185      * invoke dropComplete
186      * if its a local (same JVM) transfer, use StringTransferable.localStringFlavor
187      * find a match for the flavor
188      * check the operation
189      * get the transferable according to the chosen flavor
190      * do the transfer
191      */

192     public void drop(DropTargetDropEvent JavaDoc dtde) {
193         
194         try {
195            
196             DropTargetContext JavaDoc context = dtde.getDropTargetContext();
197
198             Transferable JavaDoc tr = dtde.getTransferable();
199
200             DataFlavor JavaDoc[] df = tr.getTransferDataFlavors();
201
202             if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor))
203             {
204                 dtde.acceptDrop ( DnDConstants.ACTION_COPY_OR_MOVE);
205                 java.util.List JavaDoc fileList = (java.util.List JavaDoc)tr.getTransferData(DataFlavor.javaFileListFlavor);
206             
207                 MainFrame.getMainInstance().openFiles(fileList);
208             }
209             else if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.gui.docking.PanelView"))
210             {
211                 java.awt.datatransfer.DataFlavor JavaDoc myFlavor = new java.awt.datatransfer.DataFlavor JavaDoc(it.businesslogic.ireport.gui.docking.PanelView.class, it.businesslogic.ireport.gui.docking.PanelView.class.getName());
212                 it.businesslogic.ireport.gui.docking.PanelView panelView = (it.businesslogic.ireport.gui.docking.PanelView)tr.getTransferData( myFlavor );
213                 
214                 // Look for the parent draggable
215
JDraggableTabbedPane dp = lastDp;
216                 
217                 if (dp != null)
218                 {
219                 
220                     if (dp.getDockingContainer() == panelView.getDockingContainer())
221                     {
222                         int containerPosition = dp.getPosition();
223                         if (panelView.getDockingContainer().getPanelCount(panelView.getPosition()) == 1 && dp.getPosition() == panelView.getPosition())
224                         {
225                             // Nothing to do
226
}
227                         if (dp.getTabCount() > 1 && dp.getPosition() == panelView.getPosition())
228                         {
229                             if (lastPosition == POSITION_UP)
230                             {
231                                 dp.getDockingContainer().moveComponent(panelView, containerPosition);
232                             }
233                             if (lastPosition == POSITION_BOTTOM)
234                             {
235                                dp.getDockingContainer().moveComponent(panelView, containerPosition+1);
236                             }
237                         }
238                         else if (dp.getPosition() != panelView.getPosition())
239                         {
240                             if (lastPosition == POSITION_UP)
241                             {
242                                 if (containerPosition-1 >= 0 &&
243                                     panelView.getPosition() < containerPosition) containerPosition = containerPosition-1;
244                                 dp.getDockingContainer().moveComponent(panelView, containerPosition);
245                             }
246                             if (lastPosition == POSITION_BOTTOM)
247                             {
248                                if (panelView.getPosition() != containerPosition+1 || panelView.getDockingContainer().getPanelCount(panelView.getPosition())>0)
249                                {
250                                     if (panelView.getPosition() > containerPosition) containerPosition +=1;
251                                     dp.getDockingContainer().moveComponent(panelView, containerPosition);
252                                }
253                             }
254                             if (lastPosition == POSITION_CENTER)
255                             {
256                                if (panelView.getPosition() >= containerPosition || panelView.getDockingContainer().getPanelCount(panelView.getPosition()) == 1)
257                                {
258                                  if (containerPosition-1 >= 0) containerPosition = containerPosition-1;
259                                }
260                                else
261                                {
262                                    
263                                }
264                                dp.getDockingContainer().moveComponent(panelView, containerPosition, DockingContainer.INSERT_MODE_SHAREDPOSTION);
265                             }
266                         }
267                     }
268                     else
269                     {
270                         int containerPosition = dp.getPosition();
271                         panelView.getDockingContainer().removePanel(panelView.getComponent());
272                         if (lastPosition == POSITION_UP)
273                         {
274                             dp.getDockingContainer().insertPanel(containerPosition, panelView.getName(), panelView.getComponent(), DockingContainer.INSERT_MODE_NEWPOSITION, panelView.isClosable());
275                         }
276                         if (lastPosition == POSITION_BOTTOM)
277                         {
278                            dp.getDockingContainer().insertPanel(containerPosition+1, panelView.getName(), panelView.getComponent(), DockingContainer.INSERT_MODE_NEWPOSITION, panelView.isClosable());
279                         }
280                         if (lastPosition == POSITION_CENTER)
281                         {
282                            //if (containerPosition-1 >= 0) containerPosition = containerPosition-1;
283
dp.getDockingContainer().insertPanel(containerPosition, panelView.getName(), panelView.getComponent(), DockingContainer.INSERT_MODE_SHAREDPOSTION, panelView.isClosable());
284                         }
285                         
286                         
287                         
288                     }
289                 }
290                /* else
291                 {
292                     if (dtde.getDropTargetContext().getComponent() instanceof DockingContainer)
293                     {
294                         DockingContainer dockContainer = (DockingContainer)(dtde.getDropTargetContext().getComponent());
295                         panelView.getDockingContainer().removePanel(panelView.getComponent());
296                         dockContainer.insertPanel(0, panelView.getName(), panelView.getComponent());
297                     }
298                 }
299                 */

300             }
301             
302             context.dropComplete(true);
303         } catch (Exception JavaDoc ex)
304         {
305             ex.printStackTrace();
306         }
307     }
308
309     public int getLastPosition() {
310         return lastPosition;
311     }
312
313     public void setLastPosition(int lastPosition) {
314         this.lastPosition = lastPosition;
315     }
316
317   }
318
Popular Tags