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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.examples.modules.minicomposer;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.openide.ErrorManager;

import org.openide.execution.*;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.*;
import org.openide.windows.InputOutput;

public class ExternalPlayer implements Player {
    
    private static final String TAG_AUFILE_PLAIN = "aufile";
    public static final String TAG_AUFILE = "{" + TAG_AUFILE_PLAIN + "}";
        
    private NbProcessDescriptor playerCommand;
    private String label;
    
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    
    public ExternalPlayer(NbProcessDescriptor playerCommand) {
        this(playerCommand, null);
    }
    
    public ExternalPlayer(NbProcessDescriptor playerCommand, String label) {
        this.playerCommand = playerCommand;
        this.label = label;
    }

    public String getLabel() {
        return label;
    }
    
    public void addPropertyChangeListener(PropertyChangeListener l) {
        pcs.addPropertyChangeListener(l);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener l) {
        pcs.removePropertyChangeListener(l);
    }
    
    public NbProcessDescriptor getPlayerCommand() {
        return playerCommand;
    }
    
    public void setPlayerCommand(NbProcessDescriptor d) {
        NbProcessDescriptor old = playerCommand;
        playerCommand = d;
        label = null; // obsoleted
        pcs.firePropertyChange("playerCommand", old, d);
    }
    
    public ExecutorTask play(String displayName, FileObject fo, ScoreCookie s) throws IOException {
        if (fo == null) {
            IOException ioe = new IOException("No .au in " + displayName);
            ErrorManager.getDefault().annotate(ioe, NbBundle.getMessage(ExternalPlayer.class, "EXC_No_AU_file_found"));
            throw ioe;
        }
        final File f = FileUtil.toFile(fo);
        if (f == null) {
            IOException ioe = new IOException("No .au in " + displayName);
            ErrorManager.getDefault().annotate(ioe, NbBundle.getMessage(ExternalPlayer.class, "EXC_file_must_be_local"));
            throw ioe;
        }
        // XXX for now there is no other apparent way to run an NbProcessDescriptor well
        // besides hacking around with subclassing ProcessExecutor and passing it dummy args
        // Could also make a custom ExecutorTask, but then no Execution View etc.
        // This does not produce the correct return value (ProcessExecutor makes a proxy ET for this)
        final Process p = playerCommand.exec(new MyFormat(f));
        // XXX I18N
        return new ProcessTask("Playing " + displayName, p);
    }
    
    private static class MyFormat extends MapFormat {
        MyFormat(File aufile) {
            super(new HashMap(1));
            getMap().put(TAG_AUFILE_PLAIN, aufile.getAbsolutePath());
        }
    }
    
    private static final class ProcessTask extends ExecutorTask {
        
        private final Process p;
        private final ExecutorTask delegate;
        
        public ProcessTask(String label, Process p) {
            this(createDelegate(label, p), p);
        }
        
        private static ExecutorTask createDelegate(String label, final Process p) {
            Runnable r = new Runnable() {
                public void run() {
                    try {
                        p.waitFor();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            return ExecutionEngine.getDefault().execute(label, r, null);
        }
        
        private ProcessTask(ExecutorTask delegate, Process p) {
            super(delegate);
            this.delegate = delegate;
            this.p = p;
        }
        
        public int result() {
            try {
                 return p.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
                return 2;
            }
        }
        
        public void stop() {
            p.destroy();
            delegate.stop();
        }
        
        public InputOutput getInputOutput() {
            return delegate.getInputOutput();
        }
        
    }
    
}
... 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.