Autopsy  3.1
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 
20 package org.sleuthkit.autopsy.casemodule;
21 
28 import java.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import javax.swing.border.*;
32 import javax.swing.table.*;
33 
47 class ButtonColumn extends AbstractCellEditor
48  implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener
49 {
50  private JTable table;
51  private Action action;
52  private int mnemonic;
53  private Border originalBorder;
54  private Border focusBorder;
55 
56  private JButton renderButton;
57  private JButton editButton;
58  private String text;
59  private boolean isButtonColumnEditor;
60 
61  String buttonName;
62 
63 
74  ButtonColumn(JTable table, Action action, int column, String buttonName)
75  {
76  this.table = table;
77  this.action = action;
78  this.buttonName = buttonName;
79 
80  renderButton = new JButton();
81  editButton = new JButton();
82  editButton.setFocusPainted( false );
83  editButton.addActionListener( this );
84  originalBorder = editButton.getBorder();
85  setFocusBorder( new LineBorder(Color.BLUE) );
86 
87  TableColumnModel columnModel = table.getColumnModel();
88  columnModel.getColumn(column).setCellRenderer( this );
89  columnModel.getColumn(column).setCellEditor( this );
90  table.addMouseListener( this );
91  }
92 
93 
99  public Border getFocusBorder()
100  {
101  return focusBorder;
102  }
103 
109  public void setFocusBorder(Border focusBorder)
110  {
111  this.focusBorder = focusBorder;
112  editButton.setBorder( focusBorder );
113  }
114 
115  public int getMnemonic()
116  {
117  return mnemonic;
118  }
119 
125  public void setMnemonic(int mnemonic)
126  {
127  this.mnemonic = mnemonic;
128  renderButton.setMnemonic(mnemonic);
129  editButton.setMnemonic(mnemonic);
130  }
131 
132  @Override
133  public Component getTableCellEditorComponent(
134  JTable table, Object value, boolean isSelected, int row, int column)
135  {
136  text = (value == null) ? "" : value.toString();
137  editButton.setText( text );
138  return editButton;
139  }
140 
141  @Override
142  public Object getCellEditorValue()
143  {
144  return text;
145  }
146 
147 //
148 // Implement TableCellRenderer interface
149 //
150  @Override
151  public Component getTableCellRendererComponent(
152  JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
153  {
154  if (isSelected)
155  {
156  renderButton.setForeground(table.getSelectionForeground());
157  renderButton.setBackground(table.getSelectionBackground());
158  }
159  else
160  {
161  renderButton.setForeground(table.getForeground());
162  renderButton.setBackground(UIManager.getColor("Button.background"));
163  }
164 
165  if (hasFocus)
166  {
167  renderButton.setBorder( focusBorder );
168  }
169  else
170  {
171  renderButton.setBorder( originalBorder );
172  }
173 
174  //renderButton.setText( (value == null) ? "" : value.toString() );
175  renderButton.setText(buttonName);
176  return renderButton;
177  }
178 
179 //
180 // Implement ActionListener interface
181 //
182  /*
183  * The button has been pressed. Stop editing and invoke the custom Action
184  */
185  @Override
186  public void actionPerformed(ActionEvent e)
187  {
188  int row = table.convertRowIndexToModel( table.getEditingRow() );
189  fireEditingStopped();
190 
191  // Invoke the Action
192 
193  ActionEvent event = new ActionEvent(
194  table,
195  ActionEvent.ACTION_PERFORMED,
196  "" + row);
197  action.actionPerformed(event);
198  }
199 
200 //
201 // Implement MouseListener interface
202 //
203  /*
204  * When the mouse is pressed the editor is invoked. If you then then drag
205  * the mouse to another cell before releasing it, the editor is still
206  * active. Make sure editing is stopped when the mouse is released.
207  */
208  @Override
209  public void mousePressed(MouseEvent e)
210  {
211  if (table.isEditing()
212  && table.getCellEditor() == this)
213  isButtonColumnEditor = true;
214  }
215 
216  @Override
217  public void mouseReleased(MouseEvent e)
218  {
219  if (isButtonColumnEditor
220  && table.isEditing())
221  table.getCellEditor().stopCellEditing();
222 
223  isButtonColumnEditor = false;
224  }
225 
226  @Override
227  public void mouseClicked(MouseEvent e) {}
228  @Override
229  public void mouseEntered(MouseEvent e) {}
230  @Override
231  public void mouseExited(MouseEvent e) {}
232 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.