content: package cn.xeblog.plugin.tools.read.ui;
  
  import javax.swing.*;
  import javax.swing.text.*;
  
  /**
   * @author LYF
   * @date 2022-06-24
   */
  public class AutoNewlineTextPane extends JTextPane {
  
      public AutoNewlineTextPane() {
          super();
          this.setEditorKit(new WarpEditorKit());
      }
  
      private static class WarpEditorKit extends StyledEditorKit {
  
          private final ViewFactory defaultFactory = new WarpColumnFactory();
  
          @Override
          public ViewFactory getViewFactory() {
              return defaultFactory;
          }
      }
  
      private static class WarpColumnFactory implements ViewFactory {
  
          @Override
          public View create(Element elem) {
              String kind = elem.getName();
              if (kind != null) {
                  if (kind.equals(AbstractDocument.ContentElementName)) {
                      return new WarpLabelView(elem);
                  } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                      return new ParagraphView(elem);
                  } else if (kind.equals(AbstractDocument.SectionElementName)) {
                      return new BoxView(elem, View.Y_AXIS);
                  } else if (kind.equals(StyleConstants.ComponentElementName)) {
                      return new ComponentView(elem);
                  } else if (kind.equals(StyleConstants.IconElementName)) {
                      return new IconView(elem);
                  }
              }
  
              // default to text display
              return new LabelView(elem);
          }
      }
  
      private static class WarpLabelView extends LabelView {
  
          public WarpLabelView(Element elem) {
              super(elem);
          }
  
          @Override
          public float getMinimumSpan(int axis) {
              switch (axis) {
                  case View.X_AXIS:
                      return 0;
                  case View.Y_AXIS:
                      return super.getMinimumSpan(axis);
                  default:
                      throw new IllegalArgumentException("Invalid axis: " + axis);
              }
          }
      }
  }
  