Example usage for javax.swing SwingUtilities invokeAndWait

List of usage examples for javax.swing SwingUtilities invokeAndWait

Introduction

In this page you can find the example usage for javax.swing SwingUtilities invokeAndWait.

Prototype

public static void invokeAndWait(final Runnable doRun) throws InterruptedException, InvocationTargetException 

Source Link

Document

Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.

Usage

From source file:edu.oregonstate.eecs.mcplan.domains.sailing.SailingVisualization.java

public void updateStateOnEDT(final SailingState s) {
    //      System.out.println( "[Visualization]: " + s );

    final int x = s.x;
    final int y = s.y;
    final int w = s.w;
    try {//from   ww  w . jav a 2 s  .c o m
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                updateState(x, y, w);
            }
        });
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
    if (s.isTerminal()) {

    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.racegrid.RacegridVisualization.java

public void updateStateOnEDT(final RacegridState s) {
    //      System.out.println( "[Visualization]: " + s );

    final int x = s.x;
    final int y = s.y;
    final int dx = s.dx;
    final int dy = s.dy;
    try {//from  ww  w . j a  v a  2  s .c o m
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                updateState(x, y, dx, dy);
            }
        });
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
    if (s.isTerminal()) {

    }
}

From source file:org.uncommons.watchmaker.swing.evolutionmonitor.IslandsView.java

public void islandPopulationUpdate(final int islandIndex,
        final PopulationData<? extends Object> populationData) {
    // Make sure the bars are added to the chart in order of island index, regardless of which island
    // reports its results first.
    if (islandIndex >= islandCount.get()) {
        try {/*from  w  w  w . j av a  2 s.c o  m*/
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    // Don't need synchronisation here because SwingUtilities queues these updates
                    // and if a second update gets queued, the loop will be a no-op so it's not a problem.
                    for (Integer i = islandCount.get(); i <= islandIndex; i++) {
                        bestDataSet.addValue(0, FITTEST_INDIVIDUAL_LABEL, i);
                        meanDataSet.add(0, 0, MEAN_FITNESS_LABEL, i);
                        islandCount.incrementAndGet();
                    }
                }
            });
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException ex) {
            throw new IllegalStateException(ex.getCause());
        }
    }

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            chart.setNotify(false);
            bestDataSet.setValue(populationData.getBestCandidateFitness(), FITTEST_INDIVIDUAL_LABEL,
                    (Integer) islandIndex);
            meanDataSet.remove(MEAN_FITNESS_LABEL, (Integer) islandIndex);
            meanDataSet.add(populationData.getMeanFitness(), populationData.getFitnessStandardDeviation(),
                    MEAN_FITNESS_LABEL, (Integer) islandIndex);
            ValueAxis rangeAxis = ((CategoryPlot) chart.getPlot()).getRangeAxis();
            // If the range is not sufficient to display all values, enlarge it.
            synchronized (maxLock) {
                max = Math.max(max, populationData.getBestCandidateFitness());
                max = Math.max(max,
                        populationData.getMeanFitness() + populationData.getFitnessStandardDeviation());
                while (max > rangeAxis.getUpperBound()) {
                    rangeAxis.setUpperBound(rangeAxis.getUpperBound() * 2);
                }
                // If the range is much bigger than it needs to be, reduce it.
                while (max < rangeAxis.getUpperBound() / 4) {
                    rangeAxis.setUpperBound(rangeAxis.getUpperBound() / 4);
                }
            }
            chart.setNotify(true);
        }
    });
}

From source file:com.aw.swing.mvp.binding.BindingComponent.java

/**
 * Set the value of the bean to the JComponent
 *//*from  www . j  a v  a2  s. com*/
public final void setValueToJComponent() {
    if (SwingUtilities.isEventDispatchThread()) {
        setValueToJComponentInternal();
    } else {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    setValueToJComponentInternal();
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
            throw new AWSystemException("Problems setting values to jcompoent:" + fieldName, e);
        }
    }

}

From source file:edu.oregonstate.eecs.mcplan.domains.frogger.FroggerVisualization.java

public void updateStateOnEDT(final FroggerState s) {
    final int x = s.frog_x;
    final int y = s.frog_y;
    final Tile[][] grid = new Tile[s.params.lanes + 2][s.params.road_length];
    for (int i = 0; i < s.params.lanes + 2; ++i) {
        for (int j = 0; j < s.params.road_length; ++j) {
            grid[i][j] = s.grid[i][j];//from w w w  .  j  a va  2  s. co  m
        }
    }
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                updateState(x, y, grid);
            }
        });
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
    if (s.isTerminal()) {

    }
}

From source file:WorkThreadPool.java

/**
 * Waits until all requests are complete.
 *//*from   ww w.  j a  va  2  s. co m*/
