1 32 33 package it.businesslogic.ireport.connection; 34 35 36 import it.businesslogic.ireport.util.I18n; 37 import java.lang.reflect.InvocationTargetException ; 38 import net.sf.jasperreports.engine.*; 39 import javax.swing.*; 40 import it.businesslogic.ireport.gui.MainFrame; 41 45 public class JRDataSourceProviderConnection extends it.businesslogic.ireport.IReportConnection { 46 47 48 private net.sf.jasperreports.engine.JRDataSourceProvider dsp; 49 private net.sf.jasperreports.engine.JRDataSource ds; 50 private java.util.HashMap properties = new java.util.HashMap (); 51 52 public net.sf.jasperreports.engine.JRDataSourceProvider getDataSourceProvider() { 53 54 if (dsp == null && this.getProperties().get("JRDataSourceProvider") != null) 55 { 56 try { 57 dsp = (JRDataSourceProvider)(Class.forName( (String )this.getProperties().get("JRDataSourceProvider"), true, it.businesslogic.ireport.gui.MainFrame.getMainInstance().getReportClassLoader()).newInstance()); 58 } catch (NoClassDefFoundError ex) 59 { 60 showErrorMessage( 61 I18n.getString("messages.JRDataSourceProviderConnection.noClassDefFoundError", 62 "No class definition found error!!\nCheck your classpath!"), 63 I18n.getString("message.title.exception","Exception")); 64 } 65 catch (ClassNotFoundException ex) 66 { 67 showErrorMessage( 68 I18n.getString("messages.JRDataSourceProviderConnection.classNotFoundError", 69 "Class not found error!!\nCheck your classpath!"), 70 I18n.getString("message.title.exception","Exception")); 71 } 72 catch (Exception ex) 73 { 74 showErrorMessage("" + ex.getMessage(), 75 I18n.getString("message.title.exception","Exception")); 76 } 77 } 78 79 return dsp; 80 } 81 82 private void showErrorMessage(final String errorMsg, final String title) 83 { 84 Runnable r = new Runnable () { 85 public void run() { 86 JOptionPane.showMessageDialog(MainFrame.getMainInstance(),errorMsg,title,JOptionPane.ERROR_MESSAGE); 87 } 88 }; 89 90 if (!SwingUtilities.isEventDispatchThread()) 91 { 92 try { 93 SwingUtilities.invokeAndWait( r ); 94 } catch (InvocationTargetException ex) { 95 ex.printStackTrace(); 96 } catch (InterruptedException ex) { 97 ex.printStackTrace(); 98 } 99 } 100 else 101 { 102 r.run(); 103 } 104 } 105 106 public String getDescription(){ return "JasperReports DataSource Provider"; } 107 108 109 public JRDataSourceProviderConnection() { 110 } 111 112 116 public net.sf.jasperreports.engine.JRDataSource getJRDataSource() { 117 118 return getJRDataSource(null); 119 } 120 121 public net.sf.jasperreports.engine.JRDataSource getJRDataSource(JasperReport jasper) { 122 123 if (ds != null) 124 { 125 JOptionPane.showMessageDialog(MainFrame.getMainInstance(),I18n.getString("messages.JRDataSourceProviderConnection.datasourceInUse","This datasource is already in use by another filling process!!"),I18n.getString("message.title.error","Error"),JOptionPane.ERROR_MESSAGE); 126 return null; 127 } 128 129 try { 130 ds = getDataSourceProvider().create(jasper); 131 } catch (Exception ex) 132 { 133 JOptionPane.showMessageDialog(MainFrame.getMainInstance(),I18n.getFormattedString("messages.JRDataSourceProviderConnection.problemsCreatingDatasource","Problems occurred creating the new datasource!!\n{0}", new Object []{""+ex.getMessage()}),I18n.getString("message.title.error","Error"),JOptionPane.ERROR_MESSAGE); 134 } 135 136 return ds; 137 } 138 139 public void disposeDataSource() 140 { 141 if (dsp != null) 142 { 143 try { 144 dsp.dispose(ds); 145 } catch (Exception ex) {} 146 ds = null; 147 } 148 } 149 150 public java.util.HashMap getProperties() 151 { 152 return properties; 153 } 154 155 159 public void loadProperties(java.util.HashMap map) 160 { 161 properties = map; 162 } 163 164 167 public void save(java.io.PrintWriter pw) 168 { 169 java.util.HashMap hm = this.getProperties(); 170 pw.println("\t<iReportConnection name=\""+ this.getName() +"\" connectionClass=\"" + this.getClass().getName() +"\">"); 171 java.util.Iterator iterator = hm.keySet().iterator(); 172 173 while (iterator.hasNext()) 174 { 175 String key = (String )iterator.next(); 176 pw.println("\t\t<connectionParameter name=\"" + key + "\"><![CDATA[" + hm.get(key) + "]]></connectionParameter>"); 177 } 178 pw.println("\t</iReportConnection>"); 179 } 180 181 } 182 | Popular Tags |