001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.relation.actions; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005import static org.openstreetmap.josm.tools.I18n.trn; 006 007import java.awt.event.ActionEvent; 008 009import javax.swing.JOptionPane; 010import javax.swing.event.DocumentEvent; 011import javax.swing.event.DocumentListener; 012 013import org.openstreetmap.josm.Main; 014import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 015import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 016import org.openstreetmap.josm.tools.ImageProvider; 017 018/** 019 * Sets a role for the selected members 020 * @since 9496 021 */ 022public class SetRoleAction extends AbstractRelationEditorAction implements DocumentListener { 023 private static final long serialVersionUID = 1L; 024 025 private final transient AutoCompletingTextField tfRole; 026 027 /** 028 * Constructs a new {@code SetRoleAction}. 029 * @param editorAccess An interface to access the relation editor contents. 030 */ 031 public SetRoleAction(IRelationEditorActionAccess editorAccess) { 032 super(editorAccess); 033 this.tfRole = editorAccess.getTextFieldRole(); 034 putValue(SHORT_DESCRIPTION, tr("Sets a role for the selected members")); 035 new ImageProvider("apply").getResource().attachImageIcon(this); 036 putValue(NAME, tr("Apply Role")); 037 updateEnabledState(); 038 } 039 040 @Override 041 protected void updateEnabledState() { 042 setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0); 043 } 044 045 protected boolean isEmptyRole() { 046 return tfRole.getText() == null || tfRole.getText().trim().isEmpty(); 047 } 048 049 protected boolean confirmSettingEmptyRole(int onNumMembers) { 050 String message = "<html>" 051 + trn("You are setting an empty role on {0} object.", 052 "You are setting an empty role on {0} objects.", onNumMembers, onNumMembers) 053 + "<br>" 054 + tr("This is equal to deleting the roles of these objects.") + 055 "<br>" 056 + tr("Do you really want to apply the new role?") + "</html>"; 057 String[] options = new String[] { 058 tr("Yes, apply it"), 059 tr("No, do not apply") 060 }; 061 int ret = ConditionalOptionPaneUtil.showOptionDialog( 062 "relation_editor.confirm_applying_empty_role", 063 Main.parent, 064 message, 065 tr("Confirm empty role"), 066 JOptionPane.YES_NO_OPTION, 067 JOptionPane.WARNING_MESSAGE, 068 options, 069 options[0] 070 ); 071 switch(ret) { 072 case JOptionPane.YES_OPTION: 073 case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION: 074 return true; 075 default: 076 return false; 077 } 078 } 079 080 @Override 081 public void actionPerformed(ActionEvent e) { 082 if (isEmptyRole() && !confirmSettingEmptyRole(editorAccess.getMemberTable().getSelectedRowCount())) { 083 return; 084 } 085 editorAccess.getMemberTableModel().updateRole(editorAccess.getMemberTable().getSelectedRows(), tfRole.getText()); 086 } 087 088 @Override 089 public void changedUpdate(DocumentEvent e) { 090 updateEnabledState(); 091 } 092 093 @Override 094 public void insertUpdate(DocumentEvent e) { 095 updateEnabledState(); 096 } 097 098 @Override 099 public void removeUpdate(DocumentEvent e) { 100 updateEnabledState(); 101 } 102}