KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JDraggableTabbedPane.java
28  *
29  * Created on January 26, 2006, 9:16 AM
30  *
31  */

32
33 package it.businesslogic.ireport.gui.docking;
34
35 import it.businesslogic.ireport.gui.MainFrame;
36 import java.awt.datatransfer.DataFlavor JavaDoc;
37 import java.awt.datatransfer.Transferable JavaDoc;
38 import java.awt.dnd.DnDConstants JavaDoc;
39 import java.awt.dnd.DragGestureEvent JavaDoc;
40 import java.awt.dnd.DragGestureListener JavaDoc;
41 import java.awt.dnd.DragSource JavaDoc;
42 import java.awt.dnd.DragSourceContext JavaDoc;
43 import java.awt.dnd.DragSourceDragEvent JavaDoc;
44 import java.awt.dnd.DragSourceDropEvent JavaDoc;
45 import java.awt.dnd.DragSourceEvent JavaDoc;
46 import java.awt.dnd.DragSourceListener JavaDoc;
47 import java.awt.dnd.DropTarget JavaDoc;
48 import java.awt.dnd.DropTargetContext JavaDoc;
49 import java.awt.dnd.DropTargetDragEvent JavaDoc;
50 import java.awt.dnd.DropTargetDropEvent JavaDoc;
51 import java.awt.dnd.DropTargetEvent JavaDoc;
52 import java.awt.dnd.DropTargetListener JavaDoc;
53 import java.awt.dnd.InvalidDnDOperationException JavaDoc;
54 import java.awt.event.MouseAdapter JavaDoc;
55 import javax.swing.JPopupMenu JavaDoc;
56 import javax.swing.JTabbedPane JavaDoc;
57 import it.businesslogic.ireport.util.LanguageChangedEvent;
58 import it.businesslogic.ireport.util.I18n;
59 import it.businesslogic.ireport.util.*;
60
61 /**
62  *
63  * @author gtoffoli
64  */

65 public class JDraggableTabbedPane extends ExtendedTabbedPane implements LanguageChangedListener {
66
67     private javax.swing.JMenuItem JavaDoc jMenuItemMerge;
68     private javax.swing.JMenuItem JavaDoc jMenuItemUnmerge;
69     private javax.swing.JPopupMenu JavaDoc jPopupMenu;
70
71
72
73     private DockingContainer dockingContainer = null;
74     private int position = 0;
75
76     /** Creates a new instance of JDraggableTabbedPane */
77     public JDraggableTabbedPane() {
78         super();
79         it.businesslogic.ireport.util.I18n.setCurrentLocale( System.getProperty("Language"), System.getProperty("Country") );
80         this.dragSource = DragSource.getDefaultDragSource();
81         this.dgListener = new DGListener();
82         this.dsListener = new DSListener((int)(1000*Math.random())) ;
83         this.dtListener = new GenericDragTargetListener();
84
85         // component, ops, listener, accepting
86
this.dropTarget = new DropTarget JavaDoc(this,
87             this.acceptableActions,
88             this.dtListener,
89         true);
90
91         // component, action, listener
92
this.dragSource.createDefaultDragGestureRecognizer(
93         this,
94         this.dragAction,
95         this.dgListener);
96
97         jPopupMenu = new JPopupMenu JavaDoc();
98
99         jMenuItemMerge = new javax.swing.JMenuItem JavaDoc();
100         jMenuItemMerge.setText("Merge panels");
101         jMenuItemMerge.setEnabled(true);
102         jMenuItemMerge.addActionListener(new java.awt.event.ActionListener JavaDoc() {
103             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
104                 jMenuItemMergeActionPerformed(evt);
105             }
106         });
107
108         jPopupMenu.add(jMenuItemMerge);
109
110         jMenuItemUnmerge = new javax.swing.JMenuItem JavaDoc();
111         jMenuItemUnmerge.setText("Separate panel");
112         jMenuItemUnmerge.setEnabled(true);
113         jMenuItemUnmerge.addActionListener(new java.awt.event.ActionListener JavaDoc() {
114             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
115                 jMenuItemUnmergeActionPerformed(evt);
116             }
117         });
118
119         jPopupMenu.add(jMenuItemUnmerge);
120
121         this.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
122             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
123                 onMouseClicked(evt);
124             }
125         });
126
127         this.setOpaque(true);
128
129         I18n.addOnLanguageChangedListener( this );
130
131     }
132
133     private void jMenuItemUnmergeActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
134
135         PanelView pv = new PanelView( JDraggableTabbedPane.this.getTitleAt( JDraggableTabbedPane.this.getSelectedIndex() ),
136                                       JDraggableTabbedPane.this.getSelectedComponent(),
137                                       JDraggableTabbedPane.this.getPosition(),
138                                       JDraggableTabbedPane.this.isClosable( JDraggableTabbedPane.this.getSelectedIndex() )
139                                       );
140         getDockingContainer().moveComponent(pv, pv.getPosition(), DockingContainer.INSERT_MODE_NEWPOSITION);
141     }
142
143
144
145     private void jMenuItemMergeActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
146         getDockingContainer().mergePosition( this.getPosition() );
147     }
148
149     public void onMouseClicked(java.awt.event.MouseEvent JavaDoc evt)
150     {
151         if (evt.getButton() == evt.BUTTON3 && evt.getClickCount() == 1) {
152             this.jPopupMenu.show(this, evt.getPoint().x, evt.getPoint().y);
153         }
154     }
155
156    /**
157    * DGListener
158    * a listener that will start the drag.
159    * has access to top level's dsListener and dragSource
160    * @see java.awt.dnd.DragGestureListener
161    * @see java.awt.dnd.DragSource
162    * @see java.awt.datatransfer.StringSelection
163    */

