source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetDiscussionPanel.java@ 7948

Last change on this file since 7948 was 7948, checked in by Don-vip, 9 years ago

fix #10939 - handle line wrapping in changeset discussion comments

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