Skip to content

Commit

Permalink
Merge pull request #12788 from BrentEaston/12769-Expression-Comment-i…
Browse files Browse the repository at this point in the history
…ssue

12769 - Fix support for expressions in Sound Clip Configurer
  • Loading branch information
uckelman authored Oct 8, 2023
2 parents f993133 + 3ab4c01 commit a6671ba
Showing 1 changed file with 112 additions and 6 deletions.
118 changes: 112 additions & 6 deletions vassal-app/src/main/java/VASSAL/configure/AudioClipConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
package VASSAL.configure;

import java.awt.Component;
import java.io.File;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import VASSAL.build.GameModule;
import VASSAL.counters.EditablePiece;
import VASSAL.i18n.Resources;
import VASSAL.tools.ArchiveWriter;
import VASSAL.tools.filechooser.AudioFileFilter;
import VASSAL.tools.filechooser.FileChooser;
Expand Down Expand Up @@ -61,17 +67,117 @@ protected FileChooser initFileChooser() {
}

@Override
protected void addToArchive(java.io.File f) {
protected void addToArchive(File f) {
archive.addSound(f.getPath(), f.getName());
}

//
// The AudioClipConfigure can take an Expression as well as a straight file name, while the super class
// FileConfigurer only stores a File as the value and this does not play well with expressions.
// The following overrides have been cut from FileConfigurer and modified to support this.
@Override
public Component getControls() {
final Component c = super.getControls();
if (button == null) {
if (p == null) {
p = new ConfigurerPanel(getName(), "[]rel[grow,fill,push]1[]", "[]rel[]rel[grow,fill,push]1[]"); // NON-NLS

final JButton b = new JButton(Resources.getString("Editor.select"));
p.add(b);

tf = new JTextField(getValueString());
tf.setEditable(editable);
if (editable) {
// Edit box selects all text when first focused
tf.addFocusListener(new java.awt.event.FocusAdapter() {
@Override
public void focusGained(java.awt.event.FocusEvent evt) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
tf.selectAll();
}
});
}
});
}
tf.setMaximumSize(new java.awt.Dimension(tf.getMaximumSize().width,
tf.getPreferredSize().height));
tf.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent evt) {
update();
}

@Override
public void insertUpdate(DocumentEvent evt) {
update();
}

@Override
public void removeUpdate(DocumentEvent evt) {
update();
}

public void update() {
final String text = tf.getText();
if (text != null && text.startsWith("{")) {
noUpdate = true;
setValue(text);
noUpdate = false;
}
else {
final File f = text != null && text.length() > 0 && !"null".equals(text) ? new File(text) : null; // NON-NLS
noUpdate = true;
setValue(f);
noUpdate = false;
}
}
});
p.add(tf, "grow");
button = new FormattedExpressionConfigurer.ExpressionButton(this, tf.getPreferredSize().height, sourcePiece);
((JPanel) c).add(button);
p.add(button);
b.addActionListener(e -> chooseNewValue());
}
return p;
}


@Override
public String getValueString() {
if (value instanceof String) {
return (String) value;
}
else {
return super.getValueString();
}
return c;
}

@Override
public void setValue(Object o) {
if (o instanceof String && ((String) o).startsWith("{")) {
// Bypass FileConfigurer File conversion for Beanshell expressions
final Object oldValue = getValue();
value = o;
if (!frozen) {
changeSupport.firePropertyChange(key, oldValue, value);
}
if (tf != null && !noUpdate) {
tf.setText(getValueString());
}
}
else {
super.setValue(o);
}
}

@Override
public void setValue(String s) {
if (s != null && s.startsWith("{")) {
// Bypass FileConfigurer File conversion for Beanshell expressions
setValue((Object) s);
}
else {
super.setValue(s);
}
}

}

0 comments on commit a6671ba

Please sign in to comment.