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.awt.Image;
import java.beans.BeanInfo;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.Action;
import org.netbeans.api.registry.Context;
import org.netbeans.api.registry.ContextException;
import org.netbeans.api.registry.ObjectRef;
import org.netbeans.examples.modules.minicomposer.registryutils.backcompatible.ContextPropertyEditor;
import org.openide.nodes.*;
import org.openide.util.*;

public class ComposerSettings {
    private ComposerSettings() {}
    private static final String PROP_PLAYER = "player";
    private static final String PROP_SAMPLE_RATE = "sampleRate";
    private static final Context c;
    private static final Context players;
    static {
        try {
            c = Context.getDefault().createSubcontext("org/netbeans/examples/modules/minicomposer");
            players = c.createSubcontext("players");
        } catch (ContextException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    public static Player getPlayer() {
         ObjectRef playerRef = getPlayerRef();
         Player p;

         if (playerRef == null) {
             System.err.println("Warning - no such player '");
             p = new InternalPlayer();
         } else {
             p = (Player) playerRef.getObject();
         }
         assert p != null : "no player" ;
         return p;
     }
    
    private static ObjectRef getPlayerRef() {
        return new ObjectRef(players, getPlayerName());    
    }

    private static String getPlayerName() {
        String n = c.getString(PROP_PLAYER, null);
        if (n == null) {
            // [PENDING] yet internal version works on Linux too...is there a
            // definite criterion? Ideally would try to get a line out and
            // switch to external only if that fails.
            if (Utilities.isWindows() || Utilities.getOperatingSystem() == Utilities.OS_SOLARIS) {
                n = "internal-player";
            } else {
                n = "external-player-unix";
            }
        }
        return n;
    }

    private static void setPlayerRef(ObjectRef r) {
        if (r != null && r.getContext() == players) {
            c.putString(PROP_PLAYER, r.getBindingName());
        }
    }
        
    public static float getSampleRate() {
        return c.getFloat(PROP_SAMPLE_RATE, 24000.0f);
    }
    private static void setSampleRate(float sampleRate) {
        c.putFloat(PROP_SAMPLE_RATE, sampleRate);
    }
    public static Node createNode() {
        return new ComposerSettingsNode();
    }
    
    private static final class ComposerSettingsNode extends AbstractNode {
        public ComposerSettingsNode() {
            super(Children.LEAF);
        }
        public String getName() {
            return ComposerSettings.class.getName();
        }
        public String getDisplayName() {
            return NbBundle.getMessage(ComposerSettings.class, "LBL_ComposerSettings");
        }
        public Image getIcon(int type) {
            if (type == BeanInfo.ICON_COLOR_16x16 || type == BeanInfo.ICON_MONO_16x16) {
                return Utilities.loadImage("org/netbeans/examples/modules/minicomposer/ScoreDataIcon.gif");
            } else {
                return null;
            }
        }
        public HelpCtx getHelpCtx() {
            return new HelpCtx("org.netbeans.examples.modules.minicomposer.settings");
        }
        public Action[] getActions(boolean context) {
            return new Action[0];
        }
        protected Sheet createSheet() {
            Sheet s = new Sheet();
            Sheet.Set ss = Sheet.createPropertiesSet();
            ss.put(new Node.Property[] {
                new PlayerProperty(),
            });
            s.put(ss);
            ss = Sheet.createExpertSet();
            ss.put(new Node.Property[] {
                new SampleRateProperty(),
            });
            s.put(ss);
            return s;
        }
    }
    
    private static final class PlayerProperty extends PropertySupport.ReadWrite {
        public PlayerProperty() {
            super(PROP_PLAYER, ObjectRef.class,
                    NbBundle.getMessage(ComposerSettings.class, "PROP_player"),
                    NbBundle.getMessage(ComposerSettings.class, "HINT_player"));
        }

        public Object getValue() {
            return getPlayerRef();
        }

        public void setValue(Object val) {
            setPlayerRef((ObjectRef) val);
        }

        public PropertyEditor getPropertyEditor() {
            String title = "Audio Players"; // XXX I18N
            return new ContextPropertyEditor(title, players, Player.class, PlayerLookSelector.getDefault());
        }
    }
    
    private static final class SampleRateProperty extends PropertySupport.ReadWrite {
        public SampleRateProperty() {
            super(PROP_SAMPLE_RATE, Float.TYPE,
                NbBundle.getMessage(ComposerSettings.class, "PROP_sampleRate"),
                NbBundle.getMessage(ComposerSettings.class, "HINT_sampleRate"));
        }
        public Object getValue() {
            return new Float(getSampleRate());
        }
        public void setValue(Object val) {
            setSampleRate(((Float)val).floatValue());
        }
        public PropertyEditor getPropertyEditor() {
            return new SampleRateEd();
        }
    }
    
    private static final class SampleRateEd extends PropertyEditorSupport {
        private static final float[] rates = new float[] {12000.0f, 24000.0f, 48000.0f};
        private static final String[] tags = new String[rates.length];
        static {
            NumberFormat format = new DecimalFormat();
            for (int i = 0; i < rates.length; i++) {
                tags[i] = format.format(rates[i]);
            }
        }
        public String[] getTags() {
            return tags;
        }
        public String getAsText() {
            float value = ((Float)getValue()).floatValue();
            for (int i = 0; i < rates.length; i++) {
                if (rates[i] == value) {
                    return tags[i];
                }
            }
            return "???";
        }
        public void setAsText(String text) throws IllegalArgumentException {
            for (int i = 0; i < tags.length; i++) {
                if (tags[i].equals(text)) {
                    setValue(new Float(rates[i]));
                    return;
                }
            }
            throw new IllegalArgumentException();
        }
    }
    
}
... 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.