1 32 33 package it.businesslogic.ireport.gui.logpane; 34 import javax.swing.*; 35 import it.businesslogic.ireport.util.Misc; 36 import it.businesslogic.ireport.gui.*; 37 import it.businesslogic.ireport.*; 38 import java.util.*; 39 import it.businesslogic.ireport.util.I18n; 40 import it.businesslogic.ireport.util.LanguageChangedEvent; 41 import it.businesslogic.ireport.util.LanguageChangedListener; 42 43 47 public class LogTextArea extends javax.swing.JPanel implements LanguageChangedListener { 48 49 private StringBuffer outputBuffer; 50 private String title = null; 51 private LogPane logPane = null; 52 private boolean removable = false; 53 private Properties properties; 54 55 private int maxlines = 5000; 56 57 58 public LogTextArea(String title) { 59 initComponents(); 60 61 outputBuffer = new StringBuffer (); 62 this.setTitle(title); 63 64 this.jEditorPaneOutput.setContentType("text/html"); 65 66 this.jEditorPaneOutput.addHyperlinkListener( new javax.swing.event.HyperlinkListener () { 67 public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent e) { 68 if (e.getEventType() == javax.swing.event.HyperlinkEvent.EventType.ACTIVATED) { 69 if (!parseError(e.getURL())) 70 JOptionPane.showMessageDialog(MainFrame.getMainInstance() ," HyperlinkEvent " + e.getURL() ); 71 } 72 } 73 }); 74 75 try { 76 maxlines = Integer.parseInt( System.getProperty("ireport.maxoutputlines", "5000")); 77 } catch (Exception ex) 78 { 79 maxlines = 5000; 80 } 81 clearConsole(); 82 } 83 84 104 public boolean parseError(java.net.URL url) { 105 if (url == null) return false; 106 107 JInternalFrame[] frames = MainFrame.getMainInstance().getJMDIDesktopPane().getAllFrames(); 109 JReportFrame jrf = null; 110 for (int k=0; k< frames.length; k++) { 111 if (frames[k] instanceof JReportFrame) { 112 if ( ((JReportFrame)frames[k]).getWindowID() == url.getPort()) { 113 jrf = (JReportFrame)frames[k]; 114 if ( !frames[k].isSelected() ) { 115 try { 116 frames[k].setSelected(true); 117 } catch (Exception ex) 118 {} 119 } 120 } 121 } 122 } 123 if (jrf == null) return false; 124 125 try { 126 if (url.getHost().equalsIgnoreCase("error")) { 127 String expression = url.getFile(); 128 129 if (expression!=null && expression.length() > 0) { 130 if (expression.startsWith("/textField_")) { 132 int index = 0; 133 int number = Integer.parseInt( expression.substring(("/textField_").length()).trim()); 134 Enumeration e = jrf.getReport().getElements().elements(); 135 while (e.hasMoreElements()) { 136 ReportElement re = (ReportElement)e.nextElement(); 137 if (re instanceof TextFieldReportElement) { 138 index++; 139 if (index == number) { 140 jrf.setSelectedElement(re); 141 MainFrame.getMainInstance().getElementPropertiesDialog().setVisible(true); 142 MainFrame.getMainInstance().getElementPropertiesDialog().gotoTab( MainFrame.getMainInstance().getElementPropertiesDialog().TEXTFIELD_TAB ); 143 return true; 144 } 145 } 146 } 147 } 148 else if (expression.startsWith("/parameterDefaultValue_")) { 149 int index = 0; 150 String name = expression.substring(("/parameterDefaultValue_").length()).trim(); 151 Enumeration e = jrf.getReport().getParameters().elements(); 152 while (e.hasMoreElements()) { 153 it.businesslogic.ireport.JRParameter param = (it.businesslogic.ireport.JRParameter)e.nextElement(); 154 if (param.getName().equals(name)) { 155 MainFrame.getMainInstance().getValuesDialog().setVisible(true); 156 MainFrame.getMainInstance().getValuesDialog().modifyErrorParameter(param ); 157 return true; 158 } 159 } 160 161 } 162 } 163 } 164 else if (url.getHost().equals("warning")) { 165 } 166 } catch (Exception ex) 167 {} 168 return false; 169 } 170 171 172 177 private void initComponents() { 179 jPopupMenuLog = new javax.swing.JPopupMenu (); 180 jMenuItemClearLog = new javax.swing.JMenuItem (); 181 jMenuItemCloseLog = new javax.swing.JMenuItem (); 182 jSeparator1 = new javax.swing.JSeparator (); 183 jMenuItemCopy = new javax.swing.JMenuItem (); 184 jMenuItemSelectAll = new javax.swing.JMenuItem (); 185 jScrollPaneOutput = new javax.swing.JScrollPane (); 186 jEditorPaneOutput = new javax.swing.JEditorPane (); 187 188 jMenuItemClearLog.setText("Clear log"); 189 jMenuItemClearLog.addActionListener(new java.awt.event.ActionListener () { 190 public void actionPerformed(java.awt.event.ActionEvent evt) { 191 jMenuItemClearLogActionPerformed(evt); 192 } 193 }); 194 195 jPopupMenuLog.add(jMenuItemClearLog); 196 197 jMenuItemCloseLog.setText("Close log"); 198 jMenuItemCloseLog.addActionListener(new java.awt.event.ActionListener () { 199 public void actionPerformed(java.awt.event.ActionEvent evt) { 200 jMenuItemCloseLogActionPerformed(evt); 201 } 202 }); 203 204 jPopupMenuLog.add(jMenuItemCloseLog); 205 206 jPopupMenuLog.add(jSeparator1); 207 208 jMenuItemCopy.setText("Copy"); 209 jMenuItemCopy.addActionListener(new java.awt.event.ActionListener () { 210 public void actionPerformed(java.awt.event.ActionEvent evt) { 211 jMenuItemCopyActionPerformed(evt); 212 } 213 }); 214 215 jPopupMenuLog.add(jMenuItemCopy); 216 217 jMenuItemSelectAll.setText("Select all"); 218 jMenuItemSelectAll.addActionListener(new java.awt.event.ActionListener () { 219 public void actionPerformed(java.awt.event.ActionEvent evt) { 220 jMenuItemSelectAllActionPerformed(evt); 221 } 222 }); 223 224 jPopupMenuLog.add(jMenuItemSelectAll); 225 226 setLayout(new java.awt.BorderLayout ()); 227 228 jScrollPaneOutput.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 229 jScrollPaneOutput.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 230 jScrollPaneOutput.setMinimumSize(new java.awt.Dimension (22, 75)); 231 jScrollPaneOutput.setPreferredSize(new java.awt.Dimension (3, 100)); 232 jEditorPaneOutput.setBackground(new java.awt.Color (204, 204, 204)); 233 jEditorPaneOutput.setEditable(false); 234 jEditorPaneOutput.setFont(new java.awt.Font ("Courier New", 0, 12)); 235 jEditorPaneOutput.addMouseListener(new java.awt.event.MouseAdapter () { 236 public void mouseClicked(java.awt.event.MouseEvent evt) { 237 jEditorPaneOutputMouseClicked(evt); 238 } 239 }); 240 241 jScrollPaneOutput.setViewportView(jEditorPaneOutput); 242 243 add(jScrollPaneOutput, java.awt.BorderLayout.CENTER); 244 245 } 247 private void jMenuItemSelectAllActionPerformed(java.awt.event.ActionEvent evt) { jEditorPaneOutput.selectAll(); 249 } 251 private void jMenuItemCopyActionPerformed(java.awt.event.ActionEvent evt) { jEditorPaneOutput.copy(); 253 } 255 private void jMenuItemCloseLogActionPerformed(java.awt.event.ActionEvent evt) { 257 this.getLogPane().removeLog( this ); 258 } 260 private void jMenuItemClearLogActionPerformed(java.awt.event.ActionEvent evt) { this.clearConsole(); 262 } 264 private void jEditorPaneOutputMouseClicked(java.awt.event.MouseEvent evt) { 266 if (evt.getButton() == evt.BUTTON3 && evt.getClickCount() == 1) { 267 this.jPopupMenuLog.show(this, evt.getPoint().x, evt.getPoint().y); 268 } 269 270 } 272 public void logOnConsole(String noHTML) { 273 274 noHTML = tail(noHTML, maxlines); 275 logOnConsole(noHTML,false); 276 } 277 278 public void logOnConsole(String s, boolean isHTML) { 279 try { 280 281 283 if ( this.jEditorPaneOutput == null) { 285 outputBuffer.append(s); 286 } 288 else { 289 this.jEditorPaneOutput.setContentType("text/html"); 290 if (!isHTML) { 291 292 s = s.replaceAll("\\>",">"); 293 s = s.replaceAll("\\<","<"); 294 s = s.replaceAll("\\t"," "); 295 s = s.replaceAll("\\n","<br>"); 296 s = s.replaceAll("\\s"," "); 297 298 299 } 305 306 310 javax.swing.text.html.HTMLDocument doc = (javax.swing.text.html.HTMLDocument )jEditorPaneOutput.getDocument(); 312 javax.swing.text.html.HTMLEditorKit editorKit = (javax.swing.text.html.HTMLEditorKit )jEditorPaneOutput.getEditorKit(); 313 314 316 if (!s.startsWith("<font") && !s.startsWith("<hr>")) s = "<font face=\"Courier New\" size=\"3\" >" + s + "</font>"; 317 318 editorKit.insertHTML(doc, doc.getLength(), s, 0, 0, null); 322 323 329 330 logPane.setActiveLog( this ); 331 } 332 334 } catch (Exception exsx) { 335 JOptionPane.showMessageDialog(this,""+exsx.getMessage()); 336 } 337 } 338 339 342 public void clearConsole() 343 { 344 this.jEditorPaneOutput.setContentType("text/html"); 345 outputBuffer.setLength(0); 346 this.jEditorPaneOutput.setText(""); 347 } 348 349 public String getTitle() { 350 return title; 351 } 352 353 public void setTitle(String title) { 354 this.title = title; 355 fireActionListenerActionPerformed( new java.awt.event.ActionEvent (this,0,title)); 356 } 357 358 private javax.swing.JEditorPane jEditorPaneOutput; 360 private javax.swing.JMenuItem jMenuItemClearLog; 361 private javax.swing.JMenuItem jMenuItemCloseLog; 362 private javax.swing.JMenuItem jMenuItemCopy; 363 private javax.swing.JMenuItem jMenuItemSelectAll; 364 private javax.swing.JPopupMenu jPopupMenuLog; 365 private javax.swing.JScrollPane jScrollPaneOutput; 366 private javax.swing.JSeparator jSeparator1; 367 369 372 private javax.swing.event.EventListenerList listenerList = null; 373 374 378 public synchronized void addActionListener(java.awt.event.ActionListener listener) { 379 380 if (listenerList == null ) { 381 listenerList = new javax.swing.event.EventListenerList (); 382 } 383 listenerList.add (java.awt.event.ActionListener .class, listener); 384 } 385 386 390 public synchronized void removeActionListener(java.awt.event.ActionListener listener) { 391 392 listenerList.remove (java.awt.event.ActionListener .class, listener); 393 } 394 395 400 private void fireActionListenerActionPerformed(java.awt.event.ActionEvent event) { 401 402 if (listenerList == null) return; 403 Object [] listeners = listenerList.getListenerList (); 404 for (int i = listeners.length - 2; i >= 0; i -= 2) { 405 if (listeners[i]==java.awt.event.ActionListener .class) { 406 ((java.awt.event.ActionListener )listeners[i+1]).actionPerformed (event); 407 } 408 } 409 } 410 411 public LogPane getLogPane() { 412 return logPane; 413 } 414 415 public void setLogPane(LogPane logPane) { 416 this.logPane = logPane; 417 } 418 419 public boolean isRemovable() { 420 return removable; 421 } 422 423 public void setRemovable(boolean removable) { 424 this.removable = removable; 425 } 426 public void applyI18n() { 428 jMenuItemClearLog.setText(it.businesslogic.ireport.util.I18n.getString("clearLog")); 431 jMenuItemCloseLog.setText(it.businesslogic.ireport.util.I18n.getString("closeLog")); 432 jMenuItemCopy.setText(it.businesslogic.ireport.util.I18n.getString("copy")); 433 jMenuItemSelectAll.setText(it.businesslogic.ireport.util.I18n.getString("selectAll")); 434 435 } 436 public void languageChanged(LanguageChangedEvent evt) { 437 this.applyI18n(); 438 } 440 441 public static int countMatches(String str, String sub) { 442 if (str == null || str.length() == 0 || sub == null || sub.length()==0) { 443 return 0; 444 } 445 int count = 0; 446 int idx = 0; 447 while ((idx = str.indexOf(sub, idx)) != -1) { 448 count++; 449 idx += sub.length(); 450 } 451 return count; 452 } 453 454 455 456 public static String tail(String str, int lines) { 457 if (str == null || str.length() == 0) { 458 return str; 459 } 460 int total = countMatches(str,"\n"); 461 int splitTo = total - lines; 462 if (splitTo <= 0) return str; 463 464 int count = 0; 465 int idx = 0; 466 while (count < splitTo && (idx = str.indexOf("\n", idx)) != -1) { 467 count++; 468 idx += 1; 469 } 470 471 return str.substring(idx); 472 } 473 474 } 475 | Popular Tags |