001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.Dimension;
008import java.awt.GridBagLayout;
009import java.util.Objects;
010
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013import javax.swing.JTable;
014import javax.swing.table.DefaultTableColumnModel;
015import javax.swing.table.TableCellRenderer;
016import javax.swing.table.TableColumn;
017
018import org.openstreetmap.josm.tools.GBC;
019import org.openstreetmap.josm.tools.Utils;
020
021/**
022 * Table column model for the {@link SaveLayersTable} in the {@link SaveLayersDialog}.
023 */
024class SaveLayersTableColumnModel extends DefaultTableColumnModel {
025    /** small renderer class that handles the "should be uploaded/saved" texts. */
026    private static class RecommendedActionsTableCell implements TableCellRenderer {
027        private final JPanel pnlEmpty = new JPanel();
028        private final JLabel needsUpload = new JLabel(tr("should be uploaded"));
029        private final JLabel needsSave = new JLabel(tr("should be saved"));
030        private static final GBC DEFAULT_CELL_STYLE = GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 0);
031
032        /**
033         * Constructs a new {@code RecommendedActionsTableCell}.
034         */
035        RecommendedActionsTableCell() {
036            pnlEmpty.setPreferredSize(new Dimension(1, 19));
037            needsUpload.setPreferredSize(new Dimension(needsUpload.getPreferredSize().width, 19));
038            needsSave.setPreferredSize(new Dimension(needsSave.getPreferredSize().width, 19));
039        }
040
041        @Override
042        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
043                boolean hasFocus, int row, int column) {
044            JPanel panel = new JPanel(new GridBagLayout());
045            SaveLayerInfo info = (SaveLayerInfo) value;
046            StringBuilder sb = new StringBuilder(24);
047            sb.append("<html>");
048            if (info != null) {
049                String htmlInfoName = Utils.escapeReservedCharactersHTML(info.getName());
050                if (info.getLayer().requiresUploadToServer() && !info.getLayer().isUploadDiscouraged()) {
051                    panel.add(needsUpload, DEFAULT_CELL_STYLE);
052                    sb.append(tr("Layer ''{0}'' has modifications which should be uploaded to the server.", htmlInfoName));
053
054                } else {
055                    if (info.isUploadable()) {
056                        panel.add(pnlEmpty, DEFAULT_CELL_STYLE);
057                    }
058                    if (info.getLayer().requiresUploadToServer()) {
059                        sb.append(tr("Layer ''{0}'' has modifications which are discouraged to be uploaded.", htmlInfoName));
060                    } else {
061                        sb.append(tr("Layer ''{0}'' has no modifications to be uploaded.", htmlInfoName));
062                    }
063                }
064                sb.append("<br/>");
065
066                if (info.getLayer().requiresSaveToFile()) {
067                    panel.add(needsSave, DEFAULT_CELL_STYLE);
068                    sb.append(tr("Layer ''{0}'' has modifications which should be saved to its associated file ''{1}''.",
069                            htmlInfoName, Objects.toString(info.getFile())));
070                } else {
071                    if (info.isSavable()) {
072                        panel.add(pnlEmpty, DEFAULT_CELL_STYLE);
073                    }
074                    sb.append(tr("Layer ''{0}'' has no modifications to be saved.", htmlInfoName));
075                }
076            }
077            sb.append("</html>");
078            panel.setToolTipText(sb.toString());
079            return panel;
080        }
081    }
082
083    /**
084     * Constructs a new {@code SaveLayersTableColumnModel}.
085     */
086    SaveLayersTableColumnModel() {
087        build();
088    }
089
090    protected void build() {
091        // column 0 - layer name, save path editor
092        LayerNameAndFilePathTableCell lnfpRenderer = new LayerNameAndFilePathTableCell();
093        LayerNameAndFilePathTableCell lnfpEditor = new LayerNameAndFilePathTableCell();
094        TableColumn col = new TableColumn(0); // keep in sync with SaveLayersModel#columnFilename
095        col.setHeaderValue(tr("Layer Name and File Path"));
096        col.setResizable(true);
097        col.setCellRenderer(lnfpRenderer);
098        col.setCellEditor(lnfpEditor);
099        col.setPreferredWidth(324);
100        addColumn(col);
101
102        // column 1 - actions required
103        col = new TableColumn(1);
104        col.setHeaderValue(tr("Recommended Actions"));
105        col.setResizable(true);
106        col.setCellRenderer(new RecommendedActionsTableCell());
107        col.setPreferredWidth(150);
108        addColumn(col);
109
110        // column 2- actions to take
111        ActionFlagsTableCell aftc = new ActionFlagsTableCell();
112        col = new TableColumn(2); // keep in sync with SaveLayersModel#columnActions
113        col.setHeaderValue(tr("Actions To Take"));
114        col.setResizable(true);
115        col.setCellRenderer(aftc);
116        col.setCellEditor(aftc);
117        col.setPreferredWidth(100);
118
119        addColumn(col);
120    }
121}