KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > logpane > LogTextArea


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * LogTextArea.java
28  *
29  * Created on 22 agosto 2005, 15.03
30  *
31  */

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 /**
44  *
45  * @author Administrator
46  */

47 public class LogTextArea extends javax.swing.JPanel JavaDoc implements LanguageChangedListener {
48
49     private StringBuffer JavaDoc outputBuffer;
50     private String JavaDoc title = null;
51     private LogPane logPane = null;
52     private boolean removable = false;
53     private Properties properties;
54     
55     private int maxlines = 5000;
56
57     /** Creates new form LogTextArea */
58     public LogTextArea(String JavaDoc title) {
59         initComponents();
60
61         outputBuffer = new StringBuffer JavaDoc();
62         this.setTitle(title);
63
64         this.jEditorPaneOutput.setContentType("text/html");
65
66         this.jEditorPaneOutput.addHyperlinkListener( new javax.swing.event.HyperlinkListener JavaDoc() {
67             public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent JavaDoc 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 JavaDoc ex)
78         {
79             maxlines = 5000;
80         }
81         clearConsole();
82     }
83
84      /** This method parse an error encoded using an url.
85      * The url must be in the form:
86      * http://{error|warinig}:{JReportFrame ID}/{expression}
87      *
88      * error: referred to an expression field
89      * warning: referred to an element position warning
90      *
91      * Possible expressions for error:
92      * variableInitialValue_<VARIABLE_NAME>
93      * textField_<TEXTFIELD NUMBER>
94      * printWhen_<PRINT WHEN EXPRESSION NUMBER>(*)
95      * parameterDefaultValue_<PARAMETER NAME>
96      * parameter_<PARAMETER NAME>
97      *
98      * Possible expressions for warning:
99      * y=<Y>,height=<Height>,band-height=<Band-Height>
100      *
101      * (*) The printWhen expression are considered only if not blank.
102      * The search order is this: bands, elements
103      */

104     public boolean parseError(java.net.URL JavaDoc url) {
105         if (url == null) return false;
106
107         // First of all activate the right frame...
108
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 JavaDoc ex)
118                         {}
119                     }
120                 }
121             }
122         }
123         if (jrf == null) return false;
124
125         try {
126             if (url.getHost().equalsIgnoreCase("error")) {
127                 String JavaDoc expression = url.getFile();
128
129                 if (expression!=null && expression.length() > 0) {
130                     // parse expression...
131
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 JavaDoc 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 JavaDoc ex)
167         {}
168         return false;
169     }
170
171
172     /** This method is called from within the constructor to
173      * initialize the form.
174      * WARNING: Do NOT modify this code. The content of this method is
175      * always regenerated by the Form Editor.
176      */

177     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
178
private void initComponents() {
179         jPopupMenuLog = new javax.swing.JPopupMenu JavaDoc();
180         jMenuItemClearLog = new javax.swing.JMenuItem JavaDoc();
181         jMenuItemCloseLog = new javax.swing.JMenuItem JavaDoc();
182         jSeparator1 = new javax.swing.JSeparator JavaDoc();
183         jMenuItemCopy = new javax.swing.JMenuItem JavaDoc();
184         jMenuItemSelectAll = new javax.swing.JMenuItem JavaDoc();
185         jScrollPaneOutput = new javax.swing.JScrollPane JavaDoc();
186         jEditorPaneOutput = new javax.swing.JEditorPane JavaDoc();
187
188         jMenuItemClearLog.setText("Clear log");
189         jMenuItemClearLog.addActionListener(new java.awt.event.ActionListener JavaDoc() {
190             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc() {
199             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc() {
210             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc() {
219             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
220                 jMenuItemSelectAllActionPerformed(evt);
221             }
222         });
223
224         jPopupMenuLog.add(jMenuItemSelectAll);
225
226         setLayout(new java.awt.BorderLayout JavaDoc());
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 JavaDoc(22, 75));
231         jScrollPaneOutput.setPreferredSize(new java.awt.Dimension JavaDoc(3, 100));
232         jEditorPaneOutput.setBackground(new java.awt.Color JavaDoc(204, 204, 204));
233         jEditorPaneOutput.setEditable(false);
234         jEditorPaneOutput.setFont(new java.awt.Font JavaDoc("Courier New", 0, 12));
235         jEditorPaneOutput.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
236             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
237                 jEditorPaneOutputMouseClicked(evt);
238             }
239         });
240
241         jScrollPaneOutput.setViewportView(jEditorPaneOutput);
242
243         add(jScrollPaneOutput, java.awt.BorderLayout.CENTER);
244
245     }// </editor-fold>//GEN-END:initComponents
246

