Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CellModelTableCellRenderer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.datasourcesummary.uiutils;
20 
21 import java.awt.Component;
22 import java.awt.Insets;
23 import javax.swing.BorderFactory;
24 import javax.swing.JLabel;
25 import javax.swing.JTable;
26 import javax.swing.border.Border;
27 import javax.swing.table.DefaultTableCellRenderer;
28 import org.apache.commons.lang3.StringUtils;
29 
34 public class CellModelTableCellRenderer extends DefaultTableCellRenderer {
35 
36  private static final long serialVersionUID = 1L;
37 
41  public enum HorizontalAlign {
42  LEFT(JLabel.LEFT),
43  CENTER(JLabel.CENTER),
44  RIGHT(JLabel.RIGHT);
45 
46  private final int jlabelAlignment;
47 
54  HorizontalAlign(int jlabelAlignment) {
55  this.jlabelAlignment = jlabelAlignment;
56  }
57 
63  return this.jlabelAlignment;
64  }
65  }
66 
70  public interface CellModel {
71 
75  String getText();
76 
80  String getTooltip();
81 
86 
90  Insets getInsets();
91  }
92 
96  public static class DefaultCellModel implements CellModel {
97 
98  private final String text;
99  private String tooltip;
101  private Insets insets;
102 
108  public DefaultCellModel(String text) {
109  this.text = text;
110  this.tooltip = text;
111  }
112 
113  @Override
114  public String getText() {
115  return text;
116  }
117 
118  @Override
119  public String getTooltip() {
120  return tooltip;
121  }
122 
130  public DefaultCellModel setTooltip(String tooltip) {
131  this.tooltip = tooltip;
132  return this;
133  }
134 
135  @Override
137  return horizontalAlignment;
138  }
139 
148  this.horizontalAlignment = alignment;
149  return this;
150  }
151 
152  @Override
153  public Insets getInsets() {
154  return insets;
155  }
156 
164  public DefaultCellModel setInsets(Insets insets) {
165  this.insets = insets;
166  return this;
167  }
168 
169  @Override
170  public String toString() {
171  return getText();
172  }
173  }
174 
175  private static final int DEFAULT_ALIGNMENT = JLabel.LEFT;
176  private static final Border DEFAULT_BORDER = BorderFactory.createEmptyBorder(1, 5, 1, 5);
177 
178  @Override
179  public Component getTableCellRendererComponent(JTable table, Object value,
180  boolean isSelected, boolean hasFocus, int row, int column) {
181 
182  JLabel c = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
183  if (value instanceof CellModel) {
184  return getTableCellRendererComponent(c, (CellModel) value);
185  } else {
186  return c;
187  }
188 
189  }
190 
200  protected Component getTableCellRendererComponent(JLabel defaultCell, CellModel cellModel) {
201  // sets the text for the cell or null if not present.
202  String text = cellModel.getText();
203  if (StringUtils.isNotBlank(text)) {
204  defaultCell.setText(text);
205  } else {
206  defaultCell.setText(null);
207  }
208 
209  // sets the tooltip for the cell if present.
210  String tooltip = cellModel.getTooltip();
211  if (StringUtils.isNotBlank(tooltip)) {
212  defaultCell.setToolTipText(tooltip);
213  } else {
214  defaultCell.setToolTipText(null);
215  }
216 
217  // sets the padding for cell text within the cell.
218  Insets insets = cellModel.getInsets();
219  if (insets != null) {
220  defaultCell.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
221  } else {
222  defaultCell.setBorder(DEFAULT_BORDER);
223  }
224 
225  // sets the JLabel alignment (left, center, right) or default alignment
226  // if no alignment is specified
227  int alignment = (cellModel.getHorizontalAlignment() == null)
228  ? DEFAULT_ALIGNMENT
230  defaultCell.setHorizontalAlignment(alignment);
231 
232  return defaultCell;
233  }
234 }
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

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.