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

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