Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AutoWrappingJTextPane.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.corecomponents;
20 
21 import java.text.MessageFormat;
22 import javax.swing.JTextPane;
23 import javax.swing.SizeRequirements;
24 import javax.swing.text.Element;
25 import javax.swing.text.View;
26 import static javax.swing.text.View.GoodBreakWeight;
27 import javax.swing.text.ViewFactory;
28 import javax.swing.text.html.HTMLEditorKit;
29 import javax.swing.text.html.InlineView;
30 import javax.swing.text.html.ParagraphView;
31 import javax.swing.text.html.StyleSheet;
34 
38 public class AutoWrappingJTextPane extends JTextPane {
39 
41  /*
42  * This appears to be an attempt to modify the wrapping behavior of the
43  * text pane. Taken form this website: http://java-sl.com/tip_html_letter_wrap.html.
44  */
45  HTMLEditorKit editorKit = new HTMLEditorKit() {
46  private static final long serialVersionUID = 1L;
47 
48  @Override
49  public ViewFactory getViewFactory() {
50 
51  return new HTMLEditorKit.HTMLFactory() {
52  @Override
53  public View create(Element e) {
54  View v = super.create(e);
55  if (v instanceof InlineView) {
56  return new InlineView(e) {
57  @Override
58  public int getBreakWeight(int axis, float pos, float len) {
59  return GoodBreakWeight;
60  }
61 
62  @Override
63  public View breakView(int axis, int p0, float pos, float len) {
64  if (axis == View.X_AXIS) {
65  checkPainter();
66  int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
67  if (p0 == getStartOffset() && p1 == getEndOffset()) {
68  return this;
69  }
70  return createFragment(p0, p1);
71  }
72  return this;
73  }
74  };
75  } else if (v instanceof ParagraphView) {
76  return new ParagraphView(e) {
77  @Override
78  protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
79  SizeRequirements requirements = r;
80  if (requirements == null) {
81  requirements = new SizeRequirements();
82  }
83  float pref = layoutPool.getPreferredSpan(axis);
84  float min = layoutPool.getMinimumSpan(axis);
85  // Don't include insets, Box.getXXXSpan will include them.
86  requirements.minimum = (int) min;
87  requirements.preferred = Math.max(requirements.minimum, (int) pref);
88  requirements.maximum = Integer.MAX_VALUE;
89  requirements.alignment = 0.5f;
90  return requirements;
91  }
92  };
93  }
94  return v;
95  }
96  };
97  }
98  };
99 
100  this.setEditorKit(editorKit);
101  }
102 
103 
104 
105  @Override
106  public void setText(String text) {
107  // setting the text format with style to avoid problems with overridden styles.
108  String style = String.format("font-family: %s; font-size: %dpt; margin: 0px; padding: 0px 0px %dpx 0px;",
110 
111  super.setText(MessageFormat.format("<pre style=\"{0}\">{1}</pre>", style, EscapeUtil.escapeHtml(text)));
112  }
113 }
static String escapeHtml(String toEscape)
Definition: EscapeUtil.java:75

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.