Search in sources :

Example 1 with SelectionListener

use of com.jme3.app.SettingsDialog.SelectionListener in project jmonkeyengine by jMonkeyEngine.

the class JmeDesktopSystem method showSettingsDialog.

@Override
public boolean showSettingsDialog(AppSettings sourceSettings, final boolean loadFromRegistry) {
    if (SwingUtilities.isEventDispatchThread()) {
        throw new IllegalStateException("Cannot run from EDT");
    }
    if (GraphicsEnvironment.isHeadless()) {
        throw new IllegalStateException("Cannot show dialog in headless environment");
    }
    final AppSettings settings = new AppSettings(false);
    settings.copyFrom(sourceSettings);
    String iconPath = sourceSettings.getSettingsDialogImage();
    if (iconPath == null) {
        iconPath = "";
    }
    final URL iconUrl = JmeSystem.class.getResource(iconPath.startsWith("/") ? iconPath : "/" + iconPath);
    if (iconUrl == null) {
        throw new AssetNotFoundException(sourceSettings.getSettingsDialogImage());
    }
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicInteger result = new AtomicInteger();
    final Object lock = new Object();
    final SelectionListener selectionListener = new SelectionListener() {

        public void onSelection(int selection) {
            synchronized (lock) {
                done.set(true);
                result.set(selection);
                lock.notifyAll();
            }
        }
    };
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            synchronized (lock) {
                SettingsDialog dialog = new SettingsDialog(settings, iconUrl, loadFromRegistry);
                dialog.setSelectionListener(selectionListener);
                dialog.showDialog();
            }
        }
    });
    synchronized (lock) {
        while (!done.get()) {
            try {
                lock.wait();
            } catch (InterruptedException ex) {
            }
        }
    }
    sourceSettings.copyFrom(settings);
    return result.get() == SettingsDialog.APPROVE_SELECTION;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) URL(java.net.URL) SettingsDialog(com.jme3.app.SettingsDialog) SelectionListener(com.jme3.app.SettingsDialog.SelectionListener)

Aggregations

SettingsDialog (com.jme3.app.SettingsDialog)1 SelectionListener (com.jme3.app.SettingsDialog.SelectionListener)1 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 URL (java.net.URL)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1