001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.history;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.BorderLayout;
007import java.awt.Dimension;
008
009import javax.swing.JPanel;
010import javax.swing.JScrollPane;
011import javax.swing.JSplitPane;
012import javax.swing.JTabbedPane;
013import javax.swing.JTable;
014
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
016import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
017import org.openstreetmap.josm.data.osm.history.History;
018
019/**
020 * HistoryBrowser is an UI component which displays history information about an {@link OsmPrimitive}.
021 *
022 *
023 */
024public class HistoryBrowser extends JPanel {
025
026    /** the model */
027    private HistoryBrowserModel model;
028    private TagInfoViewer tagInfoViewer;
029    private NodeListViewer nodeListViewer;
030    private RelationMemberListViewer relationMemberListViewer;
031    private CoordinateInfoViewer coordinateInfoViewer;
032    private JTabbedPane tpViewers;
033
034    /**
035     * embedds table in a {@link JScrollPane}
036     *
037     * @param table the table
038     * @return the {@link JScrollPane} with the embedded table
039     */
040    protected JScrollPane embeddInScrollPane(JTable table) {
041        JScrollPane pane = new JScrollPane(table);
042        pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
043        pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
044        return pane;
045    }
046
047    /**
048     * creates the table which shows the list of versions
049     *
050     * @return  the panel with the version table
051     */
052    protected JPanel createVersionTablePanel() {
053        JPanel pnl = new JPanel();
054        pnl.setLayout(new BorderLayout());
055
056        VersionTable versionTable = new VersionTable(model);
057        pnl.add(embeddInScrollPane(versionTable), BorderLayout.CENTER);
058        return pnl;
059    }
060
061    /**
062     * creates the panel which shows information about two different versions
063     * of the same {@link OsmPrimitive}.
064     *
065     * @return the panel
066     */
067    protected JPanel createVersionComparePanel() {
068        tpViewers = new JTabbedPane();
069
070        // create the viewers, but don't add them yet.
071        // see populate()
072        //
073        tagInfoViewer = new TagInfoViewer(model);
074        nodeListViewer = new NodeListViewer(model);
075        relationMemberListViewer = new RelationMemberListViewer(model);
076        coordinateInfoViewer = new CoordinateInfoViewer(model);
077        JPanel pnl = new JPanel();
078        pnl.setLayout(new BorderLayout());
079        pnl.add(tpViewers, BorderLayout.CENTER);
080        return pnl;
081    }
082
083    /**
084     * builds the GUI
085     */
086    protected void build() {
087        JPanel left;
088        JPanel right;
089        setLayout(new BorderLayout());
090        JSplitPane pane = new JSplitPane(
091                JSplitPane.HORIZONTAL_SPLIT,
092                left = createVersionTablePanel(),
093                right = createVersionComparePanel()
094        );
095        add(pane, BorderLayout.CENTER);
096
097        pane.setOneTouchExpandable(true);
098        pane.setDividerLocation(300);
099
100        Dimension minimumSize = new Dimension(100, 50);
101        left.setMinimumSize(minimumSize);
102        right.setMinimumSize(minimumSize);
103    }
104
105    /**
106     * constructor
107     */
108    public HistoryBrowser() {
109        model = new HistoryBrowserModel();
110        build();
111    }
112
113    /**
114     * constructor
115     * @param history  the history of an {@link OsmPrimitive}
116     */
117    public HistoryBrowser(History history) {
118        this();
119        populate(history);
120    }
121
122    /**
123     * populates the browser with the history of a specific {@link OsmPrimitive}
124     *
125     * @param history the history
126     */
127    public void populate(History history) {
128        model.setHistory(history);
129
130        tpViewers.removeAll();
131
132        tpViewers.add(tagInfoViewer);
133        tpViewers.setTitleAt(0, tr("Tags"));
134
135        if (history.getEarliest().getType().equals(OsmPrimitiveType.NODE)) {
136            tpViewers.add(coordinateInfoViewer);
137            tpViewers.setTitleAt(1, tr("Coordinates"));
138        } else if (history.getEarliest().getType().equals(OsmPrimitiveType.WAY)) {
139            tpViewers.add(nodeListViewer);
140            tpViewers.setTitleAt(1, tr("Nodes"));
141        } else if (history.getEarliest().getType().equals(OsmPrimitiveType.RELATION)) {
142            tpViewers.add(relationMemberListViewer);
143            tpViewers.setTitleAt(1, tr("Members"));
144        }
145        revalidate();
146    }
147
148    /**
149     * replies the {@link History} currently displayed by this browser
150     *
151     * @return the current history
152     */
153    public History getHistory() {
154        return model.getHistory();
155    }
156
157    /**
158     * replies the model used by this browser
159     * @return the model
160     */
161    public HistoryBrowserModel getModel() {
162        return model;
163    }
164}