| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package mergeoverlap.hack;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.event.ActionEvent;
|
|---|
| 5 | import java.awt.event.KeyEvent;
|
|---|
| 6 |
|
|---|
| 7 | import javax.swing.AbstractAction;
|
|---|
| 8 | import javax.swing.JComboBox;
|
|---|
| 9 | import javax.swing.JComponent;
|
|---|
| 10 | import javax.swing.JTable;
|
|---|
| 11 | import javax.swing.KeyStroke;
|
|---|
| 12 | import javax.swing.ListSelectionModel;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.gui.conflict.tags.MultiValueCellEditor;
|
|---|
| 15 | import org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictResolverColumnModel;
|
|---|
| 16 |
|
|---|
| 17 | public class MyRelationMemberConflictResolverTable extends JTable implements MultiValueCellEditor.NavigationListener {
|
|---|
| 18 |
|
|---|
| 19 | private SelectNextColumnCellAction selectNextColumnCellAction;
|
|---|
| 20 | private SelectPreviousColumnCellAction selectPreviousColumnCellAction;
|
|---|
| 21 |
|
|---|
| 22 | public MyRelationMemberConflictResolverTable(MyRelationMemberConflictResolverModel model) {
|
|---|
| 23 | super(model, new RelationMemberConflictResolverColumnModel());
|
|---|
| 24 | build();
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | protected void build() {
|
|---|
| 28 | setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
|
|---|
| 29 | setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|---|
| 30 | putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
|---|
| 31 |
|
|---|
| 32 | // make ENTER behave like TAB
|
|---|
| 33 | //
|
|---|
| 34 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
|---|
| 35 | KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "selectNextColumnCell");
|
|---|
| 36 |
|
|---|
| 37 | // install custom navigation actions
|
|---|
| 38 | //
|
|---|
| 39 | selectNextColumnCellAction = new SelectNextColumnCellAction();
|
|---|
| 40 | selectPreviousColumnCellAction = new SelectPreviousColumnCellAction();
|
|---|
| 41 | getActionMap().put("selectNextColumnCell", selectNextColumnCellAction);
|
|---|
| 42 | getActionMap().put("selectPreviousColumnCell", selectPreviousColumnCellAction);
|
|---|
| 43 |
|
|---|
| 44 | setRowHeight((int)new JComboBox<>().getPreferredSize().getHeight());
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Action to be run when the user navigates to the next cell in the table, for instance by
|
|---|
| 49 | * pressing TAB or ENTER. The action alters the standard navigation path from cell to cell: <ul>
|
|---|
| 50 | * <li>it jumps over cells in the first column</li> <li>it automatically add a new empty row
|
|---|
| 51 | * when the user leaves the last cell in the table</li> <ul>
|
|---|
| 52 | *
|
|---|
| 53 | *
|
|---|
| 54 | */
|
|---|
| 55 | class SelectNextColumnCellAction extends AbstractAction {
|
|---|
| 56 | @Override
|
|---|
| 57 | public void actionPerformed(ActionEvent e) {
|
|---|
| 58 | run();
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | public void run() {
|
|---|
| 62 | int col = getSelectedColumn();
|
|---|
| 63 | int row = getSelectedRow();
|
|---|
| 64 | if (getCellEditor() != null) {
|
|---|
| 65 | getCellEditor().stopCellEditing();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | if (col == 2 && row < getRowCount() - 1) {
|
|---|
| 69 | row++;
|
|---|
| 70 | } else if (row < getRowCount() - 1) {
|
|---|
| 71 | col = 2;
|
|---|
| 72 | row++;
|
|---|
| 73 | }
|
|---|
| 74 | changeSelection(row, col, false, false);
|
|---|
| 75 | editCellAt(getSelectedRow(), getSelectedColumn());
|
|---|
| 76 | getEditorComponent().requestFocusInWindow();
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Action to be run when the user navigates to the previous cell in the table, for instance by
|
|---|
| 82 | * pressing Shift-TAB
|
|---|
| 83 | *
|
|---|
| 84 | */
|
|---|
| 85 | class SelectPreviousColumnCellAction extends AbstractAction {
|
|---|
| 86 |
|
|---|
| 87 | @Override
|
|---|
| 88 | public void actionPerformed(ActionEvent e) {
|
|---|
| 89 | run();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | public void run() {
|
|---|
| 93 | int col = getSelectedColumn();
|
|---|
| 94 | int row = getSelectedRow();
|
|---|
| 95 | if (getCellEditor() != null) {
|
|---|
| 96 | getCellEditor().stopCellEditing();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | if (col <= 0 && row <= 0) {
|
|---|
| 100 | // change nothing
|
|---|
| 101 | } else if (row > 0) {
|
|---|
| 102 | col = 2;
|
|---|
| 103 | row--;
|
|---|
| 104 | }
|
|---|
| 105 | changeSelection(row, col, false, false);
|
|---|
| 106 | editCellAt(getSelectedRow(), getSelectedColumn());
|
|---|
| 107 | getEditorComponent().requestFocusInWindow();
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | @Override
|
|---|
| 112 | public void gotoNextDecision() {
|
|---|
| 113 | selectNextColumnCellAction.run();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | @Override
|
|---|
| 117 | public void gotoPreviousDecision() {
|
|---|
| 118 | selectPreviousColumnCellAction.run();
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|