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