Changeset 2403 in josm


Ignore:
Timestamp:
Nov 7, 2009 6:02:39 PM (4 years ago)
Author:
Gubaer
Message:

fixed #454: Right-click context menus in download text-entry boxes

File:
1 edited

Legend:

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

    r2361 r2403  
    77import java.awt.Dimension; 
    88import java.awt.GridBagLayout; 
     9import java.awt.Toolkit; 
     10import java.awt.datatransfer.DataFlavor; 
     11import java.awt.datatransfer.FlavorEvent; 
     12import java.awt.datatransfer.FlavorListener; 
     13import java.awt.datatransfer.Transferable; 
     14import java.awt.datatransfer.UnsupportedFlavorException; 
    915import java.awt.event.ActionEvent; 
    1016import java.awt.event.ActionListener; 
    1117import java.awt.event.FocusAdapter; 
    1218import java.awt.event.FocusEvent; 
    13  
     19import java.awt.event.MouseAdapter; 
     20import java.awt.event.MouseEvent; 
     21import java.io.IOException; 
     22 
     23import javax.swing.AbstractAction; 
    1424import javax.swing.BorderFactory; 
    1525import javax.swing.JLabel; 
    1626import javax.swing.JPanel; 
     27import javax.swing.JPopupMenu; 
    1728import javax.swing.JTextArea; 
    1829import javax.swing.JTextField; 
     
    2637import org.openstreetmap.josm.data.coor.LatLon; 
    2738import org.openstreetmap.josm.tools.GBC; 
     39import org.openstreetmap.josm.tools.ImageProvider; 
    2840import org.openstreetmap.josm.tools.OsmUrlToBounds; 
    2941 
     
    3951 
    4052    private JTextField[] latlon = null; 
    41     private final JTextArea osmUrl = new JTextArea(); 
     53    private final JTextArea tfOsmUrl = new JTextArea(); 
    4254    private final JTextArea showUrl = new JTextArea(); 
    4355    private DownloadDialog parent; 
    4456 
    45      
     57 
    4658    protected void buildDownloadAreaInputFields() { 
    4759        latlon = new JTextField[4]; 
     
    5870        latlon[2].addFocusListener(latChecker); 
    5971        latlon[2].addActionListener(latChecker); 
    60          
     72 
    6173        LonValueChecker lonChecker = new LonValueChecker(latlon[1]); 
    6274        latlon[1].addFocusListener(lonChecker); 
     
    6678        latlon[3].addFocusListener(lonChecker); 
    6779        latlon[3].addActionListener(lonChecker); 
    68          
    69     } 
    70      
     80    } 
     81 
    7182    public void addGui(final DownloadDialog gui) { 
    7283        buildDownloadAreaInputFields(); 
    73         JPanel dlg = new JPanel(new GridBagLayout()); 
    74  
    75         osmUrl.getDocument().addDocumentListener(new OsmUrlRefresher()); 
     84        final JPanel dlg = new JPanel(new GridBagLayout()); 
     85 
     86        tfOsmUrl.getDocument().addDocumentListener(new OsmUrlRefresher()); 
    7687 
    7788        // select content on receiving focus. this seems to be the default in the 
    7889        // windows look+feel but not for others. needs invokeLater to avoid strange 
    7990        // side effects that will cancel out the newly made selection otherwise. 
    80         osmUrl.addFocusListener(new SelectAllOnFocusHandler(osmUrl)); 
    81         osmUrl.setLineWrap(true); 
    82         osmUrl.setBorder(latlon[0].getBorder()); 
     91        tfOsmUrl.addFocusListener(new SelectAllOnFocusHandler(tfOsmUrl)); 
     92        tfOsmUrl.setLineWrap(true); 
     93        tfOsmUrl.setBorder(latlon[0].getBorder()); 
    8394 
    8495        dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0)); 
     
    92103 
    93104        dlg.add(new JLabel(tr("URL from www.openstreetmap.org (you can paste an URL here to download the area)")), GBC.eol().insets(10,20,5,0)); 
    94         dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill()); 
     105        dlg.add(tfOsmUrl, GBC.eop().insets(10,0,5,0).fill()); 
     106        tfOsmUrl.addMouseListener( 
     107                new MouseAdapter() { 
     108                    @Override 
     109                    public void mousePressed(MouseEvent e) { 
     110                        checkPopup(e); 
     111                    } 
     112 
     113                    @Override 
     114                    public void mouseClicked(MouseEvent e) { 
     115                        checkPopup(e); 
     116                    } 
     117 
     118                    @Override 
     119                    public void mouseReleased(MouseEvent e) { 
     120                        checkPopup(e); 
     121                    } 
     122 
     123                    private void checkPopup(MouseEvent e) { 
     124                        if (e.isPopupTrigger()) { 
     125                            OsmUrlPopup popup = new OsmUrlPopup(); 
     126                            popup.show(tfOsmUrl, e.getX(), e.getY()); 
     127                        } 
     128                    } 
     129                } 
     130        ); 
    95131        dlg.add(showUrl, GBC.eop().insets(10,0,5,5)); 
    96132        showUrl.setEditable(false); 
     
    102138    } 
    103139 
    104      
     140 
    105141    public void setDownloadArea(Bounds area) { 
    106142        updateBboxFields(area); 
    107143        updateUrl(area); 
    108144    } 
    109      
     145 
    110146    public Bounds getDownloadArea() { 
    111147        double[] values = new double[4]; 
     
    120156            return null; 
    121157        if (!LatLon.isValidLat(values[2]) || !LatLon.isValidLon(values[3])) 
    122             return null;         
     158            return null; 
    123159        return new Bounds(values); 
    124160    } 
    125161 
    126162    private boolean parseURL(DownloadDialog gui) { 
    127         Bounds b = OsmUrlToBounds.parse(osmUrl.getText()); 
    128         if(b == null) return false;         
     163        Bounds b = OsmUrlToBounds.parse(tfOsmUrl.getText()); 
     164        if(b == null) return false; 
    129165        gui.boundingBoxChanged(b,BoundingBoxSelection.this); 
    130166        updateBboxFields(b); 
     
    148184        showUrl.setText(OsmUrlToBounds.getURL(area)); 
    149185    } 
    150      
    151      
     186 
     187 
    152188    class LatValueChecker extends FocusAdapter implements ActionListener{ 
    153189        private JTextField tfLatValue; 
    154          
     190 
    155191        private Border errorBorder = BorderFactory.createLineBorder(Color.RED, 1); 
    156192        protected void setErrorMessage(String msg) { 
     
    160196            } else { 
    161197                tfLatValue.setBorder(UIManager.getBorder("TextField.border")); 
    162                 tfLatValue.setToolTipText("");                 
    163             } 
    164         } 
    165       
     198                tfLatValue.setToolTipText(""); 
     199            } 
     200        } 
     201 
    166202        public LatValueChecker(JTextField tfLatValue) { 
    167203            this.tfLatValue = tfLatValue; 
    168204        } 
    169          
     205 
    170206        protected void check() { 
    171207            double value = 0; 
     
    178214            if (!LatLon.isValidLat(value)) { 
    179215                setErrorMessage(tr("Value for latitude in range [-90,90] required.", tfLatValue.getText())); 
    180                 return;             
     216                return; 
    181217            } 
    182218            setErrorMessage(null); 
    183219        } 
    184          
     220 
    185221        @Override 
    186222        public void focusLost(FocusEvent e) { 
     
    189225 
    190226        public void actionPerformed(ActionEvent e) { 
    191             check();             
    192         }         
    193     } 
    194      
     227            check(); 
     228        } 
     229    } 
     230 
    195231    class LonValueChecker extends FocusAdapter implements ActionListener { 
    196232        private JTextField tfLonValue; 
     
    202238            } else { 
    203239                tfLonValue.setBorder(UIManager.getBorder("TextField.border")); 
    204                 tfLonValue.setToolTipText("");                 
    205             } 
    206         } 
    207          
     240                tfLonValue.setToolTipText(""); 
     241            } 
     242        } 
     243 
    208244        public LonValueChecker(JTextField tfLonValue) { 
    209245            this.tfLonValue = tfLonValue; 
    210246        } 
    211          
     247 
    212248        protected void check() { 
    213249            double value = 0; 
     
    215251                value = Double.parseDouble(tfLonValue.getText()); 
    216252            } catch(NumberFormatException ex) { 
    217                setErrorMessage(tr("The string ''{0}'' isn''t a valid double value.", tfLonValue.getText())); 
    218                return; 
     253                setErrorMessage(tr("The string ''{0}'' isn''t a valid double value.", tfLonValue.getText())); 
     254                return; 
    219255            } 
    220256            if (!LatLon.isValidLon(value)) { 
     
    224260            setErrorMessage(null); 
    225261        } 
    226          
     262 
    227263        @Override 
    228264        public void focusLost(FocusEvent e) { 
     
    232268        public void actionPerformed(ActionEvent e) { 
    233269            check(); 
    234         }         
    235     } 
    236      
     270        } 
     271    } 
     272 
    237273    class SelectAllOnFocusHandler extends FocusAdapter { 
    238274        private JTextComponent tfTarget; 
    239275        public SelectAllOnFocusHandler(JTextComponent tfTarget) { 
    240             this.tfTarget = tfTarget;             
    241         } 
    242          
     276            this.tfTarget = tfTarget; 
     277        } 
     278 
    243279        @Override 
    244280        public void focusGained(FocusEvent e) { 
    245281            tfTarget.selectAll(); 
    246         }         
    247     } 
    248      
     282        } 
     283    } 
     284 
    249285    class OsmUrlRefresher implements DocumentListener { 
    250286        public void changedUpdate(DocumentEvent e) { parseURL(parent); } 
     
    252288        public void removeUpdate(DocumentEvent e) { parseURL(parent); } 
    253289    } 
     290 
     291    class PasteUrlAction extends AbstractAction implements FlavorListener { 
     292 
     293        public PasteUrlAction() { 
     294            putValue(NAME, tr("Paste")); 
     295            putValue(SMALL_ICON, ImageProvider.get("paste")); 
     296            putValue(SHORT_DESCRIPTION, tr("Paste URL from clipboard")); 
     297            Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(this); 
     298        } 
     299 
     300        protected String getClipboardContent() { 
     301            Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
     302            try { 
     303                if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 
     304                    String text = (String)t.getTransferData(DataFlavor.stringFlavor); 
     305                    return text; 
     306                } 
     307            } catch (UnsupportedFlavorException ex) { 
     308                ex.printStackTrace(); 
     309                return null; 
     310            } catch (IOException ex) { 
     311                ex.printStackTrace(); 
     312                return null; 
     313            } 
     314            return null; 
     315        } 
     316 
     317        public void actionPerformed(ActionEvent e) { 
     318            String content = getClipboardContent(); 
     319            if (content != null) { 
     320                tfOsmUrl.setText(content); 
     321            } 
     322        } 
     323 
     324        protected void updateEnabledState() { 
     325            setEnabled(getClipboardContent() != null); 
     326        } 
     327 
     328        public void flavorsChanged(FlavorEvent e) { 
     329            updateEnabledState(); 
     330        } 
     331    } 
     332 
     333    class OsmUrlPopup extends JPopupMenu { 
     334        public OsmUrlPopup() { 
     335            add(new PasteUrlAction()); 
     336        } 
     337    } 
    254338} 
Note: See TracChangeset for help on using the changeset viewer.