setSize(600, 380);
setLocationRelativeTo(SwingUtilities.windowForComponent(properties));
}
private JComponent createTable(final GroupTable parent, final TableModel model) {
final GroupTable table = new GroupTable() {
public Component prepareRenderer(final TableCellRenderer renderer,
final int row, final int column) {
final Component prepareRenderer = super
.prepareRenderer(renderer, row, column);
final TableColumn tableColumn = getColumnModel().getColumn(column);
tableColumn.setPreferredWidth(Math.max(
prepareRenderer.getPreferredSize().width + 20,
tableColumn.getPreferredWidth()));
tableColumn.setPreferredWidth(Math.max(
DEFAULT_COLUMN_WIDTH,
tableColumn.getPreferredWidth()));
return prepareRenderer;
}
};
table.setModel(model);
table.setFont(table.getFont().deriveFont(11f));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// table.getColumn("Key").setMaxWidth(120);
table.setShowVerticalLines(true);
table.setIntercellSpacing(new Dimension(1, 1));
table.setGridColor(Color.lightGray);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
final JTextField editor = new JTextField();
table.setDefaultEditor(Object.class, new DefaultCellEditor(editor) {
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
TableModel tableModel = table.getModel();
if (tableModel instanceof SingleTagFieldModel) {
if (((SingleTagFieldModel) tableModel).isMultiTrackEditMode()) {//.get(row).isMultiple()) {
value = "";
}
}
JTextField c = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
c.setBorder(BorderFactory.createEmptyBorder());
c.setFont(table.getFont());
c.selectAll();
return c;
}
@Override
public void cancelCellEditing() {
super.cancelCellEditing();
}
@Override
protected void fireEditingStopped() {
TableModel tableModel = table.getModel();
if (tableModel instanceof MultiTagFieldModel) {
String value = (String) table.getCellEditor().getCellEditorValue();
if (Util.isEmpty(value) && ((MultiTagFieldModel) tableModel).getTrackInfoItems().get(table.getEditingRow()).isMultiple()) {
super.fireEditingCanceled();
return;
}
}
super.fireEditingStopped();
}
});
table.addKeyboardAction(KeyStroke.getKeyStroke("ENTER"), "startEditing", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
table.editCellAt(table.getSelectedRow(), 1);
editor.requestFocusInWindow();
}
});
table.addKeyboardAction(KeyStroke.getKeyStroke("DELETE"), "clearCell", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
table.getModel().setValueAt("", table.getSelectedRow(), 1);
table.repaint();
}
});
table.addKeyboardAction(KeyStroke.getKeyStroke("ESCAPE"), "exitOrStop", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (table.isEditing()) {
table.getCellEditor().cancelCellEditing();
} else {
cancel.doClick();
}
}
});
editor.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (table.isEditing() && (
e.getKeyCode() == KeyEvent.VK_DOWN ||
e.getKeyCode() == KeyEvent.VK_UP)) {
table.getCellEditor().cancelCellEditing();
}
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
show(e);
}
@Override
public void mousePressed(MouseEvent e) {
show(e);
}
public void show(MouseEvent e) {
if (e.isPopupTrigger()) {
int index = table.rowAtPoint(e.getPoint());
if (index != -1) {
if (!table.isRowSelected(index)) {
table.setRowSelectionInterval(index, index);
}
}
JPopupMenu contextMenu = buildContextMenu(table);
contextMenu.show(e.getComponent(), e.getX(), e.getY());
}