164   class DGListener implements DragGestureListener JavaDoc {
165     /**
166      * Start the drag if the operation is ok.
167      * uses java.awt.datatransfer.StringSelection to transfer
168      * the label's data
169      * @param e the event object
170      */

171     public void dragGestureRecognized(DragGestureEvent JavaDoc e) {
172
173       // if the action is ok we go ahead
174
// otherwise we punt
175
if((e.getDragAction() & JDraggableTabbedPane.this.dragAction) == 0)
176     return;
177
178       // get the label's text and put it inside a Transferable
179
// Transferable transferable = new StringSelection( DragLabel.this.getText() );
180
if (JDraggableTabbedPane.this.getSelectedIndex() < 0) return;
181
182       PanelView pv = new PanelView( JDraggableTabbedPane.this.getTitleAt( JDraggableTabbedPane.this.getSelectedIndex() ),
183                                     JDraggableTabbedPane.this.getSelectedComponent(),
184                                     JDraggableTabbedPane.this.getPosition(),
185                                     JDraggableTabbedPane.this.isClosable( JDraggableTabbedPane.this.getSelectedIndex() ));
186       pv.setDockingContainer( JDraggableTabbedPane.this.getDockingContainer());
187       Transferable JavaDoc transferable = new PanelTransferable( pv);
188
189       // now kick off the drag
190
try {
191     // initial cursor, transferrable, dsource listener
192
e.startDrag(DragSource.DefaultCopyNoDrop,
193       transferable,
194       JDraggableTabbedPane.this.dsListener);
195
196     // or if dragSource is a variable
197
// dragSource.startDrag(e, DragSource.DefaultCopyDrop, transferable, dsListener);
198

199
200     // or if you'd like to use a drag image if supported
201

202     /*
203       if(DragSource.isDragImageSupported() )
204       // cursor, image, point, transferrable, dsource listener
205       e.startDrag(DragSource.DefaultCopyDrop, image, point, transferable, dsListener);
206     */

207
208       }catch( InvalidDnDOperationException JavaDoc idoe ) {
209       }
210     }
211   }
212
213   /**
214    * DSListener
215    * a listener that will track the state of the DnD operation
216    *
217    * @see java.awt.dnd.DragSourceListener
218    * @see java.awt.dnd.DragSource
219    * @see java.awt.datatransfer.StringSelection
220    */

221   class DSListener implements DragSourceListener JavaDoc {
222
223       public int myId = 0;
224      public DSListener(int id)
225      {
226          super();
227          myId = id;
228      }
229
230     /**
231      * @param e the event
232      */

233     public void dragDropEnd(DragSourceDropEvent JavaDoc e) {
234
235         if (GenericDragTargetListener.lastDp != null)
236         {
237             GenericDragTargetListener.lastDp.repaint();
238             GenericDragTargetListener.lastDp = null;
239         }
240     }
241
242     /**
243      * @param e the event
244      */

245     public void dragEnter(DragSourceDragEvent JavaDoc e) {
246       DragSourceContext JavaDoc context = e.getDragSourceContext();
247       //intersection of the users selected action, and the source and target actions
248
int myaction = e.getDropAction();
249       if( (myaction & JDraggableTabbedPane.this.dragAction) != 0) {
250     context.setCursor(DragSource.DefaultCopyDrop);
251       } else {
252     context.setCursor(DragSource.DefaultCopyNoDrop);
253       }
254     }
255     /**
256      * @param e the event
257      */

258     public void dragOver(DragSourceDragEvent JavaDoc e) {
259       DragSourceContext JavaDoc context = e.getDragSourceContext();
260       int sa = context.getSourceActions();
261       int ua = e.getUserAction();
262       int da = e.getDropAction();
263       int ta = e.getTargetActions();
264
265     }
266     /**
267      * @param e the event
268      */

269     public void dragExit(DragSourceEvent JavaDoc e) {
270     }
271
272     /**
273      * for example, press shift during drag to change to
274      * a link action
275      * @param e the event
276      */

277     public void dropActionChanged (DragSourceDragEvent JavaDoc e) {
278       DragSourceContext JavaDoc context = e.getDragSourceContext();
279       context.setCursor(DragSource.DefaultCopyNoDrop);
280     }
281   }
282
283
284
285
286   private DragSource JavaDoc dragSource;
287   private DragGestureListener JavaDoc dgListener;
288   private DragSourceListener JavaDoc dsListener;
289   private DropTarget JavaDoc dropTarget;
290   private DropTargetListener JavaDoc dtListener;
291   private int dragAction = DnDConstants.ACTION_MOVE;
292   private int acceptableActions = DnDConstants.ACTION_MOVE;
293
294     public DockingContainer getDockingContainer() {
295         return dockingContainer;
296     }
297
298     public void setDockingContainer(DockingContainer dockingContainer) {
299         this.dockingContainer = dockingContainer;
300     }
301
302     public int getPosition() {
303         return position;
304     }
305
306     public void setPosition(int position) {
307         this.position = position;
308     }
309     //added by Felix Firgau on April 24th 2006
310
public void applyI18n(){
311                 // Start autogenerated code ----------------------
312
// End autogenerated code ----------------------
313
jMenuItemMerge.setText(it.businesslogic.ireport.util.I18n.getString("mergePanels","Merge panels"));
314           jMenuItemUnmerge.setText(it.businesslogic.ireport.util.I18n.getString("unmergePanel","Seperate panel"));
315         }
316
317         public void languageChanged(LanguageChangedEvent evt) {
318           applyI18n();
319         }
320     //Modification end
321
}
322
Popular Tags