Ignore:
Timestamp:
2016-01-17T04:02:53+01:00 (10 years ago)
Author:
Don-vip
Message:

reduce duplicated code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTable.java

    r9376 r9497  
    77import java.awt.Dimension;
    88import java.awt.GraphicsEnvironment;
    9 import java.awt.KeyboardFocusManager;
    109import java.awt.event.ActionEvent;
    11 import java.awt.event.KeyEvent;
    1210import java.util.ArrayList;
    1311import java.util.Arrays;
     
    1715import javax.swing.AbstractAction;
    1816import javax.swing.DropMode;
    19 import javax.swing.JComponent;
    2017import javax.swing.JPopupMenu;
    2118import javax.swing.JTable;
    2219import javax.swing.JViewport;
    23 import javax.swing.KeyStroke;
    2420import javax.swing.ListSelectionModel;
    2521import javax.swing.SwingUtilities;
     
    6157        setLayer(layer);
    6258        model.addMemberModelListener(this);
    63         init();
    64     }
    65 
    66     /**
    67      * initialize the table
    68      */
    69     protected void init() {
     59
    7060        MemberRoleCellEditor ce = (MemberRoleCellEditor) getColumnModel().getColumn(0).getCellEditor();
    7161        setRowHeight(ce.getEditor().getPreferredSize().height);
     
    7464        putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    7565
    76         // make ENTER behave like TAB
    77         //
    78         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
    79                 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "selectNextColumnCell");
    80 
     66        installCustomNavigation(0);
    8167        initHighlighting();
    82 
    83         // install custom navigation actions
    84         //
    85         getActionMap().put("selectNextColumnCell", new SelectNextColumnCellAction());
    86         getActionMap().put("selectPreviousColumnCell", new SelectPreviousColumnCellAction());
    8768
    8869        if (!GraphicsEnvironment.isHeadless()) {
     
    164145    }
    165146
    166     /**
    167      * Action to be run when the user navigates to the next cell in the table, for instance by
    168      * pressing TAB or ENTER. The action alters the standard navigation path from cell to cell: <ul>
    169      * <li>it jumps over cells in the first column</li> <li>it automatically add a new empty row
    170      * when the user leaves the last cell in the table</li></ul>
    171      */
    172     class SelectNextColumnCellAction extends AbstractAction {
    173         @Override
    174         public void actionPerformed(ActionEvent e) {
    175             run();
    176         }
    177 
    178         public void run() {
    179             int col = getSelectedColumn();
    180             int row = getSelectedRow();
    181             if (getCellEditor() != null) {
    182                 getCellEditor().stopCellEditing();
    183             }
    184 
    185             if (col == 0 && row < getRowCount() - 1) {
    186                 row++;
    187             } else if (row < getRowCount() - 1) {
    188                 col = 0;
    189                 row++;
    190             } else {
    191                 // go to next component, no more rows in this table
    192                 KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    193                 manager.focusNextComponent();
    194                 return;
    195             }
    196             changeSelection(row, col, false, false);
    197         }
    198     }
    199 
    200     /**
    201      * Action to be run when the user navigates to the previous cell in the table, for instance by
    202      * pressing Shift-TAB
    203      */
    204     private class SelectPreviousColumnCellAction extends AbstractAction {
    205 
    206         @Override
    207         public void actionPerformed(ActionEvent e) {
    208             int col = getSelectedColumn();
    209             int row = getSelectedRow();
    210             if (getCellEditor() != null) {
    211                 getCellEditor().stopCellEditing();
    212             }
    213 
    214             if (col <= 0 && row <= 0) {
    215                 // change nothing
    216             } else if (row > 0) {
    217                 col = 0;
    218                 row--;
    219             }
    220             changeSelection(row, col, false, false);
    221         }
    222     }
    223 
    224147    @Override
    225148    public void unlinkAsListener() {
Note: See TracChangeset for help on using the changeset viewer.