Autopsy  4.17.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 java.awt.event.MouseEvent;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.function.Supplier;
28 import javax.swing.BorderFactory;
29 import javax.swing.JLabel;
30 import javax.swing.JMenuItem;
31 import javax.swing.JPopupMenu;
32 import javax.swing.JTable;
33 import javax.swing.border.Border;
34 import javax.swing.table.DefaultTableCellRenderer;
35 import org.apache.commons.collections.CollectionUtils;
36 import org.apache.commons.lang3.StringUtils;
39 
44 public class CellModelTableCellRenderer extends DefaultTableCellRenderer {
45 
46  private static final long serialVersionUID = 1L;
47 
51  public enum HorizontalAlign {
52  LEFT(JLabel.LEFT),
53  CENTER(JLabel.CENTER),
54  RIGHT(JLabel.RIGHT);
55 
56  private final int jlabelAlignment;
57 
64  HorizontalAlign(int jlabelAlignment) {
65  this.jlabelAlignment = jlabelAlignment;
66  }
67 
73  return this.jlabelAlignment;
74  }
75  }
76 
80  public interface MenuItem {
81 
85  String getTitle();
86 
90  Runnable getAction();
91  }
92 
96  public static class DefaultMenuItem implements MenuItem {
97 
98  private final String title;
99  private final Runnable action;
100 
107  public DefaultMenuItem(String title, Runnable action) {
108  this.title = title;
109  this.action = action;
110  }
111 
112  @Override
113  public String getTitle() {
114  return title;
115  }
116 
117  @Override
118  public Runnable getAction() {
119  return action;
120  }
121 
122  }
123 
127  public interface CellModel {
128 
132  String getText();
133 
137  String getTooltip();
138 
143 
147  Insets getInsets();
148 
153  List<MenuItem> getPopupMenu();
154  }
155 
159  public static class DefaultCellModel implements CellModel {
160 
161  private final String text;
162  private String tooltip;
164  private Insets insets;
165  private List<MenuItem> popupMenu;
166  private Supplier<List<MenuItem>> menuItemSupplier;
167 
173  public DefaultCellModel(String text) {
174  this.text = text;
175  this.tooltip = text;
176  }
177 
178  @Override
179  public String getText() {
180  return text;
181  }
182 
183  @Override
184  public String getTooltip() {
185  return tooltip;
186  }
187 
195  public DefaultCellModel setTooltip(String tooltip) {
196  this.tooltip = tooltip;
197  return this;
198  }
199 
200  @Override
202  return horizontalAlignment;
203  }
204 
213  this.horizontalAlignment = alignment;
214  return this;
215  }
216 
217  @Override
218  public Insets getInsets() {
219  return insets;
220  }
221 
229  public DefaultCellModel setInsets(Insets insets) {
230  this.insets = insets;
231  return this;
232  }
233 
234  @Override
235  public List<MenuItem> getPopupMenu() {
236  if (popupMenu != null) {
237  return Collections.unmodifiableList(popupMenu);
238  }
239 
240  if (menuItemSupplier != null) {
241  return this.menuItemSupplier.get();
242  }
243 
244  return null;
245  }
246 
253  public DefaultCellModel setPopupMenuRetriever(Supplier<List<MenuItem>> menuItemSupplier) {
254  this.menuItemSupplier = menuItemSupplier;
255  return this;
256  }
257 
264  public DefaultCellModel setPopupMenu(List<MenuItem> popupMenu) {
265  this.popupMenu = popupMenu == null ? null : new ArrayList<>(popupMenu);
266  return this;
267  }
268 
269  @Override
270  public String toString() {
271  return getText();
272  }
273  }
274 
275  private static final int DEFAULT_ALIGNMENT = JLabel.LEFT;
276  private static final Border DEFAULT_BORDER = BorderFactory.createEmptyBorder(1, 5, 1, 5);
277 
278  @Override
279  public Component getTableCellRendererComponent(JTable table, Object value,
280  boolean isSelected, boolean hasFocus, int row, int column) {
281 
282  JLabel c = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
283  if (value instanceof CellModel) {
284  return getTableCellRendererComponent(c, (CellModel) value);
285  } else {
286  return c;
287  }
288 
289  }
290 
300  protected Component getTableCellRendererComponent(JLabel defaultCell, CellModel cellModel) {
301  // sets the text for the cell or null if not present.
302  String text = cellModel.getText();
303  if (StringUtils.isNotBlank(text)) {
304  defaultCell.setText(text);
305  } else {
306  defaultCell.setText(null);
307  }
308 
309  // sets the tooltip for the cell if present.
310  String tooltip = cellModel.getTooltip();
311  if (StringUtils.isNotBlank(tooltip)) {
312  defaultCell.setToolTipText(tooltip);
313  } else {
314  defaultCell.setToolTipText(null);
315  }
316 
317  // sets the padding for cell text within the cell.
318  Insets insets = cellModel.getInsets();
319  if (insets != null) {
320  defaultCell.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
321  } else {
322  defaultCell.setBorder(DEFAULT_BORDER);
323  }
324 
325  // sets the JLabel alignment (left, center, right) or default alignment
326  // if no alignment is specified
327  int alignment = (cellModel.getHorizontalAlignment() == null)
328  ? DEFAULT_ALIGNMENT
330  defaultCell.setHorizontalAlignment(alignment);
331 
332  return defaultCell;
333  }
334 
340 
341  @Override
342  public void mouseClicked(CellMouseEvent cellEvent) {
343  if (cellEvent.getCellValue() instanceof CellModel && cellEvent.getMouseEvent().getButton() != MouseEvent.BUTTON1) {
344  cellEvent.getTable().setRowSelectionInterval(cellEvent.getRow(), cellEvent.getRow());
345  CellModel cellModel = (CellModel) cellEvent.getCellValue();
346  List<MenuItem> menuItems = cellModel.getPopupMenu();
347 
348  // if there are menu items, show a popup menu for
349  // this item with all the menu items.
350  if (CollectionUtils.isNotEmpty(menuItems)) {
351  final JPopupMenu popupMenu = new JPopupMenu();
352  for (MenuItem mItem : menuItems) {
353  JMenuItem jMenuItem = new JMenuItem(mItem.getTitle());
354  if (mItem.getAction() != null) {
355  jMenuItem.addActionListener((evt) -> mItem.getAction().run());
356  }
357  popupMenu.add(jMenuItem);
358  }
359  popupMenu.show(cellEvent.getTable(), cellEvent.getMouseEvent().getX(), cellEvent.getMouseEvent().getY());
360  }
361  }
362  }
363  };
364 
371  }
372 }
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

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.