Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
ButtonColumn.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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.casemodule;
20 
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 import javax.swing.border.*;
31 import javax.swing.table.*;
32 
46 class ButtonColumn extends AbstractCellEditor
47  implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener {
48 
49  private JTable table;
50  private Action action;
51  private int mnemonic;
52  private Border originalBorder;
53  private Border focusBorder;
54 
55  private JButton renderButton;
56  private JButton editButton;
57  private String text;
58  private boolean isButtonColumnEditor;
59 
60  String buttonName;
61 
72  ButtonColumn(JTable table, Action action, int column, String buttonName) {
73  this.table = table;
74  this.action = action;
75  this.buttonName = buttonName;
76 
77  renderButton = new JButton();
78  editButton = new JButton();
79  editButton.setFocusPainted(false);
80  editButton.addActionListener(this);
81  originalBorder = editButton.getBorder();
82  setFocusBorder(new LineBorder(Color.BLUE));
83 
84  TableColumnModel columnModel = table.getColumnModel();
85  columnModel.getColumn(column).setCellRenderer(this);
86  columnModel.getColumn(column).setCellEditor(this);
87  table.addMouseListener(this);
88  }
89 
95  public Border getFocusBorder() {
96  return focusBorder;
97  }
98 
104  public void setFocusBorder(Border focusBorder) {
105  this.focusBorder = focusBorder;
106  editButton.setBorder(focusBorder);
107  }
108 
109  public int getMnemonic() {
110  return mnemonic;
111  }
112 
118  public void setMnemonic(int mnemonic) {
119  this.mnemonic = mnemonic;
120  renderButton.setMnemonic(mnemonic);
121  editButton.setMnemonic(mnemonic);
122  }
123 
124  @Override
125  public Component getTableCellEditorComponent(
126  JTable table, Object value, boolean isSelected, int row, int column) {
127  text = (value == null) ? "" : value.toString();
128  editButton.setText(text);
129  return editButton;
130  }
131 
132  @Override
133  public Object getCellEditorValue() {
134  return text;
135  }
136 
137 //
138 // Implement TableCellRenderer interface
139 //
140  @Override
141  public Component getTableCellRendererComponent(
142  JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
143  if (isSelected) {
144  renderButton.setForeground(table.getSelectionForeground());
145  renderButton.setBackground(table.getSelectionBackground());
146  } else {
147  renderButton.setForeground(table.getForeground());
148  renderButton.setBackground(UIManager.getColor("Button.background"));
149  }
150 
151  if (hasFocus) {
152  renderButton.setBorder(focusBorder);
153  } else {
154  renderButton.setBorder(originalBorder);
155  }
156 
157  //renderButton.setText( (value == null) ? "" : value.toString() );
158  renderButton.setText(buttonName);
159  return renderButton;
160  }
161 
162 //
163 // Implement ActionListener interface
164 //
165  /*
166  * The button has been pressed. Stop editing and invoke the custom Action
167  */
168  @Override
169  public void actionPerformed(ActionEvent e) {
170  int row = table.convertRowIndexToModel(table.getEditingRow());
171  fireEditingStopped();
172 
173  // Invoke the Action
174  ActionEvent event = new ActionEvent(
175  table,
176  ActionEvent.ACTION_PERFORMED,
177  "" + row);
178  action.actionPerformed(event);
179  }
180 
181 //
182 // Implement MouseListener interface
183 //
184  /*
185  * When the mouse is pressed the editor is invoked. If you then then drag
186  * the mouse to another cell before releasing it, the editor is still
187  * active. Make sure editing is stopped when the mouse is released.
188  */
189  @Override
190  public void mousePressed(MouseEvent e) {
191  if (table.isEditing()
192  && table.getCellEditor() == this) {
193  isButtonColumnEditor = true;
194  }
195  }
196 
197  @Override
198  public void mouseReleased(MouseEvent e) {
199  if (isButtonColumnEditor
200  && table.isEditing()) {
201  table.getCellEditor().stopCellEditing();
202  }
203 
204  isButtonColumnEditor = false;
205  }
206 
207  @Override
208  public void mouseClicked(MouseEvent e) {
209  }
210 
211  @Override
212  public void mouseEntered(MouseEvent e) {
213  }
214 
215  @Override
216  public void mouseExited(MouseEvent e) {
217  }
218 }

Copyright © 2012-2022 Basis Technology. Generated on: Wed Sep 28 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.