Source for org.jfree.report.modules.gui.swing.common.AbstractExportActionPlugin

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: AbstractExportActionPlugin.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.modules.gui.swing.common;
  33: 
  34: import java.awt.Dialog;
  35: import java.awt.Frame;
  36: import java.awt.Window;
  37: import java.lang.reflect.Constructor;
  38: 
  39: import org.jfree.report.flow.ReportJob;
  40: import org.jfree.util.Configuration;
  41: import org.jfree.util.Log;
  42: import org.jfree.util.ObjectUtilities;
  43: 
  44: /**
  45:  * Creation-Date: 02.12.2006, 14:21:07
  46:  *
  47:  * @author Thomas Morgner
  48:  */
  49: public abstract class AbstractExportActionPlugin extends AbstractActionPlugin
  50:   implements ExportActionPlugin
  51: {
  52: 
  53:   public AbstractExportActionPlugin()
  54:   {
  55:   }
  56: 
  57: 
  58:   /**
  59:    * Creates a progress dialog, and tries to assign a parent based on the given
  60:    * preview proxy.
  61:    *
  62:    * @return the progress dialog.
  63:    */
  64:   protected ExportDialog createExportDialog(final String className)
  65:       throws InstantiationException
  66:   {
  67:     final Window proxy = getContext().getWindow();
  68:     if (proxy instanceof Frame)
  69:     {
  70:       final ClassLoader classLoader =
  71:           ObjectUtilities.getClassLoader(AbstractActionPlugin.class);
  72:       try
  73:       {
  74:         final Class aClass = classLoader.loadClass(className);
  75:         final Constructor constructor =
  76:             aClass.getConstructor(new Class[]{Frame.class});
  77:         return (ExportDialog) constructor.newInstance(new Object[]{proxy});
  78:       }
  79:       catch (Exception e)
  80:       {
  81:         Log.error("Failed to instantiate Export-Dialog with a 'Frame'-parent: " + className);
  82:       }
  83:     }
  84:     else if (proxy instanceof Dialog)
  85:     {
  86:       final ClassLoader classLoader =
  87:           ObjectUtilities.getClassLoader(AbstractActionPlugin.class);
  88:       try
  89:       {
  90:         final Class aClass = classLoader.loadClass(className);
  91:         final Constructor constructor =
  92:             aClass.getConstructor(new Class[]{Dialog.class});
  93:         return (ExportDialog) constructor.newInstance(new Object[]{proxy});
  94:       }
  95:       catch (Exception e)
  96:       {
  97:         Log.error("Failed to instantiate Export-Dialog with a 'Dialog'-parent: " + className, e);
  98:       }
  99:     }
 100: 
 101:     final Object fallBack = ObjectUtilities.loadAndInstantiate
 102:         (className, AbstractActionPlugin.class, ExportDialog.class);
 103:     if (fallBack != null)
 104:     {
 105:       return (ExportDialog) fallBack;
 106:     }
 107: 
 108:     Log.error ("Failed to instantiate Export-Dialog without a parent: " + className);
 109:     throw new InstantiationException("Failed to instantiate Export-Dialog");
 110:   }
 111: 
 112: 
 113:   /**
 114:    * Exports a report.
 115:    *
 116:    * @param report the report.
 117:    * @return A boolean.
 118:    */
 119:   public boolean performShowExportDialog(final ReportJob job, final String configKey)
 120:   {
 121:     try
 122:     {
 123:       final Configuration configuration = job.getConfiguration();
 124:       final String dialogClassName = configuration.getConfigProperty(configKey);
 125:       final ExportDialog dialog = createExportDialog(dialogClassName);
 126:       return dialog.performQueryForExport(job, getContext());
 127:     }
 128:     catch (InstantiationException e)
 129:     {
 130:       Log.error ("Unable to configure the report job.");
 131:       setStatusText("Unable to configure the report job.");
 132:       return false;
 133:     }
 134:   }
 135: 
 136: }