Autopsy  4.13.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
WrapLayout.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.uicomponents;
20 
21 import java.awt.Component;
22 import java.awt.Container;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.Insets;
26 import javax.swing.JScrollPane;
27 import javax.swing.SwingUtilities;
28 
35 public class WrapLayout extends FlowLayout {
36 
37  private static final long serialVersionUID = 1L;
42  public WrapLayout() {
43  super();
44  }
45 
54  public WrapLayout(int align) {
55  super(align);
56  }
57 
70  public WrapLayout(int align, int hgap, int vgap) {
71  super(align, hgap, vgap);
72  }
73 
83  @Override
84  public Dimension preferredLayoutSize(Container target) {
85  return layoutSize(target, true);
86  }
87 
97  @Override
98  public Dimension minimumLayoutSize(Container target) {
99  Dimension minimum = layoutSize(target, false);
100  minimum.width -= (getHgap() + 1);
101  return minimum;
102  }
103 
113  private Dimension layoutSize(Container target, boolean preferred) {
114  synchronized (target.getTreeLock()) {
115  // Each row must fit with the width allocated to the containter.
116  // When the container width = 0, the preferred width of the container
117  // has not yet been calculated so lets ask for the maximum.
118 
119  int targetWidth = target.getSize().width;
120  Container container = target;
121 
122  while (container.getSize().width == 0 && container.getParent() != null) {
123  container = container.getParent();
124  }
125 
126  targetWidth = container.getSize().width;
127 
128  if (targetWidth == 0) {
129  targetWidth = Integer.MAX_VALUE;
130  }
131 
132  int hgap = getHgap();
133  int vgap = getVgap();
134  Insets insets = target.getInsets();
135  int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
136  int maxWidth = targetWidth - horizontalInsetsAndGap;
137 
138  // Fit components into the allowed width
139  Dimension dim = new Dimension(0, 0);
140  int rowWidth = 0;
141  int rowHeight = 0;
142 
143  int nmembers = target.getComponentCount();
144 
145  for (int i = 0; i < nmembers; i++) {
146  Component m = target.getComponent(i);
147 
148  if (m.isVisible()) {
149  Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
150 
151  // Can't add the component to current row. Start a new row.
152  if (rowWidth + d.width > maxWidth) {
153  addRow(dim, rowWidth, rowHeight);
154  rowWidth = 0;
155  rowHeight = 0;
156  }
157 
158  // Add a horizontal gap for all components after the first
159  if (rowWidth != 0) {
160  rowWidth += hgap;
161  }
162 
163  rowWidth += d.width;
164  rowHeight = Math.max(rowHeight, d.height);
165  }
166  }
167 
168  addRow(dim, rowWidth, rowHeight);
169 
170  dim.width += horizontalInsetsAndGap;
171  dim.height += insets.top + insets.bottom + vgap * 2;
172 
173  // When using a scroll pane or the DecoratedLookAndFeel we need to
174  // make sure the preferred size is less than the size of the
175  // target containter so shrinking the container size works
176  // correctly. Removing the horizontal gap is an easy way to do this.
177  Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
178 
179  if (scrollPane != null && target.isValid()) {
180  dim.width -= (hgap + 1);
181  }
182 
183  return dim;
184  }
185  }
186 
187  /*
188  * A new row has been completed. Use the dimensions of this row to
189  * update the preferred size for the container.
190  *
191  * @param dim update the width and height when appropriate @param
192  * rowWidth the width of the row to add @param rowHeight the height of
193  * the row to add
194  */
195  private void addRow(Dimension dim, int rowWidth, int rowHeight) {
196  dim.width = Math.max(dim.width, rowWidth);
197 
198  if (dim.height > 0) {
199  dim.height += getVgap();
200  }
201 
202  dim.height += rowHeight;
203  }
204  }
void addRow(Dimension dim, int rowWidth, int rowHeight)
WrapLayout(int align, int hgap, int vgap)
Definition: WrapLayout.java:70
Dimension preferredLayoutSize(Container target)
Definition: WrapLayout.java:84
Dimension layoutSize(Container target, boolean preferred)
Dimension minimumLayoutSize(Container target)
Definition: WrapLayout.java:98

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