Package DisplayProject.plaf

Source Code of DisplayProject.plaf.Win32LookAndFeel

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
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
            }
          }
        }
     
      }
    }
  }

}
TOP

Related Classes of DisplayProject.plaf.Win32LookAndFeel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.