KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > JReportPanel


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  * JReportPanel.java
28  *
29  * Created on 13 febbraio 2003, 23.35
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34
35 import it.businesslogic.ireport.ReportElement;
36 import it.businesslogic.ireport.Style;
37 import it.businesslogic.ireport.gui.event.ReportElementChangedEvent;
38 import java.awt.*;
39 import java.awt.dnd.*;
40 import javax.swing.*;
41 import java.awt.datatransfer.*;
42
43 /**
44  *
45  * @author Administrator
46  */

47 public class JReportPanel extends JPanel implements java.awt.dnd.DropTargetListener JavaDoc {
48     
49     private JReportFrame jrf = null;
50     /** Creates a new instance of JReportPanel */
51     public JReportPanel() {
52         this.setBackground(new Color(128,128,128));
53         new DropTarget( this, // component
54
DnDConstants.ACTION_COPY_OR_MOVE, // actions
55
this); // DropTargetListener
56
this.setIgnoreRepaint(true);
57     }
58     
59     /** Paints this component.
60      * <p>
61      * This method is called when the contents of the component should
62      * be painted in response to the component first being shown or
63      * damage needing repair. The clip rectangle in the
64      * <code>Graphics</code> parameter will be set to the area
65      * which needs to be painted.
66      * Subclasses of Component that override this method need not call
67      * super.paint(g).
68      * <p>
69      * For performance reasons, Components with zero width or height
70      * aren't considered to need painting when they are first shown,
71      * and also aren't considered to need repair.
72      *
73      * @param g the graphics context to use for painting
74      * @see #update
75      * @since JDK1.0
76      *
77      */

78     public void paint(Graphics g){
79         
80         try {
81             
82             if (jrf != null){
83              jrf.paintReportPanel(g);
84             } else {
85                 super.paint(g);
86             }
87         
88         } catch (Throwable JavaDoc ex)
89         {
90             ex.printStackTrace();
91         }
92    
93     }
94     
95     /** Getter for property jrf.
96      * @return Value of property jrf.
97      *
98      */

99     public it.businesslogic.ireport.gui.JReportFrame getJrf() {
100         return jrf;
101     }
102     
103     /** Setter for property jrf.
104      * @param jrf New value of property jrf.
105      *
106      */

107     public void setJrf(it.businesslogic.ireport.gui.JReportFrame jrf) {
108         this.jrf = jrf;
109         if (rptt != null)
110         {
111             rptt.setReportFrame(jrf);
112         }
113     }
114     
115     public java.awt.Point JavaDoc getToolTipLocation(java.awt.event.MouseEvent JavaDoc event)
116     {
117         return new Point( event.getX() + 30, event.getY() + 30);
118     }
119         
120     public void dragEnter(java.awt.dnd.DropTargetDragEvent JavaDoc dtde) {
121         dtde.acceptDrag(dtde.getDropAction());
122     }
123     
124     public void dragExit(java.awt.dnd.DropTargetEvent JavaDoc dte) {
125     }
126     
127     public void dragOver(java.awt.dnd.DropTargetDragEvent JavaDoc dtde) {
128         dtde.acceptDrag(dtde.getDropAction());
129     }
130     
131     public void drop(java.awt.dnd.DropTargetDropEvent JavaDoc dtde) {
132        try {
133            
134         DropTargetContext context = dtde.getDropTargetContext();
135
136         Transferable tr = dtde.getTransferable();
137         
138         DataFlavor[] df = tr.getTransferDataFlavors();
139         
140         
141         if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.JRField"))
142         {
143             java.awt.datatransfer.DataFlavor JavaDoc myFlavor = new java.awt.datatransfer.DataFlavor JavaDoc(it.businesslogic.ireport.JRField.class, it.businesslogic.ireport.JRField.class.getName());
144             it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField)tr.getTransferData( myFlavor );
145             // Add a field!!!
146
//System.out.println("Field name:" + field.getName() +" from "+ field.getClassType() );
147

148             // Create a standard field...
149
this.jrf.dropNewTextField( dtde.getLocation(), "$F{"+ field.getName() +"}", field.getClassType() );
150         }
151         else if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.JRVariable"))
152         {
153             java.awt.datatransfer.DataFlavor JavaDoc myFlavor = new java.awt.datatransfer.DataFlavor JavaDoc(it.businesslogic.ireport.JRVariable.class, it.businesslogic.ireport.JRVariable.class.getName());
154             it.businesslogic.ireport.JRVariable var = (it.businesslogic.ireport.JRVariable)tr.getTransferData( myFlavor );
155             // Add a field!!!
156
//System.out.println("Field name:" + field.getName() +" from "+ field.getClassType() );
157

158             // Create a standard field...
159
this.jrf.dropNewTextField( dtde.getLocation(), "$V{"+ var.getName() +"}", var.getClassType() );
160         }
161         else if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.JRParameter"))
162         {
163             java.awt.datatransfer.DataFlavor JavaDoc myFlavor = new java.awt.datatransfer.DataFlavor JavaDoc(it.businesslogic.ireport.JRParameter.class, it.businesslogic.ireport.JRParameter.class.getName());
164             it.businesslogic.ireport.JRParameter var = (it.businesslogic.ireport.JRParameter)tr.getTransferData( myFlavor );
165             // Add a field!!!
166
//System.out.println("Field name:" + field.getName() +" from "+ field.getClassType() );
167

168             // Create a standard field...
169
this.jrf.dropNewTextField( dtde.getLocation(), "$P{"+ var.getName() +"}", var.getClassType() );
170         }
171         else if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor))
172         {
173             dtde.acceptDrop ( DnDConstants.ACTION_COPY_OR_MOVE);
174             java.util.List JavaDoc fileList = (java.util.List JavaDoc)tr.getTransferData(DataFlavor.javaFileListFlavor);
175             
176             MainFrame.getMainInstance().openFiles(fileList);
177         }
178         else if (df[0].getHumanPresentableName().equals("it.businesslogic.ireport.Style"))
179         {
180             java.awt.datatransfer.DataFlavor JavaDoc myFlavor = new java.awt.datatransfer.DataFlavor JavaDoc(it.businesslogic.ireport.Style.class, it.businesslogic.ireport.JRParameter.class.getName());
181             it.businesslogic.ireport.Style var = (it.businesslogic.ireport.Style)tr.getTransferData( myFlavor );
182             // Add a field!!!
183

184             
185             ReportElement re = this.jrf.getElementAt(dtde.getLocation());
186             if (re != null)
187             {
188                 
189                 if (!jrf.getReport().getStyles().contains( var ))
190                 {
191                     boolean found = false;
192                     // Look for a style with the same name...
193
for (int i=0; i<jrf.getReport().getStyles().size(); ++i)
194                     {
195                         Style s = (Style)jrf.getReport().getStyles().elementAt(i);
196                         if (s.getName() != null && s.getName().equals( var.getName()))
197                         {
198                             var = s;
199                             found = true;
200                             break;
201                         }
202                     }
203                     if (!found)
204                     {
205                         var = new Style(var);
206                         jrf.getReport().addStyle( var );
207                     }
208                 }
209                 
210                 re.setStyle( var );
211                 jrf.fireReportListenerReportElementsChanged(new ReportElementChangedEvent(jrf,re, ReportElementChangedEvent.CHANGED));
212                 this.repaint();
213             }
214         }
215         else
216         {
217             Class JavaDoc c = Class.forName( df[0].getHumanPresentableName() );
218             if (it.businesslogic.ireport.gui.library.AbstractLibraryObject.class.isAssignableFrom( c ))
219             {
220                 java.awt.datatransfer.DataFlavor JavaDoc myFlavor = new java.awt.datatransfer.DataFlavor JavaDoc(c, df[0].getHumanPresentableName());
221                Object JavaDoc obj = tr.getTransferData( myFlavor );
222                 ((it.businesslogic.ireport.gui.library.AbstractLibraryObject )obj).drop(dtde);
223             }
224             else // if (.equals("it.businesslogic.ireport.JRParameter"))
225
{
226                 System.out.println("Dropped a "+df[0].getHumanPresentableName());
227             }
228         }
229         /*
230         else // if (.equals("it.businesslogic.ireport.JRParameter"))
231         {
232             System.out.println("Dropped a "+df[0].getHumanPresentableName());
233         }
234         */

235         context.dropComplete(true);
236         } catch (Exception JavaDoc ex)
237         {
238             System.out.println("Error in drop!");
239             ex.printStackTrace();
240         }
241     }
242     
243     public void dropActionChanged(java.awt.dnd.DropTargetDragEvent JavaDoc dtde) {
244     }
245   
246     private ReportPanelToolTip rptt = null;
247     public javax.swing.JToolTip JavaDoc createToolTip()
248     {
249         if (rptt == null)
250         {
251             rptt = new ReportPanelToolTip(this.getJrf());
252         }
253         return rptt;
254     }
255 }
256
Popular Tags