Ticket #2114: ImproveProperties.patch

File ImproveProperties.patch, 7.4 KB (added by xeen, 15 years ago)

Get rid ok <OK> Dialogs

  • src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java

     
    127127     * @param row The row of the table from which the value is edited.
    128128     */
    129129    void propertyEdit(int row) {
     130        Collection<OsmPrimitive> sel = Main.ds.getSelected();
     131        if (sel.isEmpty()) return;
     132
    130133        String key = propertyData.getValueAt(row, 0).toString();
    131134        objKey=key;
    132         Collection<OsmPrimitive> sel = Main.ds.getSelected();
    133         if (sel.isEmpty()) {
    134             JOptionPane.showMessageDialog(Main.parent, tr("Please select the objects you want to change properties for."));
    135             return;
    136         }
     135
    137136        String msg = "<html>"+trn("This will change up to {0} object.", "This will change up to {0} objects.", sel.size(), sel.size())+"<br><br>("+tr("An empty value deletes the key.", key)+")</html>";
    138137
    139138        JPanel panel = new JPanel(new BorderLayout());
     
    303302     */
    304303    void add() {
    305304        Collection<OsmPrimitive> sel = Main.ds.getSelected();
    306         if (sel.isEmpty()) {
    307             JOptionPane.showMessageDialog(Main.parent, tr("Please select objects for which you want to change properties."));
    308             return;
    309         }
     305        if (sel.isEmpty()) return;
    310306
    311307        JPanel p = new JPanel(new BorderLayout());
    312308        p.add(new JLabel("<html>"+trn("This will change up to {0} object.","This will change up to {0} objects.", sel.size(),sel.size())+"<br><br>"+tr("Please select a key")),
     
    401397        Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, null));
    402398        Main.ds.fireSelectionChanged(sel);
    403399        selectionChanged(sel); // update table
    404          
     400
    405401        int rowCount = propertyTable.getRowCount();
    406402        propertyTable.changeSelection((row < rowCount ? row : (rowCount-1)), 0, false, false);
    407403    }
     
    438434
    439435    public JComboBox taggingPresets = new JComboBox();
    440436
     437    /**
     438     * The Add/Edit/Delete buttons (needed to be able to disable them)
     439     */
     440    private final SideButton btnAdd;
     441    private final SideButton btnEdit;
     442    private final SideButton btnDel;
    441443
     444    private final JLabel selectSth = new JLabel(tr("Please select the objects you want to change properties for."));
     445
    442446    /**
    443447     * Create a new PropertiesDialog
    444448     */
     
    547551        // combine both tables and wrap them in a scrollPane
    548552        JPanel bothTables = new JPanel();
    549553        bothTables.setLayout(new GridBagLayout());
     554        bothTables.add(selectSth, GBC.eol().fill().insets(10, 10, 10, 10));
    550555        bothTables.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
    551556        bothTables.add(propertyTable, GBC.eol().fill(GBC.BOTH));
    552557        bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
     
    600605                else
    601606                {
    602607                    int sel = propertyTable.getSelectedRow();
    603                     if (e.getActionCommand().equals("Edit")) {
    604                         if(propertyTable.getRowCount() == 1)
    605                             sel = 0;
    606                         if (sel == -1)
    607                             JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to edit."));
    608                         else
    609                             propertyEdit(sel);
    610                     } else if (e.getActionCommand().equals("Delete")) {
    611                         if (sel == -1)
    612                             JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."));
    613                         else
    614                             delete(sel);
    615                     }
     608                    // Although we might edit/delete the wrong tag here, chances are still better
     609                    // than just displaying an error message (which always "fails").
     610                    if (e.getActionCommand().equals("Edit"))
     611                        propertyEdit(sel >= 0 ? sel : 0);
     612                    else if (e.getActionCommand().equals("Delete"))
     613                        delete(sel >= 0 ? sel : 0);
    616614                }
    617615            }
    618616        };
    619617
    620618        Shortcut s = Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B,
    621619        Shortcut.GROUP_MNEMONIC);
    622         buttonPanel.add(new SideButton(marktr("Add"),"add","Properties",
    623                 tr("Add a new key/value pair to all objects"), s, buttonAction));
     620        this.btnAdd = new SideButton(marktr("Add"),"add","Properties",
     621                tr("Add a new key/value pair to all objects"), s, buttonAction);
     622        buttonPanel.add(this.btnAdd);
    624623
    625624        s = Shortcut.registerShortcut("properties:edit", tr("Edit Properties"), KeyEvent.VK_I,
    626625        Shortcut.GROUP_MNEMONIC);
    627         buttonPanel.add(new SideButton(marktr("Edit"),"edit","Properties",
    628                 tr("Edit the value of the selected key for all objects"), s, buttonAction));
     626        this.btnEdit = new SideButton(marktr("Edit"),"edit","Properties",
     627                tr("Edit the value of the selected key for all objects"), s, buttonAction);
     628        buttonPanel.add(this.btnEdit);
    629629
    630630        s = Shortcut.registerShortcut("properties:delete", tr("Delete Properties"), KeyEvent.VK_Q,
    631631        Shortcut.GROUP_MNEMONIC);
    632         buttonPanel.add(new SideButton(marktr("Delete"),"delete","Properties",
    633                 tr("Delete the selected key in all objects"), s, buttonAction));
     632        this.btnDel = new SideButton(marktr("Delete"),"delete","Properties",
     633                tr("Delete the selected key in all objects"), s, buttonAction);
     634        buttonPanel.add(this.btnDel);
    634635        add(buttonPanel, BorderLayout.SOUTH);
    635636
    636637        DataSet.selListeners.add(this);
     
    651652            propertyTable.getCellEditor().cancelCellEditing();
    652653
    653654        // re-load property data
    654 
    655655        propertyData.setRowCount(0);
    656656
    657657        Map<String, Integer> keyCount = new HashMap<String, Integer>();
     
    680680            propertyData.addRow(new Object[]{e.getKey(), e.getValue()});
    681681        }
    682682
     683        boolean hasTags = !newSelection.isEmpty() && propertyData.getRowCount() > 0;
     684        boolean hasSelection = !newSelection.isEmpty();
     685        btnAdd.setEnabled(hasSelection);
     686        btnEdit.setEnabled(hasTags);
     687        btnDel.setEnabled(hasTags);
     688        propertyTable.setVisible(hasSelection);
     689        propertyTable.getTableHeader().setVisible(hasSelection);
     690        selectSth.setVisible(!hasSelection);
     691        if(hasTags) propertyTable.changeSelection(0, 0, false, false);
     692
    683693        // re-load membership data
    684694        // this is rather expensive since we have to walk through all members of all existing relationships.
    685695        // could use back references here for speed if necessary.
     
    707717        }
    708718
    709719        membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
     720        membershipTable.setVisible(membershipData.getRowCount() > 0);
    710721
    711722        if(propertyData.getRowCount() != 0 || membershipData.getRowCount() != 0) {
    712             setTitle(tr("Properties: {0} / Memberships: {1}", 
     723            setTitle(tr("Properties: {0} / Memberships: {1}",
    713724                propertyData.getRowCount(), membershipData.getRowCount()), true);
    714725        } else {
    715726            setTitle(tr("Properties / Memberships"), false);