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

Copyright © 2012-2018 Basis Technology. Generated on: Fri Jun 21 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.