001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JReportPanel.java
028: *
029: * Created on 13 febbraio 2003, 23.35
030: *
031: */
032:
033: package it.businesslogic.ireport.gui;
034:
035: import it.businesslogic.ireport.JasperTemplate;
036: import it.businesslogic.ireport.ReportElement;
037: import it.businesslogic.ireport.Style;
038: import it.businesslogic.ireport.Template;
039: import it.businesslogic.ireport.UndefinedStyle;
040: import it.businesslogic.ireport.gui.event.ReportElementChangedEvent;
041: import it.businesslogic.ireport.util.Misc;
042: import java.awt.*;
043: import java.awt.dnd.*;
044: import java.awt.event.ActionEvent;
045: import javax.swing.*;
046: import java.awt.datatransfer.*;
047: import java.util.Vector;
048:
049: /**
050: *
051: * @author Administrator
052: */
053: public class JReportPanel extends JPanel implements
054: java.awt.dnd.DropTargetListener {
055:
056: private JReportFrame jrf = null;
057:
058: /** Creates a new instance of JReportPanel */
059: public JReportPanel() {
060: this .setBackground(new Color(128, 128, 128));
061: new DropTarget(this , // component
062: DnDConstants.ACTION_COPY_OR_MOVE, // actions
063: this ); // DropTargetListener
064: this .setIgnoreRepaint(true);
065:
066: Action openPropertiesDialog = new AbstractAction() {
067: public void actionPerformed(ActionEvent e) {
068:
069: MainFrame.getMainInstance()
070: .getElementPropertiesDialog().setVisible(
071: !MainFrame.getMainInstance()
072: .getElementPropertiesDialog()
073: .isVisible());
074: }
075: };
076:
077: getInputMap().put(
078: KeyStroke.getKeyStroke(
079: java.awt.event.KeyEvent.VK_CONTEXT_MENU, 0),
080: "openPropertiesDialog");
081: getActionMap()
082: .put("openPropertiesDialog", openPropertiesDialog);
083:
084: Action selectNextReportElement = new AbstractAction() {
085: public void actionPerformed(ActionEvent e) {
086: if (getJrf() == null)
087: return;
088: Vector reportElements = getJrf().getReport()
089: .getElements();
090: if (reportElements.size() == 0)
091: return;
092:
093: Vector selectedElements = getJrf()
094: .getSelectedElements();
095: if (selectedElements.size() == 0) {
096: getJrf()
097: .setSelectedElement(
098: (ReportElement) reportElements
099: .elementAt(0), true);
100: } else {
101: if (selectedElements.size() == 1) {
102: if (reportElements.size() > 0) {
103: ReportElement selectedElem = (ReportElement) selectedElements
104: .elementAt(0);
105: for (int i = 0; i < reportElements.size(); ++i) {
106: ReportElement re = (ReportElement) reportElements
107: .elementAt(i);
108: if (re == selectedElem) {
109: // get the next...
110: i = (i + 1) % reportElements.size();
111: getJrf()
112: .setSelectedElement(
113: (ReportElement) reportElements
114: .elementAt(i),
115: true);
116: return;
117: }
118: }
119: }
120: }
121: }
122:
123: }
124: };
125:
126: getInputMap().put(
127: KeyStroke.getKeyStroke(
128: java.awt.event.KeyEvent.VK_NUMPAD6, 0),
129: "selectNextReportElement");
130: getInputMap().put(
131: KeyStroke.getKeyStroke(
132: java.awt.event.KeyEvent.VK_PERIOD, 0),
133: "selectNextReportElement");
134: getActionMap().put("selectNextReportElement",
135: selectNextReportElement);
136:
137: Action selectPrevReportElement = new AbstractAction() {
138: public void actionPerformed(ActionEvent e) {
139: if (getJrf() == null)
140: return;
141: Vector reportElements = getJrf().getReport()
142: .getElements();
143: if (reportElements.size() == 0)
144: return;
145:
146: Vector selectedElements = getJrf()
147: .getSelectedElements();
148: if (selectedElements.size() == 0) {
149: getJrf()
150: .setSelectedElement(
151: (ReportElement) reportElements
152: .elementAt(0), true);
153: } else {
154: if (selectedElements.size() == 1) {
155: if (reportElements.size() > 0) {
156: ReportElement selectedElem = (ReportElement) selectedElements
157: .elementAt(0);
158: for (int i = 0; i < reportElements.size(); ++i) {
159: ReportElement re = (ReportElement) reportElements
160: .elementAt(i);
161: if (re == selectedElem) {
162: // get the next...
163: i = (i + reportElements.size() - 1)
164: % reportElements.size();
165: getJrf()
166: .setSelectedElement(
167: (ReportElement) reportElements
168: .elementAt(i),
169: true);
170: return;
171: }
172: }
173: }
174: }
175: }
176:
177: }
178: };
179:
180: getInputMap().put(
181: KeyStroke.getKeyStroke(
182: java.awt.event.KeyEvent.VK_NUMPAD4, 0),
183: "selectPrevReportElement");
184: getInputMap().put(
185: KeyStroke.getKeyStroke(
186: java.awt.event.KeyEvent.VK_COMMA, 0),
187: "selectPrevReportElement");
188: getActionMap().put("selectPrevReportElement",
189: selectPrevReportElement);
190:
191: }
192:
193: /** Paints this component.
194: * <p>
195: * This method is called when the contents of the component should
196: * be painted in response to the component first being shown or
197: * damage needing repair. The clip rectangle in the
198: * <code>Graphics</code> parameter will be set to the area
199: * which needs to be painted.
200: * Subclasses of Component that override this method need not call
201: * super.paint(g).
202: * <p>
203: * For performance reasons, Components with zero width or height
204: * aren't considered to need painting when they are first shown,
205: * and also aren't considered to need repair.
206: *
207: * @param g the graphics context to use for painting
208: * @see #update
209: * @since JDK1.0
210: *
211: */
212: /*
213: public void paint(Graphics g){
214:
215:
216:
217: }
218: */
219:
220: public int ccc = 0;
221:
222: protected void paintComponent(Graphics g) {
223: try {
224: ccc++;
225: if (jrf != null) {
226:
227: jrf.paintReportPanel(g);
228: } else {
229: super .paint(g);
230: }
231:
232: } catch (Throwable ex) {
233: ex.printStackTrace();
234: }
235: }
236:
237: /** Getter for property jrf.
238: * @return Value of property jrf.
239: *
240: */
241: public it.businesslogic.ireport.gui.JReportFrame getJrf() {
242: return jrf;
243: }
244:
245: /** Setter for property jrf.
246: * @param jrf New value of property jrf.
247: *
248: */
249: public void setJrf(it.businesslogic.ireport.gui.JReportFrame jrf) {
250: this .jrf = jrf;
251: if (rptt != null) {
252: rptt.setReportFrame(jrf);
253: }
254: }
255:
256: public java.awt.Point getToolTipLocation(
257: java.awt.event.MouseEvent event) {
258: return new Point(event.getX() + 30, event.getY() + 30);
259: }
260:
261: public void dragEnter(java.awt.dnd.DropTargetDragEvent dtde) {
262: dtde.acceptDrag(dtde.getDropAction());
263:
264: }
265:
266: public void dragExit(java.awt.dnd.DropTargetEvent dte) {
267: }
268:
269: public void dragOver(java.awt.dnd.DropTargetDragEvent dtde) {
270:
271: if (!jrf.isInsideDocument(dtde.getLocation())) {
272: dtde.rejectDrag();
273: } else {
274: dtde.acceptDrag(dtde.getDropAction());
275: }
276: }
277:
278: public void drop(java.awt.dnd.DropTargetDropEvent dtde) {
279: try {
280:
281: DropTargetContext context = dtde.getDropTargetContext();
282:
283: Transferable tr = dtde.getTransferable();
284:
285: DataFlavor[] df = tr.getTransferDataFlavors();
286:
287: if (df[0].getHumanPresentableName().equals(
288: "it.businesslogic.ireport.JRField")) {
289: java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor(
290: it.businesslogic.ireport.JRField.class,
291: it.businesslogic.ireport.JRField.class
292: .getName());
293: it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField) tr
294: .getTransferData(myFlavor);
295: // Add a field!!!
296: //System.out.println("Field name:" + field.getName() +" from "+ field.getClassType() );
297:
298: // Create a standard field...
299: this .jrf.dropNewTextField(dtde.getLocation(), "$F{"
300: + field.getName() + "}", field.getClassType());
301: } else if (df[0].getHumanPresentableName().equals(
302: "it.businesslogic.ireport.JRVariable")) {
303: java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor(
304: it.businesslogic.ireport.JRVariable.class,
305: it.businesslogic.ireport.JRVariable.class
306: .getName());
307: it.businesslogic.ireport.JRVariable var = (it.businesslogic.ireport.JRVariable) tr
308: .getTransferData(myFlavor);
309: // Add a field!!!
310: //System.out.println("Field name:" + field.getName() +" from "+ field.getClassType() );
311:
312: // Create a standard field...
313: this .jrf.dropNewTextField(dtde.getLocation(), "$V{"
314: + var.getName() + "}", var.getClassType());
315: } else if (df[0].getHumanPresentableName().equals(
316: "it.businesslogic.ireport.JRParameter")) {
317: java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor(
318: it.businesslogic.ireport.JRParameter.class,
319: it.businesslogic.ireport.JRParameter.class
320: .getName());
321: it.businesslogic.ireport.JRParameter var = (it.businesslogic.ireport.JRParameter) tr
322: .getTransferData(myFlavor);
323: // Add a field!!!
324: //System.out.println("Field name:" + field.getName() +" from "+ field.getClassType() );
325:
326: // Create a standard field...
327: this .jrf.dropNewTextField(dtde.getLocation(), "$P{"
328: + var.getName() + "}", var.getClassType());
329: } else if (tr
330: .isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
331: dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
332: java.util.List fileList = (java.util.List) tr
333: .getTransferData(DataFlavor.javaFileListFlavor);
334:
335: MainFrame.getMainInstance().openFiles(fileList);
336: } else if (df[0].getHumanPresentableName().equals(
337: "it.businesslogic.ireport.Style")
338: || df[0].getHumanPresentableName().equals(
339: "it.businesslogic.ireport.UndefinedStyle")) {
340: Class clazz = Class.forName(df[0]
341: .getHumanPresentableName());
342: java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor(
343: clazz, clazz.getName());
344: it.businesslogic.ireport.Style var = (it.businesslogic.ireport.Style) tr
345: .getTransferData(myFlavor);
346:
347: ReportElement re = this .jrf.getElementAt(dtde
348: .getLocation());
349: if (re != null) {
350:
351: if (!jrf.getReport().getStyles().contains(var)) {
352: boolean found = false;
353: // Look for a style with the same name...
354: for (int i = 0; i < jrf.getReport().getStyles()
355: .size(); ++i) {
356: Style s = (Style) jrf.getReport()
357: .getStyles().elementAt(i);
358: if (s.getName() != null
359: && s.getName()
360: .equals(var.getName())) {
361: var = s;
362: found = true;
363: break;
364: }
365: }
366: if (!found) {
367: var = new Style(var);
368: jrf.getReport().addStyle(var);
369: }
370: }
371:
372: re.setStyle(var);
373: jrf
374: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
375: jrf, re,
376: ReportElementChangedEvent.CHANGED));
377: this .repaint();
378: }
379: } else if (df[0].getHumanPresentableName().equals(
380: "it.businesslogic.ireport.TemplateStyle")) {
381: Class clazz = Class.forName(df[0]
382: .getHumanPresentableName());
383: java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor(
384: clazz, clazz.getName());
385: it.businesslogic.ireport.TemplateStyle var = (it.businesslogic.ireport.TemplateStyle) tr
386: .getTransferData(myFlavor);
387: ReportElement re = this .jrf.getElementAt(dtde
388: .getLocation());
389: if (re != null) {
390: Style us = new UndefinedStyle(var);
391: boolean found = false;
392:
393: if (!jrf.getReport().getStyles().contains(var)) {
394:
395: // Look for a style with the same name...
396: for (int i = 0; i < jrf.getReport().getStyles()
397: .size(); ++i) {
398: Style s = (Style) jrf.getReport()
399: .getStyles().elementAt(i);
400: if (s.getName() != null
401: && s.getName()
402: .equals(var.getName())) {
403: us = s;
404: found = true;
405: break;
406: }
407: }
408: }
409:
410: JasperTemplate jt = ((it.businesslogic.ireport.TemplateStyle) var)
411: .getJasperTemplate();
412: boolean templateFound = false;
413: if (jt.getFilename() != null) {
414: String exp = "\"" + jt.getFilename() + "\"";
415: exp = Misc.string_replace("\\\\", "\\", exp);
416:
417: for (int i = 0; i < jrf.getReport()
418: .getTemplates().size(); ++i) {
419: Template t = (Template) jrf.getReport()
420: .getTemplates().elementAt(i);
421: if (t.getExpression() != null
422: && t.getExpression().equals(exp)) {
423: templateFound = true;
424: break;
425: }
426: }
427: if (!templateFound) {
428: jrf.getReport().addTemplate(
429: new Template(exp,
430: "java.lang.String"));
431: }
432: }
433:
434: if (!found) {
435: jrf.getReport().addStyle(us);
436: }
437:
438: re.setStyle(us);
439:
440: jrf
441: .fireReportListenerReportElementsChanged(new ReportElementChangedEvent(
442: jrf, re,
443: ReportElementChangedEvent.CHANGED));
444: this .repaint();
445: }
446: } else {
447: try {
448: Class c = Class.forName(df[0]
449: .getHumanPresentableName());
450: if (it.businesslogic.ireport.gui.library.AbstractLibraryObject.class
451: .isAssignableFrom(c)) {
452: java.awt.datatransfer.DataFlavor myFlavor = new java.awt.datatransfer.DataFlavor(
453: c, df[0].getHumanPresentableName());
454: Object obj = tr.getTransferData(myFlavor);
455: ((it.businesslogic.ireport.gui.library.AbstractLibraryObject) obj)
456: .drop(dtde);
457: } else // if (.equals("it.businesslogic.ireport.JRParameter"))
458: {
459: System.out.println("Dropped a "
460: + df[0].getHumanPresentableName());
461: }
462: } catch (Exception ex) {
463: System.out.println("Not valid!!");
464: }
465: }
466: /*
467: else // if (.equals("it.businesslogic.ireport.JRParameter"))
468: {
469: System.out.println("Dropped a "+df[0].getHumanPresentableName());
470: }
471: */
472: context.dropComplete(true);
473: } catch (Exception ex) {
474: System.out.println("Error in drop!");
475: ex.printStackTrace();
476: }
477:
478: MainFrame.getMainInstance().getGlassPane().setVisible(false);
479: jrf.setRedrawWithBufferedImage(false);
480: jrf.repaint();
481: }
482:
483: public void dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde) {
484:
485: }
486:
487: private ReportPanelToolTip rptt = null;
488:
489: public javax.swing.JToolTip createToolTip() {
490: if (rptt == null) {
491: rptt = new ReportPanelToolTip(this.getJrf());
492: }
493: return rptt;
494: }
495: }
|