Autopsy  4.21.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-2021 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.event.MouseEvent;
23 import java.util.List;
24 import javax.swing.BorderFactory;
25 import javax.swing.JLabel;
26 import javax.swing.JMenuItem;
27 import javax.swing.JPopupMenu;
28 import javax.swing.JTable;
29 import javax.swing.border.Border;
30 import javax.swing.table.DefaultTableCellRenderer;
31 import org.apache.commons.collections.CollectionUtils;
35 
40 public class CellModelTableCellRenderer extends DefaultTableCellRenderer {
41 
42  private static final long serialVersionUID = 1L;
43  private static final int DEFAULT_ALIGNMENT = JLabel.LEFT;
44  private static final Border DEFAULT_BORDER = BorderFactory.createEmptyBorder(2, 4, 2, 4);
45 
46  @Override
47  public Component getTableCellRendererComponent(JTable table, Object value,
48  boolean isSelected, boolean hasFocus, int row, int column) {
49 
50  JLabel c = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
51  if (value instanceof GuiCellModel) {
52  return getTableCellRendererComponent(c, (GuiCellModel) value);
53  } else {
54  return c;
55  }
56 
57  }
58 
68  protected Component getTableCellRendererComponent(JLabel defaultCell, GuiCellModel cellModel) {
69  defaultCell.setText(cellModel.getText());
70  defaultCell.setToolTipText(cellModel.getTooltip());
71  // sets the JLabel alignment (left, center, right) or default alignment
72  // if no alignment is specified
73  int alignment = (cellModel.getHorizontalAlignment() == null)
74  ? DEFAULT_ALIGNMENT
76  defaultCell.setHorizontalAlignment(alignment);
77  defaultCell.setBorder(DEFAULT_BORDER);
78  return defaultCell;
79  }
80 
86 
87  @Override
88  public void mouseClicked(CellMouseEvent cellEvent) {
89  if (cellEvent.getCellValue() instanceof GuiCellModel && cellEvent.getMouseEvent().getButton() != MouseEvent.BUTTON1) {
90  cellEvent.getTable().setRowSelectionInterval(cellEvent.getRow(), cellEvent.getRow());
91  GuiCellModel cellModel = (GuiCellModel) cellEvent.getCellValue();
92  List<MenuItem> menuItems = cellModel.getPopupMenu();
93 
94  // if there are menu items, show a popup menu for
95  // this item with all the menu items.
96  if (CollectionUtils.isNotEmpty(menuItems)) {
97  final JPopupMenu popupMenu = new JPopupMenu();
98  for (MenuItem mItem : menuItems) {
99  JMenuItem jMenuItem = new JMenuItem(mItem.getTitle());
100  if (mItem.getAction() != null) {
101  jMenuItem.addActionListener((evt) -> mItem.getAction().run());
102  }
103  popupMenu.add(jMenuItem);
104  }
105  popupMenu.show(cellEvent.getTable(), cellEvent.getMouseEvent().getX(), cellEvent.getMouseEvent().getY());
106  }
107  }
108  }
109  };
110 
117  }
118 }
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
Component getTableCellRendererComponent(JLabel defaultCell, GuiCellModel cellModel)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.