source: josm/trunk/src/org/openstreetmap/josm/gui/io/SaveLayersTableColumnModel.java@ 11848

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

fix #14613 - Special HTML characters not escaped in GUI error messages

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Dimension;
8import java.awt.GridBagLayout;
9
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12import javax.swing.JTable;
13import javax.swing.table.DefaultTableColumnModel;
14import javax.swing.table.TableCellRenderer;
15import javax.swing.table.TableColumn;
16
17import org.openstreetmap.josm.tools.GBC;
18import org.openstreetmap.josm.tools.Utils;
19
20class SaveLayersTableColumnModel extends DefaultTableColumnModel {
21 /** small renderer class that handles the "should be uploaded/saved" texts. */
22 private static class RecommendedActionsTableCell implements TableCellRenderer {
23 private final JPanel pnlEmpty = new JPanel();
24 private final JLabel needsUpload = new JLabel(tr("should be uploaded"));
25 private final JLabel needsSave = new JLabel(tr("should be saved"));
26 private static final GBC defaultCellStyle = GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 0);
27
28 /**
29 * Constructs a new {@code RecommendedActionsTableCell}.
30 */
31 RecommendedActionsTableCell() {
32 pnlEmpty.setPreferredSize(new Dimension(1, 19));
33 needsUpload.setPreferredSize(new Dimension(needsUpload.getPreferredSize().width, 19));
34 needsSave.setPreferredSize(new Dimension(needsSave.getPreferredSize().width, 19));
35 }
36
37 @Override
38 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
39 boolean hasFocus, int row, int column) {
40 JPanel panel = new JPanel(new GridBagLayout());
41 SaveLayerInfo info = (SaveLayerInfo) value;
42 StringBuilder sb = new StringBuilder(24);
43 sb.append("<html>");
44 if (info != null) {
45 String htmlInfoName = Utils.escapeReservedCharactersHTML(info.getName());
46 if (info.getLayer().requiresUploadToServer() && !info.getLayer().isUploadDiscouraged()) {
47 panel.add(needsUpload, defaultCellStyle);
48 sb.append(tr("Layer ''{0}'' has modifications which should be uploaded to the server.", htmlInfoName));
49
50 } else {
51 if (info.isUploadable()) {
52 panel.add(pnlEmpty, defaultCellStyle);
53 }
54 if (info.getLayer().requiresUploadToServer()) {
55 sb.append(tr("Layer ''{0}'' has modifications which are discouraged to be uploaded.", htmlInfoName));
56 } else {
57 sb.append(tr("Layer ''{0}'' has no modifications to be uploaded.", htmlInfoName));
58 }
59 }
60 sb.append("<br/>");
61
62 if (info.getLayer().requiresSaveToFile()) {
63 panel.add(needsSave, defaultCellStyle);
64 sb.append(tr("Layer ''{0}'' has modifications which should be saved to its associated file ''{1}''.",
65 htmlInfoName, info.getFile().toString()));
66 } else {
67 if (info.isSavable()) {
68 panel.add(pnlEmpty, defaultCellStyle);
69 }
70 sb.append(tr("Layer ''{0}'' has no modifications to be saved.", htmlInfoName));
71 }
72 }
73 sb.append("</html>");
74 panel.setToolTipText(sb.toString());
75 return panel;
76 }
77 }
78
79 /**
80 * Constructs a new {@code SaveLayersTableColumnModel}.
81 */
82 SaveLayersTableColumnModel() {
83 build();
84 }
85
86 protected void build() {
87 // column 0 - layer name, save path editor
88 LayerNameAndFilePathTableCell lnfpRenderer = new LayerNameAndFilePathTableCell();
89 LayerNameAndFilePathTableCell lnfpEditor = new LayerNameAndFilePathTableCell();
90 TableColumn col = new TableColumn(0); // keep in sync with SaveLayersModel#columnFilename
91 col.setHeaderValue(tr("Layer Name and File Path"));
92 col.setResizable(true);
93 col.setCellRenderer(lnfpRenderer);
94 col.setCellEditor(lnfpEditor);
95 col.setPreferredWidth(324);
96 addColumn(col);
97
98 // column 1 - actions required
99 col = new TableColumn(1);
100 col.setHeaderValue(tr("Recommended Actions"));
101 col.setResizable(true);
102 col.setCellRenderer(new RecommendedActionsTableCell());
103 col.setPreferredWidth(150);
104 addColumn(col);
105
106 // column 2- actions to take
107 ActionFlagsTableCell aftc = new ActionFlagsTableCell();
108 col = new TableColumn(2); // keep in sync with SaveLayersModel#columnActions
109 col.setHeaderValue(tr("Actions To Take"));
110 col.setResizable(true);
111 col.setCellRenderer(aftc);
112 col.setCellEditor(aftc);
113 col.setPreferredWidth(100);
114
115 addColumn(col);
116 }
117}
Note: See TracBrowser for help on using the repository browser.