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

Last change on this file since 11326 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 5.6 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.actions.downloadtasks.ChangesetHeaderDownloadTask;
24import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
25import org.openstreetmap.josm.data.osm.Changeset;
26import org.openstreetmap.josm.io.OnlineResource;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29/**
30 * The panel which displays the public discussion around a changeset in a scrollable table.
31 *
32 * It listens to property change events for {@link ChangesetCacheManagerModel#CHANGESET_IN_DETAIL_VIEW_PROP}
33 * and updates its view accordingly.
34 *
35 * @since 7704
36 */
37public class ChangesetDiscussionPanel extends JPanel implements PropertyChangeListener {
38
39 private final UpdateChangesetDiscussionAction actUpdateChangesets = new UpdateChangesetDiscussionAction();
40
41 private final ChangesetDiscussionTableModel model = new ChangesetDiscussionTableModel();
42
43 private JTable table;
44
45 private transient Changeset current;
46
47 protected JPanel buildActionButtonPanel() {
48 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
49
50 JToolBar tb = new JToolBar(JToolBar.VERTICAL);
51 tb.setFloatable(false);
52
53 // -- changeset discussion update
54 tb.add(actUpdateChangesets);
55 actUpdateChangesets.initProperties(current);
56
57 pnl.add(tb);
58 return pnl;
59 }
60
61 /**
62 * Updates the current changeset discussion from the OSM server
63 */
64 class UpdateChangesetDiscussionAction extends AbstractAction {
65 UpdateChangesetDiscussionAction() {
66 putValue(NAME, tr("Update changeset discussion"));
67 new ImageProvider("dialogs/changeset", "updatechangesetcontent").getResource().attachImageIcon(this);
68 putValue(SHORT_DESCRIPTION, tr("Update the changeset discussion from the OSM server"));
69 }
70
71 @Override
72 public void actionPerformed(ActionEvent evt) {
73 if (current == null)
74 return;
75 ChangesetHeaderDownloadTask task = new ChangesetHeaderDownloadTask(
76 ChangesetDiscussionPanel.this,
77 Collections.singleton(current.getId()),
78 true /* include discussion */
79 );
80 Main.worker.submit(new PostDownloadHandler(task, task.download()));
81 }
82
83 public void initProperties(Changeset cs) {
84 setEnabled(cs != null && !Main.isOffline(OnlineResource.OSM_API));
85 }
86 }
87
88 /**
89 * Constructs a new {@code ChangesetDiscussionPanel}.
90 */
91 public ChangesetDiscussionPanel() {
92 build();
93 }
94
95 protected void setCurrentChangeset(Changeset cs) {
96 current = cs;
97 if (cs == null) {
98 clearView();
99 } else {
100 updateView(cs);
101 }
102 actUpdateChangesets.initProperties(current);
103 if (cs != null && cs.getDiscussion().size() < cs.getCommentsCount()) {
104 actUpdateChangesets.actionPerformed(null);
105 }
106 }
107
108 protected final void build() {
109 setLayout(new BorderLayout());
110 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
111 add(buildActionButtonPanel(), BorderLayout.WEST);
112 add(buildDiscussionPanel(), BorderLayout.CENTER);
113 }
114
115 private Component buildDiscussionPanel() {
116 JPanel pnl = new JPanel(new BorderLayout());
117 table = new JTable(model, new ChangesetDiscussionTableColumnModel());
118 table.getColumnModel().getColumn(2).addPropertyChangeListener(evt -> {
119 if ("width".equals(evt.getPropertyName())) {
120 updateRowHeights();
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.