Search in sources :

Example 1 with TaskExecutionException

use of org.apache.pivot.util.concurrent.TaskExecutionException in project pivot by apache.

the class Image method loadFromCache.

public static Image loadFromCache(URL location) {
    Utils.checkNull(location, "image location");
    Image image = (Image) ApplicationContext.getResourceCache().get(location);
    if (image == null) {
        try {
            image = Image.load(location);
            ApplicationContext.getResourceCache().put(location, image);
        } catch (TaskExecutionException exception) {
            throw new IllegalArgumentException(exception);
        }
    }
    return image;
}
Also used : TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) BufferedImage(java.awt.image.BufferedImage)

Example 2 with TaskExecutionException

use of org.apache.pivot.util.concurrent.TaskExecutionException in project pivot by apache.

the class SplashScreenTest method startup.

@Override
public void startup(final Display display, final Map<String, String> properties) throws Exception {
    File splashFile = new File("org/apache/pivot/tests/splash.png");
    System.out.println("Startup the application at " + new Date());
    System.out.println("To show the Splash Screen, remember to run as a Standard Java Application this way:\n" + "java -splash:" + splashFile.getPath() + " <mainclassname> --preserveSplashScreen=true\n" + "or no splash screen will be shown.");
    // Create a Task that will load a BXML file and simulate some other
    // processing while updating a progress meter on the SplashScreen
    final Task<Void> prepareApplicationTask = new Task<Void>() {

        final SplashScreenProgressOverlay progressOverlay = new SplashScreenProgressOverlay();

        @Override
        public Void execute() throws TaskExecutionException {
            // Load the main BXML
            progressOverlay.increment(0);
            loadBXML(display, 0.1);
            // Simulate other tasks until the progress meter has been filled
            final Random random = new Random();
            while (progressOverlay.getPercentage() < 1.0) {
                // Short random sleep to simulate some processing
                try {
                    Thread.sleep(random.nextInt(50) + 100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // Update the progress meter by a random amount
                progressOverlay.increment((1 + random.nextInt(10)) / 100.0);
            }
            return null;
        }

        // Load the Pivot UI
        private void loadBXML(final Display displayArgument, final double weight) {
            try {
                ApplicationContext.queueCallback(() -> {
                    Window window = null;
                    try {
                        window = (Window) new BXMLSerializer().readObject(this.getClass().getResource("splash.bxml"));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    if (window != null) {
                        window.open(displayArgument);
                        progressOverlay.increment(weight);
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    // Hide the SplashScreen when the Task finishes by making the Pivot host
    // window visible.
    final TaskListener<Void> taskListener = new TaskListener<Void>() {

        @Override
        public void taskExecuted(final Task<Void> task) {
            finished();
        }

        @Override
        public void executeFailed(final Task<Void> task) {
            System.err.println(String.format("Failed\n%s", task.getFault()));
            task.getFault().printStackTrace();
            finished();
        }

        private void finished() {
            DesktopApplicationContext.replaceSplashScreen(display);
        }
    };
    // Run the Task asynchronously
    prepareApplicationTask.execute(new TaskAdapter<>(taskListener));
}
Also used : Window(org.apache.pivot.wtk.Window) Task(org.apache.pivot.util.concurrent.Task) Date(java.util.Date) TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) Random(java.util.Random) TaskListener(org.apache.pivot.util.concurrent.TaskListener) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) Display(org.apache.pivot.wtk.Display)

Example 3 with TaskExecutionException

use of org.apache.pivot.util.concurrent.TaskExecutionException in project pivot by apache.

the class Pivot894 method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    System.out.println("public startup(...)");
    System.out.println("\n" + "Attention: now the application will go in an infinite loop, to be able to see the memory leak.\n" + "Note that probably you'll have to kill the application from outside (kill the Java process).\n" + "\n");
    // add some sleep to let users see the warning messages in console ...
    Thread.sleep(2000);
    final CardPane cardPane = new CardPane();
    cardPane.getStyles().put(Style.selectionChangeEffect, CardPaneSkin.SelectionChangeEffect.HORIZONTAL_SLIDE);
    final Window window = new Window(cardPane);
    window.open(display);
    ApplicationContext.scheduleRecurringCallback(new Runnable() {

        @Override
        public void run() {
            Thread.currentThread().setName("switcher-thread");
            // temp
            System.out.println("Run num " + num++);
            // 
            try {
                // Before the fixes for PIVOT-861 (part two) it was causing
                // out of memory ...
                // 
                // Note that this has been moved to another issue, but the
                // problem is due to the usage
                // of dataRenderer tags (and then instancing
                // ButtonDataRenderer) in the loaded bxml,
                // so probably even this test will be updated ...
                // 
                final GridPane grid = (GridPane) new BXMLSerializer().readObject(Pivot894.class, "btn_grid.bxml");
                EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Iterator<Component> iterator = cardPane.iterator();
                        List<Component> componentList = new ArrayList<>();
                        while (iterator.hasNext()) {
                            Component card = iterator.next();
                            if (!card.isShowing()) {
                                componentList.add(card);
                            }
                        }
                        for (Component card : componentList) {
                            cardPane.remove(card);
                        }
                        cardPane.setSelectedIndex(cardPane.add(grid));
                        System.out.println(cardPane.getSelectedIndex());
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @SuppressWarnings("unused")
        private Row createGridRow() {
            Row row = new Row();
            try {
                // note that this method doesn't use ApplicationContext
                // cache for images ...
                row.add(new PushButton(new ButtonData(Image.load(new File("clock_icon.png").toURI().toURL()), "Clock")));
                row.add(new PushButton(new ButtonData(Image.load(new File("clock_icon.png").toURI().toURL()), "Clock")));
                row.add(new PushButton(new ButtonData(Image.load(new File("clock_icon.png").toURI().toURL()), "Clock")));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (TaskExecutionException e) {
                e.printStackTrace();
            }
            return row;
        }
    }, 100);
}
Also used : Window(org.apache.pivot.wtk.Window) CardPane(org.apache.pivot.wtk.CardPane) MalformedURLException(java.net.MalformedURLException) GridPane(org.apache.pivot.wtk.GridPane) ArrayList(java.util.ArrayList) MalformedURLException(java.net.MalformedURLException) TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) ButtonData(org.apache.pivot.wtk.content.ButtonData) Row(org.apache.pivot.wtk.GridPane.Row) Component(org.apache.pivot.wtk.Component) PushButton(org.apache.pivot.wtk.PushButton) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 4 with TaskExecutionException

use of org.apache.pivot.util.concurrent.TaskExecutionException in project pivot by apache.

the class BackgroundTasks method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    activityIndicator = (ActivityIndicator) namespace.get("activityIndicator");
    executeSynchronousButton = (PushButton) namespace.get("executeSynchronousButton");
    executeAsynchronousButton = (PushButton) namespace.get("executeAsynchronousButton");
    executeSynchronousButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            activityIndicator.setActive(true);
            System.out.println("Starting synchronous task execution.");
            SleepTask sleepTask = new SleepTask();
            String result = null;
            try {
                result = sleepTask.execute();
            } catch (TaskExecutionException exception) {
                System.err.println(exception);
            }
            System.out.println("Synchronous task execution complete: \"" + result + "\"");
            activityIndicator.setActive(false);
        }
    });
    executeAsynchronousButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            activityIndicator.setActive(true);
            setEnabled(false);
            System.out.println("Starting asynchronous task execution.");
            SleepTask sleepTask = new SleepTask();
            TaskListener<String> taskListener = new TaskListener<String>() {

                @Override
                public void taskExecuted(Task<String> task) {
                    activityIndicator.setActive(false);
                    setEnabled(true);
                    System.out.println("Synchronous task execution complete: \"" + task.getResult() + "\"");
                }

                @Override
                public void executeFailed(Task<String> task) {
                    activityIndicator.setActive(false);
                    setEnabled(true);
                    System.err.println(task.getFault());
                }
            };
            sleepTask.execute(new TaskAdapter<>(taskListener));
        }
    });
}
Also used : ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) TaskAdapter(org.apache.pivot.wtk.TaskAdapter) TaskListener(org.apache.pivot.util.concurrent.TaskListener)

