You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionNamePanel.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.actioneditor;
  23. import com.dmdirc.config.prefs.validator.FileNameValidator;
  24. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  25. import java.beans.PropertyChangeEvent;
  26. import java.beans.PropertyChangeListener;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.JLabel;
  29. import javax.swing.JPanel;
  30. import javax.swing.JTextField;
  31. import javax.swing.UIManager;
  32. import net.miginfocom.swing.MigLayout;
  33. /**
  34. * Action name panel.
  35. */
  36. public class ActionNamePanel extends JPanel implements PropertyChangeListener {
  37. /**
  38. * A version number for this class. It should be changed whenever the class
  39. * structure is changed (or anything else that would prevent serialized
  40. * objects being unserialized with the new class).
  41. */
  42. private static final long serialVersionUID = 1;
  43. /** Original name. */
  44. private final String originalName;
  45. /** Action name field. */
  46. private ValidatingJTextField name;
  47. /** Instantiates the panel. */
  48. public ActionNamePanel() {
  49. this("");
  50. }
  51. /**
  52. * Instantiates the panel.
  53. *
  54. * @param name Initial name of the action
  55. */
  56. public ActionNamePanel(final String name) {
  57. super();
  58. if (name == null) {
  59. this.originalName = "";
  60. } else {
  61. this.originalName = name;
  62. }
  63. initComponents();
  64. addListeners();
  65. layoutComponents();
  66. this.name.checkError();
  67. }
  68. /**
  69. * Sets the action name.
  70. *
  71. * @param name new name
  72. */
  73. void setActionName(final String name) {
  74. this.name.setText(name);
  75. }
  76. /** Validates the name. */
  77. public void validateName() {
  78. name.checkError();
  79. }
  80. /** Initialises the components. */
  81. private void initComponents() {
  82. name = new ValidatingJTextField(new JTextField(originalName),
  83. new FileNameValidator());
  84. }
  85. /** Adds the listeners. */
  86. private void addListeners() {
  87. name.addPropertyChangeListener("validationResult", this);
  88. }
  89. /** Lays out the components. */
  90. private void layoutComponents() {
  91. setLayout(new MigLayout("wrap 1"));
  92. setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  93. "TitledBorder.border"), "Name"));
  94. add(new JLabel("This action's name:"));
  95. add(name, "growx, pushx");
  96. }
  97. /**
  98. * Has the action's name changed.
  99. *
  100. * @return true if the action name has changed.
  101. */
  102. public boolean hasNameChanged() {
  103. return getActionName().equals(originalName);
  104. }
  105. /**
  106. * Returns the name represented by this component.
  107. *
  108. * @return Current name of this action
  109. */
  110. public String getActionName() {
  111. return name.getText();
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public void setEnabled(final boolean enabled) {
  116. name.setEnabled(enabled);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void propertyChange(final PropertyChangeEvent evt) {
  121. firePropertyChange("validationResult", evt.getOldValue(), evt.
  122. getNewValue());
  123. }
  124. }