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

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

See #16388: Fix Checkstyle / Test issues.

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 editorAccess An interface to access the relation editor contents.
30 */
31 public SetRoleAction(IRelationEditorActionAccess editorAccess) {
32 super(editorAccess);
33 this.tfRole = editorAccess.getTextFieldRole();
34 putValue(SHORT_DESCRIPTION, tr("Sets a role for the selected members"));
35 new ImageProvider("apply").getResource().attachImageIcon(this);
36 putValue(NAME, tr("Apply Role"));
37 updateEnabledState();
38 }
39
40 @Override
41 protected void updateEnabledState() {
42 setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0);
43 }
44
45 protected boolean isEmptyRole() {
46 return tfRole.getText() == null || tfRole.getText().trim().isEmpty();
47 }
48
49 protected boolean confirmSettingEmptyRole(int onNumMembers) {
50 String message = "<html>"
51 + trn("You are setting an empty role on {0} object.",
52 "You are setting an empty role on {0} objects.", onNumMembers, onNumMembers)
53 + "<br>"
54 + tr("This is equal to deleting the roles of these objects.") +
55 "<br>"
56 + tr("Do you really want to apply the new role?") + "</html>";
57 String[] options = new String[] {
58 tr("Yes, apply it"),
59 tr("No, do not apply")
60 };
61 int ret = ConditionalOptionPaneUtil.showOptionDialog(
62 "relation_editor.confirm_applying_empty_role",
63 Main.parent,
64 message,
65 tr("Confirm empty role"),
66 JOptionPane.YES_NO_OPTION,
67 JOptionPane.WARNING_MESSAGE,
68 options,
69 options[0]
70 );
71 switch(ret) {
72 case JOptionPane.YES_OPTION:
73 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
74 return true;
75 default:
76 return false;
77 }
78 }
79
80 @Override
81 public void actionPerformed(ActionEvent e) {
82 if (isEmptyRole() && !confirmSettingEmptyRole(editorAccess.getMemberTable().getSelectedRowCount())) {
83 return;
84 }
85 editorAccess.getMemberTableModel().updateRole(editorAccess.getMemberTable().getSelectedRows(), tfRole.getText());
86 }
87
88 @Override
89 public void changedUpdate(DocumentEvent e) {
90 updateEnabledState();
91 }
92
93 @Override
94 public void insertUpdate(DocumentEvent e) {
95 updateEnabledState();
96 }
97
98 @Override
99 public void removeUpdate(DocumentEvent e) {
100 updateEnabledState();
101 }
102}
Note: See TracBrowser for help on using the repository browser.