source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SetRoleAction.java@ 14027

Last change on this file since 14027 was 14027, checked in by michael2402, 6 years ago

See #16388: New mechanism for plugins to register relation editor actions.

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