package tk.amberide.ide.gui.dialogs.res;
import tk.amberide.Amber;
import tk.amberide.engine.al.Audio;
import tk.amberide.engine.al.AudioIO;
import tk.amberide.ide.data.io.FileIO;
import tk.amberide.ide.data.state.Scope;
import tk.amberide.ide.data.state.State;
import tk.amberide.ide.gui.misc.ErrorHandler;
import tk.amberide.ide.gui.misc.AudioPlayerPanel;
import tk.amberide.ide.os.filechooser.FileDialogFactory;
import tk.amberide.ide.os.filechooser.IFileDialog;
import tk.amberide.ide.swing.UIUtil;
import java.awt.GridBagLayout;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.swing.table.TableModel;
/**
*
* @author Tudor
*/
public class NewAudioDialog extends javax.swing.JDialog {
private IFileDialog browser;
private Audio audio;
@State(Scope.PROJECT)
private static String lastImportLocation;
static {
Amber.getStateManager().registerStateOwner(NewAudioDialog.class);
}
/**
* Creates new form NewAudioDialog
*/
public NewAudioDialog(java.awt.Frame parent) {
super(parent);
initComponents();
browser = FileDialogFactory.newFileDialog("Select audio clip...", Amber.getUI());
browser.setFilter("Audio files (*.wav; *.ogg; *.aiff; *.midi; *.mid)|*.wav;*.aiff;*.ogg;*.midi;*.mid");
UIUtil.adjustColumnPreferredWidths(detailsTable);
previewGroup.setLayout(new GridBagLayout());
}
public NewAudioDialog(java.awt.Frame parent, File clip) {
this(parent);
nameField.setText(FileIO.getFileName(clip));
audioLocationField.setText(clip.getAbsolutePath());
updatePreview();
checkCreateableStatus();
}
private void checkCreateableStatus() {
if (Amber.getResourceManager().getAudio(nameField.getText()) != null) {
return;
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createButton.setEnabled(new File(audioLocationField.getText()).exists() && !nameField.getText().isEmpty());
getRootPane().setDefaultButton(createButton);
}
});
}
private void updatePreview() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
final File dir = new File(audioLocationField.getText());
previewGroup.removeAll();
if (dir.exists()) {
detailsTable.setEnabled(true);
// This is bad; we are initializing OpenAL on the EDT,
// and OAL can take a very long time to start.
try {
audio = AudioIO.read(dir);
previewGroup.add(new AudioPlayerPanel(dir));
} catch (Exception ex) {
ErrorHandler.alert(ex);
}
AudioFormat data = audio.getFormat();
if (data != null) {
TableModel mod = detailsTable.getModel();
mod.setValueAt(data.getEncoding(), 0, 1);
mod.setValueAt(data.getFrameRate() + "Hz", 1, 1);
mod.setValueAt(data.getFrameSize() + " bytes", 2, 1);
mod.setValueAt(data.getSampleRate(), 3, 1);
mod.setValueAt(data.getSampleSizeInBits() + " bits", 4, 1);
mod.setValueAt(data.getChannels(), 5, 1);
mod.setValueAt(data.getChannels() == 2 ? "stereo" : "mono", 6, 1);
mod.setValueAt(data.isBigEndian(), 7, 1);
}
UIUtil.adjustColumnPreferredWidths(detailsTable);
} else {
detailsTable.setEnabled(false);
previewGroup.add(previewLabel);
}
previewGroup.validate();
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
audioGroup = new javax.swing.JPanel();
nameLabel = new javax.swing.JLabel();
audioLocationField = new tk.amberide.ide.swing.textbox.HintTextField();
nameField = new tk.amberide.ide.swing.textbox.HintTextField();
browseButton = new javax.swing.JButton();
clipLabel = new javax.swing.JLabel();
createButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
detailsScrollPane = new javax.swing.JScrollPane();
detailsTable = new javax.swing.JTable();
previewGroup = new javax.swing.JPanel();
previewLabel = new javax.swing.JLabel();
jLabel1.setText("jLabel1");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("New Audio");
setModal(true);
setResizable(false);
audioGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Audio"));
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("amber/Bundle"); // NOI18N
nameLabel.setText(bundle.getString("NewTilesetDialog.nameLabel.text")); // NOI18N
audioLocationField.setHint("Clip path...");
audioLocationField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
audioLocationFieldKeyTyped(evt);
}
});
nameField.setHint("Type in a name for clip...");
nameField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
nameFieldKeyTyped(evt);
}
});
browseButton.setText(bundle.getString("NewTilesetDialog.browseButton.text")); // NOI18N
browseButton.setDefaultCapable(false);
browseButton.setRequestFocusEnabled(false);
browseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseButtonActionPerformed(evt);
}
});
clipLabel.setText("Clip:");
javax.swing.GroupLayout audioGroupLayout = new javax.swing.GroupLayout(audioGroup);
audioGroup.setLayout(audioGroupLayout);
audioGroupLayout.setHorizontalGroup(
audioGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(audioGroupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(audioGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(clipLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(audioGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(audioGroupLayout.createSequentialGroup()
.addComponent(audioLocationField, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(browseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
audioGroupLayout.setVerticalGroup(
audioGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(audioGroupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(audioGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(audioGroupLayout.createSequentialGroup()
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(audioGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(audioLocationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(browseButton)
.addComponent(clipLabel)))
.addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
createButton.setText(bundle.getString("NewTilesetDialog.createButton.text")); // NOI18N
createButton.setEnabled(false);
createButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createButtonActionPerformed(evt);
}
});
cancelButton.setText(bundle.getString("NewTilesetDialog.cancelButton.text")); // NOI18N
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
detailsScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Details"));
detailsScrollPane.setMinimumSize(null);
detailsTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"Encoding", null},
{"Framerate", null},
{"Frame size", null},
{"Sample rate", null},
{"Sample size", null},
{"Channels", null},
{"Mode", null},
{"Big endian", null}
},
new String [] {
"Property", "Value"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Object.class
};
boolean[] canEdit = new boolean [] {
false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
detailsTable.getTableHeader().setReorderingAllowed(false);
detailsScrollPane.setViewportView(detailsTable);
previewGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Preview"));
previewGroup.setLayout(new java.awt.BorderLayout());
previewLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
previewLabel.setText("No preview available.");
previewGroup.add(previewLabel, java.awt.BorderLayout.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(256, 256, 256)
.addComponent(createButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(audioGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(detailsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(previewGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(audioGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(detailsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
.addComponent(previewGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(createButton))
.addGap(6, 6, 6))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void audioLocationFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_audioLocationFieldKeyTyped
updatePreview();
checkCreateableStatus();
}//GEN-LAST:event_audioLocationFieldKeyTyped
private void nameFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameFieldKeyTyped
checkCreateableStatus();
}//GEN-LAST:event_nameFieldKeyTyped
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
if (lastImportLocation != null) {
File dir = new File(lastImportLocation);
if (dir.exists() && dir.isDirectory()) {
browser.setInitial(dir);
}
}
if (browser.show()) {
audioLocationField.setText(browser.getFile().getAbsolutePath());
updatePreview();
checkCreateableStatus();
}
}//GEN-LAST:event_browseButtonActionPerformed
private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
File file = new File(audioLocationField.getText());
lastImportLocation = file.getParent();
Amber.getResourceManager().importAudio(nameField.getText(), audio, file);
dispose();
}//GEN-LAST:event_createButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel audioGroup;
private tk.amberide.ide.swing.textbox.HintTextField audioLocationField;
private javax.swing.JButton browseButton;
private javax.swing.JButton cancelButton;
private javax.swing.JLabel clipLabel;
private javax.swing.JButton createButton;
private javax.swing.JScrollPane detailsScrollPane;
private javax.swing.JTable detailsTable;
private javax.swing.JLabel jLabel1;
private tk.amberide.ide.swing.textbox.HintTextField nameField;
private javax.swing.JLabel nameLabel;
private javax.swing.JPanel previewGroup;
private javax.swing.JLabel previewLabel;
// End of variables declaration//GEN-END:variables
}