Author: seanfinan
Date: Wed Oct 19 17:53:57 2022
New Revision: 1904700

URL: http://svn.apache.org/viewvc?rev=1904700&view=rev
Log:
Format styles for substrings in separate lambda threads

Modified:
    
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/LoggerTextFilter.java

Modified: 
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/LoggerTextFilter.java
URL: 
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/LoggerTextFilter.java?rev=1904700&r1=1904699&r2=1904700&view=diff
==============================================================================
--- 
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/LoggerTextFilter.java
 (original)
+++ 
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/component/LoggerTextFilter.java
 Wed Oct 19 17:53:57 2022
@@ -12,12 +12,12 @@ import java.util.Map;
  */
 public class LoggerTextFilter extends DocumentFilter {
 
-   final private TextFormatter _textFormatter;
+   private final Map<String, Style> _styles = new HashMap<>();
 
 
    public LoggerTextFilter( final DefaultStyledDocument document ) {
-      _textFormatter = new TextFormatter( document );
       document.setDocumentFilter( this );
+      createStyles( document );
    }
 
    /**
@@ -35,9 +35,7 @@ public class LoggerTextFilter extends Do
    public void insertString( final FilterBypass fb, final int begin, final 
String text, final AttributeSet attr )
          throws BadLocationException {
       super.insertString( fb, begin, text, attr );
-      if ( shouldReformat( fb.getDocument(), begin, text.length() ) ) {
-         formatText( fb.getDocument() );
-      }
+      formatText( fb.getDocument(), begin, text.length(), text );
    }
 
    /**
@@ -48,94 +46,46 @@ public class LoggerTextFilter extends Do
                         final AttributeSet attrs )
          throws BadLocationException {
       super.replace( fb, begin, length, text, attrs );
-      if ( shouldReformat( fb.getDocument(), begin, length ) ) {
-         formatText( fb.getDocument() );
-      }
-   }
-
-   static private boolean shouldReformat( final Document document, final int 
begin, final int length )
-         throws BadLocationException {
-      final int testLength = Math.min( length + 2, document.getLength() - 
begin );
-      final String deltaText = document.getText( begin, testLength );
-      return deltaText.contains( " " ) || deltaText.contains( "\n" ) || 
deltaText.contains( "\t" );
-   }
-
-   private void formatText( final Document document ) {
-      if ( document instanceof StyledDocument ) {
-         SwingUtilities.invokeLater( _textFormatter );
-      }
+      formatText( fb.getDocument(), begin, length, text );
    }
 
-   static private class TextFormatter implements Runnable {
-
-      final StyledDocument _document;
-      final Map<String, Style> _styles = new HashMap<>();
-
-      private TextFormatter( final StyledDocument document ) {
-         _document = document;
-         createStyles();
+   private void formatText( final Document document, final int begin, final 
int length, final String text ) {
+      if ( !( document instanceof StyledDocument ) ) {
+         return;
       }
-
-      @Override
-      public void run() {
-         try {
-            final String text = _document.getText( 0, _document.getLength() );
-            int lineBegin = 0;
-            boolean lineEnded = false;
-            for ( int i = 0; i < _document.getLength(); i++ ) {
-               lineEnded = false;
-               if ( text.charAt( i ) == '\n' ) {
-                  formatLine( lineBegin, i );
-                  lineBegin = i + 1;
-                  lineEnded = true;
-               }
-            }
-            if ( !lineEnded ) {
-               formatLine( lineBegin, _document.getLength() );
-            }
-         } catch ( BadLocationException blE ) {
-//            LOGGER.error( blE.getMessage() );
-         }
-      }
-
-      private void createStyles() {
-         StyleConstants.setItalic( createStyle( "COMMENT", Color.GRAY ), true 
);
-         StyleConstants.setBold( createStyle( "ELLIPSES", Color.BLUE ), true );
-         createStyle( "ERROR", Color.RED.darker() );
-         createStyle( "WARNING", Color.ORANGE.darker() );
-         createStyle( "PLAIN", Color.BLACK );
-      }
-
-      private Style createStyle( final String name, final Color color ) {
-         final Style style = _document.addStyle( name, null );
-         StyleConstants.setForeground( style, color );
-         _styles.put( name, style );
-         return style;
-      }
-
-      void formatLine( final int begin, final int end ) throws 
BadLocationException {
-         final int length = end - begin;
-         if ( length <= 0 ) {
-            return;
-         }
-         final String text = _document.getText( begin, length );
-         System.out.println( "formatting " + text );
+      final StyledDocument doc = (StyledDocument) document;
+      SwingUtilities.invokeLater( () -> {
          if ( text.startsWith( "#" ) || text.startsWith( "//" ) ) {
-            _document.setCharacterAttributes( begin, length, _styles.get( 
"COMMENT" ), true );
+            doc.setCharacterAttributes( begin, length, _styles.get( "COMMENT" 
), false );
             return;
          }
          final String lower = text.toLowerCase();
          if ( lower.contains( "error" ) ) {
-            _document.setCharacterAttributes( begin, length, _styles.get( 
"ERROR" ), true );
+            doc.setCharacterAttributes( begin, length, _styles.get( "ERROR" ), 
false );
          } else if ( lower.contains( "warn" ) ) {
-            _document.setCharacterAttributes( begin, length, _styles.get( 
"WARNING" ), true );
+            doc.setCharacterAttributes( begin, length, _styles.get( "WARNING" 
), false );
          } else if ( lower.contains( "..." ) ) {
-            _document.setCharacterAttributes( begin, length, _styles.get( 
"ELLIPSES" ), true );
+            doc.setCharacterAttributes( begin, length, _styles.get( "ELLIPSES" 
), false );
          } else {
-            _document.setCharacterAttributes( begin, length, _styles.get( 
"PLAIN" ), true );
+            doc.setCharacterAttributes( begin, length, _styles.get( "PLAIN" ), 
false );
          }
-      }
+      } );
+   }
 
+   private void createStyles( final StyledDocument doc ) {
+      StyleConstants.setItalic( createStyle( doc, "COMMENT", Color.GRAY ), 
true );
+      createStyle( doc, "ELLIPSES", Color.BLUE );
+      createStyle( doc, "ERROR", Color.RED.darker() );
+      createStyle( doc, "WARNING", Color.ORANGE.darker() );
+      createStyle( doc, "PLAIN", Color.BLACK );
+   }
+
+   private Style createStyle( final StyledDocument doc, final String name, 
final Color color ) {
+      final Style style = doc.addStyle( name, null );
+      StyleConstants.setForeground( style, color );
+      _styles.put( name, style );
+      return style;
    }
 
+
 }


Reply via email to