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.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.JTable;
|
---|
21 | import javax.swing.JToolBar;
|
---|
22 | import javax.swing.SwingConstants;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.actions.downloadtasks.ChangesetHeaderDownloadTask;
|
---|
25 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
26 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
27 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
28 | import org.openstreetmap.josm.gui.NoteInputDialog;
|
---|
29 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
30 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
31 | import org.openstreetmap.josm.io.OsmApi;
|
---|
32 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
33 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 | import org.openstreetmap.josm.tools.Logging;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * The panel which displays the public discussion around a changeset in a scrollable table.
|
---|
39 | *
|
---|
40 | * It listens to property change events for {@link ChangesetCacheManagerModel#CHANGESET_IN_DETAIL_VIEW_PROP}
|
---|
41 | * and updates its view accordingly.
|
---|
42 | *
|
---|
43 | * @since 7704
|
---|
44 | */
|
---|
45 | public class ChangesetDiscussionPanel extends JPanel implements PropertyChangeListener {
|
---|
46 |
|
---|
47 | private final UpdateChangesetDiscussionAction actUpdateChangesets = new UpdateChangesetDiscussionAction();
|
---|
48 | private final AddChangesetCommentAction actAddChangesetComment = new AddChangesetCommentAction();
|
---|
49 |
|
---|
50 | private final ChangesetDiscussionTableModel model = new ChangesetDiscussionTableModel();
|
---|
51 |
|
---|
52 | private JTable table;
|
---|
53 |
|
---|
54 | private transient Changeset current;
|
---|
55 |
|
---|
56 | protected JPanel buildActionButtonPanel() {
|
---|
57 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
58 |
|
---|
59 | JToolBar tb = new JToolBar(SwingConstants.VERTICAL);
|
---|
60 | tb.setFloatable(false);
|
---|
61 |
|
---|
62 | // -- changeset discussion update
|
---|
63 | tb.add(actUpdateChangesets);
|
---|
64 | // -- add a comment to changeset discussion
|
---|
65 | tb.add(actAddChangesetComment);
|
---|
66 |
|
---|
67 | initProperties();
|
---|
68 |
|
---|
69 | pnl.add(tb);
|
---|
70 | return pnl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void initProperties() {
|
---|
74 | actUpdateChangesets.initProperties(current);
|
---|
75 | actAddChangesetComment.initProperties(current);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Updates the current changeset discussion from the OSM server
|
---|
80 | */
|
---|
81 | class UpdateChangesetDiscussionAction extends AbstractAction {
|
---|
82 | UpdateChangesetDiscussionAction() {
|
---|
83 | putValue(NAME, tr("Update changeset discussion"));
|
---|
84 | new ImageProvider("dialogs/changeset", "updatechangesetcontent").getResource().attachImageIcon(this);
|
---|
85 | putValue(SHORT_DESCRIPTION, tr("Update the changeset discussion from the OSM server"));
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public void actionPerformed(ActionEvent evt) {
|
---|
90 | if (current == null)
|
---|
91 | return;
|
---|
92 | ChangesetHeaderDownloadTask task = new ChangesetHeaderDownloadTask(
|
---|
93 | ChangesetDiscussionPanel.this,
|
---|
94 | Collections.singleton(current.getId()),
|
---|
95 | true /* include discussion */
|
---|
96 | );
|
---|
97 | MainApplication.worker.submit(new PostDownloadHandler(task, task.download()));
|
---|
98 | }
|
---|
99 |
|
---|
100 | void initProperties(Changeset cs) {
|
---|
101 | setEnabled(cs != null && !NetworkManager.isOffline(OnlineResource.OSM_API));
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Adds a discussion comment to the current changeset
|
---|
107 | */
|
---|
108 | class AddChangesetCommentAction extends AbstractAction {
|
---|
109 | AddChangesetCommentAction() {
|
---|
110 | putValue(NAME, tr("Comment"));
|
---|
111 | new ImageProvider("dialogs/notes", "note_comment").getResource().attachImageIcon(this);
|
---|
112 | putValue(SHORT_DESCRIPTION, tr("Add comment"));
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public void actionPerformed(ActionEvent evt) {
|
---|
117 | if (current == null)
|
---|
118 | return;
|
---|
119 | NoteInputDialog dialog = new NoteInputDialog(MainApplication.getMainFrame(), tr("Comment on changeset"), tr("Add comment"));
|
---|
120 | dialog.showNoteDialog(tr("Add comment to changeset:"), ImageProvider.get("dialogs/notes", "note_comment"));
|
---|
121 | if (dialog.getValue() != 1) {
|
---|
122 | return;
|
---|
123 | }
|
---|
124 | try {
|
---|
125 | OsmApi.getOsmApi().addCommentToChangeset(current, dialog.getInputText(), null);
|
---|
126 | } catch (OsmTransferException e) {
|
---|
127 | Logging.error(e);
|
---|
128 | JOptionPane.showMessageDialog(
|
---|
129 | MainApplication.getMainFrame(),
|
---|
130 | ExceptionUtil.explainOsmTransferException(e),
|
---|
131 | tr("Error"),
|
---|
132 | JOptionPane.ERROR_MESSAGE);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | void initProperties(Changeset cs) {
|
---|
137 | setEnabled(cs != null && !cs.isOpen() && !NetworkManager.isOffline(OnlineResource.OSM_API));
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Constructs a new {@code ChangesetDiscussionPanel}.
|
---|
143 | */
|
---|
144 | public ChangesetDiscussionPanel() {
|
---|
145 | build();
|
---|
146 | }
|
---|
147 |
|
---|
148 | protected void setCurrentChangeset(Changeset cs) {
|
---|
149 | current = cs;
|
---|
150 | if (cs == null) {
|
---|
151 | clearView();
|
---|
152 | } else {
|
---|
153 | updateView(cs);
|
---|
154 | }
|
---|
155 | initProperties();
|
---|
156 | if (cs != null && cs.getDiscussion().size() < cs.getCommentsCount()) {
|
---|
157 | actUpdateChangesets.actionPerformed(null);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | protected final void build() {
|
---|
162 | setLayout(new BorderLayout());
|
---|
163 | setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
---|
164 | add(buildActionButtonPanel(), BorderLayout.WEST);
|
---|
165 | add(buildDiscussionPanel(), BorderLayout.CENTER);
|
---|
166 | }
|
---|
167 |
|
---|
168 | private Component buildDiscussionPanel() {
|
---|
169 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
170 | table = new JTable(model, new ChangesetDiscussionTableColumnModel());
|
---|
171 | table.setRowSorter(new ChangesetDiscussionTableRowSorter(model));
|
---|
172 | table.getTableHeader().setReorderingAllowed(false);
|
---|
173 |
|
---|
174 | table.getColumnModel().getColumn(2).addPropertyChangeListener(evt -> {
|
---|
175 | if ("width".equals(evt.getPropertyName())) {
|
---|
176 | updateRowHeights();
|
---|
177 | }
|
---|
178 | });
|
---|
179 | pnl.add(new JScrollPane(table), BorderLayout.CENTER);
|
---|
180 | return pnl;
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected void clearView() {
|
---|
184 | model.populate(null);
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected void updateView(Changeset cs) {
|
---|
188 | model.populate(cs.getDiscussion());
|
---|
189 | updateRowHeights();
|
---|
190 | }
|
---|
191 |
|
---|
192 | protected void updateRowHeights() {
|
---|
193 | int intercellWidth = table.getIntercellSpacing().width;
|
---|
194 | int colWidth = table.getColumnModel().getColumn(2).getWidth();
|
---|
195 | // Update row heights
|
---|
196 | for (int row = 0; row < table.getRowCount(); row++) {
|
---|
197 | int rowHeight = table.getRowHeight();
|
---|
198 |
|
---|
199 | Component comp = table.prepareRenderer(table.getCellRenderer(row, 2), row, 2);
|
---|
200 | // constrain width of component
|
---|
201 | comp.setBounds(new Rectangle(0, 0, colWidth - intercellWidth, Integer.MAX_VALUE));
|
---|
202 | rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
|
---|
203 |
|
---|
204 | table.setRowHeight(row, rowHeight);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* ---------------------------------------------------------------------------- */
|
---|
209 | /* interface PropertyChangeListener */
|
---|
210 | /* ---------------------------------------------------------------------------- */
|
---|
211 | @Override
|
---|
212 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
213 | if (!evt.getPropertyName().equals(ChangesetCacheManagerModel.CHANGESET_IN_DETAIL_VIEW_PROP))
|
---|
214 | return;
|
---|
215 | setCurrentChangeset((Changeset) evt.getNewValue());
|
---|
216 | }
|
---|
217 | }
|
---|