Example 5 with TaskExecutionException

use of org.apache.pivot.util.concurrent.TaskExecutionException in project pivot by apache.

the class TerraTheme method loadMessageIcons.

private void loadMessageIcons(Map<String, String> messageIconNames, Map<MessageType, Image> messageIconsMap) {
    for (String messageIconType : messageIconNames) {
        String messageIconName = messageIconNames.get(messageIconType);
        Image messageIcon;
        try {
            messageIcon = Image.load(getClass().getResource(messageIconName));
        } catch (TaskExecutionException exception) {
            throw new RuntimeException(exception);
        }
        messageIconsMap.put(MessageType.fromString(messageIconType), messageIcon);
    }
}
Also used : TaskExecutionException(org.apache.pivot.util.concurrent.TaskExecutionException) Image(org.apache.pivot.wtk.media.Image)

Aggregations

TaskExecutionException (org.apache.pivot.util.concurrent.TaskExecutionException)5 File (java.io.File)2 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)2 TaskListener (org.apache.pivot.util.concurrent.TaskListener)2 PushButton (org.apache.pivot.wtk.PushButton)2 Window (org.apache.pivot.wtk.Window)2 BufferedImage (java.awt.image.BufferedImage)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Random (java.util.Random)1 Task (org.apache.pivot.util.concurrent.Task)1 Button (org.apache.pivot.wtk.Button)1 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)1 CardPane (org.apache.pivot.wtk.CardPane)1 Component (org.apache.pivot.wtk.Component)1 Display (org.apache.pivot.wtk.Display)1 GridPane (org.apache.pivot.wtk.GridPane)1 Row (org.apache.pivot.wtk.GridPane.Row)1 TaskAdapter (org.apache.pivot.wtk.TaskAdapter)1