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.Rectangle;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.beans.PropertyChangeEvent;
|
---|
12 | import java.beans.PropertyChangeListener;
|
---|
13 | import java.util.Collections;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.JTable;
|
---|
20 | import javax.swing.JToolBar;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.actions.downloadtasks.ChangesetHeaderDownloadTask;
|
---|
24 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
25 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
26 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
27 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The panel which displays the public discussion around a changeset in a scrollable table.
|
---|
32 | *
|
---|
33 | * It listens to property change events for {@link ChangesetCacheManagerModel#CHANGESET_IN_DETAIL_VIEW_PROP}
|
---|
34 | * and updates its view accordingly.
|
---|
35 | *
|
---|
36 | * @since 7704
|
---|
37 | */
|
---|
38 | public class ChangesetDiscussionPanel extends JPanel implements PropertyChangeListener {
|
---|
39 |
|
---|
40 | private final UpdateChangesetDiscussionAction actUpdateChangesets = new UpdateChangesetDiscussionAction();
|
---|
41 |
|
---|
42 | private final ChangesetDiscussionTableModel model = new ChangesetDiscussionTableModel();
|
---|
43 |
|
---|
44 | private JTable table;
|
---|
45 |
|
---|
46 | private transient Changeset current;
|
---|
47 |
|
---|
48 | protected JPanel buildActionButtonPanel() {
|
---|
49 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
50 |
|
---|
51 | JToolBar tb = new JToolBar(JToolBar.VERTICAL);
|
---|
52 | tb.setFloatable(false);
|
---|
53 |
|
---|
54 | // -- changeset discussion update
|
---|
55 | tb.add(actUpdateChangesets);
|
---|
56 | actUpdateChangesets.initProperties(current);
|
---|
57 |
|
---|
58 | pnl.add(tb);
|
---|
59 | return pnl;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Updates the current changeset discussion from the OSM server
|
---|
64 | */
|
---|
65 | class UpdateChangesetDiscussionAction extends AbstractAction {
|
---|
66 | UpdateChangesetDiscussionAction() {
|
---|
67 | putValue(NAME, tr("Update changeset discussion"));
|
---|
68 | new ImageProvider("dialogs/changeset", "updatechangesetcontent").getResource().attachImageIcon(this);
|
---|
69 | putValue(SHORT_DESCRIPTION, tr("Update the changeset discussion from the OSM server"));
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public void actionPerformed(ActionEvent evt) {
|
---|
74 | if (current == null)
|
---|
75 | return;
|
---|
76 | ChangesetHeaderDownloadTask task = new ChangesetHeaderDownloadTask(
|
---|
77 | ChangesetDiscussionPanel.this,
|
---|
78 | Collections.singleton(current.getId()),
|
---|
79 | true /* include discussion */
|
---|
80 | );
|
---|
81 | MainApplication.worker.submit(new PostDownloadHandler(task, task.download()));
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void initProperties(Changeset cs) {
|
---|
85 | setEnabled(cs != null && !Main.isOffline(OnlineResource.OSM_API));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Constructs a new {@code ChangesetDiscussionPanel}.
|
---|
91 | */
|
---|
92 | public ChangesetDiscussionPanel() {
|
---|
93 | build();
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected void setCurrentChangeset(Changeset cs) {
|
---|
97 | current = cs;
|
---|
98 | if (cs == null) {
|
---|
99 | clearView();
|
---|
100 | } else {
|
---|
101 | updateView(cs);
|
---|
102 | }
|
---|
103 | actUpdateChangesets.initProperties(current);
|
---|
104 | if (cs != null && cs.getDiscussion().size() < cs.getCommentsCount()) {
|
---|
105 | actUpdateChangesets.actionPerformed(null);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected final void build() {
|
---|
110 | setLayout(new BorderLayout());
|
---|
111 | setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
---|
112 | add(buildActionButtonPanel(), BorderLayout.WEST);
|
---|
113 | add(buildDiscussionPanel(), BorderLayout.CENTER);
|
---|
114 | }
|
---|
115 |
|
---|
116 | private Component buildDiscussionPanel() {
|
---|
117 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
118 | table = new JTable(model, new ChangesetDiscussionTableColumnModel());
|
---|
119 | table.getColumnModel().getColumn(2).addPropertyChangeListener(evt -> {
|
---|
120 | if ("width".equals(evt.getPropertyName())) {
|
---|
121 | updateRowHeights();
|
---|
122 | }
|
---|
123 | });
|
---|
124 | pnl.add(new JScrollPane(table), BorderLayout.CENTER);
|
---|
125 | return pnl;
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void clearView() {
|
---|
129 | model.populate(null);
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected void updateView(Changeset cs) {
|
---|
133 | model.populate(cs.getDiscussion());
|
---|
134 | updateRowHeights();
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected void updateRowHeights() {
|
---|
138 | int intercellWidth = table.getIntercellSpacing().width;
|
---|
139 | int colWidth = table.getColumnModel().getColumn(2).getWidth();
|
---|
140 | // Update row heights
|
---|
141 | for (int row = 0; row < table.getRowCount(); row++) {
|
---|
142 | int rowHeight = table.getRowHeight();
|
---|
143 |
|
---|
144 | Component comp = table.prepareRenderer(table.getCellRenderer(row, 2), row, 2);
|
---|
145 | // constrain width of component
|
---|
146 | comp.setBounds(new Rectangle(0, 0, colWidth - intercellWidth, Integer.MAX_VALUE));
|
---|
147 | rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
|
---|
148 |
|
---|
149 | table.setRowHeight(row, rowHeight);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /* ---------------------------------------------------------------------------- */
|
---|
154 | /* interface PropertyChangeListener */
|
---|
155 | /* ---------------------------------------------------------------------------- */
|
---|
156 | @Override
|
---|
157 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
158 | if (!evt.getPropertyName().equals(ChangesetCacheManagerModel.CHANGESET_IN_DETAIL_VIEW_PROP))
|
---|
159 | return;
|
---|
160 | setCurrentChangeset((Changeset) evt.getNewValue());
|
---|
161 | }
|
---|
162 | }
|
---|