■ ■ ■ ■ ■ ■
src/main/java/com/nccgroup/loggerplusplus/util/userinterface/dialog/TagTable.java
| 1 | + | package com.nccgroup.loggerplusplus.util.userinterface.dialog; |
| 2 | + | |
| 3 | + | import com.nccgroup.loggerplusplus.filter.tag.Tag; |
| 4 | + | import com.nccgroup.loggerplusplus.filterlibrary.FilterLibraryController; |
| 5 | + | import com.nccgroup.loggerplusplus.util.userinterface.renderer.ButtonRenderer; |
| 6 | + | import com.nccgroup.loggerplusplus.util.userinterface.renderer.FilterRenderer; |
| 7 | + | |
| 8 | + | import javax.swing.*; |
| 9 | + | import java.awt.*; |
| 10 | + | import java.awt.event.MouseAdapter; |
| 11 | + | import java.awt.event.MouseEvent; |
| 12 | + | |
| 13 | + | /** |
| 14 | + | * Created by corey on 19/07/17. |
| 15 | + | */ |
| 16 | + | public class TagTable extends JTable { |
| 17 | + | private final FilterLibraryController filterLibraryController; |
| 18 | + | |
| 19 | + | TagTable(FilterLibraryController filterLibraryController) { |
| 20 | + | this.filterLibraryController = filterLibraryController; |
| 21 | + | this.setModel(new TagTableModel(filterLibraryController)); |
| 22 | + | this.setFillsViewportHeight(true); |
| 23 | + | this.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); |
| 24 | + | this.setAutoCreateRowSorter(false); |
| 25 | + | this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 26 | + | this.setRowHeight(25); |
| 27 | + | ((JComponent) this.getDefaultRenderer(Boolean.class)).setOpaque(true); // to remove the white background of the checkboxes! |
| 28 | + | ((JComponent) this.getDefaultRenderer(JButton.class)).setOpaque(true); |
| 29 | + | |
| 30 | + | this.getColumnModel().getColumn(1).setCellRenderer(new FilterRenderer()); |
| 31 | + | this.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer()); |
| 32 | + | |
| 33 | + | |
| 34 | + | this.setDragEnabled(true); |
| 35 | + | this.setDropMode(DropMode.INSERT); |
| 36 | + | |
| 37 | + | int[] minWidths = {100, 250, 100, 100}; |
| 38 | + | for (int i = 0; i < minWidths.length; i++) { |
| 39 | + | this.getColumnModel().getColumn(i).setMinWidth(minWidths[i]); |
| 40 | + | } |
| 41 | + | int[] maxWidths = {9999, 9999, 100, 100}; |
| 42 | + | for (int i = 0; i < maxWidths.length; i++) { |
| 43 | + | this.getColumnModel().getColumn(i).setMaxWidth(maxWidths[i]); |
| 44 | + | } |
| 45 | + | this.setMinimumSize(new Dimension(850, 200)); |
| 46 | + | |
| 47 | + | final JTable _this = this; |
| 48 | + | this.addMouseListener(new MouseAdapter() { |
| 49 | + | @Override |
| 50 | + | public void mouseReleased(MouseEvent mouseEvent) { |
| 51 | + | if (SwingUtilities.isLeftMouseButton(mouseEvent)) { |
| 52 | + | int col = _this.columnAtPoint(mouseEvent.getPoint()); |
| 53 | + | int row = _this.rowAtPoint(mouseEvent.getPoint()); |
| 54 | + | ((TagTableModel) getModel()).onClick(row, col); |
| 55 | + | } |
| 56 | + | } |
| 57 | + | }); |
| 58 | + | } |
| 59 | + | |
| 60 | + | public void moveSelectedUp() { |
| 61 | + | if (this.getSelectedRow() > 0) { |
| 62 | + | ((TagTableModel) this.getModel()).switchRows(this.getSelectedRow(), this.getSelectedRow() - 1); |
| 63 | + | this.getSelectionModel().setSelectionInterval(this.getSelectedRow() - 1, this.getSelectedRow() - 1); |
| 64 | + | Tag filter = ((TagTableModel) this.getModel()).getTagAtRow(this.getSelectedRow()); |
| 65 | + | filterLibraryController.updateTag(filter); |
| 66 | + | } |
| 67 | + | } |
| 68 | + | |
| 69 | + | public void moveSelectedDown() { |
| 70 | + | if (this.getSelectedRow() >= 0 && this.getSelectedRow() < this.getRowCount()) { |
| 71 | + | ((TagTableModel) this.getModel()).switchRows(this.getSelectedRow(), this.getSelectedRow() + 1); |
| 72 | + | this.getSelectionModel().setSelectionInterval(this.getSelectedRow() + 1, this.getSelectedRow() + 1); |
| 73 | + | Tag filter = ((TagTableModel) this.getModel()).getTagAtRow(this.getSelectedRow()); |
| 74 | + | filterLibraryController.updateTag(filter); |
| 75 | + | } |
| 76 | + | } |
| 77 | + | } |
| 78 | + | |