Changeset 5003 in josm for trunk/src/org


Ignore:
Timestamp:
2012-02-19T23:17:34+01:00 (8 years ago)
Author:
xeen
Message:

fix #3409

Location:
trunk/src/org/openstreetmap/josm/gui/io
Files:
2 added
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r4932 r5003  
    99import java.awt.Dimension;
    1010import java.awt.FlowLayout;
     11import java.awt.Graphics2D;
    1112import java.awt.GridBagConstraints;
    1213import java.awt.GridBagLayout;
     14import java.awt.Image;
    1315import java.awt.event.ActionEvent;
    1416import java.awt.event.WindowAdapter;
    1517import java.awt.event.WindowEvent;
     18import java.awt.image.BufferedImage;
    1619import java.beans.PropertyChangeEvent;
    1720import java.beans.PropertyChangeListener;
     
    2427import javax.swing.AbstractAction;
    2528import javax.swing.DefaultListCellRenderer;
     29import javax.swing.ImageIcon;
    2630import javax.swing.JComponent;
    2731import javax.swing.JDialog;
     
    3337import javax.swing.KeyStroke;
    3438import javax.swing.WindowConstants;
     39import javax.swing.event.TableModelEvent;
     40import javax.swing.event.TableModelListener;
    3541
    3642import org.openstreetmap.josm.Main;
     
    4551import org.openstreetmap.josm.tools.WindowGeometry;
    4652
    47 public class SaveLayersDialog extends JDialog {
     53public class SaveLayersDialog extends JDialog implements TableModelListener {
    4854    static public enum UserAction {
    4955        /**
     
    7682
    7783        model = new SaveLayersModel();
    78         SaveLayersTable table;
    79         JScrollPane pane = new JScrollPane(table = new SaveLayersTable(model));
     84        SaveLayersTable table = new SaveLayersTable(model);
     85        JScrollPane pane = new JScrollPane(table);
    8086        model.addPropertyChangeListener(table);
     87        table.getModel().addTableModelListener(this);
     88
    8189        getContentPane().add(pane, BorderLayout.CENTER);
    8290        getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
    83         table.getTableHeader().setPreferredSize(new Dimension(table.getTableHeader().getWidth(), 40));
    8491
    8592        addWindowListener(new WindowClosingAdapter());
    8693        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    8794    }
     95
     96    private SideButton saveAndProceedActionButton = null;
    8897
    8998    /**
     
    98107        saveAndProceedAction = new SaveAndProceedAction();
    99108        model.addPropertyChangeListener(saveAndProceedAction);
    100         pnl.add(new SideButton(saveAndProceedAction));
     109        pnl.add(saveAndProceedActionButton = new SideButton(saveAndProceedAction));
    101110
    102111        discardAndProceedAction = new DiscardAndProceedAction();
     
    327336
    328337        public void initForDiscardAndExit() {
    329             putValue(NAME, tr("Discard and Exit"));
     338            putValue(NAME, tr("Exit now!"));
    330339            putValue(SHORT_DESCRIPTION, tr("Exit JOSM without saving. Unsaved changes are lost."));
    331340            putValue(SMALL_ICON, ImageProvider.get("exit"));
     
    333342
    334343        public void initForDiscardAndDelete() {
    335             putValue(NAME, tr("Discard and Delete"));
     344            putValue(NAME, tr("Delete now!"));
    336345            putValue(SHORT_DESCRIPTION, tr("Delete layers without saving. Unsaved changes are lost."));
    337346            putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
     
    353362    }
    354363
    355     class SaveAndProceedAction extends AbstractAction implements PropertyChangeListener {
     364    final class SaveAndProceedAction extends AbstractAction implements PropertyChangeListener {
     365        private static final int is = 24; // icon size
     366        private static final String BASE_ICON = "BASE_ICON";
     367        private final Image save = ImageProvider.get("save").getImage();
     368        private final Image upld = ImageProvider.get("upload").getImage();
     369        private final Image saveDis = new BufferedImage(is, is, BufferedImage.TYPE_4BYTE_ABGR);
     370        private final Image upldDis = new BufferedImage(is, is, BufferedImage.TYPE_4BYTE_ABGR);
     371
    356372        public SaveAndProceedAction() {
     373            // get disabled versions of icons
     374            new JLabel(ImageProvider.get("save")).getDisabledIcon().paintIcon(new JPanel(), saveDis.getGraphics(), 0, 0);
     375            new JLabel(ImageProvider.get("upload")).getDisabledIcon().paintIcon(new JPanel(), upldDis.getGraphics(), 0, 0);
    357376            initForSaveAndExit();
    358377        }
    359378
    360379        public void initForSaveAndExit() {
    361             putValue(NAME, tr("Save/Upload and Exit"));
     380            putValue(NAME, tr("Perform actions before exiting"));
    362381            putValue(SHORT_DESCRIPTION, tr("Exit JOSM with saving. Unsaved changes are uploaded and/or saved."));
    363             putValue(SMALL_ICON, ImageProvider.get("exit"));
     382            putValue(BASE_ICON, ImageProvider.get("exit"));
     383            redrawIcon();
    364384        }
    365385
    366386        public void initForSaveAndDelete() {
    367             putValue(NAME, tr("Save/Upload and Delete"));
     387            putValue(NAME, tr("Perform actions before deleting"));
    368388            putValue(SHORT_DESCRIPTION, tr("Save/Upload layers before deleting. Unsaved changes are not lost."));
    369             putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
     389            putValue(BASE_ICON, ImageProvider.get("dialogs", "delete"));
     390            redrawIcon();
     391        }
     392
     393        public void redrawIcon() {
     394            try { // Can fail if model is not yet setup properly
     395                Image base = ((ImageIcon) getValue(BASE_ICON)).getImage();
     396                BufferedImage newIco = new BufferedImage(is*3, is, BufferedImage.TYPE_4BYTE_ABGR);
     397                Graphics2D g = newIco.createGraphics();
     398                g.drawImage(model.getLayersToUpload().isEmpty() ? upldDis : upld, is*0, 0, is, is, null);
     399                g.drawImage(model.getLayersToSave().isEmpty()   ? saveDis : save, is*1, 0, is, is, null);
     400                g.drawImage(base,                                                 is*2, 0, is, is, null);
     401                putValue(SMALL_ICON, new ImageIcon(newIco));
     402            } catch(Exception e) {
     403                putValue(SMALL_ICON, getValue(BASE_ICON));
     404            }
    370405        }
    371406
     
    546581        }
    547582    }
     583
     584    @Override
     585    public void tableChanged(TableModelEvent arg0) {
     586        boolean dis = model.getLayersToSave().isEmpty() && model.getLayersToUpload().isEmpty();
     587        if(saveAndProceedActionButton != null) {
     588            saveAndProceedActionButton.setEnabled(!dis);
     589        }
     590        saveAndProceedAction.redrawIcon();
     591    }
    548592}
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersModel.java

    r4310 r5003  
    2424    private Mode mode;
    2525    private PropertyChangeSupport support;
     26
     27    // keep in sync with how the columns are ordered in SaveLayersTableColumnModel#build
     28    private static final int columnFilename = 0;
     29    private static final int columnActions = 2;
    2630
    2731    public SaveLayersModel() {
     
    8387    @Override
    8488    public boolean isCellEditable(int row, int column) {
    85         return column == 3 || column == 4 || column == 5;
     89        return column == columnFilename || column == columnActions;
    8690    }
    8791
     
    8993    public void setValueAt(Object value, int row, int column) {
    9094        switch(column) {
    91             case 3 /* file name */:
    92                 this.layerInfo.get(row).setFile((File)value);
    93                 this.layerInfo.get(row).setDoSaveToFile(true);
    94                 break;
    95             case 4 /* upload */: this.layerInfo.get(row).setDoUploadToServer((Boolean)value);break;
    96             case 5 /* save */: this.layerInfo.get(row).setDoSaveToFile((Boolean)value);break;
     95        case columnFilename:
     96            this.layerInfo.get(row).setFile((File)value);
     97            this.layerInfo.get(row).setDoSaveToFile(true);
     98            break;
     99        case columnActions:
     100            boolean[] values = (boolean[]) value;
     101            this.layerInfo.get(row).setDoUploadToServer(values[0]);
     102            this.layerInfo.get(row).setDoSaveToFile(values[1]);
     103            break;
    97104        }
    98105        fireTableDataChanged();
     
    104111
    105112    public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
    106         List<SaveLayerInfo> ret =new ArrayList<SaveLayerInfo>();
     113        List<SaveLayerInfo> ret = new ArrayList<SaveLayerInfo>();
    107114        for (SaveLayerInfo info: layerInfo) {
    108115            if (info.isDoSaveToFile() && info.getFile() == null) {
     
    215222        return ret;
    216223    }
    217 
    218224}
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersTable.java

    r3083 r5003  
    22package org.openstreetmap.josm.gui.io;
    33
     4import java.awt.Dimension;
    45import java.beans.PropertyChangeEvent;
    56import java.beans.PropertyChangeListener;
     
    1314        super(model, new SaveLayersTableColumnModel());
    1415        putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
     16        super.setRowHeight(39);
     17        super.getTableHeader().setPreferredSize(new Dimension(super.getTableHeader().getWidth(), 24));
     18        super.getTableHeader().setReorderingAllowed(false);
    1519    }
    1620
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersTableColumnModel.java

    r3083 r5003  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     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;
    613import javax.swing.table.DefaultTableColumnModel;
    7 import javax.swing.table.TableCellEditor;
    814import javax.swing.table.TableCellRenderer;
    915import javax.swing.table.TableColumn;
    1016
     17import org.openstreetmap.josm.tools.GBC;
     18
    1119class SaveLayersTableColumnModel extends DefaultTableColumnModel {
     20    /** small renderer class that handles the "should be uploaded/saved" texts. */
     21    private static class RecommendedActionsTableCell extends JPanel implements TableCellRenderer {
     22        private final static JPanel pnlEmpty = new JPanel();
     23        private final static JLabel needsUpload = new JLabel(tr("should be uploaded"));
     24        private final static JLabel needsSave = new JLabel(tr("should be saved"));
     25        private final static GBC defaultCellStyle = GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 0);
     26
     27        public RecommendedActionsTableCell() {
     28            setLayout(new GridBagLayout());
     29            pnlEmpty.setPreferredSize(new Dimension(1, 19));
     30            needsUpload.setPreferredSize(new Dimension(needsUpload.getPreferredSize().width, 19));
     31            needsSave.setPreferredSize(new Dimension(needsSave.getPreferredSize().width, 19));
     32        }
     33
     34        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
     35                boolean hasFocus, int row, int column) {
     36            SaveLayerInfo info = (SaveLayerInfo)value;
     37            StringBuilder sb = new StringBuilder();
     38            sb.append("<html>");
     39            if (info.getLayer().requiresUploadToServer()) {
     40                add(needsUpload, defaultCellStyle);
     41                sb.append(tr("Layer ''{0}'' has modifications which should be uploaded to the server.", info.getName()));
     42
     43            } else {
     44                add(pnlEmpty, defaultCellStyle);
     45                sb.append(tr("Layer ''{0}'' has no modifications to be uploaded.", info.getName()));
     46            }
     47            sb.append("<br/>");
     48
     49            if (info.getLayer().requiresSaveToFile()) {
     50                add(needsSave, defaultCellStyle);
     51                sb.append(tr("Layer ''{0}'' has modifications which should be saved to its associated file ''{1}''.", info.getName(), info.getFile().toString()));
     52            } else {
     53                add(pnlEmpty, defaultCellStyle);
     54                sb.append(tr("Layer ''{0}'' has no modifications to be saved.", info.getName()));
     55            }
     56            sb.append("</html>");
     57            setToolTipText(sb.toString());
     58            return this;
     59        }
     60    }
     61
    1262    protected void build() {
    1363        TableColumn col = null;
    14         TableCellRenderer renderer = new SaveLayerInfoCellRenderer();
    15         TableCellEditor fileNameEditor = new FilenameCellEditor();
    16         TableCellEditor saveFlagEditor = new SaveFlagCellEditor();
    1764
    18         // column 0 - Layer
    19         col = new TableColumn(0);
    20         col.setHeaderValue(tr("Layer"));
     65        // column 0 - layer name, save path editor
     66        LayerNameAndFilePathTableCell lnafptc = new LayerNameAndFilePathTableCell();
     67        col = new TableColumn(0); // keep in sync with SaveLayersModel#columnFilename
     68        col.setHeaderValue(tr("Layer Name and File Path"));
    2169        col.setResizable(true);
    22         col.setCellRenderer(renderer);
    23         col.setPreferredWidth(100);
     70        col.setCellRenderer(lnafptc);
     71        col.setCellEditor(lnafptc);
     72        col.setPreferredWidth(324);
    2473        addColumn(col);
    2574
    26         // column 1 - Upload required
     75        // column 1 - actions required
    2776        col = new TableColumn(1);
    28         col.setHeaderValue(tr("Should upload?"));
     77        col.setHeaderValue(tr("Recommended Actions"));
    2978        col.setResizable(true);
    30         col.setCellRenderer(renderer);
    31         col.setPreferredWidth(50);
     79        col.setCellRenderer(new RecommendedActionsTableCell());
     80        col.setPreferredWidth(150);
    3281        addColumn(col);
    3382
    34         // column 2 - Save to file required
    35         col = new TableColumn(2);
    36         col.setHeaderValue(tr("Should save?"));
     83        // column 2- actions to take
     84        ActionFlagsTableCell aftc = new ActionFlagsTableCell();
     85        col = new TableColumn(2); // keep in sync with SaveLayersModel#columnActions
     86        col.setHeaderValue(tr("Actions To Take"));
    3787        col.setResizable(true);
    38         col.setCellRenderer(renderer);
    39         col.setPreferredWidth(50);
    40         addColumn(col);
    41 
    42         // column 3 - filename
    43         col = new TableColumn(3);
    44         col.setHeaderValue(tr("Filename"));
    45         col.setResizable(true);
    46         col.setCellRenderer(renderer);
    47         col.setCellEditor(fileNameEditor);
    48         col.setPreferredWidth(200);
    49         addColumn(col);
    50 
    51         // column 4 - Upload
    52         col = new TableColumn(4);
    53         col.setHeaderValue(tr("Upload"));
    54         col.setResizable(true);
    55         col.setCellRenderer(renderer);
    56         col.setCellEditor(saveFlagEditor);
    57         col.setPreferredWidth(30);
    58         addColumn(col);
    59 
    60         // column 5 - Save
    61         col = new TableColumn(5);
    62         col.setHeaderValue(tr("Save"));
    63         col.setResizable(true);
    64         col.setCellRenderer(renderer);
    65         col.setCellEditor(saveFlagEditor);
    66         col.setPreferredWidth(30);
     88        col.setCellRenderer(aftc);
     89        col.setCellEditor(aftc);
     90        col.setPreferredWidth(100);
    6791
    6892        addColumn(col);
Note: See TracChangeset for help on using the changeset viewer.