Changeset 6261 in josm


Ignore:
Timestamp:
2013-09-26T02:11:21+02:00 (8 years ago)
Author:
Don-vip
Message:

Sonar - refactor duplicated code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/JumpToMarkerActions.java

    r5449 r6261  
    3838    }
    3939
    40     public static final class JumpToNextMarker extends AbstractAction implements MultikeyShortcutAction {
     40    private static abstract class JumpToMarker extends AbstractAction implements MultikeyShortcutAction {
    4141
    4242        private final Layer layer;
     43        private final Shortcut multikeyShortcut;
    4344        private WeakReference<Layer> lastLayer;
    44         private Shortcut multikeyShortcut;
    45 
    46         public JumpToNextMarker(JumpToMarkerLayer layer) {
    47             multikeyShortcut = Shortcut.registerShortcut("core_multikey:nextMarker", tr("Multikey: {0}", tr("Next marker")),
    48                     KeyEvent.VK_J, Shortcut.ALT_CTRL);
    49             multikeyShortcut.setAccelerator(this);
    50             putValue(SHORT_DESCRIPTION, tr("Jump to next marker"));
    51             putValue(NAME, tr("Jump to next marker"));
    52 
    53             this.layer = (Layer)layer;
     45       
     46        public JumpToMarker(JumpToMarkerLayer layer, Shortcut shortcut) {
     47            this.layer = (Layer) layer;
     48            this.multikeyShortcut = shortcut;
     49            this.multikeyShortcut.setAccelerator(this);
     50        }
     51       
     52        protected final void setLastLayer(Layer l) {
     53            lastLayer = new WeakReference<Layer>(l);
    5454        }
    5555
     
    7979        }
    8080
    81         private void execute(Layer l) {
    82             ((JumpToMarkerLayer)l).jumpToNextMarker();
    83             lastLayer = new WeakReference<Layer>(l);
    84         }
     81        protected abstract void execute(Layer l);
    8582
    8683        @Override
     
    8885            return LayerListDialog.getLayerInfoByClass(JumpToMarkerLayer.class);
    8986        }
    90 
    91         @Override
    92         public MultikeyInfo getLastMultikeyAction() {
    93             if (lastLayer != null)
    94                 return LayerListDialog.getLayerInfo(lastLayer.get());
    95             else
    96                 return null;
    97         }
    98 
    99     }
    100 
    101     public static final class JumpToPreviousMarker extends AbstractAction implements MultikeyShortcutAction {
    102 
    103         private WeakReference<Layer> lastLayer;
    104         private final Layer layer;
    105         private Shortcut multikeyShortcut;
    106 
    107         public JumpToPreviousMarker(JumpToMarkerLayer layer) {
    108             this.layer = (Layer)layer;
    109 
    110             multikeyShortcut = Shortcut.registerShortcut("core_multikey:previousMarker", tr("Multikey: {0}", tr("Previos marker")),
    111                     KeyEvent.VK_P, Shortcut.ALT_CTRL);
    112             multikeyShortcut.setAccelerator(this);
    113             putValue(SHORT_DESCRIPTION, tr("Jump to previous marker"));
    114             putValue(NAME, tr("Jump to previous marker"));
    115         }
    116 
    117         @Override
    118         public Shortcut getMultikeyShortcut() {
    119             return multikeyShortcut;
    120         }
    121 
    122         @Override
    123         public void actionPerformed(ActionEvent e) {
    124             execute(layer);
    125         }
    126 
    127         @Override
    128         public void executeMultikeyAction(int index, boolean repeat) {
    129             Layer l = LayerListDialog.getLayerForIndex(index);
    130             if (l != null) {
    131                 if (l instanceof JumpToMarkerLayer) {
    132                     execute(l);
    133                 }
    134             } else if (repeat && lastLayer != null) {
    135                 l = lastLayer.get();
    136                 if (LayerListDialog.isLayerValid(l)) {
    137                     execute(l);
    138                 }
    139             }
    140         }
    141 
    142         private void execute(Layer l) {
    143             ((JumpToMarkerLayer) l).jumpToPreviousMarker();
    144             lastLayer = new WeakReference<Layer>(l);
    145         }
    146 
    147         @Override
    148         public List<MultikeyInfo> getMultikeyCombinations() {
    149             return LayerListDialog.getLayerInfoByClass(JumpToMarkerLayer.class);
    150         }
    151 
     87       
    15288        @Override
    15389        public MultikeyInfo getLastMultikeyAction() {
     
    15894        }
    15995    }
     96
     97    public static final class JumpToNextMarker extends JumpToMarker {
     98
     99        public JumpToNextMarker(JumpToMarkerLayer layer) {
     100            super(layer, Shortcut.registerShortcut("core_multikey:nextMarker", tr("Multikey: {0}", tr("Next marker")),
     101                    KeyEvent.VK_J, Shortcut.ALT_CTRL));
     102            putValue(SHORT_DESCRIPTION, tr("Jump to next marker"));
     103            putValue(NAME, tr("Jump to next marker"));
     104        }
     105
     106        @Override
     107        protected void execute(Layer l) {
     108            ((JumpToMarkerLayer)l).jumpToNextMarker();
     109            setLastLayer(l);
     110        }
     111    }
     112
     113    public static final class JumpToPreviousMarker extends JumpToMarker {
     114
     115        public JumpToPreviousMarker(JumpToMarkerLayer layer) {
     116            super(layer, Shortcut.registerShortcut("core_multikey:previousMarker", tr("Multikey: {0}", tr("Previous marker")),
     117                    KeyEvent.VK_P, Shortcut.ALT_CTRL));
     118            putValue(SHORT_DESCRIPTION, tr("Jump to previous marker"));
     119            putValue(NAME, tr("Jump to previous marker"));
     120        }
     121
     122        @Override
     123        protected void execute(Layer l) {
     124            ((JumpToMarkerLayer) l).jumpToPreviousMarker();
     125            setLastLayer(l);
     126        }
     127    }
    160128}
Note: See TracChangeset for help on using the changeset viewer.