1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import javax.swing.event.DocumentEvent;
|
---|
11 | import javax.swing.event.DocumentListener;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
15 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
|
---|
16 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
|
---|
17 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Sets a role for the selected members
|
---|
22 | * @since 9496
|
---|
23 | */
|
---|
24 | public class SetRoleAction extends AbstractRelationEditorAction implements DocumentListener {
|
---|
25 |
|
---|
26 | private final transient AutoCompletingTextField tfRole;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code SetRoleAction}.
|
---|
30 | * @param memberTable member table
|
---|
31 | * @param memberTableModel member table model
|
---|
32 | * @param tfRole role text field
|
---|
33 | */
|
---|
34 | public SetRoleAction(MemberTable memberTable, MemberTableModel memberTableModel, AutoCompletingTextField tfRole) {
|
---|
35 | super(memberTable, memberTableModel, null);
|
---|
36 | this.tfRole = tfRole;
|
---|
37 | putValue(SHORT_DESCRIPTION, tr("Sets a role for the selected members"));
|
---|
38 | putValue(SMALL_ICON, ImageProvider.get("apply"));
|
---|
39 | putValue(NAME, tr("Apply Role"));
|
---|
40 | updateEnabledState();
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | protected void updateEnabledState() {
|
---|
45 | setEnabled(memberTable.getSelectedRowCount() > 0);
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected boolean isEmptyRole() {
|
---|
49 | return tfRole.getText() == null || tfRole.getText().trim().isEmpty();
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected boolean confirmSettingEmptyRole(int onNumMembers) {
|
---|
53 | String message = "<html>"
|
---|
54 | + trn("You are setting an empty role on {0} object.",
|
---|
55 | "You are setting an empty role on {0} objects.", onNumMembers, onNumMembers)
|
---|
56 | + "<br>"
|
---|
57 | + tr("This is equal to deleting the roles of these objects.") +
|
---|
58 | "<br>"
|
---|
59 | + tr("Do you really want to apply the new role?") + "</html>";
|
---|
60 | String[] options = new String[] {
|
---|
61 | tr("Yes, apply it"),
|
---|
62 | tr("No, do not apply")
|
---|
63 | };
|
---|
64 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
65 | "relation_editor.confirm_applying_empty_role",
|
---|
66 | Main.parent,
|
---|
67 | message,
|
---|
68 | tr("Confirm empty role"),
|
---|
69 | JOptionPane.YES_NO_OPTION,
|
---|
70 | JOptionPane.WARNING_MESSAGE,
|
---|
71 | options,
|
---|
72 | options[0]
|
---|
73 | );
|
---|
74 | switch(ret) {
|
---|
75 | case JOptionPane.YES_OPTION:
|
---|
76 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
77 | return true;
|
---|
78 | default:
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public void actionPerformed(ActionEvent e) {
|
---|
85 | if (isEmptyRole() && !confirmSettingEmptyRole(memberTable.getSelectedRowCount())) {
|
---|
86 | return;
|
---|
87 | }
|
---|
88 | memberTableModel.updateRole(memberTable.getSelectedRows(), tfRole.getText());
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public void changedUpdate(DocumentEvent e) {
|
---|
93 | updateEnabledState();
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void insertUpdate(DocumentEvent e) {
|
---|
98 | updateEnabledState();
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public void removeUpdate(DocumentEvent e) {
|
---|
103 | updateEnabledState();
|
---|
104 | }
|
---|
105 | }
|
---|