1 16 17 package com.jaspersoft.jasperserver.irplugin; 18 19 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor; 20 import com.jaspersoft.jasperserver.irplugin.gui.jrxmlvalidator.ImageElementValidationItem; 21 import com.jaspersoft.jasperserver.irplugin.gui.jrxmlvalidator.JrxmlValidationDialog; 22 import com.sun.jmx.mbeanserver.Repository; 23 import it.businesslogic.ireport.CrosstabReportElement; 24 import it.businesslogic.ireport.ImageReportElement; 25 import it.businesslogic.ireport.JRProperty; 26 import it.businesslogic.ireport.Report; 27 import it.businesslogic.ireport.ReportElement; 28 import it.businesslogic.ireport.gui.JReportFrame; 29 import it.businesslogic.ireport.gui.MainFrame; 30 import it.businesslogic.ireport.gui.event.ReportFrameActivatedEvent; 31 import it.businesslogic.ireport.gui.event.ReportFrameActivatedListener; 32 import it.businesslogic.ireport.util.Misc; 33 import java.util.Enumeration ; 34 import java.util.List ; 35 import java.util.Vector ; 36 import javax.swing.JInternalFrame ; 37 import java.io.File ; 38 import javax.swing.JOptionPane ; 39 40 41 45 public class RepositoryJrxmlFile extends RepositoryFile implements ReportFrameActivatedListener { 46 47 private JReportFrame reportFrame = null; 48 49 public RepositoryJrxmlFile(JServer server, ResourceDescriptor descriptor) { 50 super( server, descriptor); 51 MainFrame.getMainInstance().addReportFrameActivatedListener( this ); 52 } 53 54 public String toString() 55 { 56 if (getDescriptor() != null) 57 { 58 return ""+getDescriptor().getLabel(); 59 } 60 return "???"; 61 } 62 63 public void reportFrameActivated(ReportFrameActivatedEvent reportFrameActivatedEvent) { 64 65 66 if (getReportFrame() == null) return; 67 if (reportFrameActivatedEvent.getReportFrame() == null) 68 { 69 setReportFrame(null); 71 IRPlugin.getMainInstance().getRepositoryExplorer().updateToolBar(); 72 return; 73 } 74 75 JInternalFrame [] frames = MainFrame.getMainInstance().getJMDIDesktopPane().getAllFrames(); 77 78 boolean found = false; 79 for (int i=0; i<frames.length; ++i) 80 { 81 if (frames[i] == getReportFrame()) 82 { 83 found = true; 84 break; 85 } 86 } 87 if (!found) 88 { 89 setReportFrame(null); 90 IRPlugin.getMainInstance().getRepositoryExplorer().updateToolBar(); 91 } 92 93 } 94 95 public void editFile() throws Exception 96 { 97 if (getReportFrame() == null) 98 { 99 String tmpFileName = IRPlugin.createTmpFileName(this.getDescriptor().getName(),".jrxml"); 102 getServer().getWSClient().get(this.getDescriptor(), new File (tmpFileName)); 103 setReportFrame( MainFrame.getMainInstance().openFile( tmpFileName ) ); 104 105 loadRepositoryImages(getReportFrame().getReport().getElements()); 107 } 108 else 109 { 110 getReportFrame().setSelected(true); 111 } 112 } 113 114 118 protected void loadRepositoryImages(java.util.Vector elements) 119 { 120 Enumeration enumElements = elements.elements(); 121 while (enumElements.hasMoreElements()) 122 { 123 ReportElement re = (ReportElement)enumElements.nextElement(); 124 if (re instanceof ImageReportElement) 125 { 126 ImageReportElement ire = (ImageReportElement)re; 127 String expression =ire.getImageExpression(); 129 expression = Misc.string_replace("\\","\\\\",expression); 130 expression = Misc.string_replace("","\"",expression); 131 132 if ((ire.getImageClass().length() == 0 || ire.getImageClass().equals("java.lang.String")) && 133 expression.toLowerCase().startsWith("repo:")) 134 { 135 ResourceDescriptor rd = new ResourceDescriptor(); 136 expression = expression.substring(5); 137 if (expression.startsWith("/")) 138 { 139 rd.setUriString(expression); 140 } 141 else 142 { 143 rd.setUriString(this.getDescriptor().getParentFolder() + "/" + expression); 144 } 145 RepositoryFile rf = new RepositoryFile(this.getServer(), rd); 147 try { 148 String imgFile = rf.getFile(); 149 if (imgFile != null) 150 { 151 javax.swing.ImageIcon img = new javax.swing.ImageIcon (imgFile); 152 ire.setImg(img.getImage()); 153 } 154 } catch (Exception ex) 155 { 156 ex.printStackTrace(); 157 } 158 } 159 } 160 if (re instanceof CrosstabReportElement) 161 { 162 loadRepositoryImages( ((CrosstabReportElement)re).getElements()); 163 } 164 } 165 166 167 } 168 169 public JReportFrame getReportFrame() { 170 return reportFrame; 171 } 172 173 public void setReportFrame(JReportFrame reportFrame) { 174 this.reportFrame = reportFrame; 175 } 176 177 180 public boolean validateUrls(JServer server, RepositoryReportUnit repoUnit) { 181 182 List imageElementValidationItems = new java.util.ArrayList (); 183 184 if (getReportFrame() != null) 185 { 186 Vector elements = getReportFrame().getReport().getElements(); 187 for (int i=0; i<elements.size(); ++i) 188 { 189 ReportElement re = (ReportElement) elements.elementAt(i); 190 if (re instanceof ImageReportElement) 191 { 192 ImageReportElement ire = (ImageReportElement)re; 193 194 String fname = ire.getImageExpression(); 195 196 if (ire.getImageClass().equals("java.lang.String") && 197 !ire.getImageExpression().startsWith("\"repo:") && 198 fname.length()>3 && 199 fname.charAt(0)=='\"' && 200 fname.charAt(fname.length()-1 ) == '\"' && 201 !(ire.getImageExpression().indexOf("$F{") >= 0 || 202 ire.getImageExpression().indexOf("$P{") >= 0 || 203 ire.getImageExpression().indexOf("$V{") >= 0 || 204 ire.getImageExpression().indexOf("$R{") >= 0)) 205 { 206 208 fname = Misc.string_replace("\\","\\\\",fname); 209 fname = Misc.string_replace("\\","\\\\",fname); 211 fname = Misc.string_replace("","\"",fname); 212 213 File f = new File ( fname ); 214 if (f.exists()) 215 { 216 ImageElementValidationItem ievi = new ImageElementValidationItem(); 217 ievi.setOriginalFileName( f ); 218 219 String name = getValidName( f.getName(), repoUnit ); 220 ievi.setResourceName( name ); 221 ievi.setProposedExpression("repo:" + name ); 222 ievi.setReportElement(ire); 223 imageElementValidationItems.add(ievi); 224 } 225 } 226 } 227 } 228 229 Report report = getReportFrame().getReport(); 230 boolean skipValidation = false; 231 for (int i=0; i< report.getJRproperties().size(); ++i) 232 { 233 JRProperty prop = (JRProperty)report.getJRproperties().get(i); 234 if (prop.getName() != null && prop.getName().equals("com.jaspersoft.irplugin.validation")) 235 { 236 if (prop.getValue().equals("0")) 237 { 238 skipValidation = true; 239 } 240 break; 241 } 242 } 243 244 if (imageElementValidationItems.size() > 0 && !skipValidation) 245 { 246 JrxmlValidationDialog jvd = new JrxmlValidationDialog(MainFrame.getMainInstance(),true); 247 jvd.setImageElementVelidationItems( imageElementValidationItems ); 248 jvd.setServer( server ); 249 jvd.setReportUnit( repoUnit ); 250 jvd.setReport( getReportFrame().getReport() ); 251 jvd.setVisible(true); 252 253 if (jvd.getDialogResult() == JOptionPane.CANCEL_OPTION) 254 { 255 return false; 256 } 257 } 258 } 259 260 return true; 261 262 } 263 264 public String getValidName( String name, RepositoryReportUnit repoUnit) 265 { 266 return getValidName( name, repoUnit, 0); 267 } 268 269 private String getValidName( String name, RepositoryReportUnit repoUnit, int k ) 270 { 271 String ext = ""; 273 String base = name; 274 if (name.lastIndexOf(".") > 0) 275 { 276 ext = name.substring( name.lastIndexOf(".") ); 277 base = name.substring(0, name.lastIndexOf(".") ); 278 } 279 String newName = base + ( (k==0) ? "" : "-"+k) + ext; 280 281 for (int i=0; i<repoUnit.getDescriptor().getChildren().size(); ++i) 282 { 283 ResourceDescriptor rd = (ResourceDescriptor) repoUnit.getDescriptor().getChildren().get(i); 284 if (rd.getName().equals(newName)) 285 { 286 return getValidName( name, repoUnit, ++k ); 287 } 288 } 289 return newName; 290 } 291 292 } 293 | Popular Tags |