Changeset 230 in josm for src


Ignore:
Timestamp:
2007-05-08T00:54:13+02:00 (18 years ago)
Author:
framm
Message:

Introduced generic UploadHook which allows plugins to veto an upload; refactored
existing upload confirmation dialog to be just one (the only default) element
in the list of UploadHooks. Based on patch by Francisco R. Santos <frsantos@…>.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/UploadAction.java

    r178 r230  
    3333 */
    3434public class UploadAction extends JosmAction {
     35       
     36        /** Upload Hook */
     37        public interface UploadHook {
     38                /**
     39                 * Checks the upload.
     40                 * @param add The added primitives
     41                 * @param update The updated primitives
     42                 * @param delete The deleted primitives
     43                 * @return true, if the upload can continue
     44                 */
     45                public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete);
     46        }
     47       
     48        /**
     49         * The list of upload hooks. These hooks will be called one after the other
     50         * when the user wants to upload data. Plugins can insert their own hooks here
     51         * if they want to be able to veto an upload.
     52         *
     53         * Be dafault, the standard upload dialog is the only element in the list.
     54         * Plugins shold normally insert their code before that, so that the upload
     55         * dialog is the last thing shown before upload really starts; on occasion
     56         * however, a plugin might also want to insert something after that.
     57         */
     58        public final Collection<UploadHook> uploadHooks = new LinkedList<UploadHook>();
     59
    3560        public UploadAction() {
    3661                super(tr("Upload to OSM"), "upload", tr("Upload all changes to the OSM server."), KeyEvent.VK_U, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK, true);
     62
     63                /**
     64                 * Displays a screen where the actions that would be taken are displayed and
     65                 * give the user the possibility to cancel the upload.
     66                 */
     67                uploadHooks.add(new UploadHook() {
     68                        public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
     69
     70                                JPanel p = new JPanel(new GridBagLayout());
     71
     72                                OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
     73
     74                                if (!add.isEmpty()) {
     75                                        p.add(new JLabel(tr("Objects to add:")), GBC.eol());
     76                                        JList l = new JList(add.toArray());
     77                                        l.setCellRenderer(renderer);
     78                                        l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
     79                                        p.add(new JScrollPane(l), GBC.eol().fill());
     80                                }
     81
     82                                if (!update.isEmpty()) {
     83                                        p.add(new JLabel(tr("Objects to modify:")), GBC.eol());
     84                                        JList l = new JList(update.toArray());
     85                                        l.setCellRenderer(renderer);
     86                                        l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
     87                                        p.add(new JScrollPane(l), GBC.eol().fill());
     88                                }
     89
     90                                if (!delete.isEmpty()) {
     91                                        p.add(new JLabel(tr("Objects to delete:")), GBC.eol());
     92                                        JList l = new JList(delete.toArray());
     93                                        l.setCellRenderer(renderer);
     94                                        l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
     95                                        p.add(new JScrollPane(l), GBC.eol().fill());
     96                                }
     97
     98                                return JOptionPane.showConfirmDialog(Main.parent, p, tr("Upload this changes?"),
     99                                                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
     100                        }
     101                });
    37102        }
    38103
     
    63128                                delete.add(osm);
    64129                }
     130               
     131                if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
     132                        JOptionPane.showMessageDialog(Main.parent,tr("No changes to upload."));
     133                        return;
     134                }
    65135
    66                 if (!displayUploadScreen(add, update, delete))
    67                         return;
    68 
     136                // Call all upload hooks in sequence. The upload confirmation dialog
     137                // is one of these.
     138                for(UploadHook hook : uploadHooks)
     139                        if(!hook.checkUpload(add, update, delete))
     140                                return;
     141               
    69142                final OsmServerWriter server = new OsmServerWriter();
    70143                final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
     
    86159                Main.worker.execute(uploadTask);
    87160        }
    88 
    89         /**
    90          * Displays a screen where the actions that would be taken are displayed and
    91          * give the user the possibility to cancel the upload.
    92          * @return <code>true</code>, if the upload should continue. <code>false</code>
    93          *                      if the user requested cancel.
    94          */
    95         private boolean displayUploadScreen(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
    96                 if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
    97                         JOptionPane.showMessageDialog(Main.parent,tr("No changes to upload."));
    98                         return false;
    99                 }
    100 
    101                 JPanel p = new JPanel(new GridBagLayout());
    102 
    103                 OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
    104 
    105                 if (!add.isEmpty()) {
    106                         p.add(new JLabel(tr("Objects to add:")), GBC.eol());
    107                         JList l = new JList(add.toArray());
    108                         l.setCellRenderer(renderer);
    109                         l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
    110                         p.add(new JScrollPane(l), GBC.eol().fill());
    111                 }
    112 
    113                 if (!update.isEmpty()) {
    114                         p.add(new JLabel(tr("Objects to modify:")), GBC.eol());
    115                         JList l = new JList(update.toArray());
    116                         l.setCellRenderer(renderer);
    117                         l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
    118                         p.add(new JScrollPane(l), GBC.eol().fill());
    119                 }
    120 
    121                 if (!delete.isEmpty()) {
    122                         p.add(new JLabel(tr("Objects to delete:")), GBC.eol());
    123                         JList l = new JList(delete.toArray());
    124                         l.setCellRenderer(renderer);
    125                         l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
    126                         p.add(new JScrollPane(l), GBC.eol().fill());
    127                 }
    128 
    129                 return JOptionPane.showConfirmDialog(Main.parent, p, tr("Upload this changes?"),
    130                                 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
    131         }
    132161}
Note: See TracChangeset for help on using the changeset viewer.