Skip to content

Commit

Permalink
Some formatting and disabled JavaFXSoundClip
Browse files Browse the repository at this point in the history
  • Loading branch information
reportmill committed Nov 20, 2021
1 parent b18bb4a commit 0aa6162
Show file tree
Hide file tree
Showing 12 changed files with 1,365 additions and 997 deletions.
14 changes: 4 additions & 10 deletions src/snap/swing/AWTEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,17 @@ public Image getImageForSizeAndScale(double aWidth, double aHeight, boolean hasA
*/
public SoundClip getSound(Object aSource)
{
try {
Class cls = Class.forName("snap.swing.JFXSoundClip");
return (SoundClip) cls.getDeclaredConstructor(Object.class).newInstance(aSource);
}
catch(Exception e) { throw new RuntimeException(e); }
// return new JFXSoundClip(aSource);
return new SwingSoundClip(aSource);
}

/**
* Creates a sound for given source.
*/
public SoundClip createSound()
{
try {
Class cls = Class.forName("snap.swing.JFXSoundClip");
return (SoundClip) cls.newInstance();
}
catch(Exception e) { throw new RuntimeException(e); }
// return new JFXSoundClip();
return new SwingSoundClip(null);
}

/**
Expand Down
146 changes: 102 additions & 44 deletions src/snap/swing/AWTPrefs.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
package snap.swing;

import java.security.AccessControlException;
import java.util.*;
import java.util.prefs.*;

import snap.util.*;

/**
* AWT implementation of Snap Prefs.
*/
public class AWTPrefs extends Prefs {

// The AWT Preferences
private Preferences _prefs;
private Preferences _prefs;

// The shared AWT Prefs
private static Map<String,AWTPrefs> _shared = new HashMap();
private static Map<String, AWTPrefs> _shared = new HashMap();

// A special preferences instance to use if we don't have preferences permissions
private static Preferences _bogus = null;
private static Preferences _bogus = null;

/**
* Creates new AWTPrefs.
*/
private AWTPrefs(String aName)
{
// Get root prefs
try { _prefs = Preferences.userRoot(); }
catch(AccessControlException ex) { _prefs = getBogus(); }
try {
_prefs = Preferences.userRoot();
} catch (AccessControlException ex) {
_prefs = getBogus();
}

// Get named prefs
if (aName!=null) _prefs = _prefs.node(aName);
if (aName != null) _prefs = _prefs.node(aName);
}

/**
* Creates new AWTPrefs.
*/
private AWTPrefs(Preferences aPrefs) { _prefs = aPrefs; }
private AWTPrefs(Preferences aPrefs)
{
_prefs = aPrefs;
}

@Override
public Object getValue(String aKey, Object aDefault)
{
// Get value using default type
if (aDefault instanceof String)
return _prefs.get(aKey, (String)aDefault);
return _prefs.get(aKey, (String) aDefault);
else if (aDefault instanceof Boolean)
return _prefs.getBoolean(aKey, ((Boolean)aDefault).booleanValue());
return _prefs.getBoolean(aKey, ((Boolean) aDefault).booleanValue());
else if (aDefault instanceof Float)
return _prefs.getFloat(aKey, ((Float)aDefault).floatValue());
return _prefs.getFloat(aKey, ((Float) aDefault).floatValue());
else if (aDefault instanceof Double)
return _prefs.getDouble(aKey, ((Double)aDefault).doubleValue());
return _prefs.getDouble(aKey, ((Double) aDefault).doubleValue());
else if (aDefault instanceof Integer)
return _prefs.getInt(aKey, ((Integer)aDefault).intValue());
return _prefs.getInt(aKey, ((Integer) aDefault).intValue());
else if (aDefault instanceof Long)
return _prefs.getLong(aKey, ((Long)aDefault).longValue());
return _prefs.getLong(aKey, ((Long) aDefault).longValue());
else if (aDefault instanceof Enum)
return _prefs.get(aKey, aDefault.toString());

// Otherwise, assume string
String val = _prefs.get(aKey, (String)null);
return val!=null ? val : aDefault;
String val = _prefs.get(aKey, (String) null);
return val != null ? val : aDefault;
}

/**
Expand All @@ -67,20 +75,20 @@ public void setValue(String aKey, Object aValue)
{
// Put value
if (aValue instanceof String)
_prefs.put(aKey, (String)aValue);
_prefs.put(aKey, (String) aValue);
else if (aValue instanceof Boolean)
_prefs.putBoolean(aKey, ((Boolean)aValue).booleanValue());
_prefs.putBoolean(aKey, ((Boolean) aValue).booleanValue());
else if (aValue instanceof Float)
_prefs.putFloat(aKey, ((Float)aValue).floatValue());
_prefs.putFloat(aKey, ((Float) aValue).floatValue());
else if (aValue instanceof Double)
_prefs.putDouble(aKey, ((Double)aValue).doubleValue());
_prefs.putDouble(aKey, ((Double) aValue).doubleValue());
else if (aValue instanceof Integer)
_prefs.putInt(aKey, ((Integer)aValue).intValue());
_prefs.putInt(aKey, ((Integer) aValue).intValue());
else if (aValue instanceof Long)
_prefs.putLong(aKey, ((Long)aValue).longValue());
_prefs.putLong(aKey, ((Long) aValue).longValue());
else if (aValue instanceof Enum)
_prefs.put(aKey, aValue.toString());
else if (aValue==null)
else if (aValue == null)
_prefs.remove(aKey);
else System.err.println("Unsupported prefsPut() type: " + aValue.getClass().getName());
}
Expand Down Expand Up @@ -130,31 +138,44 @@ public boolean getBoolean(String aKey, boolean aDefault)
*/
public String[] getKeys()
{
try { return _prefs.keys(); }
catch(Exception e) { System.err.println(e); return new String[0]; }
try {
return _prefs.keys();
} catch (Exception e) {
System.err.println(e);
return new String[0];
}
}

/**
* Returns a child prefs for given name.
*/
public AWTPrefs getChild(String aName) { return new AWTPrefs(_prefs.node(aName)); }
public AWTPrefs getChild(String aName)
{
return new AWTPrefs(_prefs.node(aName));
}

/**
* Updates this persistant store associated with these preferences.
*/
public void flush()
{
try { _prefs.flush(); }
catch(Exception e) { e.printStackTrace(); }
try {
_prefs.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Clears all the preferences.
*/
public void clear()
{
try { _prefs.removeNode(); }
catch(Exception e) { e.printStackTrace(); }
try {
_prefs.removeNode();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
Expand All @@ -163,7 +184,8 @@ public void clear()
public static AWTPrefs getPrefs(String aName)
{
AWTPrefs prefs = _shared.get(aName);
if(prefs==null) _shared.put(aName, prefs = new AWTPrefs(aName));
if (prefs == null)
_shared.put(aName, prefs = new AWTPrefs(aName));
return prefs;
}

Expand All @@ -172,8 +194,8 @@ public static AWTPrefs getPrefs(String aName)
*/
private static Preferences getBogus()
{
if (_bogus!=null) return _bogus;
_bogus = new BogusPrefs(null,"");
if (_bogus != null) return _bogus;
_bogus = new BogusPrefs(null, "");
return _bogus;
}

Expand All @@ -185,16 +207,52 @@ private static class BogusPrefs extends AbstractPreferences {

Map _store = new HashMap();

public BogusPrefs(AbstractPreferences parent, String name) { super(parent, name); }

protected void syncSpi() throws BackingStoreException { }
protected void flushSpi() throws BackingStoreException { }
protected void removeSpi(String key) { _store.remove(key); }
protected void removeNodeSpi() throws BackingStoreException { _store.clear(); }
protected void putSpi(String key, String value) { _store.put(key,value); }
protected String[] keysSpi() throws BackingStoreException { return (String[])_store.keySet().toArray(); }
protected String getSpi(String key) { return (String)_store.get(key); }
protected AbstractPreferences childSpi(String name) { return this; }
protected String[] childrenNamesSpi() throws BackingStoreException { return null; }
public BogusPrefs(AbstractPreferences parent, String name)
{
super(parent, name);
}

protected void syncSpi() throws BackingStoreException
{
}

protected void flushSpi() throws BackingStoreException
{
}

protected void removeSpi(String key)
{
_store.remove(key);
}

protected void removeNodeSpi() throws BackingStoreException
{
_store.clear();
}

protected void putSpi(String key, String value)
{
_store.put(key, value);
}

protected String[] keysSpi() throws BackingStoreException
{
return (String[]) _store.keySet().toArray();
}

protected String getSpi(String key)
{
return (String) _store.get(key);
}

protected AbstractPreferences childSpi(String name)
{
return this;
}

protected String[] childrenNamesSpi() throws BackingStoreException
{
return null;
}
}
}
Loading

0 comments on commit 0aa6162

Please sign in to comment.