1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.beans.PropertyChangeEvent;
|
---|
11 | import java.beans.PropertyChangeListener;
|
---|
12 | import java.util.Collections;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 | import javax.swing.JTable;
|
---|
19 | import javax.swing.JToolBar;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
23 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * The panel which displays the public discussion around a changeset in a scrollable table.
|
---|
27 | *
|
---|
28 | * It listens to property change events for {@link ChangesetCacheManagerModel#CHANGESET_IN_DETAIL_VIEW_PROP}
|
---|
29 | * and updates its view accordingly.
|
---|
30 | *
|
---|
31 | * @since 7704
|
---|
32 | */
|
---|
33 | public class ChangesetDiscussionPanel extends JPanel implements PropertyChangeListener {
|
---|
34 |
|
---|
35 | private final UpdateChangesetDiscussionAction actUpdateChangesets = new UpdateChangesetDiscussionAction();
|
---|
36 |
|
---|
37 | private final ChangesetDiscussionTableModel model = new ChangesetDiscussionTableModel();
|
---|
38 |
|
---|
39 | private JTable table;
|
---|
40 |
|
---|
41 | private Changeset current = null;
|
---|
42 |
|
---|
43 | protected JPanel buildActionButtonPanel() {
|
---|
44 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
45 |
|
---|
46 | JToolBar tb = new JToolBar(JToolBar.VERTICAL);
|
---|
47 | tb.setFloatable(false);
|
---|
48 |
|
---|
49 | // -- changeset discussion update
|
---|
50 | tb.add(actUpdateChangesets);
|
---|
51 | actUpdateChangesets.initProperties(current);
|
---|
52 |
|
---|
53 | pnl.add(tb);
|
---|
54 | return pnl;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Updates the current changeset discussion from the OSM server
|
---|
59 | */
|
---|
60 | class UpdateChangesetDiscussionAction extends AbstractAction {
|
---|
61 | public UpdateChangesetDiscussionAction() {
|
---|
62 | putValue(NAME, tr("Update changeset discussion"));
|
---|
63 | putValue(SMALL_ICON, ChangesetCacheManager.UPDATE_CONTENT_ICON);
|
---|
64 | putValue(SHORT_DESCRIPTION, tr("Update the changeset discussion from the OSM server"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void actionPerformed(ActionEvent evt) {
|
---|
69 | if (current == null) return;
|
---|
70 | Main.worker.submit(
|
---|
71 | new ChangesetHeaderDownloadTask(
|
---|
72 | ChangesetDiscussionPanel.this,
|
---|
73 | Collections.singleton(current.getId()),
|
---|
74 | true /* include discussion */
|
---|
75 | )
|
---|
76 | );
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void initProperties(Changeset cs) {
|
---|
80 | setEnabled(cs != null && !Main.isOffline(OnlineResource.OSM_API));
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Constructs a new {@code ChangesetDiscussionPanel}.
|
---|
86 | */
|
---|
87 | public ChangesetDiscussionPanel() {
|
---|
88 | build();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void setCurrentChangeset(Changeset cs) {
|
---|
92 | current = cs;
|
---|
93 | if (cs == null) {
|
---|
94 | clearView();
|
---|
95 | } else {
|
---|
96 | updateView(cs);
|
---|
97 | }
|
---|
98 | actUpdateChangesets.initProperties(current);
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected final void build() {
|
---|
102 | setLayout(new BorderLayout());
|
---|
103 | setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
|
---|
104 | add(buildActionButtonPanel(), BorderLayout.WEST);
|
---|
105 | add(buildDiscussionPanel(), BorderLayout.CENTER);
|
---|
106 | }
|
---|
107 |
|
---|
108 | private Component buildDiscussionPanel() {
|
---|
109 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
110 | table = new JTable(model, new ChangesetDiscussionTableColumnModel());
|
---|
111 | //tblContent.addMouseListener(new PopupMenuLauncher(new ChangesetContentTablePopupMenu()));
|
---|
112 | pnl.add(new JScrollPane(table), BorderLayout.CENTER);
|
---|
113 | return pnl;
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected void clearView() {
|
---|
117 | model.populate(null);
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected void updateView(Changeset cs) {
|
---|
121 | model.populate(cs.getDiscussion());
|
---|
122 | // Update row heights
|
---|
123 | for (int row = 0; row < table.getRowCount(); row++) {
|
---|
124 | int rowHeight = table.getRowHeight();
|
---|
125 |
|
---|
126 | Component comp = table.prepareRenderer(table.getCellRenderer(row, 2), row, 2);
|
---|
127 | rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
|
---|
128 |
|
---|
129 | table.setRowHeight(row, rowHeight);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /* ---------------------------------------------------------------------------- */
|
---|
134 | /* interface PropertyChangeListener */
|
---|
135 | /* ---------------------------------------------------------------------------- */
|
---|
136 | @Override
|
---|
137 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
138 | if (! evt.getPropertyName().equals(ChangesetCacheManagerModel.CHANGESET_IN_DETAIL_VIEW_PROP))
|
---|
139 | return;
|
---|
140 | setCurrentChangeset((Changeset)evt.getNewValue());
|
---|
141 | }
|
---|
142 | }
|
---|