[Schmitzm-commits] r1783 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . log4j

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Nov 17 14:03:54 CET 2011


Author: mojays
Date: 2011-11-17 14:03:53 +0100 (Thu, 17 Nov 2011)
New Revision: 1783

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java
Log:
TextComponentAppender: using invokeLater(.)
SwingUtil: new methods to initialize Font in JComponent

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2011-11-17 12:09:40 UTC (rev 1782)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2011-11-17 13:03:53 UTC (rev 1783)
@@ -77,6 +77,7 @@
 import javax.swing.JCheckBox;
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JComboBox;
+import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JFormattedTextField;
 import javax.swing.JFrame;
@@ -91,7 +92,9 @@
 import javax.swing.JScrollPane;
 import javax.swing.JSpinner;
 import javax.swing.JTable;
+import javax.swing.JTextArea;
 import javax.swing.JTextField;
+import javax.swing.JTextPane;
 import javax.swing.JToggleButton;
 import javax.swing.JToolBar;
 import javax.swing.JTree;
@@ -1700,8 +1703,48 @@
         button.setText(null);
       return button;
     }
+    
+    /**
+     * Creates a {@link JTextArea} with the specified style.
+     * @param font the font (if {@code null} {@link #DEFAULT_FONT} is used)
+     * @param size font size
+     * @param style font style (e.g. {@link Font#BOLD} or {@link Font#ITALIC})
+     */
+    public static JTextArea createTextArea(Font font, Float size, Integer style) {
+      return initJComponent(new JTextArea(), font, size, style);
+    }
 
     /**
+     * Creates a {@link JTextPane} with the specified style.
+     * @param font the font (if {@code null} {@link #DEFAULT_FONT} is used)
+     * @param size font size
+     * @param style font style (e.g. {@link Font#BOLD} or {@link Font#ITALIC})
+     */
+    public static JTextPane createTextPane(Font font, Float size, Integer style) {
+      return initJComponent(new JTextPane(), font, size, style);
+    }
+    
+    /**
+     * Initializes the font style of a {@link JTextComponent}.
+     * @param textComp the component
+     * @param font the font (if {@code null} {@link #DEFAULT_FONT} is used)
+     * @param size font size
+     * @param style font style (e.g. {@link Font#BOLD} or {@link Font#ITALIC})
+     * @return simply the given parameter {@code textComp}
+     */
+    public static <E extends JComponent> E initJComponent(E textComp, Font font, Float size, Integer style) {
+      if ( font == null )
+        font = DEFAULT_FONT;
+      if ( size != null )
+        font = font.deriveFont(size);
+      if ( style != null )
+        font = font.deriveFont(style);
+      textComp.setFont(font);
+      
+      return textComp;
+    }
+
+    /**
 	 * Create a {@link JMenu} that allows to switch the Log-Level of the root
 	 * logger.
 	 */

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java	2011-11-17 12:09:40 UTC (rev 1782)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/log4j/TextComponentAppender.java	2011-11-17 13:03:53 UTC (rev 1783)
@@ -7,8 +7,10 @@
 
 package de.schmitzm.swing.log4j;
 
-import java.lang.reflect.InvocationTargetException;
+import java.awt.Font;
 
+import javax.swing.JTextArea;
+import javax.swing.JTextPane;
 import javax.swing.SwingUtilities;
 import javax.swing.text.JTextComponent;
 
@@ -19,6 +21,11 @@
 
 /**
  * Panel which shows all log message in a {@link JTextComponent}.
+ * <b>Note:</b><br>
+ * Take into account that the rendering of a {@link JTextPane} might
+ * be very slow when many messages should be shown. So the use
+ * of {@link JTextArea} is recommended (e.g. with an adaquate {@link Font}).  
+ *      
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
 public class TextComponentAppender extends AppenderSkeleton {
@@ -84,9 +91,16 @@
     Runnable work = new Runnable() {
       @Override
       public void run() {
+        lastUpdate = System.currentTimeMillis();
+        // Optimization: use the append method for
+        //               JTextArea
+        if ( textComp instanceof JTextArea ) {
+          ((JTextArea)textComp).append(textBuffer.toString());
+          return;
+        }
+        // general JTextComponent implementation
         textComp.setText( textComp.getText() + textBuffer.toString() );
         textBuffer = new StringBuffer();
-        lastUpdate = System.currentTimeMillis();
       }
     };
     if ( SwingUtilities.isEventDispatchThread() )
@@ -94,16 +108,17 @@
     else
 // MS: GUI blocks some times for a some 100ms if invokeLater is used
 //     with invokeAndWait it works fine??!!
-//      SwingUtilities.invokeLater(work);
-      try {
-        SwingUtilities.invokeAndWait(work);
-      } catch (InterruptedException err) {
-        // TODO Auto-generated catch block
-        err.printStackTrace();
-      } catch (InvocationTargetException err) {
-        // TODO Auto-generated catch block
-        err.printStackTrace();
-      }
+//     Reason: seams to be the "slow" rendering of JTextPane
+      SwingUtilities.invokeLater(work);
+//      try {
+//        SwingUtilities.invokeAndWait(work);
+//      } catch (InterruptedException err) {
+//        // TODO Auto-generated catch block
+//        err.printStackTrace();
+//      } catch (InvocationTargetException err) {
+//        // TODO Auto-generated catch block
+//        err.printStackTrace();
+//      }
   }
 
   /**



More information about the Schmitzm-commits mailing list