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