Changeset 9556 in josm


Ignore:
Timestamp:
2016-01-21T01:32:52+01:00 (9 years ago)
Author:
Don-vip
Message:

add unit test

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r9543 r9556  
    99import java.awt.Dimension;
    1010import java.awt.Graphics2D;
     11import java.awt.GraphicsEnvironment;
    1112import java.awt.GridBagConstraints;
    1213import java.awt.GridBagLayout;
     
    165166    }
    166167
    167     private static class  LayerListWarningMessagePanel extends JPanel {
    168         private JLabel lblMessage;
    169         private JList<SaveLayerInfo> lstLayers;
     168    private static class LayerListWarningMessagePanel extends JPanel {
     169        private final JLabel lblMessage = new JLabel();
     170        private final JList<SaveLayerInfo> lstLayers = new JList<>();
     171
     172        LayerListWarningMessagePanel(String msg, List<SaveLayerInfo> infos) {
     173            build();
     174            lblMessage.setText(msg);
     175            lstLayers.setListData(infos.toArray(new SaveLayerInfo[0]));
     176        }
    170177
    171178        protected void build() {
     
    177184            gc.weightx = 1.0;
    178185            gc.weighty = 0.0;
    179             add(lblMessage = new JLabel(), gc);
     186            add(lblMessage, gc);
    180187            lblMessage.setHorizontalAlignment(JLabel.LEFT);
    181             lstLayers = new JList<>();
    182188            lstLayers.setCellRenderer(
    183189                    new ListCellRenderer<SaveLayerInfo>() {
     
    199205            add(lstLayers, gc);
    200206        }
    201 
    202         LayerListWarningMessagePanel(String msg, List<SaveLayerInfo> infos) {
    203             build();
    204             lblMessage.setText(msg);
    205             lstLayers.setListData(infos.toArray(new SaveLayerInfo[0]));
    206         }
    207     }
    208 
    209     protected void warnLayersWithConflictsAndUploadRequest(List<SaveLayerInfo> infos) {
    210         String msg = trn("<html>{0} layer has unresolved conflicts.<br>"
     207    }
     208
     209    private static void warn(String msg, List<SaveLayerInfo> infos, String title) {
     210        JPanel panel = new LayerListWarningMessagePanel(msg, infos);
     211        // For unit test coverage in headless mode
     212        if (!GraphicsEnvironment.isHeadless()) {
     213            JOptionPane.showConfirmDialog(Main.parent, panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
     214        }
     215    }
     216
     217    protected static void warnLayersWithConflictsAndUploadRequest(List<SaveLayerInfo> infos) {
     218        warn(trn("<html>{0} layer has unresolved conflicts.<br>"
    211219                + "Either resolve them first or discard the modifications.<br>"
    212220                + "Layer with conflicts:</html>",
     
    215223                + "Layers with conflicts:</html>",
    216224                infos.size(),
    217                 infos.size());
    218         JOptionPane.showConfirmDialog(
    219                 Main.parent,
    220                 new LayerListWarningMessagePanel(msg, infos),
    221                 tr("Unsaved data and conflicts"),
    222                 JOptionPane.DEFAULT_OPTION,
    223                 JOptionPane.WARNING_MESSAGE
    224         );
    225     }
    226 
    227     protected void warnLayersWithoutFilesAndSaveRequest(List<SaveLayerInfo> infos) {
    228         String msg = trn("<html>{0} layer needs saving but has no associated file.<br>"
     225                infos.size()),
     226             infos, tr("Unsaved data and conflicts"));
     227    }
     228
     229    protected static void warnLayersWithoutFilesAndSaveRequest(List<SaveLayerInfo> infos) {
     230        warn(trn("<html>{0} layer needs saving but has no associated file.<br>"
    229231                + "Either select a file for this layer or discard the changes.<br>"
    230232                + "Layer without a file:</html>",
     
    233235                + "Layers without a file:</html>",
    234236                infos.size(),
    235                 infos.size());
    236         JOptionPane.showConfirmDialog(
    237                 Main.parent,
    238                 new LayerListWarningMessagePanel(msg, infos),
    239                 tr("Unsaved data and missing associated file"),
    240                 JOptionPane.DEFAULT_OPTION,
    241                 JOptionPane.WARNING_MESSAGE
    242         );
    243     }
    244 
    245     protected void warnLayersWithIllegalFilesAndSaveRequest(List<SaveLayerInfo> infos) {
    246         String msg = trn("<html>{0} layer needs saving but has an associated file<br>"
     237                infos.size()),
     238             infos, tr("Unsaved data and missing associated file"));
     239    }
     240
     241    protected static void warnLayersWithIllegalFilesAndSaveRequest(List<SaveLayerInfo> infos) {
     242        warn(trn("<html>{0} layer needs saving but has an associated file<br>"
    247243                + "which cannot be written.<br>"
    248244                + "Either select another file for this layer or discard the changes.<br>"
     
    253249                + "Layers with non-writable files:</html>",
    254250                infos.size(),
    255                 infos.size());
    256         JOptionPane.showConfirmDialog(
    257                 Main.parent,
    258                 new LayerListWarningMessagePanel(msg, infos),
    259                 tr("Unsaved data non-writable files"),
    260                 JOptionPane.DEFAULT_OPTION,
    261                 JOptionPane.WARNING_MESSAGE
    262         );
    263     }
    264 
    265     protected boolean confirmSaveLayerInfosOK() {
     251                infos.size()),
     252             infos, tr("Unsaved data non-writable files"));
     253    }
     254
     255    static boolean confirmSaveLayerInfosOK(SaveLayersModel model) {
    266256        List<SaveLayerInfo> layerInfos = model.getLayersWithConflictsAndUploadRequest();
    267257        if (!layerInfos.isEmpty()) {
     
    388378
    389379    final class SaveAndProceedAction extends AbstractAction implements PropertyChangeListener {
    390         private static final int is = 24; // icon size
     380        private static final int ICON_SIZE = 24;
    391381        private static final String BASE_ICON = "BASE_ICON";
    392382        private final transient Image save = ImageProvider.get("save").getImage();
    393383        private final transient Image upld = ImageProvider.get("upload").getImage();
    394         private final transient Image saveDis = new BufferedImage(is, is, BufferedImage.TYPE_4BYTE_ABGR);
    395         private final transient Image upldDis = new BufferedImage(is, is, BufferedImage.TYPE_4BYTE_ABGR);
     384        private final transient Image saveDis = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
     385        private final transient Image upldDis = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
    396386
    397387        SaveAndProceedAction() {
     
    419409            try { // Can fail if model is not yet setup properly
    420410                Image base = ((ImageIcon) getValue(BASE_ICON)).getImage();
    421                 BufferedImage newIco = new BufferedImage(is*3, is, BufferedImage.TYPE_4BYTE_ABGR);
     411                BufferedImage newIco = new BufferedImage(ICON_SIZE*3, ICON_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
    422412                Graphics2D g = newIco.createGraphics();
    423                 g.drawImage(model.getLayersToUpload().isEmpty() ? upldDis : upld, is*0, 0, is, is, null);
    424                 g.drawImage(model.getLayersToSave().isEmpty()   ? saveDis : save, is*1, 0, is, is, null);
    425                 g.drawImage(base,                                                 is*2, 0, is, is, null);
     413                g.drawImage(model.getLayersToUpload().isEmpty() ? upldDis : upld, ICON_SIZE*0, 0, ICON_SIZE, ICON_SIZE, null);
     414                g.drawImage(model.getLayersToSave().isEmpty()   ? saveDis : save, ICON_SIZE*1, 0, ICON_SIZE, ICON_SIZE, null);
     415                g.drawImage(base,                                                 ICON_SIZE*2, 0, ICON_SIZE, ICON_SIZE, null);
    426416                putValue(SMALL_ICON, new ImageIcon(newIco));
    427417            } catch (Exception e) {
     418                Main.warn(e);
    428419                putValue(SMALL_ICON, getValue(BASE_ICON));
    429420            }
     
    432423        @Override
    433424        public void actionPerformed(ActionEvent e) {
    434             if (!confirmSaveLayerInfosOK())
     425            if (!confirmSaveLayerInfosOK(model))
    435426                return;
    436427            launchSafeAndUploadTask();
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersModel.java

    r9078 r9556  
    123123    public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
    124124        List<SaveLayerInfo> ret = new ArrayList<>();
    125         for (SaveLayerInfo info: layerInfo) {
    126             if (info.isDoSaveToFile() && info.getFile() == null) {
    127                 ret.add(info);
     125        if (layerInfo != null) {
     126            for (SaveLayerInfo info: layerInfo) {
     127                if (info.isDoSaveToFile() && info.getFile() == null) {
     128                    ret.add(info);
     129                }
    128130            }
    129131        }
     
    133135    public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() {
    134136        List<SaveLayerInfo> ret = new ArrayList<>();
    135         for (SaveLayerInfo info: layerInfo) {
    136             if (info.isDoSaveToFile() && info.getFile() != null && info.getFile().exists() && !info.getFile().canWrite()) {
    137                 ret.add(info);
     137        if (layerInfo != null) {
     138            for (SaveLayerInfo info: layerInfo) {
     139                if (info.isDoSaveToFile() && info.getFile() != null && info.getFile().exists() && !info.getFile().canWrite()) {
     140                    ret.add(info);
     141                }
    138142            }
    139143        }
     
    143147    public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
    144148        List<SaveLayerInfo> ret = new ArrayList<>();
    145         for (SaveLayerInfo info: layerInfo) {
    146             AbstractModifiableLayer l = info.getLayer();
    147             if (info.isDoUploadToServer() && l instanceof OsmDataLayer && !((OsmDataLayer) l).getConflicts().isEmpty()) {
    148                 ret.add(info);
     149        if (layerInfo != null) {
     150            for (SaveLayerInfo info: layerInfo) {
     151                AbstractModifiableLayer l = info.getLayer();
     152                if (info.isDoUploadToServer() && l instanceof OsmDataLayer && !((OsmDataLayer) l).getConflicts().isEmpty()) {
     153                    ret.add(info);
     154                }
    149155            }
    150156        }
     
    154160    public List<SaveLayerInfo> getLayersToUpload() {
    155161        List<SaveLayerInfo> ret = new ArrayList<>();
    156         for (SaveLayerInfo info: layerInfo) {
    157             if (info.isDoUploadToServer()) {
    158                 ret.add(info);
     162        if (layerInfo != null) {
     163            for (SaveLayerInfo info: layerInfo) {
     164                if (info.isDoUploadToServer()) {
     165                    ret.add(info);
     166                }
    159167            }
    160168        }
     
    164172    public List<SaveLayerInfo> getLayersToSave() {
    165173        List<SaveLayerInfo> ret = new ArrayList<>();
    166         for (SaveLayerInfo info: layerInfo) {
    167             if (info.isDoSaveToFile()) {
    168                 ret.add(info);
     174        if (layerInfo != null) {
     175            for (SaveLayerInfo info: layerInfo) {
     176                if (info.isDoSaveToFile()) {
     177                    ret.add(info);
     178                }
    169179            }
    170180        }
Note: See TracChangeset for help on using the changeset viewer.