Changeset 9657 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-01-27T22:18:36+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #12410 - new button to refresh relation in relation editor (patch by kolesar, modified for checkstyle, javadoc, unit test)

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r9588 r9657  
    7777import org.openstreetmap.josm.gui.dialogs.relation.actions.OKAction;
    7878import org.openstreetmap.josm.gui.dialogs.relation.actions.PasteMembersAction;
     79import org.openstreetmap.josm.gui.dialogs.relation.actions.RefreshAction;
    7980import org.openstreetmap.josm.gui.dialogs.relation.actions.RemoveAction;
    8081import org.openstreetmap.josm.gui.dialogs.relation.actions.RemoveSelectedAction;
     
    128129    private JButton sortBelowButton;
    129130    /**
     131     * Action for performing the {@link RefreshAction}
     132     */
     133    private RefreshAction refreshAction;
     134    /**
     135     * Action for performing the {@link ApplyAction}
     136     */
     137    private ApplyAction applyAction;
     138    /**
    130139     * Action for performing the {@link CancelAction}
    131140     */
     
    172181
    173182        tagEditorPanel = new TagEditorPanel(relation, presetHandler);
    174 
    175         // populate the models
    176         //
    177         if (relation != null) {
    178             tagEditorPanel.getModel().initFromPrimitive(relation);
    179             this.memberTableModel.populate(relation);
    180             if (!getLayer().data.getRelations().contains(relation)) {
    181                 // treat it as a new relation if it doesn't exist in the
    182                 // data set yet.
    183                 setRelation(null);
    184             }
    185         } else {
    186             tagEditorPanel.getModel().clear();
    187             this.memberTableModel.populate(null);
    188         }
     183        populateModels(relation);
    189184        tagEditorPanel.getModel().ensureOneTag();
    190185
     
    259254    }
    260255
    261     protected void cancel() {
     256    /**
     257     * Reloads data from relation.
     258     */
     259    public void reloadDataFromRelation() {
     260        setRelation(getRelation());
     261        populateModels(getRelation());
     262        refreshAction.updateEnabledState();
     263    }
     264
     265    private void populateModels(Relation relation) {
     266        if (relation != null) {
     267            tagEditorPanel.getModel().initFromPrimitive(relation);
     268            memberTableModel.populate(relation);
     269            if (!getLayer().data.getRelations().contains(relation)) {
     270                // treat it as a new relation if it doesn't exist in the data set yet.
     271                setRelation(null);
     272            }
     273        } else {
     274            tagEditorPanel.getModel().clear();
     275            memberTableModel.populate(null);
     276        }
     277    }
     278
     279    /**
     280     * Apply changes.
     281     * @see ApplyAction
     282     */
     283    public void apply() {
     284        applyAction.actionPerformed(null);
     285    }
     286
     287    /**
     288     * Cancel changes.
     289     * @see CancelAction
     290     */
     291    public void cancel() {
    262292        cancelAction.actionPerformed(null);
    263293    }
     
    269299     */
    270300    protected JToolBar buildToolBar() {
    271         JToolBar tb  = new JToolBar();
     301        JToolBar tb = new JToolBar();
    272302        tb.setFloatable(false);
    273         tb.add(new ApplyAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this));
     303        refreshAction = new RefreshAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this);
     304        applyAction = new ApplyAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this);
     305        tb.add(refreshAction);
     306        tb.add(applyAction);
    274307        tb.add(new DuplicateRelationAction(memberTableModel, tagEditorPanel.getModel(), getLayer()));
    275308        DeleteCurrentRelationAction deleteAction = new DeleteCurrentRelationAction(getLayer(), this);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r9632 r9657  
    235235        }
    236236        RelationMember member = members.get(rowIndex);
    237         RelationMember newMember = new RelationMember(value.toString(), member.getMember());
     237        String role = value.toString();
     238        if (member.hasRole(role))
     239            return;
     240        RelationMember newMember = new RelationMember(role, member.getMember());
    238241        members.remove(rowIndex);
    239242        members.add(rowIndex, newMember);
     243        fireTableDataChanged();
    240244    }
    241245
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/ApplyAction.java

    r9496 r9657  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Component;
    76import java.awt.event.ActionEvent;
    87
     8import java.beans.PropertyChangeEvent;
     9import java.beans.PropertyChangeListener;
     10
     11import javax.swing.event.TableModelEvent;
     12import javax.swing.event.TableModelListener;
     13
     14import org.openstreetmap.josm.gui.dialogs.relation.GenericRelationEditor;
    915import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
    1016import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
     
    1824 * @since 9496
    1925 */
    20 public class ApplyAction extends SavingAction {
     26public class ApplyAction extends SavingAction implements PropertyChangeListener, TableModelListener {
    2127
    2228    /**
     
    3440        putValue(SMALL_ICON, ImageProvider.get("save"));
    3541        putValue(NAME, tr("Apply"));
    36         setEnabled(true);
     42        updateEnabledState();
     43        memberTableModel.addTableModelListener(this);
     44        tagModel.addPropertyChangeListener(this);
    3745    }
    3846
    3947    @Override
    4048    public void actionPerformed(ActionEvent e) {
    41         if (editor.getRelation() == null) {
    42             applyNewRelation(tagModel);
    43         } else if (!memberTableModel.hasSameMembersAs(editor.getRelationSnapshot()) || tagModel.isDirty()) {
    44             if (editor.isDirtyRelation()) {
    45                 if (confirmClosingBecauseOfDirtyState()) {
    46                     if (layer.getConflicts().hasConflictForMy(editor.getRelation())) {
    47                         warnDoubleConflict();
    48                         return;
    49                     }
    50                     applyExistingConflictingRelation(tagModel);
    51                     if (editor instanceof Component) {
    52                         ((Component) editor).setVisible(false);
    53                     }
    54                 }
    55             } else {
    56                 applyExistingNonConflictingRelation(tagModel);
    57             }
     49        if (applyChanges()) {
     50            ((GenericRelationEditor) editor).reloadDataFromRelation();
    5851        }
    5952    }
     53
     54    @Override
     55    protected void updateEnabledState() {
     56        setEnabled(isEditorDirty());
     57    }
     58
     59    @Override
     60    public void propertyChange(PropertyChangeEvent evt) {
     61        updateEnabledState();
     62    }
     63
     64    @Override
     65    public void tableChanged(TableModelEvent e) {
     66        updateEnabledState();
     67    }
    6068}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/CancelAction.java

    r9512 r9657  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Component;
    76import java.awt.event.ActionEvent;
    87
     
    6665                //copied from OKAction.run()
    6766                Main.pref.put("relation.editor.generic.lastrole", tfRole.getText());
    68                 if (editor.getRelation() == null) {
    69                     applyNewRelation(tagModel);
    70                 } else if (!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty()) {
    71                     if (editor.isDirtyRelation()) {
    72                         if (confirmClosingBecauseOfDirtyState()) {
    73                             if (layer.getConflicts().hasConflictForMy(editor.getRelation())) {
    74                                 warnDoubleConflict();
    75                                 return;
    76                             }
    77                             applyExistingConflictingRelation(tagModel);
    78                         } else
    79                             return;
    80                     } else {
    81                         applyExistingNonConflictingRelation(tagModel);
    82                     }
    83                 }
     67                if (!applyChanges())
     68                    return;
    8469            } else if (ret == 2 || ret == JOptionPane.CLOSED_OPTION) //Cancel, continue editing
    8570                return;
    8671            //in case of "No, discard", there is no extra action to be performed here.
    8772        }
    88         if (editor instanceof Component) {
    89             ((Component) editor).setVisible(false);
    90         }
     73        hideEditor();
    9174    }
    9275
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/OKAction.java

    r9496 r9657  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Component;
    76import java.awt.event.ActionEvent;
    87
     
    4342        Main.pref.put("relation.editor.generic.lastrole", tfRole.getText());
    4443        memberTable.stopHighlighting();
    45         if (editor.getRelation() == null) {
    46             applyNewRelation(tagModel);
    47         } else if (!memberTableModel.hasSameMembersAs(editor.getRelationSnapshot()) || tagModel.isDirty()) {
    48             if (editor.isDirtyRelation()) {
    49                 if (confirmClosingBecauseOfDirtyState()) {
    50                     if (layer.getConflicts().hasConflictForMy(editor.getRelation())) {
    51                         warnDoubleConflict();
    52                         return;
    53                     }
    54                     applyExistingConflictingRelation(tagModel);
    55                 } else
    56                     return;
    57             } else {
    58                 applyExistingNonConflictingRelation(tagModel);
    59             }
    60         }
    61         if (editor instanceof Component) {
    62             ((Component) editor).setVisible(false);
    63         }
     44        if (!applyChanges())
     45            return;
     46        hideEditor();
    6447    }
    6548}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java

    r9496 r9657  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Component;
    67import java.util.ArrayList;
    78import java.util.List;
     
    111112        Main.main.undoRedo.add(new ChangeCommand(editor.getRelation(), editedRelation));
    112113        layer.data.fireSelectionChanged();
    113         // this will refresh the snapshot and update the dialog title
    114         //
    115         editor.setRelation(editor.getRelation());
    116114    }
    117115
     
    166164        // Do nothing
    167165    }
     166
     167    protected boolean applyChanges() {
     168        if (editor.getRelation() == null) {
     169            applyNewRelation(tagModel);
     170        } else if (isEditorDirty()) {
     171            if (editor.isDirtyRelation()) {
     172                if (confirmClosingBecauseOfDirtyState()) {
     173                    if (layer.getConflicts().hasConflictForMy(editor.getRelation())) {
     174                        warnDoubleConflict();
     175                        return false;
     176                    }
     177                    applyExistingConflictingRelation(tagModel);
     178                    hideEditor();
     179                } else
     180                    return false;
     181            } else {
     182                applyExistingNonConflictingRelation(tagModel);
     183            }
     184        }
     185        editor.setRelation(editor.getRelation());
     186        return true;
     187    }
     188
     189    protected void hideEditor() {
     190        if (editor instanceof Component) {
     191            ((Component) editor).setVisible(false);
     192        }
     193    }
     194
     195    protected boolean isEditorDirty() {
     196        Relation snapshot = editor.getRelationSnapshot();
     197        return (snapshot != null && !memberTableModel.hasSameMembersAs(snapshot)) || tagModel.isDirty();
     198    }
    168199}
  • trunk/src/org/openstreetmap/josm/gui/tagging/TagEditorModel.java

    r9623 r9657  
    166166     */
    167167    public void clear() {
     168        boolean wasEmpty = tags.isEmpty();
    168169        tags.clear();
    169         setDirty(true);
    170         fireTableDataChanged();
     170        if (!wasEmpty) {
     171            setDirty(true);
     172            fireTableDataChanged();
     173        }
    171174    }
    172175
     
    335338        tags.add(tag);
    336339        fireTableDataChanged();
    337         setDirty(true);
    338340    }
    339341
Note: See TracChangeset for help on using the changeset viewer.