public void waitForRequests() {
    if (threads == null)
        return;

    synchronized (waitForAllLock) {
        while (requestCount != 0) {
            try {
                waitForAllLock.wait();
            } catch (InterruptedException ie) {

            }
        }
    }

    if (SwingUtilities.isEventDispatchThread()) {
        // do any queued AWT runnables
        doAWTRequests();
    } else {
        try {
            SwingUtilities.invokeAndWait(new RunRequestsInAWTThread());
        } catch (Exception e) {

        }
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.racetrack.RacetrackVisualization.java

public void updateStateOnEDT(final RacetrackState s) {
    final double x = s.car_x;
    final double y = s.car_y;
    final double dx = s.car_dx;
    final double dy = s.car_dy;
    final double theta = s.car_theta;
    final int sector = s.sector;
    final int laps_to_go = s.laps_to_go;
    try {// www.  ja va2  s .  com
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                updateState(x, y, dx, dy, theta, sector, laps_to_go);
            }
        });
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
    if (s.isTerminal()) {

    }
}

From source file:com.aw.swing.mvp.action.ActionManager.java

public void executeAction(final Action action) {
    atBeginningOfAction(action);/*from www  . ja  v a 2  s. c  o  m*/
    AWActionTipPainter.instance().hideTipWindow();
    if (action instanceof RoundTransitionAction) {
        ((RoundTransitionAction) action).setTransitionStoppedWithException(false);
    }

    GridProviderManager gridProviderManager = action.getPst().getGridProviderMgr();
    gridProviderManager.removeEditors();

    try {
        action.checkBasicConditions();
    } catch (FlowBreakSilentlyException ex) {
        logger.info("Exit flow method silently");
        return;
    } catch (AWException ex) {
        logger.error("AW Exception:", ex);
        if (action instanceof RoundTransitionAction) {
            ((RoundTransitionAction) action).setTransitionStoppedWithException(true);
        }
        PainterMessages.paintException(ex);
        return;
    }

    String confirmMsg = action.getConfirmMsg();
    if (StringUtils.hasText(confirmMsg)) {
        boolean isCancelAction = action instanceof CancelAction;
        boolean isFindPst = action.getPst() instanceof FindPresenter;
        boolean isModeReadOnly = ViewMode.MODE_READONLY.equals(action.getPst().getViewMode());
        boolean isShowCancelMsgConfirmation = action.getPst().isShowCancelMsgConfirmation();
        if (!isCancelAction || (isShowCancelMsgConfirmation && !isModeReadOnly && !isFindPst)) {
            if (!MsgDisplayer.showConfirmMessage(confirmMsg)) {
                logger.debug(
                        "The action:<" + action.toString() + ">will not be executed because was not confirmed");
                return;
            }
        }
    }
    try {
        action.checkConditions();
        AWInputVerifier.getInstance().disable();
        Presenter pst = action.getPst();
        if (action.execBinding) {
            pst.setValuesToBean();
        }
        if (action.execValidation) {
            pst.validate();
        }
    } catch (AWException ex) {
        if (action instanceof RoundTransitionAction) {
            ((RoundTransitionAction) action).setTransitionStoppedWithException(true);
        }
        if (ex instanceof FlowBreakSilentlyException) {
            return;
        }
        logger.error("AW Exception:", ex);
        PainterMessages.paintException(ex);
        return;
    } finally {
        AWInputVerifier.getInstance().enable();
    }

    if (action.useMessageBlocker) {
        try {
            if (!SwingUtilities.isEventDispatchThread()) {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        ProcessMsgBlocker.instance().showMessage("Procesando ...");
                    }
                });
            } else {
                ProcessMsgBlocker.instance().showMessage("Procesando ...");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

        SwingWorker swingWorker = new SwingWorker() {
            protected Object doInBackground() throws Exception {
                executeActionInternal(action);
                return null;
            }

            protected void done() {
                ProcessMsgBlocker.instance().removeMessage();
                action.afterExecute();
            }
        };
        swingWorker.execute();

    } else {
        executeActionInternal(action);
        action.afterExecute();
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.taxi.TaxiVisualization.java

public void updateStateOnEDT(final TaxiState s) {
    final int[] taxi = s.taxi;
    final int passenger = s.passenger;
    final int[][] other_taxis = s.other_taxis;
    final int destination = s.destination;
    final boolean illegal = s.illegal_pickup_dropoff;
    final boolean goal = s.goal;
    try {//from ww w . j ava 2 s  .co  m
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                updateState(taxi, passenger, other_taxis, destination, illegal, goal);
            }
        });
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
    if (s.isTerminal()) {

    }
}

From source file:com.aw.swing.mvp.Presenter.java

/**
 * Init the presenter//w ww.  j  a va  2  s. co m
 */
public final void init() {
    if (SwingUtilities.isEventDispatchThread()) {
        initInternal();
    } else {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    initInternal();
                }
            });
        } catch (Throwable e) {
            throw new AWBusinessException("Problems initializing the presenter:" + this, e);
        }
    }
}