Changeset 2331 in josm


Ignore:
Timestamp:
Oct 27, 2009 2:51:46 PM (4 years ago)
Author:
Gubaer
Message:

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

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/DownloadAction.java

    r2330 r2331  
    3535public class DownloadAction extends JosmAction { 
    3636    private static final Logger logger = Logger.getLogger(DownloadAction.class.getName()); 
    37      
    38     private DownloadDialog dialog; 
    39     private ExtendedDialog downloadDialog; 
    40  
     37  
    4138    public DownloadAction() { 
    4239        super(tr("Download from OSM..."), "download", tr("Download map data from the OSM server."), 
     
    4542    } 
    4643 
    47     /** 
    48      * Creates the download dialog 
    49      *  
    50      * @return the downlaod dialog 
    51      */ 
    52     protected ExtendedDialog createDownloadDialog() { 
    53         if (dialog == null)  
    54             dialog = new DownloadDialog(); 
    55         dialog.restoreSettings(); 
    56  
    57         final String prefName = dialog.getClass().getName()+ ".geometry"; 
    58         final WindowGeometry wg = WindowGeometry.centerInWindow(Main.parent, 
    59                 new Dimension(1000,600)); 
    60  
    61         if (downloadDialog == null) { 
    62             JPanel downPanel = new JPanel(new BorderLayout()); 
    63             downPanel.add(dialog, BorderLayout.CENTER); 
    64             downloadDialog= new ExtendedDialog(Main.parent, 
    65                 tr("Download"), 
    66                 new String[] {tr("OK"), tr("Cancel")}); 
    67             downloadDialog.setContent(downPanel, false /* don't use a scroll pane inside the dialog */); 
    68             downloadDialog.setButtonIcons(new String[] {"ok", "cancel"}); 
    69             downloadDialog.setRememberWindowGeometry(prefName, wg); 
    70         } 
    71         return downloadDialog; 
    72     } 
    73  
    7444    public void actionPerformed(ActionEvent e) { 
    75         ExtendedDialog dlg = createDownloadDialog(); 
    76         boolean finish = false; 
    77         while (!finish) {             
    78             dlg.showDialog(); 
    79             if (dlg.getValue() == 1 /* OK */) { 
    80                 dialog.rememberSettings(); 
    81                 Bounds area = dialog.getSelectedDownloadArea();                 
    82                 if (dialog.isDownloadOsmData()) { 
    83                     DownloadOsmTask task = new DownloadOsmTask(); 
    84                     Future<?> future = task.download(dialog.isNewLayerRequired(), area, null); 
    85                     Main.worker.submit(new PostDownloadHandler(task, future)); 
    86                     finish = true; 
    87                 } 
    88                 if (dialog.isDownloadGpxData()) { 
    89                     DownloadGpsTask task = new DownloadGpsTask(); 
    90                     Future<?> future = task.download(dialog.isNewLayerRequired(),area, null); 
    91                     Main.worker.submit(new PostDownloadHandler(task, future)); 
    92                     finish = true; 
    93                 } 
    94             } else { 
    95                 finish = true; 
     45        DownloadDialog dialog = DownloadDialog.getInstance(); 
     46        dialog.setVisible(true); 
     47        if (! dialog.isCanceled()) { 
     48            dialog.rememberSettings(); 
     49            Bounds area = dialog.getSelectedDownloadArea();                 
     50            if (dialog.isDownloadOsmData()) { 
     51                DownloadOsmTask task = new DownloadOsmTask(); 
     52                Future<?> future = task.download(dialog.isNewLayerRequired(), area, null); 
     53                Main.worker.submit(new PostDownloadHandler(task, future)); 
    9654            } 
    97             if (!finish) { 
    98                 JOptionPane.showMessageDialog( 
    99                         Main.parent, 
    100                         tr("Please select at least one task to download"), 
    101                         tr("Error"), 
    102                         JOptionPane.ERROR_MESSAGE 
    103                 ); 
     55            if (dialog.isDownloadGpxData()) { 
     56                DownloadGpsTask task = new DownloadGpsTask(); 
     57                Future<?> future = task.download(dialog.isNewLayerRequired(),area, null); 
     58                Main.worker.submit(new PostDownloadHandler(task, future)); 
    10459            } 
    105         } 
    106     } 
     60        }  
     61    }     
    10762} 
  • 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} 
  • trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java

    r2327 r2331  
    233233        submitSearch.addActionListener(new ActionListener() { 
    234234            public void actionPerformed(ActionEvent e) { 
    235                 queryServer(gui); 
     235                queryServer(gui.getRootPane()); 
    236236            } 
    237237        }); 
     
    239239        searchTerm.addActionListener(new ActionListener() { 
    240240            public void actionPerformed(ActionEvent e) { 
    241                 queryServer(gui); 
     241                queryServer(gui.getRootPane()); 
    242242            } 
    243243        }); 
Note: See TracChangeset for help on using the changeset viewer.