Changeset 9916 in josm for trunk/src


Ignore:
Timestamp:
2016-03-02T22:54:34+01:00 (9 years ago)
Author:
Don-vip
Message:

ChangesetCacheManager: add unit tests

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManager.java

    r9526 r9916  
    99import java.awt.Dimension;
    1010import java.awt.FlowLayout;
     11import java.awt.GraphicsEnvironment;
     12import java.awt.Window;
    1113import java.awt.event.ActionEvent;
    1214import java.awt.event.KeyEvent;
     
    4850import org.openstreetmap.josm.gui.help.HelpUtil;
    4951import org.openstreetmap.josm.gui.io.CloseChangesetTask;
     52import org.openstreetmap.josm.gui.util.GuiHelper;
    5053import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    5154import org.openstreetmap.josm.io.ChangesetQuery;
     
    6063 * downloading, querying, closing changesets, in addition to removing changesets from
    6164 * the local cache.
    62  *
     65 * @since 2689
    6366 */
    6467public class ChangesetCacheManager extends JFrame {
     
    8689
    8790    /**
    88      * Hides and destroys the unique instance of the changeset cache
    89      * manager.
     91     * Hides and destroys the unique instance of the changeset cache manager.
    9092     *
    9193     */
     
    109111
    110112    /**
    111      * Creates the various models required
    112      */
    113     protected void buildModel() {
     113     * Creates the various models required.
     114     * @return the changeset cache model
     115     */
     116    static ChangesetCacheManagerModel buildModel() {
    114117        DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
    115118        selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    116         model = new ChangesetCacheManagerModel(selectionModel);
    117 
    118         actRemoveFromCacheAction = new RemoveFromCacheAction();
    119         actCloseSelectedChangesetsAction = new CloseSelectedChangesetsAction();
    120         actDownloadSelectedChangesets = new DownloadSelectedChangesetsAction();
    121         actDownloadSelectedContent = new DownloadSelectedChangesetContentAction();
     119        return new ChangesetCacheManagerModel(selectionModel);
    122120    }
    123121
     
    127125     * @return the toolbar panel
    128126     */
    129     protected JPanel buildToolbarPanel() {
     127    static JPanel buildToolbarPanel() {
    130128        JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
    131129
     
    143141     * @return the button row pane
    144142     */
    145     protected JPanel buildButtonPanel() {
     143    static JPanel buildButtonPanel() {
    146144        JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
    147145
     
    150148
    151149        //-- help action
    152         pnl.add(new SideButton(
    153                 new ContextSensitiveHelpAction(
    154                         HelpUtil.ht("/Dialog/ChangesetManager"))
    155         )
    156         );
     150        pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/ChangesetManager"))));
    157151
    158152        return pnl;
     
    266260        tblChangesets.addMouseListener(new MouseEventHandler());
    267261        tblChangesets.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "showDetails");
    268         tblChangesets.getActionMap().put("showDetails", new ShowDetailAction());
    269         model.getSelectionModel().addListSelectionListener(new ChangesetDetailViewSynchronizer());
     262        tblChangesets.getActionMap().put("showDetails", new ShowDetailAction(model));
     263        model.getSelectionModel().addListSelectionListener(new ChangesetDetailViewSynchronizer(model));
    270264
    271265        // activate DEL on the table
     
    285279        cp.setLayout(new BorderLayout());
    286280
    287         buildModel();
     281        model = buildModel();
     282        actRemoveFromCacheAction = new RemoveFromCacheAction(model);
     283        actCloseSelectedChangesetsAction = new CloseSelectedChangesetsAction(model);
     284        actDownloadSelectedChangesets = new DownloadSelectedChangesetsAction(model);
     285        actDownloadSelectedContent = new DownloadSelectedChangesetContentAction(model);
     286
    288287        cp.add(buildToolbarPanel(), BorderLayout.NORTH);
    289288        cp.add(buildContentPanel(), BorderLayout.CENTER);
     
    340339
    341340        @Override
    342         public void windowActivated(WindowEvent arg0) {
     341        public void windowActivated(WindowEvent e) {
    343342            if (needsSplitPaneAdjustment) {
    344343                spContent.setDividerLocation(0.5);
     
    363362
    364363        @Override
    365         public void actionPerformed(ActionEvent arg0) {
     364        public void actionPerformed(ActionEvent e) {
    366365            cancelAndClose();
    367366        }
     
    371370     * The action to query and download changesets
    372371     */
    373     class QueryAction extends AbstractAction {
     372    static class QueryAction extends AbstractAction {
     373
    374374        QueryAction() {
    375375            putValue(NAME, tr("Query"));
     
    381381        @Override
    382382        public void actionPerformed(ActionEvent evt) {
    383             ChangesetQueryDialog dialog = new ChangesetQueryDialog(ChangesetCacheManager.this);
    384             dialog.initForUserInput();
    385             dialog.setVisible(true);
    386             if (dialog.isCanceled())
    387                 return;
    388 
    389             try {
    390                 ChangesetQuery query = dialog.getChangesetQuery();
    391                 if (query == null) return;
    392                 ChangesetQueryTask task = new ChangesetQueryTask(ChangesetCacheManager.this, query);
    393                 ChangesetCacheManager.getInstance().runDownloadTask(task);
    394             } catch (IllegalStateException e) {
    395                 JOptionPane.showMessageDialog(ChangesetCacheManager.this, e.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE);
     383            Window parent = GuiHelper.getWindowAncestorFor(evt);
     384            if (!GraphicsEnvironment.isHeadless()) {
     385                ChangesetQueryDialog dialog = new ChangesetQueryDialog(parent);
     386                dialog.initForUserInput();
     387                dialog.setVisible(true);
     388                if (dialog.isCanceled())
     389                    return;
     390
     391                try {
     392                    ChangesetQuery query = dialog.getChangesetQuery();
     393                    if (query != null) {
     394                        ChangesetCacheManager.getInstance().runDownloadTask(new ChangesetQueryTask(parent, query));
     395                    }
     396                } catch (IllegalStateException e) {
     397                    JOptionPane.showMessageDialog(parent, e.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE);
     398                }
    396399            }
    397400        }
     
    402405     *
    403406     */
    404     class RemoveFromCacheAction extends AbstractAction implements ListSelectionListener {
    405         RemoveFromCacheAction() {
     407    static class RemoveFromCacheAction extends AbstractAction implements ListSelectionListener {
     408        private final ChangesetCacheManagerModel model;
     409
     410        RemoveFromCacheAction(ChangesetCacheManagerModel model) {
    406411            putValue(NAME, tr("Remove from cache"));
    407412            putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
    408413            putValue(SHORT_DESCRIPTION, tr("Remove the selected changesets from the local cache"));
     414            this.model = model;
    409415            updateEnabledState();
    410416        }
    411417
    412418        @Override
    413         public void actionPerformed(ActionEvent arg0) {
    414             List<Changeset> selected = model.getSelectedChangesets();
    415             ChangesetCache.getInstance().remove(selected);
     419        public void actionPerformed(ActionEvent e) {
     420            ChangesetCache.getInstance().remove(model.getSelectedChangesets());
    416421        }
    417422
     
    430435     *
    431436     */
    432     class CloseSelectedChangesetsAction extends AbstractAction implements ListSelectionListener {
    433         CloseSelectedChangesetsAction() {
     437    static class CloseSelectedChangesetsAction extends AbstractAction implements ListSelectionListener {
     438        private final ChangesetCacheManagerModel model;
     439
     440        CloseSelectedChangesetsAction(ChangesetCacheManagerModel model) {
    434441            putValue(NAME, tr("Close"));
    435442            putValue(SMALL_ICON, ImageProvider.get("closechangeset"));
    436443            putValue(SHORT_DESCRIPTION, tr("Close the selected changesets"));
     444            this.model = model;
    437445            updateEnabledState();
    438446        }
    439447
    440448        @Override
    441         public void actionPerformed(ActionEvent arg0) {
    442             List<Changeset> selected = model.getSelectedChangesets();
    443             Main.worker.submit(new CloseChangesetTask(selected));
     449        public void actionPerformed(ActionEvent e) {
     450            Main.worker.submit(new CloseChangesetTask(model.getSelectedChangesets()));
    444451        }
    445452
     
    472479     *
    473480     */
    474     class DownloadSelectedChangesetsAction extends AbstractAction implements ListSelectionListener {
    475         DownloadSelectedChangesetsAction() {
     481    static class DownloadSelectedChangesetsAction extends AbstractAction implements ListSelectionListener {
     482        private final ChangesetCacheManagerModel model;
     483
     484        DownloadSelectedChangesetsAction(ChangesetCacheManagerModel model) {
    476485            putValue(NAME, tr("Update changeset"));
    477486            putValue(SMALL_ICON, ImageProvider.get("dialogs/changeset", "updatechangeset"));
    478487            putValue(SHORT_DESCRIPTION, tr("Updates the selected changesets with current data from the OSM server"));
     488            this.model = model;
    479489            updateEnabledState();
    480490        }
    481491
    482492        @Override
    483         public void actionPerformed(ActionEvent arg0) {
    484             List<Changeset> selected = model.getSelectedChangesets();
    485             ChangesetHeaderDownloadTask task = ChangesetHeaderDownloadTask.buildTaskForChangesets(ChangesetCacheManager.this, selected);
    486             ChangesetCacheManager.getInstance().runDownloadTask(task);
     493        public void actionPerformed(ActionEvent e) {
     494            if (!GraphicsEnvironment.isHeadless()) {
     495                ChangesetCacheManager.getInstance().runDownloadTask(
     496                        ChangesetHeaderDownloadTask.buildTaskForChangesets(GuiHelper.getWindowAncestorFor(e), model.getSelectedChangesets()));
     497            }
    487498        }
    488499
     
    501512     *
    502513     */
    503     class DownloadSelectedChangesetContentAction extends AbstractAction implements ListSelectionListener {
    504         DownloadSelectedChangesetContentAction() {
     514    static class DownloadSelectedChangesetContentAction extends AbstractAction implements ListSelectionListener {
     515        private final ChangesetCacheManagerModel model;
     516
     517        DownloadSelectedChangesetContentAction(ChangesetCacheManagerModel model) {
    505518            putValue(NAME, tr("Download changeset content"));
    506519            putValue(SMALL_ICON, DOWNLOAD_CONTENT_ICON);
    507520            putValue(SHORT_DESCRIPTION, tr("Download the content of the selected changesets from the server"));
     521            this.model = model;
    508522            updateEnabledState();
    509523        }
    510524
    511525        @Override
    512         public void actionPerformed(ActionEvent arg0) {
    513             ChangesetContentDownloadTask task = new ChangesetContentDownloadTask(ChangesetCacheManager.this, model.getSelectedChangesetIds());
    514             ChangesetCacheManager.getInstance().runDownloadTask(task);
     526        public void actionPerformed(ActionEvent e) {
     527            if (!GraphicsEnvironment.isHeadless()) {
     528                ChangesetCacheManager.getInstance().runDownloadTask(
     529                        new ChangesetContentDownloadTask(GuiHelper.getWindowAncestorFor(e), model.getSelectedChangesetIds()));
     530            }
    515531        }
    516532
     
    525541    }
    526542
    527     class ShowDetailAction extends AbstractAction {
    528 
    529         public void showDetails() {
     543    static class ShowDetailAction extends AbstractAction {
     544        private final ChangesetCacheManagerModel model;
     545
     546        ShowDetailAction(ChangesetCacheManagerModel model) {
     547            this.model = model;
     548        }
     549
     550        protected void showDetails() {
    530551            List<Changeset> selected = model.getSelectedChangesets();
    531             if (selected.size() != 1) return;
    532             model.setChangesetInDetailView(selected.get(0));
    533         }
    534 
    535         @Override
    536         public void actionPerformed(ActionEvent arg0) {
     552            if (selected.size() == 1) {
     553                model.setChangesetInDetailView(selected.get(0));
     554            }
     555        }
     556
     557        @Override
     558        public void actionPerformed(ActionEvent e) {
    537559            showDetails();
    538560        }
    539561    }
    540562
    541     class DownloadMyChangesets extends AbstractAction {
     563    static class DownloadMyChangesets extends AbstractAction {
    542564        DownloadMyChangesets() {
    543565            putValue(NAME, tr("My changesets"));
     
    547569        }
    548570
    549         protected void alertAnonymousUser() {
     571        protected void alertAnonymousUser(Component parent) {
    550572            HelpAwareOptionPane.showOptionDialog(
    551                     ChangesetCacheManager.this,
     573                    parent,
    552574                    tr("<html>JOSM is currently running with an anonymous user. It cannot download<br>"
    553575                            + "your changesets from the OSM server unless you enter your OSM user name<br>"
     
    561583
    562584        @Override
    563         public void actionPerformed(ActionEvent arg0) {
     585        public void actionPerformed(ActionEvent e) {
     586            Window parent = GuiHelper.getWindowAncestorFor(e);
    564587            JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
    565588            if (im.isAnonymous()) {
    566                 alertAnonymousUser();
     589                alertAnonymousUser(parent);
    567590                return;
    568591            }
     
    573596                query = query.forUser(im.getUserName());
    574597            }
    575             ChangesetQueryTask task = new ChangesetQueryTask(ChangesetCacheManager.this, query);
    576             ChangesetCacheManager.getInstance().runDownloadTask(task);
     598            if (!GraphicsEnvironment.isHeadless()) {
     599                ChangesetCacheManager.getInstance().runDownloadTask(new ChangesetQueryTask(parent, query));
     600            }
    577601        }
    578602    }
     
    587611        public void mouseClicked(MouseEvent evt) {
    588612            if (isDoubleClick(evt)) {
    589                 new ShowDetailAction().showDetails();
     613                new ShowDetailAction(model).showDetails();
    590614            }
    591615        }
     
    601625    }
    602626
    603     class ChangesetDetailViewSynchronizer implements ListSelectionListener {
     627    static class ChangesetDetailViewSynchronizer implements ListSelectionListener {
     628        private final ChangesetCacheManagerModel model;
     629
     630        ChangesetDetailViewSynchronizer(ChangesetCacheManagerModel model) {
     631            this.model = model;
     632        }
     633
    604634        @Override
    605635        public void valueChanged(ListSelectionEvent e) {
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/ChangesetQueryDialog.java

    r8836 r9916  
    66import java.awt.BorderLayout;
    77import java.awt.Container;
    8 import java.awt.Dialog;
    98import java.awt.Dimension;
    109import java.awt.FlowLayout;
    11 import java.awt.Frame;
     10import java.awt.Window;
    1211import java.awt.event.ActionEvent;
    1312import java.awt.event.KeyEvent;
     
    9594    }
    9695
    97     public ChangesetQueryDialog(Dialog parent) {
    98         super(parent, ModalityType.DOCUMENT_MODAL);
    99         build();
    100     }
    101 
    102     public ChangesetQueryDialog(Frame parent) {
     96    public ChangesetQueryDialog(Window parent) {
    10397        super(parent, ModalityType.DOCUMENT_MODAL);
    10498        build();
  • trunk/src/org/openstreetmap/josm/gui/util/GuiHelper.java

    r9826 r9916  
    2727import java.lang.reflect.InvocationTargetException;
    2828import java.util.Enumeration;
     29import java.util.EventObject;
    2930import java.util.concurrent.Callable;
    3031import java.util.concurrent.ExecutionException;
     
    463464        return GraphicsEnvironment.isHeadless() ? null : Toolkit.getDefaultToolkit().getSystemSelection();
    464465    }
     466
     467    /**
     468     * Returns the first <code>Window</code> ancestor of event source, or
     469     * {@code null} if event source is not a component contained inside a <code>Window</code>.
     470     * @param e event object
     471     * @return a Window, or {@code null}
     472     * @since 9916
     473     */
     474    public static Window getWindowAncestorFor(EventObject e) {
     475        return e != null && e.getSource() instanceof Component ? SwingUtilities.getWindowAncestor((Component) e.getSource()) : null;
     476    }
    465477}
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9732 r9916  
    88import java.awt.Color;
    99import java.awt.Font;
     10import java.awt.HeadlessException;
    1011import java.awt.Toolkit;
    1112import java.awt.datatransfer.Clipboard;
     
    635636     */
    636637    public static String getClipboardContent() {
    637         Transferable t = getTransferableContent(Toolkit.getDefaultToolkit().getSystemClipboard());
    638638        try {
     639            Transferable t = getTransferableContent(Toolkit.getDefaultToolkit().getSystemClipboard());
    639640            if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
    640641                return (String) t.getTransferData(DataFlavor.stringFlavor);
    641642            }
    642         } catch (UnsupportedFlavorException | IOException ex) {
     643        } catch (UnsupportedFlavorException | IOException | HeadlessException ex) {
    643644            Main.error(ex);
    644645            return null;
Note: See TracChangeset for help on using the changeset viewer.