Ignore:
Timestamp:
2017-12-31T03:09:43+01:00 (6 years ago)
Author:
Don-vip
Message:

see #15709 - fix a lot of memory leaks. Now gui.layer.geoImage.* classes are correctly garbage collected when the mapframe is destroyed

File:
1 edited

Legend:

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

    r13264 r13265  
    1515import java.text.SimpleDateFormat;
    1616
    17 import javax.swing.AbstractAction;
    1817import javax.swing.Box;
    1918import javax.swing.JButton;
    20 import javax.swing.JComponent;
    2119import javax.swing.JPanel;
    2220import javax.swing.JToggleButton;
    2321
     22import org.openstreetmap.josm.actions.JosmAction;
    2423import org.openstreetmap.josm.gui.MainApplication;
    2524import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
     
    4140public final class ImageViewerDialog extends ToggleDialog implements LayerChangeListener, ActiveLayerChangeListener {
    4241
    43     private static final String COMMAND_ZOOM = "zoom";
    44     private static final String COMMAND_CENTERVIEW = "centre";
    45     private static final String COMMAND_NEXT = "next";
    46     private static final String COMMAND_REMOVE = "remove";
    47     private static final String COMMAND_REMOVE_FROM_DISK = "removefromdisk";
    48     private static final String COMMAND_PREVIOUS = "previous";
    49     private static final String COMMAND_COLLAPSE = "collapse";
    50     private static final String COMMAND_FIRST = "first";
    51     private static final String COMMAND_LAST = "last";
    52     private static final String COMMAND_COPY_PATH = "copypath";
     42    private final ImageZoomAction imageZoomAction = new ImageZoomAction();
     43    private final ImageCenterViewAction imageCenterViewAction = new ImageCenterViewAction();
     44    private final ImageNextAction imageNextAction = new ImageNextAction();
     45    private final ImageRemoveAction imageRemoveAction = new ImageRemoveAction();
     46    private final ImageRemoveFromDiskAction imageRemoveFromDiskAction = new ImageRemoveFromDiskAction();
     47    private final ImagePreviousAction imagePreviousAction = new ImagePreviousAction();
     48    private final ImageCollapseAction imageCollapseAction = new ImageCollapseAction();
     49    private final ImageFirstAction imageFirstAction = new ImageFirstAction();
     50    private final ImageLastAction imageLastAction = new ImageLastAction();
     51    private final ImageCopyPathAction imageCopyPathAction = new ImageCopyPathAction();
    5352
    5453    private final ImageDisplay imgDisplay = new ImageDisplay();
     
    9089        Dimension buttonDim = new Dimension(26, 26);
    9190
    92         ImageAction prevAction = new ImageAction(COMMAND_PREVIOUS, new ImageProvider("dialogs", "previous"), tr("Previous"));
    93         btnPrevious = new JButton(prevAction);
     91        btnPrevious = new JButton(imagePreviousAction);
    9492        btnPrevious.setPreferredSize(buttonDim);
    95         Shortcut scPrev = Shortcut.registerShortcut(
    96                 "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.DIRECT);
    97         final String previousImage = "Previous Image";
    98         MainApplication.registerActionShortcut(prevAction, scPrev);
    99         btnPrevious.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), previousImage);
    100         btnPrevious.getActionMap().put(previousImage, prevAction);
    10193        btnPrevious.setEnabled(false);
    10294
    103         final String removePhoto = tr("Remove photo from layer");
    104         ImageAction delAction = new ImageAction(COMMAND_REMOVE, new ImageProvider("dialogs", "delete"), removePhoto);
    105         JButton btnDelete = new JButton(delAction);
     95        JButton btnDelete = new JButton(imageRemoveAction);
    10696        btnDelete.setPreferredSize(buttonDim);
    107         Shortcut scDelete = Shortcut.registerShortcut(
    108                 "geoimage:deleteimagefromlayer", tr("Geoimage: {0}", tr("Remove photo from layer")), KeyEvent.VK_DELETE, Shortcut.SHIFT);
    109         MainApplication.registerActionShortcut(delAction, scDelete);
    110         btnDelete.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDelete.getKeyStroke(), removePhoto);
    111         btnDelete.getActionMap().put(removePhoto, delAction);
    112 
    113         ImageAction delFromDiskAction = new ImageAction(COMMAND_REMOVE_FROM_DISK,
    114                 new ImageProvider("dialogs", "geoimage/deletefromdisk"), tr("Delete image file from disk"));
    115         JButton btnDeleteFromDisk = new JButton(delFromDiskAction);
     97
     98        JButton btnDeleteFromDisk = new JButton(imageRemoveFromDiskAction);
    11699        btnDeleteFromDisk.setPreferredSize(buttonDim);
    117         Shortcut scDeleteFromDisk = Shortcut.registerShortcut(
    118                 "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.CTRL_SHIFT);
    119         final String deleteImage = "Delete image file from disk";
    120         MainApplication.registerActionShortcut(delFromDiskAction, scDeleteFromDisk);
    121         btnDeleteFromDisk.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), deleteImage);
    122         btnDeleteFromDisk.getActionMap().put(deleteImage, delFromDiskAction);
    123 
    124         ImageAction copyPathAction = new ImageAction(COMMAND_COPY_PATH, new ImageProvider("copy"), tr("Copy image path"));
    125         JButton btnCopyPath = new JButton(copyPathAction);
     100
     101        JButton btnCopyPath = new JButton(imageCopyPathAction);
    126102        btnCopyPath.setPreferredSize(buttonDim);
    127         Shortcut scCopyPath = Shortcut.registerShortcut(
    128                 "geoimage:copypath", tr("Geoimage: {0}", tr("Copy image path")), KeyEvent.VK_C, Shortcut.ALT_CTRL_SHIFT);
    129         final String copyImage = "Copy image path";
    130         MainApplication.registerActionShortcut(copyPathAction, scCopyPath);
    131         btnCopyPath.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scCopyPath.getKeyStroke(), copyImage);
    132         btnCopyPath.getActionMap().put(copyImage, copyPathAction);
    133 
    134         ImageAction nextAction = new ImageAction(COMMAND_NEXT, new ImageProvider("dialogs", "next"), tr("Next"));
    135         btnNext = new JButton(nextAction);
     103
     104        btnNext = new JButton(imageNextAction);
    136105        btnNext.setPreferredSize(buttonDim);
    137         Shortcut scNext = Shortcut.registerShortcut(
    138                 "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.DIRECT);
    139         final String nextImage = "Next Image";
    140         MainApplication.registerActionShortcut(nextAction, scNext);
    141         btnNext.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), nextImage);
    142         btnNext.getActionMap().put(nextImage, nextAction);
    143106        btnNext.setEnabled(false);
    144107
    145         MainApplication.registerActionShortcut(
    146                 new ImageAction(COMMAND_FIRST, null, null),
    147                 Shortcut.registerShortcut(
    148                         "geoimage:first", tr("Geoimage: {0}", tr("Show first Image")), KeyEvent.VK_HOME, Shortcut.DIRECT)
    149         );
    150         MainApplication.registerActionShortcut(
    151                 new ImageAction(COMMAND_LAST, null, null),
    152                 Shortcut.registerShortcut(
    153                         "geoimage:last", tr("Geoimage: {0}", tr("Show last Image")), KeyEvent.VK_END, Shortcut.DIRECT)
    154         );
    155 
    156         tbCentre = new JToggleButton(new ImageAction(COMMAND_CENTERVIEW,
    157                 new ImageProvider("dialogs", "centreview"), tr("Center view")));
     108        tbCentre = new JToggleButton(imageCenterViewAction);
    158109        tbCentre.setPreferredSize(buttonDim);
    159110
    160         JButton btnZoomBestFit = new JButton(new ImageAction(COMMAND_ZOOM,
    161                 new ImageProvider("dialogs", "zoom-best-fit"), tr("Zoom best fit and 1:1")));
     111        JButton btnZoomBestFit = new JButton(imageZoomAction);
    162112        btnZoomBestFit.setPreferredSize(buttonDim);
    163113
    164         btnCollapse = new JButton(new ImageAction(COMMAND_COLLAPSE,
    165                 new ImageProvider("dialogs", "collapse"), tr("Move dialog to the side pane")));
     114        btnCollapse = new JButton(imageCollapseAction);
    166115        btnCollapse.setPreferredSize(new Dimension(20, 20));
    167116        btnCollapse.setAlignmentY(Component.TOP_ALIGNMENT);
     
    202151        MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
    203152        MainApplication.getLayerManager().removeLayerChangeListener(this);
     153        // Manually destroy actions until JButtons are replaced by standard SideButtons
     154        imageFirstAction.destroy();
     155        imageLastAction.destroy();
     156        imagePreviousAction.destroy();
     157        imageNextAction.destroy();
     158        imageCenterViewAction.destroy();
     159        imageCollapseAction.destroy();
     160        imageCopyPathAction.destroy();
     161        imageRemoveAction.destroy();
     162        imageRemoveFromDiskAction.destroy();
     163        imageZoomAction.destroy();
    204164        super.destroy();
    205     }
    206 
    207     class ImageAction extends AbstractAction {
    208         private final String action;
    209 
    210         ImageAction(String action, ImageProvider provider, String toolTipText) {
    211             this.action = action;
    212             putValue(SHORT_DESCRIPTION, toolTipText);
    213             if (provider != null) {
    214                 provider.getResource().attachImageIcon(this, true);
    215             }
    216         }
    217 
    218         @Override
    219         public void actionPerformed(ActionEvent e) {
    220             if (COMMAND_NEXT.equals(action)) {
    221                 if (currentLayer != null) {
    222                     currentLayer.showNextPhoto();
    223                 }
    224             } else if (COMMAND_PREVIOUS.equals(action)) {
    225                 if (currentLayer != null) {
    226                     currentLayer.showPreviousPhoto();
    227                 }
    228             } else if (COMMAND_FIRST.equals(action) && currentLayer != null) {
     165        dialog = null;
     166    }
     167
     168    private class ImageNextAction extends JosmAction {
     169        ImageNextAction() {
     170            super(null, new ImageProvider("dialogs", "next"), tr("Next"), Shortcut.registerShortcut(
     171                    "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.DIRECT),
     172                  false, null, false);
     173        }
     174
     175        @Override
     176        public void actionPerformed(ActionEvent e) {
     177            if (currentLayer != null) {
     178                currentLayer.showNextPhoto();
     179            }
     180        }
     181    }
     182
     183    private class ImagePreviousAction extends JosmAction {
     184        ImagePreviousAction() {
     185            super(null, new ImageProvider("dialogs", "previous"), tr("Previous"), Shortcut.registerShortcut(
     186                    "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.DIRECT),
     187                  false, null, false);
     188        }
     189
     190        @Override
     191        public void actionPerformed(ActionEvent e) {
     192            if (currentLayer != null) {
     193                currentLayer.showPreviousPhoto();
     194            }
     195        }
     196    }
     197
     198    private class ImageFirstAction extends JosmAction {
     199        ImageFirstAction() {
     200            super(null, (ImageProvider) null, null, Shortcut.registerShortcut(
     201                    "geoimage:first", tr("Geoimage: {0}", tr("Show first Image")), KeyEvent.VK_HOME, Shortcut.DIRECT),
     202                  false, null, false);
     203        }
     204
     205        @Override
     206        public void actionPerformed(ActionEvent e) {
     207            if (currentLayer != null) {
    229208                currentLayer.showFirstPhoto();
    230             } else if (COMMAND_LAST.equals(action) && currentLayer != null) {
     209            }
     210        }
     211    }
     212
     213    private class ImageLastAction extends JosmAction {
     214        ImageLastAction() {
     215            super(null, (ImageProvider) null, null, Shortcut.registerShortcut(
     216                    "geoimage:last", tr("Geoimage: {0}", tr("Show last Image")), KeyEvent.VK_END, Shortcut.DIRECT),
     217                  false, null, false);
     218        }
     219
     220        @Override
     221        public void actionPerformed(ActionEvent e) {
     222            if (currentLayer != null) {
    231223                currentLayer.showLastPhoto();
    232             } else if (COMMAND_CENTERVIEW.equals(action)) {
    233                 final JToggleButton button = (JToggleButton) e.getSource();
    234                 centerView = button.isEnabled() && button.isSelected();
    235                 if (centerView && currentEntry != null && currentEntry.getPos() != null) {
    236                     MainApplication.getMap().mapView.zoomTo(currentEntry.getPos());
    237                 }
    238             } else if (COMMAND_ZOOM.equals(action)) {
    239                 imgDisplay.zoomBestFitOrOne();
    240             } else if (COMMAND_REMOVE.equals(action)) {
    241                 if (currentLayer != null) {
    242                     currentLayer.removeCurrentPhoto();
    243                 }
    244             } else if (COMMAND_REMOVE_FROM_DISK.equals(action)) {
    245                 if (currentLayer != null) {
    246                     currentLayer.removeCurrentPhotoFromDisk();
    247                 }
    248             } else if (COMMAND_COPY_PATH.equals(action)) {
    249                 if (currentLayer != null) {
    250                     currentLayer.copyCurrentPhotoPath();
    251                 }
    252             } else if (COMMAND_COLLAPSE.equals(action)) {
    253                 collapseButtonClicked = true;
    254                 detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING));
    255             }
     224            }
     225        }
     226    }
     227
     228    private class ImageCenterViewAction extends JosmAction {
     229        ImageCenterViewAction() {
     230            super(null, new ImageProvider("dialogs", "centreview"), tr("Center view"), null,
     231                  false, null, false);
     232        }
     233
     234        @Override
     235        public void actionPerformed(ActionEvent e) {
     236            final JToggleButton button = (JToggleButton) e.getSource();
     237            centerView = button.isEnabled() && button.isSelected();
     238            if (centerView && currentEntry != null && currentEntry.getPos() != null) {
     239                MainApplication.getMap().mapView.zoomTo(currentEntry.getPos());
     240            }
     241        }
     242    }
     243
     244    private class ImageZoomAction extends JosmAction {
     245        ImageZoomAction() {
     246            super(null, new ImageProvider("dialogs", "zoom-best-fit"), tr("Zoom best fit and 1:1"), null,
     247                  false, null, false);
     248        }
     249
     250        @Override
     251        public void actionPerformed(ActionEvent e) {
     252            imgDisplay.zoomBestFitOrOne();
     253        }
     254    }
     255
     256    private class ImageRemoveAction extends JosmAction {
     257        ImageRemoveAction() {
     258            super(null, new ImageProvider("dialogs", "delete"), tr("Remove photo from layer"), Shortcut.registerShortcut(
     259                    "geoimage:deleteimagefromlayer", tr("Geoimage: {0}", tr("Remove photo from layer")), KeyEvent.VK_DELETE, Shortcut.SHIFT),
     260                  false, null, false);
     261        }
     262
     263        @Override
     264        public void actionPerformed(ActionEvent e) {
     265            if (currentLayer != null) {
     266                currentLayer.removeCurrentPhoto();
     267            }
     268        }
     269    }
     270
     271    private class ImageRemoveFromDiskAction extends JosmAction {
     272        ImageRemoveFromDiskAction() {
     273            super(null, new ImageProvider("dialogs", "geoimage/deletefromdisk"), tr("Delete image file from disk"),
     274                  Shortcut.registerShortcut(
     275                    "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.CTRL_SHIFT),
     276                  false, null, false);
     277        }
     278
     279        @Override
     280        public void actionPerformed(ActionEvent e) {
     281            if (currentLayer != null) {
     282                currentLayer.removeCurrentPhotoFromDisk();
     283            }
     284        }
     285    }
     286
     287    private class ImageCopyPathAction extends JosmAction {
     288        ImageCopyPathAction() {
     289            super(null, new ImageProvider("copy"), tr("Copy image path"), Shortcut.registerShortcut(
     290                    "geoimage:copypath", tr("Geoimage: {0}", tr("Copy image path")), KeyEvent.VK_C, Shortcut.ALT_CTRL_SHIFT),
     291                  false, null, false);
     292        }
     293
     294        @Override
     295        public void actionPerformed(ActionEvent e) {
     296            if (currentLayer != null) {
     297                currentLayer.copyCurrentPhotoPath();
     298            }
     299        }
     300    }
     301
     302    private class ImageCollapseAction extends JosmAction {
     303        ImageCollapseAction() {
     304            super(null, new ImageProvider("dialogs", "collapse"), tr("Move dialog to the side pane"), null,
     305                  false, null, false);
     306        }
     307
     308        @Override
     309        public void actionPerformed(ActionEvent e) {
     310            collapseButtonClicked = true;
     311            detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING));
    256312        }
    257313    }
     
    455511        }
    456512    }
    457 
    458513}
Note: See TracChangeset for help on using the changeset viewer.