Ignore:
Timestamp:
2016-01-24T01:39:52+01:00 (8 years ago)
Author:
Don-vip
Message:

HistoryBrowserDialog: fix Sonar/Coverity issues + add basic unit test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserDialog.java

    r9543 r9602  
    3434 * This is non-modal dialog, always showing on top, which displays history information
    3535 * about a given {@link org.openstreetmap.josm.data.osm.OsmPrimitive}.
    36  *
     36 * @since 1709
    3737 */
    3838public class HistoryBrowserDialog extends JDialog implements HistoryDataSetListener {
    3939
    4040    /** the embedded browser */
    41     private HistoryBrowser browser;
    42     private CloseAction closeAction;
    43     private JLabel titleLabel;
    44 
    45     /**
    46      * displays the title for this dialog
     41    private final HistoryBrowser browser = new HistoryBrowser();
     42    private final CloseAction closeAction = new CloseAction();
     43    private final JLabel titleLabel = new JLabel("", JLabel.CENTER);
     44
     45    /**
     46     * Constructs a new {@code HistoryBrowserDialog}.
     47     *
     48     * @param history the history to be displayed
     49     */
     50    public HistoryBrowserDialog(History history) {
     51        super(JOptionPane.getFrameForComponent(Main.parent), false);
     52        build();
     53        setHistory(history);
     54        setTitle(buildTitle(history));
     55        pack();
     56        if (getInsets().top > 0) {
     57            titleLabel.setVisible(false);
     58        }
     59        HistoryDataSet.getInstance().addHistoryDataSetListener(this);
     60        addWindowListener(new WindowClosingAdapter());
     61    }
     62
     63    /**
     64     * Constructs the title for this dialog
    4765     *
    4866     * @param h the current history
    49      */
    50     protected void renderTitle(History h) {
    51         String title = "";
    52         switch(h.getEarliest().getType()) {
    53         case NODE:  title = marktr("History for node {0}"); break;
    54         case WAY: title = marktr("History for way {0}"); break;
    55         case RELATION:  title = marktr("History for relation {0}"); break;
    56         }
    57         setTitle(tr(
    58                 title,
    59                 Long.toString(h.getId())
    60         ));
     67     * @return the title for this dialog
     68     */
     69    static String buildTitle(History h) {
     70        String title;
     71        switch (h.getEarliest().getType()) {
     72        case NODE: title = marktr("History for node {0}");
     73            break;
     74        case WAY: title = marktr("History for way {0}");
     75            break;
     76        case RELATION: title = marktr("History for relation {0}");
     77            break;
     78        default: title = "";
     79        }
     80        return tr(title, Long.toString(h.getId()));
    6181    }
    6282
     
    7595        setLayout(new BorderLayout());
    7696
    77         titleLabel = new JLabel();
    78         titleLabel.setHorizontalAlignment(JLabel.CENTER);
    7997        add(titleLabel, BorderLayout.NORTH);
    8098
    81         browser = new HistoryBrowser();
    8299        add(browser, BorderLayout.CENTER);
    83100
     
    88105        pnl.add(btn);
    89106
    90         btn = new SideButton(closeAction = new CloseAction());
     107        btn = new SideButton(closeAction);
    91108        final String closeHistoryBrowserDialogKey = "CloseHistoryBrowserDialog";
    92109        KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
     
    105122
    106123    /**
    107      * Constructs a new {@code HistoryBrowserDialog}.
    108      *
    109      * @param history the history to be displayed
    110      */
    111     public HistoryBrowserDialog(History history) {
    112         super(JOptionPane.getFrameForComponent(Main.parent), false);
    113         build();
    114         setHistory(history);
    115         renderTitle(history);
    116         pack();
    117         if (getInsets().top > 0) {
    118             titleLabel.setVisible(false);
    119         }
    120         HistoryDataSet.getInstance().addHistoryDataSetListener(this);
    121         addWindowListener(new WindowClosingAdapter());
    122     }
    123 
    124     /**
    125124     * Sets the current history.
    126125     * @param history current history
     
    140139    /* interface HistoryDataSetListener                                                   */
    141140    /* ---------------------------------------------------------------------------------- */
     141
    142142    @Override
    143143    public void historyUpdated(HistoryDataSet source, PrimitiveId primitiveId) {
    144144        if (primitiveId == null || primitiveId.equals(browser.getHistory().getPrimitiveId())) {
    145             browser.populate(source.getHistory(browser.getHistory().getPrimitiveId()));
     145            History history = source.getHistory(browser.getHistory().getPrimitiveId());
     146            if (history != null) {
     147                browser.populate(history);
     148            }
    146149        }
    147150    }
Note: See TracChangeset for help on using the changeset viewer.