247     private void jMenuItemSelectAllActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemSelectAllActionPerformed
248
jEditorPaneOutput.selectAll();
249     }//GEN-LAST:event_jMenuItemSelectAllActionPerformed
250

251     private void jMenuItemCopyActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemCopyActionPerformed
252
jEditorPaneOutput.copy();
253     }//GEN-LAST:event_jMenuItemCopyActionPerformed
254

255     private void jMenuItemCloseLogActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemCloseLogActionPerformed
256

257         this.getLogPane().removeLog( this );
258     }//GEN-LAST:event_jMenuItemCloseLogActionPerformed
259

260     private void jMenuItemClearLogActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemClearLogActionPerformed
261
this.clearConsole();
262     }//GEN-LAST:event_jMenuItemClearLogActionPerformed
263

264     private void jEditorPaneOutputMouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jEditorPaneOutputMouseClicked
265

266         if (evt.getButton() == evt.BUTTON3 && evt.getClickCount() == 1) {
267             this.jPopupMenuLog.show(this, evt.getPoint().x, evt.getPoint().y);
268         }
269
270     }//GEN-LAST:event_jEditorPaneOutputMouseClicked
271

272     public void logOnConsole(String JavaDoc noHTML) {
273         
274         noHTML = tail(noHTML, maxlines);
275         logOnConsole(noHTML,false);
276     }
277
278     public void logOnConsole(String JavaDoc s, boolean isHTML) {
279         try {
280
281             //String text = Misc.string_replace("","</body></html>",this.jEditorPaneOutput.getText());
282

283             //s = + s+"</body></html>";
284
if ( this.jEditorPaneOutput == null) {
285                 outputBuffer.append(s);
286                 //System.out.println(outputBuffer);
287
}
288             else {
289                 this.jEditorPaneOutput.setContentType("text/html");
290                 if (!isHTML) {
291                     
292                     s = s.replaceAll("\\>","&gt;");
293                     s = s.replaceAll("\\<","&lt;");
294                     s = s.replaceAll("\\t","&nbsp;&nbsp;&nbsp;&nbsp;");
295                     s = s.replaceAll("\\n","<br>");
296                     s = s.replaceAll("\\s","&nbsp;");
297                     
298                    
299                     //s = Misc.string_replace("&gt;",">",s);
300
//s = Misc.string_replace("&lt;","<",s);
301
//s = Misc.string_replace("&nbsp;"," ",s);
302
//s = Misc.string_replace("&nbsp;&nbsp;&nbsp;&nbsp;","\t",s);
303
//s = Misc.string_replace("<br>", "\n", s);
304
}
305
306                 /*
307                 outputBuffer.append(s);
308                 this.jEditorPaneOutput.setText("<html><body><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td nowrap><font face=\"Courier New\" size=\"3\">" + outputBuffer + "</font></td></tr></table></body></html>");
309                 */

310                 // Perform an append instead...
311
javax.swing.text.html.HTMLDocument JavaDoc doc = (javax.swing.text.html.HTMLDocument JavaDoc)jEditorPaneOutput.getDocument();
312                 javax.swing.text.html.HTMLEditorKit JavaDoc editorKit = (javax.swing.text.html.HTMLEditorKit JavaDoc)jEditorPaneOutput.getEditorKit();
313
314                 //javax.swing.text.Element ele = doc.get getElement("inserthere");
315

316                 if (!s.startsWith("<font") && !s.startsWith("<hr>")) s = "<font face=\"Courier New\" size=\"3\" >" + s + "</font>";
317
318                 //if (ele != null)
319
//{
320
//doc.insertBeforeEnd(ele, s); //- "".length()
321
editorKit.insertHTML(doc, doc.getLength(), s, 0, 0, null);
322
323                 //}
324
//else
325
//{
326
// this.jEditorPaneOutput.setText("<body top=0><p align=\"left\" id=\"inserthere\">"+ s + "</p></body>");
327
//}
328

329
330                 logPane.setActiveLog( this );
331             }
332             //this.jEditorPaneOutput.getDocument().insertString(this.jEditorPaneOutput.getDocument().getLength(), s , null);
333

334         } catch (Exception JavaDoc exsx) {
335             JOptionPane.showMessageDialog(this,""+exsx.getMessage());
336         }
337     }
338
339     /**
340      * Clear console
341      */

