/** Updates the information displayed in the fields. */
  public void update() {
    super.update(); // update the common fields

    InteractiveEntity interactiveEntity = (InteractiveEntity) entity;
    String sprite = interactiveEntity.getProperty("sprite");
    String behavior = interactiveEntity.getProperty("behavior");
    EntitySubtype subtype = interactiveEntity.getSubtype();

    boolean hasSprite = (!sprite.equals("_none"));

    withSpriteField.setSelected(hasSprite);
    spriteField.setSelectedId(hasSprite ? sprite : "");

    spriteField.setEnabled(hasSprite);

    if (behavior.equals("map")) {
      behaviorField.setSelectedIndex(1);
      messageField.setEnabled(false);
      itemField.setEnabled(false);
    } else if (behavior.substring(0, 5).equals("item#")) {
      behaviorField.setSelectedIndex(2);
      messageField.setEnabled(false);
      itemField.setEnabled(true);
      itemField.setSelectedId(behavior.substring(5));
    } else if (behavior.substring(0, 7).equals("dialog#")) {
      behaviorField.setSelectedIndex(0);
      messageField.setEnabled(true);
      itemField.setEnabled(false);
      messageField.setText(behavior.substring(7));
    }
  }
  /**
   * Constructor.
   *
   * @param tileset A tileset.
   * @param oldPatternId The tile pattern id to change in this tileset.
   */
  public TilePatternIdRefactoringComponent(Tileset tileset, String oldPatternId) {
    super();

    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    JPanel patternIdPanel = new JPanel();
    patternIdPanel.setLayout(new BoxLayout(patternIdPanel, BoxLayout.LINE_AXIS));
    patternIdPanel.setAlignmentX(1.0f);
    JLabel patternIdLabel = new JLabel("Tile pattern id:");
    patternIdPanel.add(patternIdLabel);
    patternIdPanel.add(Box.createHorizontalStrut(10));
    patternIdField = new JTextField(oldPatternId, 20);
    patternIdField.selectAll();
    patternIdPanel.add(patternIdField);
    patternIdPanel.setAlignmentX(0.0f);
    add(patternIdPanel);
    add(Box.createVerticalStrut(10));
    updateMapsCheckBox = new JCheckBox("Update references in existing maps");
    updateMapsCheckBox.setSelected(true);
    updateMapsCheckBox.setAlignmentX(0.0f);
    add(updateMapsCheckBox);
  }
  /**
   * Returns the specific part of the action made on the entity.
   *
   * @return the specific part of the action made on the entity
   */
  protected ActionEditEntitySpecific getSpecificAction() {

    String sprite = spriteField.getSelectedId();
    if (!withSpriteField.isSelected()) {
      sprite = "_none";
    }

    String behavior = "map";
    if (messageField.isEnabled()) {
      behavior = "dialog#" + messageField.getText();
    } else if (itemField.isEnabled()) {
      behavior = "item#" + itemField.getSelectedId();
    }

    return new ActionEditEntitySpecific(entity, sprite, behavior);
  }
  /** Creates the specific fields for this kind of entity. */
  protected void createSpecificFields() {

    // has a sprite?
    withSpriteField = new JCheckBox("Display a sprite");
    addField("Visibility", withSpriteField);

    // sprite name
    spriteField = new ResourceChooser(ResourceType.SPRITE, true);
    addField("Sprite name", spriteField);

    // behavior
    behaviorField = new RadioField("Show a message", "Call the map script", "Call an item script");
    addField("Action", behaviorField);

    // message
    messageField = new JTextField(15);
    addField("Message to show", messageField);

    // item
    itemField = new ItemChooser(false, false);
    addField("Item script to call", itemField);

    // listeners
    withSpriteField.addChangeListener(
        new ChangeListener() {
          public void stateChanged(ChangeEvent ev) {
            spriteField.setEnabled(withSpriteField.isSelected());
          }
        });

    behaviorField.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {
            messageField.setEnabled(behaviorField.getSelectedIndex() == 0);
            itemField.setEnabled(behaviorField.getSelectedIndex() == 2);
          }
        });

    subtypeField.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ev) {

            Subtype subtype = (Subtype) subtypeField.getValue();
            withSpriteField.setEnabled(subtype != Subtype.NON_PLAYING_CHARACTER);
          }
        });
  }
 /** Returns whether the user wants to update existing maps refering to this tile pattern. */
 public boolean isUpdateMapsChecked() {
   return updateMapsCheckBox.isSelected();
 }