Autopsy  4.17.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 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 javax.swing.JTextPane;
22 import javax.swing.SizeRequirements;
23 import javax.swing.text.Element;
24 import javax.swing.text.View;
25 import static javax.swing.text.View.GoodBreakWeight;
26 import javax.swing.text.ViewFactory;
27 import javax.swing.text.html.HTMLEditorKit;
28 import javax.swing.text.html.InlineView;
29 import javax.swing.text.html.ParagraphView;
31 
35 public class AutoWrappingJTextPane extends JTextPane {
36 
38  /*
39  * This appears to be an attempt to modify the wrapping behavior of the
40  * text pane. Taken form this website: http://java-sl.com/tip_html_letter_wrap.html.
41  */
42  HTMLEditorKit editorKit = new HTMLEditorKit() {
43  private static final long serialVersionUID = 1L;
44 
45  @Override
46  public ViewFactory getViewFactory() {
47 
48  return new HTMLEditorKit.HTMLFactory() {
49  @Override
50  public View create(Element e) {
51  View v = super.create(e);
52  if (v instanceof InlineView) {
53  return new InlineView(e) {
54  @Override
55  public int getBreakWeight(int axis, float pos, float len) {
56  return GoodBreakWeight;
57  }
58 
59  @Override
60  public View breakView(int axis, int p0, float pos, float len) {
61  if (axis == View.X_AXIS) {
62  checkPainter();
63  int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
64  if (p0 == getStartOffset() && p1 == getEndOffset()) {
65  return this;
66  }
67  return createFragment(p0, p1);
68  }
69  return this;
70  }
71  };
72  } else if (v instanceof ParagraphView) {
73  return new ParagraphView(e) {
74  @Override
75  protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
76  SizeRequirements requirements = r;
77  if (requirements == null) {
78  requirements = new SizeRequirements();
79  }
80  float pref = layoutPool.getPreferredSpan(axis);
81  float min = layoutPool.getMinimumSpan(axis);
82  // Don't include insets, Box.getXXXSpan will include them.
83  requirements.minimum = (int) min;
84  requirements.preferred = Math.max(requirements.minimum, (int) pref);
85  requirements.maximum = Integer.MAX_VALUE;
86  requirements.alignment = 0.5f;
87  return requirements;
88  }
89  };
90  }
91  return v;
92  }
93  };
94  }
95  };
96 
97  this.setEditorKit(editorKit);
98  }
99 
100  @Override
101  public void setText(String text) {
102  super.setText("<pre>" + EscapeUtil.escapeHtml(text) + "</pre>");
103  }
104 }
static String escapeHtml(String toEscape)
Definition: EscapeUtil.java:75

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.