Ignore:
Timestamp:
2009-10-27T14:51:46+01:00 (17 years ago)
Author:
Gubaer
Message:

Cleanup in DownloadAction and DownloadDialog
Added context-sensitive help to DownloadDialog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java

    r2330 r2331  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.BorderLayout;
    67import java.awt.Color;
     8import java.awt.Component;
     9import java.awt.Dimension;
     10import java.awt.FlowLayout;
    711import java.awt.Font;
    812import java.awt.GridBagLayout;
     
    1317import java.awt.event.InputEvent;
    1418import java.awt.event.KeyEvent;
     19import java.awt.event.WindowAdapter;
     20import java.awt.event.WindowEvent;
    1521import java.util.ArrayList;
    1622import java.util.List;
     
    1925import javax.swing.AbstractAction;
    2026import javax.swing.JCheckBox;
     27import javax.swing.JComponent;
     28import javax.swing.JDialog;
    2129import javax.swing.JLabel;
     30import javax.swing.JOptionPane;
    2231import javax.swing.JPanel;
    2332import javax.swing.JTabbedPane;
     
    2736import org.openstreetmap.josm.data.Bounds;
    2837import org.openstreetmap.josm.gui.MapView;
     38import org.openstreetmap.josm.gui.SideButton;
     39import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
     40import org.openstreetmap.josm.gui.help.HelpUtil;
    2941import org.openstreetmap.josm.plugins.PluginHandler;
    3042import org.openstreetmap.josm.tools.GBC;
     43import org.openstreetmap.josm.tools.ImageProvider;
    3144import org.openstreetmap.josm.tools.OsmUrlToBounds;
     45import org.openstreetmap.josm.tools.WindowGeometry;
     46import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    3247
    3348/**
    34  * Main download dialog.
    35  *
    36  * Can be extended by plugins in two ways:
    37  * (1) by adding download tasks that are then called with the selected bounding box
    38  * (2) by adding "DownloadSelection" objects that implement different ways of selecting a bounding box
    39  *
    40  * @author Frederik Ramm <frederik@remote.org>
    41  *
     49 *
    4250 */
    43 public class DownloadDialog extends JPanel {
     51public class DownloadDialog extends JDialog {
    4452    static private final Logger logger = Logger.getLogger(DownloadDialog.class.getName());
     53   
     54    /** the unique instance of the download dialog */
     55    static private DownloadDialog instance;
     56   
     57    /**
     58     * Replies the unique instance of the download dialog
     59     *
     60     * @return the unique instance of the download dialog
     61     */
     62    static public DownloadDialog getInstance() {
     63        if (instance == null)
     64            instance = new DownloadDialog(Main.parent);
     65        return instance;
     66    }   
    4567
    4668    private final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
    4769    private final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
    48     private final JCheckBox cbNewLayer;
     70    private JCheckBox cbNewLayer;
    4971    private final JLabel sizeCheck = new JLabel();
    50 
    5172    private Bounds currentBounds = null;
     73    private boolean canceled;
    5274
    5375    private JCheckBox cbDownloadOsmData = new JCheckBox(tr("OpenStreetMap data"), true);
    5476    private JCheckBox cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"));
    5577
    56 
    57     public DownloadDialog() {
    58         setLayout(new GridBagLayout());
     78   
     79    public JPanel buildMainPanel() {
     80        JPanel pnl = new JPanel();
     81        pnl.setLayout(new GridBagLayout());
    5982
    6083        // adding the download tasks
    61         add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
    62         add(cbDownloadOsmData,  GBC.eol().insets(20,0,0,0));
    63         add(cbDownloadGpxData,  GBC.eol().insets(20,0,0,0));
     84        pnl.add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0));
     85        pnl.add(cbDownloadOsmData,  GBC.eol().insets(20,0,0,0));
     86        pnl.add(cbDownloadGpxData,  GBC.eol().insets(20,0,0,0));
    6487       
    6588        // predefined download selections
     
    81104   
    82105        cbNewLayer = new JCheckBox(tr("Download as new layer"));
    83         add(cbNewLayer, GBC.eol().insets(0,5,0,0));
    84 
    85         add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
    86         add(tpDownloadAreaSelectors, GBC.eol().fill());
     106        pnl.add(cbNewLayer, GBC.eol().insets(0,5,0,0));
     107
     108        pnl. add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0));
     109        pnl.add(tpDownloadAreaSelectors, GBC.eol().fill());
    87110
    88111        try {
     
    94117        Font labelFont = sizeCheck.getFont();
    95118        sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
    96         add(sizeCheck, GBC.eop().insets(0,5,5,10));
    97 
    98         getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
     119        pnl.add(sizeCheck, GBC.eop().insets(0,5,5,10));
     120        return pnl;
     121    }
     122   
     123    protected JPanel buildButtonPanel() {
     124        JPanel pnl = new JPanel();
     125        pnl.setLayout(new FlowLayout());
     126       
     127        pnl.add(new SideButton(new DownloadAction()));
     128        pnl.add(new SideButton(new CancelAction()));
     129        pnl.add(new SideButton(new ContextSensitiveHelpAction(ht("/Dialog/DownloadDialog"))));
     130        return pnl;       
     131    }
     132   
     133    public DownloadDialog(Component parent) {
     134        super(JOptionPane.getFrameForComponent(parent),tr("Download"), true /* modal */);
     135        getContentPane().setLayout(new BorderLayout());
     136        getContentPane().add(buildMainPanel(), BorderLayout.CENTER);
     137        getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
     138       
     139        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    99140                KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "checkClipboardContents");
    100141
    101         getActionMap().put("checkClipboardContents", new AbstractAction() {
     142        getRootPane().getActionMap().put("checkClipboardContents", new AbstractAction() {
    102143            public void actionPerformed(ActionEvent e) {
    103144                checkClipboardContents();
    104145            }
    105146        });
    106        
     147        HelpUtil.setHelpContext(getRootPane(), ht("/Dialog/DownloadDialog"));
     148        addWindowListener(new WindowEventHandler());
    107149        restoreSettings();
    108150    }
     151   
    109152
    110153    private void checkClipboardContents() {
     
    247290    }
    248291   
     292    @Override
     293    public void setVisible(boolean visible) {
     294        if (visible) {
     295            new WindowGeometry(
     296                    getClass().getName() + ".geometry",
     297                    WindowGeometry.centerInWindow(
     298                            getParent(),
     299                            new Dimension(1000,600)
     300                    )
     301            ).apply(this);
     302        } else if (!visible && isShowing()){
     303            new WindowGeometry(this).remember(getClass().getName() + ".geometry");
     304        }
     305        super.setVisible(visible);
     306    }
     307
     308    /**
     309     * Replies true if the dialog was canceled
     310     *
     311     * @return true if the dialog was canceled
     312     */
     313    public boolean isCanceled() {
     314        return canceled;
     315    }
     316
     317    protected void setCanceled(boolean canceled) {
     318        this.canceled = canceled;
     319    }
     320   
     321    class CancelAction extends AbstractAction {
     322        public CancelAction() {
     323            putValue(NAME, tr("Cancel"));
     324            putValue(SMALL_ICON, ImageProvider.get("cancel"));
     325            putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort downloading"));           
     326        }
     327       
     328        public void run() {
     329            setCanceled(true);
     330            setVisible(false);   
     331        }
     332       
     333        public void actionPerformed(ActionEvent e) {
     334            run();
     335        }       
     336    }
     337
     338    class DownloadAction extends AbstractAction {
     339        public DownloadAction() {
     340            putValue(NAME, tr("Download"));
     341            putValue(SMALL_ICON, ImageProvider.get("download"));
     342            putValue(SHORT_DESCRIPTION, tr("Click do download the currently selected area"));           
     343        }
     344       
     345        public void actionPerformed(ActionEvent e) {
     346            if (currentBounds == null) {
     347                JOptionPane.showMessageDialog(
     348                        DownloadDialog.this,
     349                        tr("Please select a download area first."),
     350                        tr("Error"),
     351                        JOptionPane.ERROR_MESSAGE
     352                );
     353                return;
     354            }
     355            if (!isDownloadOsmData() && !isDownloadOsmData()) {
     356                JOptionPane.showMessageDialog(
     357                        DownloadDialog.this,
     358                        tr("<html>Neither <strong>{0}</strong> nor <strong>{1}</strong> is enabled.<br>"
     359                                + "Please chose to either download OSM data, or GPX data, or both.</html>",
     360                        cbDownloadOsmData.getText(),
     361                        cbDownloadGpxData.getText()
     362                        ),
     363                        tr("Error"),
     364                        JOptionPane.ERROR_MESSAGE
     365                );
     366                return;
     367            }
     368            setCanceled(false);
     369            setVisible(false);           
     370        }       
     371    }
     372   
     373    class WindowEventHandler extends WindowAdapter {
     374        @Override
     375        public void windowClosing(WindowEvent e) {
     376            new CancelAction().run();
     377        }       
     378    }
    249379}
Note: See TracChangeset for help on using the changeset viewer.