342     public void clearConsole()
343     {
344         this.jEditorPaneOutput.setContentType("text/html");
345             outputBuffer.setLength(0);
346             this.jEditorPaneOutput.setText("");
347     }
348
349     public String JavaDoc getTitle() {
350         return title;
351     }
352
353     public void setTitle(String JavaDoc title) {
354         this.title = title;
355         fireActionListenerActionPerformed( new java.awt.event.ActionEvent JavaDoc(this,0,title));
356     }
357
358     // Variables declaration - do not modify//GEN-BEGIN:variables
359
private javax.swing.JEditorPane JavaDoc jEditorPaneOutput;
360     private javax.swing.JMenuItem JavaDoc jMenuItemClearLog;
361     private javax.swing.JMenuItem JavaDoc jMenuItemCloseLog;
362     private javax.swing.JMenuItem JavaDoc jMenuItemCopy;
363     private javax.swing.JMenuItem JavaDoc jMenuItemSelectAll;
364     private javax.swing.JPopupMenu JavaDoc jPopupMenuLog;
365     private javax.swing.JScrollPane JavaDoc jScrollPaneOutput;
366     private javax.swing.JSeparator JavaDoc jSeparator1;
367     // End of variables declaration//GEN-END:variables
368

369     /**
370      * Utility field used by event firing mechanism.
371      */

372     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
373
374     /**
375      * Registers ActionListener to receive events.
376      * @param listener The listener to register.
377      */

378     public synchronized void addActionListener(java.awt.event.ActionListener JavaDoc listener) {
379
380         if (listenerList == null ) {
381             listenerList = new javax.swing.event.EventListenerList JavaDoc();
382         }
383         listenerList.add (java.awt.event.ActionListener JavaDoc.class, listener);
384     }
385
386     /**
387      * Removes ActionListener from the list of listeners.
388      * @param listener The listener to remove.
389      */

390     public synchronized void removeActionListener(java.awt.event.ActionListener JavaDoc listener) {
391
392         listenerList.remove (java.awt.event.ActionListener JavaDoc.class, listener);
393     }
394
395     /**
396      * Notifies all registered listeners about the event.
397      *
398      * @param event The event to be fired
399      */

400     private void fireActionListenerActionPerformed(java.awt.event.ActionEvent JavaDoc event) {
401
402         if (listenerList == null) return;
403         Object JavaDoc[] listeners = listenerList.getListenerList ();
404         for (int i = listeners.length - 2; i >= 0; i -= 2) {
405             if (listeners[i]==java.awt.event.ActionListener JavaDoc.class) {
406                 ((java.awt.event.ActionListener JavaDoc)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     //Added by Felix Firgau for I18n on Feb 10th 2006
427
public void applyI18n() {
428                 // Start autogenerated code ----------------------
429
// End autogenerated code ----------------------
430
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 }//End
439

440
441 public static int countMatches(String JavaDoc str, String JavaDoc 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 JavaDoc tail(String JavaDoc 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