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

reduce duplicated code

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/conflict/tags/TagConflictResolverTable.java

    r8510 r9497  
    22package org.openstreetmap.josm.gui.conflict.tags;
    33
    4 import java.awt.event.ActionEvent;
    5 import java.awt.event.KeyEvent;
    6 
    7 import javax.swing.AbstractAction;
    8 import javax.swing.JComponent;
    94import javax.swing.JTable;
    10 import javax.swing.KeyStroke;
    115import javax.swing.ListSelectionModel;
    126
    137import org.openstreetmap.josm.gui.widgets.JosmComboBox;
     8import org.openstreetmap.josm.gui.widgets.JosmTable;
    149
    15 public class TagConflictResolverTable extends JTable implements MultiValueCellEditor.NavigationListener {
     10public class TagConflictResolverTable extends JosmTable implements MultiValueCellEditor.NavigationListener {
    1611
    17     private SelectNextColumnCellAction selectNextColumnCellAction;
    18     private SelectPreviousColumnCellAction selectPreviousColumnCellAction;
    19 
     12    /**
     13     * Constructs a new {@code TagConflictResolverTable}.
     14     * @param model table model
     15     */
    2016    public TagConflictResolverTable(TagConflictResolverModel model) {
    2117        super(model, new TagConflictResolverColumnModel());
    22         build();
    23     }
    2418
    25     protected final void build() {
    2619        setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    2720        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    2821        putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    2922
    30         // make ENTER behave like TAB
    31         //
    32         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
    33                 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "selectNextColumnCell");
    34 
    35         // install custom navigation actions
    36         //
    37         selectNextColumnCellAction = new SelectNextColumnCellAction();
    38         selectPreviousColumnCellAction = new SelectPreviousColumnCellAction();
    39         getActionMap().put("selectNextColumnCell", selectNextColumnCellAction);
    40         getActionMap().put("selectPreviousColumnCell", selectPreviousColumnCellAction);
     23        installCustomNavigation(2);
    4124
    4225        ((MultiValueCellEditor) getColumnModel().getColumn(2).getCellEditor()).addNavigationListener(this);
     
    4528    }
    4629
    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             if (editCellAt(getSelectedRow(), getSelectedColumn())) {
    76                 getEditorComponent().requestFocusInWindow();
    77             }
    78         }
    79     }
    80 
    81     /**
    82      * Action to be run when the user navigates to the previous cell in the table, for instance by
    83      * pressing Shift-TAB
    84      *
    85      */
    86     class SelectPreviousColumnCellAction extends AbstractAction {
    87 
    88         @Override
    89         public void actionPerformed(ActionEvent e) {
    90             run();
    91         }
    92 
    93         public void run() {
    94             int col = getSelectedColumn();
    95             int row = getSelectedRow();
    96             if (getCellEditor() != null) {
    97                 getCellEditor().stopCellEditing();
    98             }
    99 
    100             if (col <= 0 && row <= 0) {
    101                 // change nothing
    102             } else if (row > 0) {
    103                 col = 2;
    104                 row--;
    105             }
    106             changeSelection(row, col, false, false);
    107             if (editCellAt(getSelectedRow(), getSelectedColumn())) {
    108                 getEditorComponent().requestFocusInWindow();
    109             }
    110         }
    111     }
    112 
    11330    @Override
    11431    public void gotoNextDecision() {
    115         selectNextColumnCellAction.run();
     32        selectNextColumnCellAction.actionPerformed(null);
    11633    }
    11734
    11835    @Override
    11936    public void gotoPreviousDecision() {
    120         selectPreviousColumnCellAction.run();
     37        selectPreviousColumnCellAction.actionPerformed(null);
    12138    }
    12239}
  • 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() {
  • trunk/src/org/openstreetmap/josm/gui/widgets/OsmPrimitivesTable.java

    r8308 r9497  
    66
    77import javax.swing.JPopupMenu;
    8 import javax.swing.JTable;
    98import javax.swing.ListSelectionModel;
    109import javax.swing.SwingUtilities;
     
    1615import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1716
    18 public abstract class OsmPrimitivesTable extends JTable {
     17/**
     18 * Table displaying OSM primitives.
     19 * @since 5297
     20 */
     21public abstract class OsmPrimitivesTable extends JosmTable {
    1922
    2023    /**
     
    2730    private ZoomToAction zoomToAction;
    2831
    29     public final OsmDataLayer getLayer() {
    30         return layer;
    31     }
    32 
    33     public final void setLayer(OsmDataLayer layer) {
    34         this.layer = layer;
    35     }
    36 
     32    /**
     33     * Constructs a new {@code OsmPrimitivesTable}.
     34     * @param dm table model
     35     * @param cm column model
     36     * @param sm selection model
     37     */
    3738    public OsmPrimitivesTable(OsmPrimitivesTableModel dm, TableColumnModel cm, ListSelectionModel sm) {
    3839        super(dm, cm, sm);
     
    4142    }
    4243
     44    /**
     45     * Returns the table model.
     46     * @return the table model
     47     */
    4348    public OsmPrimitivesTableModel getOsmPrimitivesTableModel() {
    4449        return (OsmPrimitivesTableModel) getModel();
     50    }
     51
     52    /**
     53     * Returns the data layer.
     54     * @return the data layer
     55     */
     56    public final OsmDataLayer getLayer() {
     57        return layer;
     58    }
     59
     60    /**
     61     * Sets the data layer.
     62     * @param layer the data layer
     63     */
     64    public final void setLayer(OsmDataLayer layer) {
     65        this.layer = layer;
    4566    }
    4667
     
    7293    }
    7394
     95    /**
     96     * Returns primitive at the specified row.
     97     * @param row table row
     98     * @param layer unused in this implementation, can be useful for subclasses
     99     * @return primitive at the specified row
     100     */
    74101    public OsmPrimitive getPrimitiveInLayer(int row, OsmDataLayer layer) {
    75102        return getOsmPrimitivesTableModel().getReferredPrimitive(row);
Note: See TracChangeset for help on using the changeset viewer.