package DisplayProject.plaf;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.ToolTipManager;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
/**
* The Win32 look and feel is just like the windows classic look and feel with a few minor differences.
* It was created to mimic how Forte looked.
*
* @author Craig Mitchell
* @since 29/04/2008
*/
@SuppressWarnings("serial")
public class Win32LookAndFeel extends WindowsClassicLookAndFeel {
public Win32LookAndFeel() {
super();
// Forte never dismissed tool tips, so mirror this.
ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
}
@Override
public UIDefaults getDefaults() {
UIDefaults defaults = super.getDefaults();
defaults.put("OptionPaneUI", "DisplayProject.plaf.Win32OptionPaneUI");
defaults.put("AuditoryCues.playList", new Object[] {
"OptionPane.errorSound",
"OptionPane.informationSound",
"OptionPane.questionSound",
"OptionPane.warningSound" });
defaults.put("OptionPane.informationSound", "sounds/OptionPaneInformation.wav");
defaults.put("OptionPane.warningSound", "sounds/OptionPaneWarning.wav");
defaults.put("OptionPane.errorSound", "sounds/OptionPaneError.wav");
defaults.put("OptionPane.questionSound", "sounds/OptionPaneQuestion.wav");
defaults.put("OptionPane.sameSizeButtons", true);
return defaults;
}
protected void playSound(Action audioAction) {
if (audioAction != null) {
Object[] audioStrings = (Object[])UIManager.get("AuditoryCues.playList");
if (audioStrings != null) {
// create a HashSet to help us decide to play or not
HashSet<Object> audioCues = new HashSet<Object>();
for (int i = 0; i < audioStrings.length; i++) {
audioCues.add(audioStrings[i]);
}
// get the name of the Action
String actionName = (String)audioAction.getValue(Action.NAME);
// if the actionName is in the audioCues HashSet, play it.
if (audioCues.contains(actionName)) {
audioAction.actionPerformed(new
ActionEvent(this, ActionEvent.ACTION_PERFORMED,
actionName));
}
}
}
}
public static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof Win32LookAndFeel) {
Object[] audioStrings = (Object[])UIManager.get("AuditoryCues.playList");
if (audioStrings != null){
for (int i = 0; i < audioStrings.length; i++) {
if (actionKey.equals(audioStrings[i])){
try {
String audioFileName = (String)UIManager.get(audioStrings[i]);
URL audioURL = Win32LookAndFeel.class.getResource(audioFileName);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(audioURL.getFile()));
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[524288];
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (UnsupportedAudioFileException e) {
// do noting
} catch (IOException e) {
// do noting
} catch (LineUnavailableException e) {
// do noting
}
}
}
}
}
}
}