| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.relation.actions;
|
|---|
| 3 |
|
|---|
| 4 | import javax.swing.Action;
|
|---|
| 5 |
|
|---|
| 6 | import org.openstreetmap.josm.data.osm.IRelation;
|
|---|
| 7 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 8 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 9 | import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
|
|---|
| 10 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
|
|---|
| 11 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
|
|---|
| 12 | import org.openstreetmap.josm.gui.dialogs.relation.SelectionTable;
|
|---|
| 13 | import org.openstreetmap.josm.gui.dialogs.relation.SelectionTableModel;
|
|---|
| 14 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 15 | import org.openstreetmap.josm.gui.tagging.TagEditorModel;
|
|---|
| 16 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * This interface provides access to the relation editor for actions.
|
|---|
| 20 | * <p>
|
|---|
| 21 | *
|
|---|
| 22 | * @author Michael Zangl
|
|---|
| 23 | * @since 14027
|
|---|
| 24 | */
|
|---|
| 25 | public interface IRelationEditorActionAccess {
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Adds a keyboard action to the member table.
|
|---|
| 29 | * @param actionMapKey The key to use
|
|---|
| 30 | * @param action The action to map for that key.
|
|---|
| 31 | */
|
|---|
| 32 | default void addMemberTableAction(String actionMapKey, Action action) {
|
|---|
| 33 | getMemberTable().getActionMap().put(actionMapKey, action);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Get the member table that is used by the dialog.
|
|---|
| 38 | * @return The member table
|
|---|
| 39 | */
|
|---|
| 40 | MemberTable getMemberTable();
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Get the model the member table is using.
|
|---|
| 44 | * @return That model
|
|---|
| 45 | */
|
|---|
| 46 | MemberTableModel getMemberTableModel();
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Get the table that displays the current user selection
|
|---|
| 50 | * @return That table
|
|---|
| 51 | */
|
|---|
| 52 | SelectionTable getSelectionTable();
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Get the model that the selection table is based on.
|
|---|
| 56 | * @return The model
|
|---|
| 57 | */
|
|---|
| 58 | SelectionTableModel getSelectionTableModel();
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Get the current relation editor
|
|---|
| 62 | * @return The relation editor object.
|
|---|
| 63 | */
|
|---|
| 64 | IRelationEditor getEditor();
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Gets the model for the tag table.
|
|---|
| 68 | * @return The tag editor model.
|
|---|
| 69 | */
|
|---|
| 70 | TagEditorModel getTagModel();
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Get the changed relation
|
|---|
| 74 | * @return The changed relation (note: will not be part of a dataset) or the
|
|---|
| 75 | * value returned by {@code getEditor().getRelation()}. This should never be {@code null}.
|
|---|
| 76 | * If called for a temporary use of the relation instance, make sure to cleanup a copy to avoid memory leaks, see #23527
|
|---|
| 77 | * @since 18413
|
|---|
| 78 | */
|
|---|
| 79 | default IRelation<?> getChangedRelation() {
|
|---|
| 80 | final Relation newRelation;
|
|---|
| 81 | final Relation oldRelation = getEditor().getRelation();
|
|---|
| 82 | boolean isUploadInProgress = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class)
|
|---|
| 83 | .stream().anyMatch(OsmDataLayer::isUploadInProgress);
|
|---|
| 84 | if (isUploadInProgress || (oldRelation != null && oldRelation.getDataSet() != null && oldRelation.getDataSet().isLocked())) {
|
|---|
| 85 | // If the dataset is locked, then we cannot change the relation. See JOSM #22024.
|
|---|
| 86 | // We should also avoid changing the relation if there is an upload in progress. See JOSM #22268/#22398.
|
|---|
| 87 | // There appears to be a race condition where a dataset might not be locked in the check, then is locked while using the
|
|---|
| 88 | // copy relation constructor.
|
|---|
| 89 | // This is due to the `setMembers` -> `addReferrer` call chain requires that the dataset is not read only.
|
|---|
| 90 | return oldRelation;
|
|---|
| 91 | } else if (oldRelation != null) {
|
|---|
| 92 | newRelation = new Relation(oldRelation);
|
|---|
| 93 | } else {
|
|---|
| 94 | newRelation = new Relation();
|
|---|
| 95 | }
|
|---|
| 96 | getTagModel().applyToPrimitive(newRelation);
|
|---|
| 97 | getMemberTableModel().applyToRelation(newRelation);
|
|---|
| 98 | return newRelation;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Check if the changed relation would be useful.
|
|---|
| 103 | * @return true if the saved relation has at least one tag and one member
|
|---|
| 104 | * @since 19014
|
|---|
| 105 | */
|
|---|
| 106 | default boolean wouldRelationBeUseful() {
|
|---|
| 107 | return (getTagModel().getRowCount() > 0 && getMemberTableModel().getRowCount() > 0);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Get the text field that is used to edit the role.
|
|---|
| 112 | * @return The role text field.
|
|---|
| 113 | */
|
|---|
| 114 | AutoCompletingTextField getTextFieldRole();
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Tells the member table editor to stop editing and accept any partially edited value as the value of the editor.
|
|---|
| 118 | * The editor returns false if editing was not stopped; this is useful for editors that validate and can not accept invalid entries.
|
|---|
| 119 | * @return {@code true} if editing was stopped; {@code false} otherwise
|
|---|
| 120 | * @since 18118
|
|---|
| 121 | */
|
|---|
| 122 | default boolean stopMemberCellEditing() {
|
|---|
| 123 | return getMemberTable().isEditing() && getMemberTable().getCellEditor().stopCellEditing();
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|