alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.examples.modules.audioloader;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.*;
import org.openide.*;
import org.openide.cookies.ViewCookie;
import org.openide.filesystems.*;
import org.openide.loaders.*;
import org.openide.util.*;
public class MicrophoneTemplateIterator implements TemplateWizard.Iterator, WizardDescriptor.Panel {
    private transient TemplateWizard wiz;
    private transient boolean onMainPanel;
    private transient byte[] recorded;
    private transient AudioFormat format;
    private transient Set listeners = new HashSet (1); // Set
    private transient JComponent recordPanel;
    // TemplateWizard.Iterator methods:
    public Set instantiate (TemplateWizard wiz) throws IOException {
        DataFolder folder = wiz.getTargetFolder ();
        String name = wiz.getTargetName ();
        if (name == null) name = wiz.getTemplate ().getName ();
        DataObject template = wiz.getTemplate ();
        String extension = template.getPrimaryFile ().getExt ();
        FileObject fo = folder.getPrimaryFile ().createData (name, extension);
        FileLock lock = fo.lock ();
        try {
            OutputStream os = fo.getOutputStream (lock);
            try {
                if (onMainPanel) {
                    // Creating sample from recording.
                    if (recorded == null) throw new IOException ("should never happen");
                    InputStream is = new ByteArrayInputStream (recorded);
                    AudioInputStream ais = new AudioInputStream (is, format, recorded.length / format.getFrameSize ());
                    AudioFileFormat.Type filetype;
                    if ("aif".equals (extension) || "aiff".equals (extension)) {
                        filetype = AudioFileFormat.Type.AIFF;
                    } else if ("aifc".equals (extension)) {
                        filetype = AudioFileFormat.Type.AIFC;
                    } else if ("wav".equals (extension)) {
                        filetype = AudioFileFormat.Type.WAVE;
                    } else if ("snd".equals (extension)) {
                        filetype = AudioFileFormat.Type.SND;
                    } else {
                        // Decent fallback format, incl. for MIDI templates.
                        filetype = AudioFileFormat.Type.AU;
                    }
                    AudioSystem.write (ais, filetype, os);
                } else {
                    // Simply copying template.
                    InputStream is = template.getPrimaryFile ().getInputStream ();
                    try {
                        byte[] buf = new byte[4096];
                        int i;
                        while ((i = is.read (buf)) != -1) {
                            os.write (buf, 0, i);
                        }
                    } finally {
                        is.close ();
                    }
                }
            } finally {
                os.close ();
            }
        } finally {
            lock.releaseLock ();
        }
        DataObject obj = DataObject.find (fo);
        ViewCookie view = (ViewCookie) obj.getCookie (ViewCookie.class);
        if (view != null) {
            view.view ();
        }
        return Collections.singleton (obj);
    }
    public void initialize (TemplateWizard wiz) {
        onMainPanel = false;
        recorded = null;
        format = null;
        this.wiz = wiz;
        recordPanel = new RecordPanel (this);
        Component c = wiz.targetChooser ().getComponent ();
        String[] steps = new String[] {
            c.getName (),
            NbBundle.getMessage (MicrophoneTemplateIterator.class, "LBL_wizard_iterator"),
        };
        if (c instanceof JComponent) {
            JComponent jc = (JComponent) c;
            jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (0));
            jc.putClientProperty ("WizardPanel_contentData", steps);
        }
        recordPanel.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (1));
        recordPanel.putClientProperty ("WizardPanel_contentData", steps);
    }
    public void uninitialize (TemplateWizard wiz) {
        wiz = null;
        recorded = null;
        format = null;
        recordPanel = null;
    }
    // Wizard.Iterator methods:
    public WizardDescriptor.Panel current () {
        return onMainPanel ? this : wiz.targetChooser ();
    }
    public void previousPanel () {
        if (! onMainPanel) throw new NoSuchElementException ();
        onMainPanel = false;
    }
    public void nextPanel () {
        if (onMainPanel) throw new NoSuchElementException ();
        onMainPanel = true;
    }
    public String name () {
        return NbBundle.getMessage (MicrophoneTemplateIterator.class, "LBL_wizard_iterator");
    }
    public boolean hasPrevious () {
        return onMainPanel;
    }
    public boolean hasNext () {
        return ! onMainPanel;
    }
    // Wizard.Iterator and WizardDescriptor.Panel both:
    public synchronized void addChangeListener (ChangeListener l) {
        listeners.add (l);
    }
    public synchronized void removeChangeListener (ChangeListener l) {
        listeners.remove (l);
    }
    // WizardDescriptor.Panel:
    public Component getComponent () {
        return recordPanel;
    }
    public HelpCtx getHelp () {
        return HelpCtx.DEFAULT_HELP;
    }
    public boolean isValid () {
        return recorded != null;
    }
    public void readSettings (Object settings) {
        // No state between panels, except what is in this class.
    }
    public void storeSettings (Object settings) {
        // No state between panels, except what is in this class.
    }
    // Access methods for RecordPanel:
    private synchronized void fireChangeEvent () {
        ChangeEvent ev = new ChangeEvent (this);
        Iterator it = listeners.iterator ();
        while (it.hasNext ()) {
            ChangeListener l = (ChangeListener) it.next ();
            l.stateChanged (ev);
        }
    }
    void setRecorded (byte[] audiodata, AudioFormat f) {
        recorded = audiodata;
        format = f;
        fireChangeEvent ();
    }
    private void readObject (ObjectInputStream in) throws ClassNotFoundException, IOException {
        in.defaultReadObject ();
        listeners = new HashSet (1);
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.